You are on page 1of 4

ProjectArduino

October1,2015||Potentiometers

DigitalvsAnalog
Wenowmoveawayfromdigitalsignals,forthetimebeing,to
startworkwith
analog
signals.

Beforewestart,letstakealookat
eachsignalandhowtheArduinoutilizes
them.

Digital
signalsareeither:
OnorOff
HIGHorLOW
5voltsor0volts.

Inotherwords,digitalsignalshaveonlytwopossiblevalues.Wecan
usedigitalsignalstoturnonandoffLEDs.WeoutputaHIGHsignal
toturnontheLED,andthenoutputaLOWsignaltoturnofftheLED.
WhenweoutputaHIGHsignal,weareoutputting5voltsof
electricitytothedigitalpinthattheLEDisconnectedto.Whenwe
outputaLOWsignal,weareoutputting0voltsofelectricitytothe
digitalpinthattheLEDisconnectedto.

Analog
signals,ontheotherhand,havearangeofvalues.
Lookattheimageofananaloganddigitalwavesabove.Ananalog
wavecanhavearangeofvalues,whereasthedigitalwavehasonly
twopossiblevalues.

IMPORTANT:Lifeisanalog.Deathisdigital.

TheArduinousesspecialANALOGINpinsto
readanalogsignals.Wecanattachseveral
typesofsensorstoananalogpinandcode
theArduinotodosomethingwiththerange
ofvaluesitwillreceivefromthesensor.
Noticethattheseare
INPUT
pins.Wehave
previouslyworkedwith
OUTPUT
pinswhen
lightingLEDs,becauseweareoutputting
currenttotheLEDinordertomakethem
lightup.Now,wewill
read
valuesfromtheanalogpinsinsteadof
writing
tothem.Readingavalue=inputtingavalue.Writingavalue
=outputtingavalue.

Potentiometers
Potentiometersareaspecialtypeofsensorwecanusewith
theArduinotogiveusananalogsignal.

Apotentiometerisaknob,much
likethevolumecontrolonyour
carradio,withastartingand
stoppingpoint.Asyoucanseein
thediagramtotheleft,the
potentiometerisconnectedtothe
5Vpin,theGroundpin,andanalog
pin2.Asyouturntheknob,the
signalthatisreadbytheanalog
pinchanges.Thesignalranges
from0to1023.Soifyouturnthe
potentiometerknoballthewayin
onedirection,theanalogsignalwillbe0,andasyouturntheknob
intheotherdirection,thesignalwillincreaseuntilyouturnthe
knoballthewayintheotherdirection,wheretheanalogsignalis
1023.

UsingapotentiometertocontroltherateofanLEDblinking:
Wellnowthatweknowhowapotentiometerworks,andwealready
knowhowtomakeanLEDblink,wewillcombinethetwoideasandmake
aprogramwhichincreasestherateofblinkingwhenweturnthe
potentiometer.Wewillbeusingthreeintegervariablesforthis
project
intledPin,intpotPin,
and
intval.
ThevariableledPinwill
beassignedthepinnumberwhereweplugitin,andwearegoingto
beusing
pin1
forthisproject.ThevariablepotPinistheanalogin
pinthatwewillbepluggingthepotentiometerinto,inthiscase0.
Andintegervalisjustthenumericalvalue(between0and1023)that
wewillassigntothedelaybetweenblinks.WhentheArduinoreads
thesignalfromthepotentiometer,itwillsetthevariable
val
equal
tothatsignal.Sofaryoushouldhavethis:

intpotPin=0
intledPin=1
intval

NowwewillclassifyledPinasanoutputinvoidsetup,whichwillbe
theonlythinginthesetup.WedontneedtoclassifypotPinasan
inputbecausewhenweuseafunctioninthevoidloop,called
analogRead
,thearduinoknowsthatithastobeananalog.Sothe
voidsetupshouldlooklike

voidsetup(){
pinMode(ledPin,OUTPUT)
}

Thevoidloopisfairlysimplebutusesanewfunctioncalled
analogRead
.Whatthisfunctionbasicallydoesis
read
thevaluefrom
the
analog
pin.Inthefunctionweneedtodefinewhatanalogweare
readingfrom,inthiscasepotPin.Weassignthisvalue,whateverit
maybe,totheintegerval.Thiswillbeusedinthedelayfunction
lateron.AfterthatwewillturntheLEDonwithdigitalWrite.This
worksoppositetoanalogRead,weassignavaluetothedigitalpin.
Thenwewilldelaywiththedelayfunction,butinsteadofputtinga
numberinthefunctionwewillputavariable.Thereasonwedothis
isbecausewewantthedelaytochangeaccordingtowhatwe
read
from
the
analog
pin.ThenweturntheLEDoff,anddelayitagainwith
anotherdigitalWriteandanotherdelay(againwith
val
inthe
parentheses).Itshouldlooklike

voidloop(){
val=analogRead(potPin)
readsvaluefromanalogpinand
assignsittothevalvariable
digitalWrite(ledPin,HIGH)
turnsLEDon
delay(val)

digitalWrite(ledPin,LOW)
turnsLEDoff
delay(val)
}

Aftercompilinganduploadingtoyourarduino,ifyouthinkitblinks
toofastortooslowtrymakingalltheblinksslowerorfaster.
(Hint:applyamathematicaloperationsto
val
,likeaddition,
subtraction,multiplication,ordivision).

Servomotors
Aservoiscompactmotorthatis
capableofrotatingtospecificpositions
withinitsrangeofmotion.Ithasthree
wires,red,brown,orange.Tousethe
servoyoumustconnecttheredwiretothe
5vpowersourcefromtheArduino.Then
connectthebrown(orblack)wiretoa
groundandfinallyconnecttheorangewire
toAnalogpin0.
Thecodeforusingaservolookslike
this:

#include<Servo.h>

Servoservo1
//createservoobjecttocontrolaservo

intpotpin=2
//analogpinusedtoconnectthepotentiometer
intval
//variabletoreadthevaluefromtheanalogpin

voidsetup(){
myservo.attach(9)
//attachestheservoonpin9totheservo
object
}

voidloop(){
val=analogRead(potpin)
//readsthevalueofthe
potentiometer(valuebetween0and1023)
val=map(val,0,1023,0,180)
//scaleittouseitwiththe
servo(valuebetween0and180)
myservo.write(val)
//setstheservoposition
accordingtothescaledvalue
delay(15)
//waitsfortheservotoget
there
}

You might also like