Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM...
Transcript of Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM...
![Page 1: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/1.jpg)
Physics124:Lecture3
Motors:Servo;DC;StepperMessingwithPWM(and2-wayserial)
TheMotorShieldFromT.Murphy’sPHYS124lectures
![Page 2: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/2.jpg)
ThreeTypes(forus)
• Servomotor– PWMsetsposition,usedforR/Cplanes,cars,etc.– 180° rangelimit,typically– 5Vsupply
• Steppermotor– Forpreciseangularcontrolorspeedcontrol– Canrotateindefinitely– Lotsofholdingtorque
• DCmotor– simplesttechnology;giveuponprecisecontrol– goodwhenyoujustneedsomethingtoSPIN!
2Phys124:Lecture3
![Page 3: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/3.jpg)
WhenanyoldPWMwon’tdo
• ThefunctionanalogWrite() givesyoueasycontroloverthedutycycleofPWMoutput– butnocontrolatalloverfrequency
• ConsidertheHitec servomotorswe’llbeusing:
• Wantsa50Hzpulserate,andadutycyclefrom4.5%to10.5%(11/255to27/255)todrivefullrange
3Phys124:Lecture3
![Page 4: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/4.jpg)
WhatfrequencyisArduino PWM?
• Dependsonwhichoutputisused• Pins5and6:default~977Hz– 16MHzclockratedividedby214 =16384
• Pins3,9,10,11:default488Hz– 16MHz/215
• Neitherisatalllikethe50Hzweneedfortheservomotor
Phys124:Lecture3 4
![Page 5: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/5.jpg)
Whatchoicedowehave?• Wecanchangetheclockdivideronanyofthreecounters
internaltotheATMega328– timer/counter0,1,and2,considerregistermap:
– noteinparticularthelowest3bitsinTCCR2B– Timer/CounterControlRegisters,WaveformGenerationMode,
CompareMatchOutput– settingtheseaccordingtothefollowingrubricscalesspeed
Phys124:Lecture3 5
![Page 6: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/6.jpg)
ValidDividerOptionsPWMpins
Register scaler values Divisor Approx. frequencies(Hz)
5, 6 TCCR0B 1,2,3, 4,5 1,8,64,256,1024 62500,7812, 977,244,61.0
9,10 TCCR1B 1,2,3,4,5 1,8,64,256,1024 31250,3906,488,122,30.5
3,11 TCCR2B 1,2,3,4,5,6,7 1,8,64,256,1024… 31250,3906,977,488,244,122,30.5
Phys124:Lecture3 6
• Defaultsinred• Limited choices,wecan’tpreciselyhit50Hztarget• Closestistousetimer0withdivideroption5(61Hz)• 0.9to2.1mspulsescorrespondto14/255to33/255• only20possiblestepsbythisscheme
Canyoufindoutwhyabovescalervaluesinbinarydon’tmatchtheCStableabove?
http://playground.arduino.cc/Main/TimerPWMCheatsheet
![Page 7: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/7.jpg)
HowtosetdividerandchangePWMfreq.
• It’sactuallynotthathard– candoinsetuporinmainloop
• BrokenDown:– modifyingTCCR0Bassociatedwithpins5&6– & isbitwiseAND operator– 0b11111000isbinarymask,saying“keepfirstfiveas-is”– whilezeroingfinalthreebits(because0ANDanythingis0)– | isbitwiseOR operator,effectivelycombiningtwopieces– 0x05ishexfor5,whichwillselect61.0HzonTimer0– ifTCCR0Bstartedasvwxyzabc,itendsupasvwxyz101
Phys124:Lecture3 7
TCCR0B = TCCR0B & 0b11111000 | 0x05;
![Page 8: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/8.jpg)
• Ifyouwanttoknowmore,understandbetter:Lesson12ofthisclassSecretsofArduinoPWM:https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWMTimerPWMcheatsheet:http://playground.arduino.cc/Main/TimerPWMCheatsheetUnderstandingTCCRnA/B:http://www.righto.com/2009/07/secrets-of-arduino-pwm.html
Phys124:Lecture3 8
FastPWM:
Phase-correctPWM:
![Page 9: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/9.jpg)
CodetointeractivelyexplorePWMfrequencies
• Willuseserialcommunicationsinbothdirections
– continuedonnextslide…
Phys124:Lecture3 9
const int LED = 5; // or any PWM pin (3,5,6,9,10,11)char ch; // holds character for serial command
void setup(){
pinMode(LED,OUTPUT); // need to config for outputSerial.begin(9600);
}
![Page 10: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/10.jpg)
…continued
Phys124:Lecture3 10
void loop(){
analogWrite(LED,128); // 50% makes freq. meas. easierif (Serial.available()){ // check if incoming (to chip)ch = Serial.read(); // read single character, or Serial.parseInt()
if (ch >=‘0’ && ch <=‘7’){// valid rangeif (LED == 3 || LED == 11){ // will use timer2TCCR2B = TCCR2B & 0b11111000 | int(ch - ‘0’);Serial.print(“Switching pin ”);Serial.print(LED);Serial.print(“ to setting “);Serial.println(ch);
}}if (ch >=‘0’ && ch <=‘5’){// valid for other timers
if (LED == 5 || LED == 6){ // will use timer0TCCR0B = TCCR0B & 0b11111000 | int(ch – ‘0’);Serial.print(same stuff as before…);
}if (LED == 9 || LED == 10){ // uses timer1TCCR1B etc.
} } } } // would indent more cleanly if space
![Page 11: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/11.jpg)
Usingtheinteractiveprogram
• Useserialmonitor(Tools:SerialMonitor)– makesurebaudrateinlowerrightissameasinsetup()– cansendcharacterstoo– inthiscase,typesingledigitandreturn(orpresssend)– getbackmessagelike:
• Switching pin 11 to setting 6
– andshouldseefrequencychangeaccordingly
Phys124:Lecture3 11
![Page 12: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/12.jpg)
RiggingaServotosort-ofwork
• Originalmotivationwasgettinga50Hzservotowork
– continuednextslide…
Phys124:Lecture3 12
const int SERVO = 5;char ch; // for interactive serial controlint level = 23; // 23 is 1.5 ms; 14 is 0.9; 33 is 2.1
void setup(){
pinMode(SERVO, OUTPUT); // set servo pin for outputSerial.begin(9600);TCCR0B = TCCR0B & 0b11111000 | 0x05; // for 61 HzanalogWrite(SERVO, level); // start centered
}
![Page 13: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/13.jpg)
Continuation:mainloop
• Beinglazyandonlyacceptingsingle-charactercommands,limitedtotenvalues,mappingonto20– themap() functionisusefulhere– thech - ‘0’ does“ASCIIsubtraction”
Phys124:Lecture3 13
void loop(){
if (Serial.available()){ // check if incoming serial datach = Serial.read(); // read single characterif (ch >=‘0’ && ch <=‘9’){// use 10 step range for demo
level = map(ch-’0’,0,9,14,33); // map 0-9 onto 14-33analogWrite(SERVO, level); // send to servoSerial.print(“Setting servo level to: “);Serial.println(level);
}}delay(20); // interactive program, so slow
}
![Page 14: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/14.jpg)
Abetter(andeasier!)way• Thepreviousapproachwasapoorfit– poormatchtofrequency,andnotmuchresolution
• Arduino hasalibraryspecificallyforthis:Servo.h• VariouslibrariescomewiththeArduino distribution– in/Applications/Arduino.app/Contents/Resources/Java/librariesonmyMac
– Handlesstepperandservomotors,LCDs,memorystorageineitherEEPROM(on-board)orSDcard;severalcommoncommunicationprotocols(ethernet—forusewithshield,SPI,2-wire,andemulatedserial)
– canlookatcodeasmuchasyouwant
Phys124:Lecture3 14
EEPROM/ Firmata/ SD/ Servo/ Stepper/Ethernet/ LiquidCrystal/ SPI/ SoftwareSerial/ Wire/
![Page 15: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/15.jpg)
ExampleusingServolibrary
• Watchhoweasy:onedegreeresolution
Phys124:Lecture3 15
// servo_test . . . . slew servo back and forth thru 180 deg#include <Servo.h>
Servo hitec; // instantiate a servoint deg; // where is servo (in degrees)
void setup(){hitec.attach(9,620,2280); // servo physically hooked to pin 9// 620, 2280 are min, max pulse duration in microseconds// default is 544, 2400; here tuned to give 0 deg and 180 deg
}void loop(){for(deg = 0; deg <= 180; deg++){ // visit full range
hitec.write(deg); // send servo to degdelay(20);
}for(deg = 180; deg >= 0; deg--){ // return trip
hitec.write(deg); // send servo to degdelay(20);
}}
![Page 16: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/16.jpg)
AvailableServoMethods• attach(pin)
– Attachesaservomotortoani/opin.• attach(pin,min,max)
– Attachestoapinsettingminandmaxvaluesinmicroseconds;defaultminis544,maxis2400
• write(deg)– Setstheservoangleindegrees.(invalidanglethatisvalidaspulsein
microsecondsistreatedasmicroseconds)• writeMicroseconds(us)
– Setstheservopulsewidthinmicroseconds(givesveryhighresolution)• read()
– Getsthe lastwrittenservopulsewidthasananglebetween0and180.• readMicroseconds()
– Getsthe lastwrittenservopulsewidthinmicroseconds• attached()
– Returnstrueifthereisaservoattached.• detach()
– Stopsanattachedservofrompulsingits i/opin.
Phys124:Lecture3 16
![Page 17: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/17.jpg)
Libraries:Documentation
• Learnhowtousestandardlibrariesat:– http://arduino.cc/en/Reference/Libraries
• Butalsoanumberofcontributedlibraries• Upside:workanddeepunderstandingalreadydone• Downside:willyoulearnanythingbypickinguppre-madesophisticatedpieces?
Phys124:Lecture3 17
![Page 18: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/18.jpg)
DCMotor
• Coiltoproducemagneticfield,onrotatingshaft• Permanentmagnetorfixedelectromagnet• Commutator toswitchpolarityofrotatingmagnetasitrevolves– the“carrot”isalwaysoutfront(andwillalsogetpushfrombehindifswitchoveristimedright)
Phys124:Lecture3 18
![Page 19: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/19.jpg)
DCTorque-speedCurve• Seehttp://lancet.mit.edu/motors/motors3.html
• Stallsatτs;noloadatωn
• Outputmechanicalpowerisτω– areaofrectangletouchingcurve– maxpoweristhenPmax =¼τsωn
Phys124:Lecture3 19
![Page 20: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/20.jpg)
ElectricalExpectations
• Windinghasresistance,R,typicallyinthe10Ω range
• Ifprovidedaconstantvoltage,V– windingeatspowerPw =V2/R– motordeliversPm =τω– currentrequiredisItot =(Pw +Pm)/V
• Atmaxpoweroutput(Pm =¼τsωn)– turnsoutwindinglossiscomparable,for~50%efficiency
Phys124:Lecture3 20
![Page 21: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/21.jpg)
Example2.4Vmotor
• Randomonlinespecfor2.4Vmotor(bewareflippedaxes)– noteatpowermax0.0008Nm;0.7A;8000RPM(837rad/s)
• totalconsumption2.4×0.7=1.68W• outputmechanicalpower0.0008×837=0.67W;efficiency40%• atconstantV=2.4,totalpowerconsumptionrisesà 3Wtowardstall• 1.25AatstallimplieswindingR =V/I =1.9Ω
Phys124:Lecture3 21
![Page 22: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/22.jpg)
Anotherrandomexample
• Noteprovisionofstalltorqueandno-loadspeed– suggestsmaxoutputpowerof
¼×2π(5500)/60×0.0183=2.6W– abouthalfthisatmaxefficiencypoint
2π(4840)/60×0.00248=1.25W– atmaxefficiency,0.17×12=2.04W,suggesting61%eff.– impliedcoilresistance12/1.06≈11Ω (judgedatstall)
• Lesson:forDCmotors,electricalcurrentdependsonloadingcondition– currentismaximumwhenmotorstrainingagainststall
Phys124:Lecture3 22
![Page 23: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/23.jpg)
ServoInternals
• AServomotorisjustaseriouslygear-reducedDCmotorwithafeedbackmechanism(e.g,potentiometer)toshutitoffwhenitissatisfiedwithitsposition– anddrivemotorfasterorslowerdependingonhowfarofftarget
Phys124:Lecture3 23
DCmotor
potentiometer
gearreduction
![Page 24: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/24.jpg)
CleverSteppers
• Steppermotorsworkinbabysteps• Insimplestversion,therearetwoDCwindings– typicallyarrangedinnumerousloopsaroundcasing– dependingondirectionofcurrentflow,fieldisreversible
• Rotorhaspermanentmagnetsperiodicallyarranged– butadifferingnumberfromtheexternalcoils
Phys124:Lecture3 24
teethonrotor
8“dentures”aroundoutside
NEMAstandard(NationalElectricalManufacturersAssociation),NEMA17:faceplate1.7in
![Page 25: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/25.jpg)
ACarefullyChoreographedSequence
• Fourdifferentcombinationscanbepresentedtothetwocoils(A&B;eachbi-directional)– eachcombinationattractstherotortoa(usu.slightly)differentposition/phase
– steppingthroughthesecombinationsinsequencewalkstherotorbythehandtothenextstep
Phys124:Lecture3 25
Inpractice,rotorhasmanypolesaround (inteeth,often), soeachstepismuchfiner.
![Page 26: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/26.jpg)
ToothedAnimation• Noteteethare
notphasedwith“dentures”allthewayaround– eachis90° from
neighbor• Thissequenceis
typicalofcenter-tapsteppers– canactivateone
sideofcoilatatime
• Noteusuallyhavemorethanfour“dentures”aroundoutside
Phys124:Lecture3 26
![Page 27: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/27.jpg)
SteppingSchemes
• Cangoinfullsteps,halfsteps,orevenmicrostep– fullstepiswhereonecoilisonandhasfullattentionofrotor– iftwoadjacentcoilsareon,they“split”positionofrotor– sohalf-steppingallowsfinercontrol,buthighercurrentdraw
• everyotherstepdoublesnominalcurrent
– insteadofcoilsbeingallonoralloff,canapplydifferingcurrents(orPWM)toeach;calledmicrostepping• socanselectacontinuousrangeofpositionsbetweenfullsteps
• Obviously,controllingasteppermotorismorecomplicatedthanourotheroptions– mustmanagestatesofcoils,andstepthroughsequencesensibly
Phys124:Lecture3 27
![Page 28: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/28.jpg)
TheStepperLibrary
• PartoftheArduino StandardLibraryset• Availablecommands:– Stepper(steps,pin1,pin2)– Stepper(steps,pin1,pin2,pin3,pin4)– setSpeed(rpm)– step(steps)
• ButArduino cannotdrivestepperdirectly– can’thandlecurrent– needtransistorstocontrolcurrentflow– arrangementcalledH-bridgeideallysuited
Phys124:Lecture3 28
![Page 29: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/29.jpg)
Examplestrippedcode
Phys124:Lecture3 29
#include <Stepper.h>#define STEPS 100 // change for your stepper
Stepper stepper(STEPS, 8, 9, 10, 11);
int previous = 0;
void setup(){stepper.setSpeed(30); // 30 RPM
}
void loop(){int val = analogRead(0); // get the sensor value
// move a number of steps equal to the change in the// sensor readingstepper.step(val - previous);
// remember the previous value of the sensorprevious = val;
}
![Page 30: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/30.jpg)
AUnipolar StepperMotor:CenterTap
• Aunipolar stepperhasacentertapforeachcoil– halfofcoilcanbeactivatedatatime– candrivewithtwoArduino pins(leftarrangement)– orfourpins(right)– bothuseULN2003orULN2004DarlingtonArray
Phys124:Lecture3 30
Centertapnotshownoncoil!
Centertap->
Drivingacoil:
![Page 31: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/31.jpg)
Phys124:Lecture3 31
ADarlingtonarrayprovidesbufferstohandlecurrent
EachchannelisapairoftransistorsinaDarlingtonconfiguration
• wheninputgoeshigh, theoutputwillbepulleddownnearground.(detailsinnextslides)
• whichthenpresentsmotorwithvoltagedropacrosscoil(COMMONisatthesupplyvoltage)
![Page 32: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/32.jpg)
Parenthesis:Transistorasaswitch
Phys124:Lecture3 32
Operateineithercut-off(OFF)orsaturation(ON)regions
Example:Bipolarjunction transistor(BJT)
http://www.electronics-tutorials.ws/transistor/tran_4.html
![Page 33: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/33.jpg)
Phys124:Lecture3 33
Parenthesis:Transistorasaswitch
OFF
ON
http://www.electronics-tutorials.ws/transistor/tran_4.html
![Page 34: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/34.jpg)
Phys124:Lecture3 34
NPNTransistorasaswitch
PNPTransistorasaswitch
Sinkscurrent
Sourcescurrent
http://www.electronics-tutorials.ws/transistor/tran_4.html
𝐼" = 𝛽𝐼&
![Page 35: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/35.jpg)
BacktotheDarlingtontransistor
Phys124:Lecture3 35
EachDarlington:
Exampleuses:
Whatisthatdiodenexttothemotor?
Typeequationhere.
𝐼"~𝛽6𝐼&
![Page 36: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/36.jpg)
Studentquestion:Flyback/freewheeldiode?
Phys124:Lecture3 36https://en.m.wikipedia.org/wiki/Flyback_diode
Eliminatesthesuddenvoltagespikeseenacrossaninductive loadwhenitssupplycurrentissuddenly reduced/interrupted.
![Page 37: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/37.jpg)
Unipolar hookup;controlwithfourpins
• Yellowmotorleadsarecentertap,connectedtoexternalpowersupply𝑉8 (jackhangingoffbottom)
Phys124:Lecture3 37
![Page 38: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/38.jpg)
ABipolarStepperMotor:NoCenterTap
• Inthiscase,thecoilmustseeonesideatgroundwhiletheotherisatthesupplyvoltage
• Atleftis2-pincontrol;rightis4-pincontrol– H-bridgeisL293Dorequiv.– transistorsjustmakeforlogicinversion(1inopp.2in,etc.)
Phys124:Lecture3 38
![Page 39: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/39.jpg)
H-bridgeInternals• AnH-bridgeisso-calledbecauseofthearrangementof
transistorswithamotorcoilspanningacross– twotransistors(diagonallyopposite)willconductatatime,witha
motorcoilinbetween,e.g.,onebridgepercoil
Phys124:Lecture3 39Noticetheflyback diodes.
24waysofmakinganH-bridge:http://www.talkingelectronics.com/projects/H-Bridge/H-Bridge-1.htmlhttp://www.modularcircuits.com/blog/articles/h-bridge-secrets/h-bridges-the-basics/
![Page 40: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/40.jpg)
L293D:quadrupolehalf-Hdrivers
Phys124:Lecture3 40
![Page 41: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/41.jpg)
BipolarHookup;controlwithfourpins
• Inputsupplyshownasjackhangingoffbottom
Phys124:Lecture3 41
![Page 42: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/42.jpg)
Steppermotorwiring
Phys124:Lecture3 42
![Page 43: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/43.jpg)
Full-stepandhalf-stepbipolar drives
Phys124:Lecture3 43
Fullstep
Halfstep
![Page 44: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/44.jpg)
Internalwiringexamples…todriveyou&yourmotorcrazy
Phys124:Lecture3 44
Moreathttp://www.allaboutcircuits.com/textbook/alternating-current/chpt-13/stepper-motors/
![Page 45: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/45.jpg)
TheMotorShield
• Wehavekitshieldsthatcandrivea“motorparty”– 2servosplus2steppers,or– 2servosplus4DCmotors,or– 2servosplus2DCmotorsplus1stepper
• Allowsexternalpowersupply:motorscantakealotofjuice
Phys124:Lecture3 45
![Page 46: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/46.jpg)
TheMotorShield’sAssociatedLibrary
• Seeinstructionsat– http://learn.adafruit.com/adafruit-motor-shield– Installlibrarylinkedfromabovesite– followinstructionsfoundattopofabovepage– mayneedtomakedirectorycalledlibraries intheplacewhereyourArduino sketchesarestored• specifiedinArduino preferences
– andstoreinittheunpackedlibrariesasthedirectoryAFMotor
• Onceinstalled,justincludeinyoursketch:– #include <AFMotor.h>
• Openincludedexamplestogetgoingquickly
Phys124:Lecture3 46
![Page 47: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/47.jpg)
ExampleCode
• StepperCommandsinAFMotor– #include <AFMotor.h>
• grablibrary
– AF_Stepper my_stepper(#S/R,port);• my_stepper isarbitrarynameyouwanttocallmotor• argumentsarestepsperrevolution,whichshieldport(1or2)
– my_stepper.setSpeed(30);• setRPMofmotorforlargemoves(here30RPM)
– my_stepper.step(NSTEPS,DIRECTION,STEP_TYPE);• takeNSTEPSsteps,eitherFORWARD orBACKWARD• candoSINGLE,DOUBLE,INTERLEAVE,MICROSTEP
– my_stepper.release();• turnoffcoilsforfreemotion
Phys124:Lecture3 47
![Page 48: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/48.jpg)
StepTypes• SINGLE
– oneleadatatimeenergized,insequence3,2,4,1• ascounteddownwardonleftport(port1)onmotorshield
– normalstepsize• DOUBLE
– twoleadsatatimeareenergized:1/3,3/2,2/4,4/1• splitspositionofprevioussteps;tugofwar
– normalstepsize,buttwicethecurrent,power,torque• INTERLEAVE
– combinesbothabove:1/3,3,3/2,2,2/4,4,4/1,1– stepsarehalf-size,alternatingbetweensinglecurrentand
doublecurrent(so50%morepowerthanSINGLE)• MICROSTEP
– usesPWMtosmoothlyrampfromofftoenergized– inprinciplecanbeusedtogoanywherebetweenhardsteps
Phys124:Lecture3 48
![Page 49: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/49.jpg)
DCMotorswithmotorshield/AFMotor
• DCmotorsarehandledwiththefollowingcommands– #include <AFMotor.h>
• grablibrary
– AF_DCMotor mymotor(port);• portis1,2,3,or4accordingtoM1,M2,M3,M4onshield
– mymotor.setSpeed(200);• justaPWMvalue(0−255)tomoderatevoltagesenttomotor• notRPM,notload-independent,etc.— crudecontrol
– mymotor.run(DIRECTION);• FORWARD,BACKWARD,orRELEASE• depends,ofcourse,onhookupdirection
Phys124:Lecture3 49
![Page 50: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/50.jpg)
ServosontheShield• TwoServohookupsareprovidedontheshield• Reallyjustpower,ground,andsignalcontrol
– signalcontrolisArduino pins9and10– useServo.h standardlibrary– pin9à Servo2onshield;pin10à Servo1onshield
Phys124:Lecture3 50
Powerjumper
![Page 51: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/51.jpg)
Thisismykindof(stepper)party:12!
Phys124:Lecture3 51
BeagleBone GreenBNCtotriggermotion
![Page 52: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/52.jpg)
RJ45ðcablesgotosteppers
Phys124:Lecture3 52
BeagleBone Green
![Page 53: Physics 124: Lecture 3physics124.barreiro.ucsd.edu/wp-content/uploads/...Valid Divider Options PWM pins Register scaler values Divisor Approx.frequencies (Hz) 5, 6 TCCR0B 1, 2, 3,4,](https://reader033.fdocuments.us/reader033/viewer/2022050509/5f9a11eb77068d5fe85372b2/html5/thumbnails/53.jpg)
Announcements
• RememberTAofficehours:– TBD
• Turninprev.week’slabbystartofnextlabperiod,at2PM(daydep.onMon/Tuesection)– candropinslotonTAroominbackofMHA3544anytime
Phys124:Lecture3 53