You are on page 1of 7

ECE480 Design Team 3

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

Tom Manner April 4, 20

page

o! "

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

Table of Contents
# $ntroduction %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 2# &otation Sensors %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 4 3# Speed Detection %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% '

page 2 o! "

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

1 Introduction (rus)less DC motors are a *er+ attracti*e option !or use in s+stems t)at re,uire small )ig) tor,ue motors, -ut t)e+ do not pro*ide an+ !eed-ac.# To sol*e t)is pro-lem a microcontroller /it) at least one interrupt pin can -e con!igured to recei*e !eed-ac. !rom an optical sensor attac)ed to t)e motor# T)e Arduino de*elopment plat!orm uses ATmega '8 and ATmega328 processors, t)is note /ill !ocus on t)e ATmega328# T)e ATmega328 )as an 8M01 core cloc. speed, 322( o! !las) memor+ on -oard and in most Arduino products it is con!igured to )a*e 8 0%-it analog inputs, and 4 digital input output pins, t/o o! /)ic) can -e attac)ed to interrupts# Figure 1 P)oto o! t)e Arduino 3io, /)ic) is -eing used to interpret motor !eed-ac.# Pins la-eled A4 are analog inputs, D4 are digital input output pins, t)e ones mar.ed /it) an asteris. support pulse /idt) modulated output# 353 outputs a constant 3#35olts, 67D t)e relati*e ground and A&E3 can -e used to set an e8ternal re!erence *oltage !or t)e analog inputs#

page 3 o! "

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

2 Rotation Sensors A p)oto interrupter can -e used to detect rotation -+ placing a gear /it) spo.es on t)e dri*e s)a!t o! t)e motor so t)at eac) spo.e /ill interrupt t)e -eam# A p)oto interrupter in its most -asic !orm is an led placed near a p)ototransistor so t)at /)en an o-9ect comes in -et/een t)e led and t)e transistor t)e source to drain *oltage drops# T)e p)oto interrupter used )ere is t)e S)arp Microelectronics 6P A"3A:0003# $t )as t)ree pins, 5cc, 5out, and 6nd, /it) 5cc connected to t)e s+stem;s *oltage source and 6nd connected to ground, 5out is near 6nd /)en t)e -eam is uninterrupted, and near 5cc /)en t)e -eam is interrupted# T)is results in a s,uare /a*e output /it) a !re,uenc+ t)at is proportional to t)e speed o! t)e motor and t)e num-er o! spo.es on t)e gear# (+ routing t)is signal into one o! t)e interrupt pins on t)e Arduino, t)e num-er o! rotations can -e calculated and stored in a persistent *aria-le# T)e !ollo/ing is a simple code e8ample t)at sets up a pin !or interrupts and updates a *aria-le# <<Simple interrupts *oid setup=> ? <<attac)$nterrupt=@interruptPinA,@interruptSer*ice&outineA,@triggerA>B << interruptPin is 0 or , !or digital pin 2 or 3, interruptSer*ice&outine is t)e !unction to run << trigger is /)at /ill cause t)e $S& to run, can -e CDE, &$S$76, 3ACC$76 or C0A76E attac)$nterrupt=0,updatePosition,3ACC$76>B <<Setup pin 3 as an output, to !las) t)e -uilt in led pinMode= 3,DUTPUT>B F *olatile int position G 0B *olatile -oolean direction G !alseB <<T)e main program loop t)at runs /)ile t)e -oard is acti*e << T)is loop !las)es t)e pin 3 led at 01, eac) dela+ is H00ms *oid loop=> ? digitalErite= 3,0$60>B dela+=H00>B digitalErite= 3,CDE>B dela+=H00>B F *oid updatePosition=> ? i!=direction G true> <<mo*ing in t)e positi*e direction, increment position ?positionIIF else <<mo*ing in t)e negati*e direction, decrement position ?position%%BF F page 4 o! " Using Arduino Microcontrollers to Sense DC Motor Speed and Position

Direction in t)e a-o*e code is -eing trac.ed -ased on a *aria-le, /)ic) /ould -e generated -+ t)e Arduino /)en it sent a signal to a motor to mo*e it# To ma.e t)e s+stem more ro-ust a second p)oto interrupter can -e added suc) t)at t)e spo.es /ill interrupt -ot) at once# E)en one is interrupted, t)e $S& /ill run, it can test a digital input pin connected to t)e second sensor# $ts *alue at t)e time o! t)e interrupt /ill -e dependent on t)e direction t)e motor is spinning# T)e !ollo/ing is code t)at )as -een updated to detect t)e motion -ased on sensor input# T)is code /ill detect motor direction /it)out )a*ing to in!er an+t)ing -ased on t)e current output con!iguration# T)is means e*en e8ternal !orces acting on t)e motor s)a!t /ill -e properl+ interpreted -+ t)e controller# <<Direction Detection *oid setup=> ? <<attac)$nterrupt=@interruptPinA,@interruptSer*ice&outineA,@triggerA>B << interruptPin is 0 or , !or digital pin 2 or 3, interruptSer*ice&outine is t)e !unction to run << trigger is /)at /ill cause t)e $S& to run, can -e CDE, &$S$76, 3ACC$76 or C0A76E attac)$nterrupt=0,updatePosition,3ACC$76>B <<Setup pin 3 as an output, to !las) t)e -uilt in led pinMode= 3,DUTPUT>B <<Setup pin 3 as an input, to read t)e second sensor !or input pinMode=4,$7PUT>B F *olatile int position G 0B *olatile -oolean direction G !alseB <<T)e main program loop t)at runs /)ile t)e -oard is acti*e << T)is loop !las)es t)e pin 3 led at 01, eac) dela+ is H00ms *oid loop=> ? digitalErite= 3,0$60>B dela+=H00>B digitalErite= 3,CDE>B dela+=H00>B F *oid updatePosition=> ? i!=digital&ead=4> GG 0$60> <<mo*ing in t)e positi*e direction, increment position ?positionIIF else <<mo*ing in t)e negati*e direction, decrement position ?position%%BF F

page H o! "

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

3 Speed Detection $n order to detect t)e speed o! t)e motor and compensate !or it time must -e ta.en into account# $! t)e time is recorded at eac) pulse !rom t)e p)oto interrupter and compared to t)e time o! t)e pre*ious pulse, t)e motor rpm can -e calculated# <<Direction and Speed Detection *oid setup=> ? <<attac)$nterrupt=@interruptPinA,@interruptSer*ice&outineA,@triggerA>B << interruptPin is 0 or , !or digital pin 2 or 3, interruptSer*ice&outine is t)e !unction to run << trigger is /)at /ill cause t)e $S& to run, can -e CDE, &$S$76, 3ACC$76 or C0A76E attac)$nterrupt=0,updatePosition,3ACC$76>B <<Setup pin 3 as an output, to !las) t)e -uilt in led pinMode= 3,DUTPUT>B <<Setup pin 3 as an input, to read t)e second sensor !or input pinMode=4,$7PUT>B F *olatile int position G 0B *olatile -oolean direction G !alseB *olatile unsigned long time G 0B *olatile unsigned int dTimeB *olatile int rpmB const int spo.es G 2B <<T)e main program loop t)at runs /)ile t)e -oard is acti*e << T)is loop !las)es t)e pin 3 led at 01, eac) dela+ is H00ms *oid loop=> ? digitalErite= 3,0$60>B dela+=H00>B digitalErite= 3,CDE>B dela+=H00>B F *oid updatePosition=> ? i!=digital&ead=4> GG 0$60> <<mo*ing in t)e positi*e direction, increment position ?positionIIF else <<mo*ing in t)e negati*e direction, decrement position ?position%%BF <<millis=> returns t)e time in milliseconds since t)e -oard /as turned on dTime G millis=>%timeB time G time I dTimeB <<use t)e time calculation and t)e num-er o! spo.es to calculate t)e rpm o! t)e motor rpm G <spo.es J <dTime J 000 J '0 F page ' o! " Using Arduino Microcontrollers to Sense DC Motor Speed and Position

4 Conclusion Con!iguration o! an Arduino microcontroller to determine motor speed and direction is a !airl+ simple process# (+ using t/o p)oto interrupters on a motor and one interrupt line on t)e controller -ot) *alues can -e !ound relia-l+# Figure 2 T)e internal circuitr+ !or t)e S)arp Microelectronics p)oto interrupter# Eit) a pull up resistor -et/een 5out and 5cc t)e circuit operates as descri-ed a-o*e#

page " o! "

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

You might also like