101

Arduino LED Projects - Robert Davis

Embed Size (px)

DESCRIPTION

electronics

Citation preview

Page 1: Arduino LED Projects - Robert Davis
Page 2: Arduino LED Projects - Robert Davis

Arduino

LED

Projects

RobertJDavisII

Page 3: Arduino LED Projects - Robert Davis

ArduinoLEDprojectsbyRobertJDavisII

Copyright2013RobertJDavisII

LEDstandsforLightEmittingDiode.WewillbeusingoneormoreLED’sineveryprojectinthisbook.AnLEDisadiodeinthatcurrentonlyflowswhenpowerisconnectedinonedirection.LED’susuallylightupataround1.6volts.Sinceourpowersourceis5voltsweneedtoaddresistorstokeepfromdamagingtheLED’s.Resistorsresisttheflowofelectricity.SinceanidealcurrentforaLEDis10maor.01ampsandR=E/I,(5-1.6volts)/.01is3.4/.01or340ohms.Sotheresistorthatweuseshouldbelargerthan340ohms.Forthefirstfewprojectswecanuse470or1000ohmresistors.

Thereaderorbuildertakesallresponsibilityforthesafeconstructionandoperationoftheprojectsthatarecontainedinthisbook.Itisassumedthatthereaderhasatleastaminimalbackgroundinprogrammingandinelectronics.Itishighlyrecommendedthatyoufirstreadatleastoneofthetwobooksfoundinthebibliography.Thisbookdoesnotgointoasmuchdetailastohowtowritethesoftware,orhowtobuildthehardware.

Theprojectsfoundinthisbookaremeanttobesimpleenoughfortheaveragebeginner.Thepartscountswerekepttoabareminimumtogetthejobdone.IfIhavetimelateronIwillwriteasecondbookthatwillgointomorecomplexcircuitsanddesigns.Thebasicsaregivenheresothatwithsomemoreexperimentingsomeonecouldbuildmorecomplexcircuits.

Eachprojectinthisbookhasaquickexplanation,aschematic,alistofpartsneeded,andasoftwareorsketchlisting.ThisbookisonlymeanttogetyoustartedatbuildingsomeArduinobasedprojects.Feelfreetomodify,improve,oreven“play”withthesoftwareandhardware.Electronicscanbelotsoffunandthatiswhatyoushoulddowiththeseprojects.Havefun!

MostofthepartstobuildtheprojectsinthisbookareavailableatyourlocalRadioShackstore.IfyouhavelotsofpatiencethenyoucanalsofindthemforsaleoneBay.BeawarethatmanyofthesellersoneBayareinlocatedinChinaandhenceitwilltakealongtimeforyourpartstobeshippedtoyoufromChina.

Page 4: Arduino LED Projects - Robert Davis

Chapters:

Introduction

1.IntroductiontoElectronicComponents

2.IntroductiontoMicroprocessors

3.SettingupanArduino

4.ElectronicCoinFlipper

5.ElectronicDice

6.WheelofPrizes

7.BackandForthScanner

8.QuizPracticeMachine

9.IntroducingLEDArrays

10.SevenSegmentDisplayCounter

11.Meter/TemperatureDisplay

12.ThreeDigitMeter

13.EightbyEightLEDArray

14.DualColorEightbyEightLEDArray

15.DualColorEightby15LEDArray

16.ThreeColorEightbyEightLEDArray

17.DualColorEightby40LEDArray

Bibliography

Page 5: Arduino LED Projects - Robert Davis
Page 6: Arduino LED Projects - Robert Davis

Chapter1

IntroductiontoElectronicComponents

Inthisbookwewillbeusingseveralelectroniccomponents.Thisisonlyabriefintroductiontosomeofthecomponentsthatwewillbeusinginthisbook.

A“resistor”isadevicethatresistsorlimitstheflowofelectricity.Usuallyitismadeoutofcarbon,butthesedaystherearelotsofthingsthatareusedtomakeresistors.Thisiswhatsomeresistorslooklike.Thereareonequarterwattresistorsontheleft,onehalfwattresistorsontherightandaonewattresistoronthefarright.

Thisistheschematicsymbolforaresistor.

Thereisalsowhatiscalleda“Variableresistor”.Theseresistorscanbevariedbyturningaknoboradjustedwithascrewdriver.Theycanalsobeconnectedtoapowersourcesuchthattheycanadjustthevoltageoutthrougharangeofvoltages.Herearesometypicalvariableresistors.

Page 7: Arduino LED Projects - Robert Davis

Thisistheschematicsymbolforavariableresistor.

Diodesaredevicesthatonlyallowcurrenttoflowinonedirection.BecauseofthattheycanbeusedtoconvertAC(AlternatingCurrent)toDC(DirectCurrent).Theyarealsousedasprotectiondevicestoprotectdelicateelectronicsfromsurges.Hereisapictureofsometypicaldiodes.

Theschematicsymbolforadiodeisanarrowpointinginonedirection.

NextupwehavetheLEDorLightEmittingDiode.Thisdeviceisadiodeintheelectricitywillonlyflowinonedirection.Whenelectricityflowsthroughit,theLEDwill

Page 8: Arduino LED Projects - Robert Davis

lightup.MostLED’slightupataround1.6volts.Sincemostsourcesofelectricityarealotmorethan1.6volts,aresistorshouldbeplacedinserieswithaLEDtodropthevoltagedowntothecorrectvoltagefortheLED.HerearesometypicalLED’s,anarrayofLED’sandseveralindividualLED’s.

ThisistheschematicsymbolforaLED.

Switchesaredevicesthatcanturnonandturnoffelectricityortheycanrerouteitsoitgoestoadifferentplace.TheycomeinNormallyOpen(NO),NormallyClosed(NC),SinglePoleSingleThrow(SPST)DoublePoleDoubleThrow(DPDT)andmanyversionsofMultiplePoleMultipleThrow.Someswitcheshaveasmanyas12positionsormore.Hereisapictureofsomeswitches.Thetoprowhasmomentarycontacttype,thebottomrowhasSPSTandDPDTswitches.

Page 9: Arduino LED Projects - Robert Davis

Herearesomeschematicdiagramsofsometypicalswitches.

Transistorsarelikeswitches,buttheyworkelectronically.Theycannotonlyturnthingsonandoff,buttheycanamplifytoo.Thatmeansthattheycantakeasmallervoltageorcurrentinandthenturnonoroffalargervoltageorcurrent.Hereisapictureofsometypicaltransistors.Theoneontherightisa“powertransistor”.

Page 10: Arduino LED Projects - Robert Davis

Herearesomeschematicdiagramsoftypicaltransistors.

Ifyouputmanytransistorstogetherintoone“case”youhavewhatiscalledan“IntegratedCircuitorICforshort.Somehaveasfewaseighttransistors,andsomehavemanymillionsoftransistorsinsideofthem.HereisapictureofsometypicalIC’s.

IC’sareusuallydiagramedasaboxwithabunchofwiresgoinginoroutofit.Theirconnectionshavepinnumbersthatstartwithpin1atthenotchedend,andtheythengocounterclockwisearoundtheICwhenviewedfromabove.Hereistheschematicdiagramofa74HC595IC.

Page 11: Arduino LED Projects - Robert Davis

A“Breadboard”isadevicethatallowsyoutoeasilyandtemporallymakeandtroubleshootanelectronicdesign.Ithasseveralconnectorsinsidethatconnectsetsofpinstogether.Hereiswhatonelookslikewiththecoverremovedandarowofconnectorsexposed.Therearehorizontalrowsthatthecomponentsareconnectedon,andthereareverticalrows(toptobottom)thatareusuallyusedaspowersupplybusses.

Page 12: Arduino LED Projects - Robert Davis

Chapter2

IntroductiontoMicroprocessors

Microprocessorshavebeenaroundforalongtime.Actuallymosthomecomputersinusetodayarebaseduponmicroprocessors.Howeverwhatwearereferringtohereinthisbookarethemicroprocessorsthatareprimarilyusedbyhobbyists.

Amicroprocessorrunswhatiscalled“Code”or“Software”.Thissoftwareworkslikeashoppinglist.Inatypicalshoppinglistyouaretoldtogotothefruitisleandgetanorange,thengotothecerealisleandgetfruitloops,thengotothemeatsectionandgetsomechickenwings.Inmuchthesamewayamicroprocessorexecutescommandsoneatatime.Microprocessorsareratedonhowmuchcodetheycanhold(RAMandROM),onwhatI/O(InputsandOutputs)theyhave,andonhowfasttheycanrunthecode.

Thefirsthobbyistmicroprocessorthatwascommonlyusedwasthe8052AHBasic.Itwasanenhancedversionofthe8051.Therearemanyversionsofthe8051availabletodaysuchasthe80C451.The8052AHBasicfeaturedserialcommunicationwithautomaticbaudratedetectionanditincludedBasicinROM.ThetwomaindrawbackswerethattoaddmorememoryyouhadtosacrificeseveralI/OPinsandthatithadnoanaloginputinputs.AsaresultyouusuallymemorymappedtheI/Odevicessuchasananalogtodigitalconverter.

Thenextcommonhobbyistmicroprocessorwasthe68HC11.Itoffered8analoginputs,anduptosixeightbitdigitalinputandoutputportsdependingontheversion.Thatisatotalofupto56I/OpinsontheF1version.Italsofeaturedbuiltinserialcommunications.Ithad512bytesofEEPROMand1024bytesofRAMforyourprograms.Thedrawbackswerethatitwasprogrammedinassembler,andthatyouhadtoprogramitononecircuitboardandthentransfertheCPUtothe“live”circuitboardorsacrificealotofI/Opins.Thereweremanyversionsofthe68HC11thatwereusedinmanydifferentapplications.Youmightevenhaveonethatisbuiltintoandthatisactuallyrunningyourcar!

Thispictureisofatypicalhomemade68HC11-F1controller.Theeightdigitalinputsareontheleft,theeightanaloginputsareacrossthetop,andtheeightdigitaloutputsareontheright.ThereisaULN2008driveronthedigitaloutputssotheycandriveoutputrelays.ThevoltageregulatorislocatedinbottomcenterandtheRS-422serialislocated

Page 13: Arduino LED Projects - Robert Davis

onthebottomleft.TherearefourDIP(DualInlinePackage)switchesonthebottomrighttoselecttheserialaddresssothatseveralofthesecontrollerscanbeconnectedtogetheronthesameRS-422serialbuss.

Thenextcommonhobbyistmicroprocessorwascalledthe“BasicStamp”.ItfeaturedBasicinROM.TheCPUandalltherequiredsupportpartsweremountedonasmall24pinchipaboutthesizeofalargepostagestamp.Itfeatured2048bytesofEEPROMand32bytesofRAMforyourprograms.Itfeatured16I/Opins.DrawbacksoftheBasicStampincludedthattherewerenoanaloginputsandBasicisrelativelyslow.Howeverthiswasthemostpopularhobbymicroprocessorupuntilveryrecently.

Inthepicturebelow,theBasicStampchipistheredchipthatistopandcenter.Ontheleftsideistheserialinterfacejack,andontherightsideistheoptionalvoltageregulator.InthebottombreadboardareatherearetwosevensegmentLEDdisplaysthatwereusedforaBasicStampproject.

Page 14: Arduino LED Projects - Robert Davis

Thelatestcommonhobbyistmicroprocessor,andthesubjectofthisbook,iscalledthe“Arduino”.Itfeaturesbothsixanaloginputs,14digitalI/Opinsandahigherlevelprogramminglanguage.ItisessentiallyprogrammedinaversionofC.Italsooffers14digital,softwareselected,inputoroutputpins.SomeoftheI/Opinscanbeconfiguredasanalogoutputsaswell.Ithas1024bytesofEEPROM,2028bytesofRAMandthenagenerous32K(that’s32,000bytes)ofFlashmemoryforyourapplications.OneofthebestfeaturesisthesupportforprogrammingtheArduinoviaaUSBcable.Manynewercomputersarelackingtheserialportsthatwereneededtoworkwithmostoftheoldermicroprocessors.

ThepicturebelowisofanArduinoUNOrevision3.Ontheleftsidethereareeightpowerpinswithreset,regulated,3.3volts,regulated5volts,ground,andunregulated“Vin”(Voltageinputfromthepowerjack)outputs.Belowthatontheleftsidetherearethesixanaloginputpins.Ontherightsidethereare14digitalinputoroutputpins.ThereisalsoagroundandAnalogtoDigital(AtoD)converterreferencevoltageoutputavailableontherightside.Acrossthetop,ontheleftthereisapowerjackfor6to9voltsDCfromanoptionalACadapterandontherightthereisaUSBjacktoplugintoacomputertoprogramtheArduinomicroprocessor.

Page 15: Arduino LED Projects - Robert Davis

BelowisapictureofanArduinoclone.Itisacloneofanearlierversionsoithaslesspins.Thelabelsonthepinsaremucheasiertoread.NoticethatoverallitisverysimilartotheArduinoabove.

Page 16: Arduino LED Projects - Robert Davis

Chapter3

SettingupanArduino

TogetstartedwiththeArduinoitisrecommendedthatyoufirstreadoneortwoofthebooksthatarefoundinthebibliography.HoweverhereisaverybriefsetofinstructionsthatmightactuallybesufficienttogetyoustartedwithanArduino.

FirstyouwillneedtodownloadandthenunziptheArduinodriversoftware.Inthepicturebelowthedriversoftwarepackageiscalled“Arduino-0022”.

UnfortunatelywhenyoupluganArduinointotheUSBportitisnotautomaticallyrecognizedbyWindows.YouhavetosteertheWidowshardwareinstallertothedirectorywhereyouhaveunzippedthedriverssothatitcanfindtheDLLfilesasseeninthepicturebelow.

Page 17: Arduino LED Projects - Robert Davis

Alsonotethe“Arduino.exe”filethatwaslistedamongtheunzippedfilestwopicturesback.ItiswhatyouwillneedtoclickontostartworkingwiththeArduinoafteryouplugintheserialcableandgetthedriversproperlyinstalled.IusuallycreateashortcuttoArduino.exeonthedesktopofmycomputer.WhenyouclickonArduino.exeyoushouldseethis:

Page 18: Arduino LED Projects - Robert Davis

Youcannowcutandpasteyourcodeintothewhiteareabelowthe“sketch”tab.TouploadyourcodeintotheArduinoyouwillneedtomakesurewhatyourmodelofArduinoisandthattheserialportsareproperlyselectedfirst.Theyaresetupunder“tools”“board”and“tools”“serialport”.IhavesometimeshadtoguessattheboardnameonsomeArduinoclones.

BelowisthelistofthemanysupportedArduinoboardsatthistime.NotethattheyaremostlybasedoneithertheATMega168ortheATMega328chips.Thesecondquestionisdoesitoperateon5voltsoron3.3volts?Theprojectsinthisbookassume5voltoperationbuttheylikelywillalsoworkon3.3voltversionsoftheArduinoaswell.

Ifeverythingseemstobeworkingproperly,youcannowconnectacoupleofLED’stotheArduino.ThenyoucanuploadsomesimplecodetoseeiftheLED’sblink.Hopefullyyouarenowupandrunningsoyoucanstartbuildingthecircuitsfoundinthisbook.

EveryArduinoprogramor“Sketch”shouldhavefourparts.Firstyoushouldidentifytheprogram,theauthor,thedate,andperhapstheversion.Itcouldlooksomethinglikethis:

//Name:LEDbackandforthblinker//Author:BobDavis

Page 19: Arduino LED Projects - Robert Davis

//Date:5March,2013//Version:1.0

CommentsthataretobeignoredbytheArduinocanstartwith“//”.Secondlyyouneedtoestablishyourvariables,likethis.Notethat“int”standsforinteger.

//PinsfortheLED’sintLED1Pin=1;intLED2Pin=2;Thirdlyyouneedtohavewhatiscalled“setup()”.Itestablishesthingsthatonlyneedtobedoneonlyonce.SincethedigitalpinscanbeeitherinputoroutputpinsyouwillneedtotelltheArduinoiftheywillbeusedforinputoroutput.

//SetthepinstooutputtotheLED’svoidsetup(){pinMode(LED1Pin,OUTPUT);pinMode(LED2Pin,OUTPUT);}Lastlyyouhavetheheartoftheprogram,itiscalledthe“loop()”.ThissimpleprogramwillblinkthetwoLED’sbackandforth,whentheyareconnectedtoArduinodigitalI/Opinsoneandtwo.

voidloop(){digitalWrite(LED1Pin,HIGH);digitalWrite(LED2Pin,LOW);

delay(500);digitalWrite(LED2Pin,HIGH);digitalWrite(LED1Pin,LOW);

delay(500);}

Page 20: Arduino LED Projects - Robert Davis

Mostofthecircuitsinthisbookwerefirstmadeonwhatiscalleda“breadboard”.HereisapictureofatypicalbreadboardsetupwithanArduinoclone.Thebluewireisconnectedtoground.IftheLED’sareconnectedbackwardstheywillnotlightup.ThereisaflatspotontheLED’srightorgroundsideinthepicture.

InthepictureabovetheArduinocloneisontheleftandontherightisasmallbreadboard.YoucanuseshortjumperwiresfromtheArduinooutputstothebreadboard.ThatarrangementwouldmaketheLED’seasiertosetup.TheLED’slightupat1.6voltssoresistorsneedtobewiredinseriessothattheydonotoverheatordamagethefivevoltArduinooutputsortheLED’s.Iused470ohmresistors,butanyvaluefrom100ohmsallthewayupto1000ohmsshouldworkjustfine.

Theinputswitchesthatareusedinsomeoftheseprojectsshouldnotrequiretheuseofpullupresistorstoworkproperly.ThereareinternalpullupresistorsinsideoftheArduinoprocessor.Theyareturnedonbywritinga“HIGH”toaninputpinsuchas:“digitalWrite(inputpin,HIGH)”.

HereisapictureoftypicalinputswitcheswithwiressolderedontobeabletoplugthemintoabreadboardortheArduino.

TherearesomelimitsonwhatyoucandowithwhatI/Opin.ForinstanceD0hastobeaninputpin.IfyoumakeitanoutputpinyouwilllikelyloosetheabilitytouploadcodetotheArduinovia

TheanaloginputpinscanbecalledA0toA5ortheycanbecalledD14toD19.Besidesthattheycanalsobeusedasdigitalinputorasdigitaloutputpins.Tochangetheanalogpinsmodejustusethe“pinMode(A0,OUTPUT)”or“pinMode(A0,INPUT)”command.

Page 21: Arduino LED Projects - Robert Davis

Onceitisadigitalinputpin,youcanalsoactivatetheinternalpullupresistor.

Page 22: Arduino LED Projects - Robert Davis

Chapter4

ElectronicCoinFlipper

MostArduinoprojectbookswillstartyouoffwithaprojectthatblinksoneortwoLED’s.HerewewillstartoffwithtwoLED’sandthenaddaswitchtomakesomethingthatmightactuallybeuseful.Itisan“ElectronicCoinFlipper”.Eachtimeyoupushtheswitch,itwillrandomlycomeupwitheitherthe“Heads”or“Tails”LED’s.

ItworksbyreadingtheanaloginputpintoseeiftheswitchisbeingdepressedbeforeadvancingtothenextLED.AslongastheswitchishelddowntheLED’swillrapidlyblinkbackandforth.WhenyoustoppushingdownontheswitchtheLED’swillstopatthelastone.

Hereisthewiringschematic:

Hereisalistoftheneededpartstomakethiswork,besidestheArduinoitself.

1. Normallyopen,momentarycontactswitch½inchindiameter.

Page 23: Arduino LED Projects - Robert Davis

2. TwoLED’sthatthatcanbeanycolor.3. Two470ohmresistors

Hereisthecodeorsketchtomakeitwork.NotethatIswitchedtheanaloginputsintodigitalinputmodeandturnedontheinternalpullupresistor.

//****************************************************////Name:LEDDriverCoinFlip//Author:BobDavis//Date:5March,2013//Version:1.1//****************************************************////PinsfortheLED’sintLED1Pin=1;intLED2Pin=2;intLED3Pin=3;intLED4Pin=4;intLED5Pin=5;intLED6Pin=6;intLED7Pin=7;//SetthepinstooutputtotheLED’svoidsetup(){pinMode(LED1Pin,OUTPUT);pinMode(LED2Pin,OUTPUT);pinMode(LED3Pin,OUTPUT);pinMode(LED4Pin,OUTPUT);pinMode(LED5Pin,OUTPUT);pinMode(LED6Pin,OUTPUT);pinMode(LED7Pin,OUTPUT);pinMode(A0,INPUT);digitalWrite(A0,HIGH);}

Page 24: Arduino LED Projects - Robert Davis

voidloop(){if(digitalRead(A0)==LOW){digitalWrite(LED1Pin,HIGH);digitalWrite(LED2Pin,LOW);

}delay(50);if(digitalRead(A0)==LOW){digitalWrite(LED2Pin,HIGH);digitalWrite(LED1Pin,LOW);

}delay(50);}

Page 25: Arduino LED Projects - Robert Davis

Chapter5

ElectronicDice

Ifwetaketheelectroniccoinflipperonestepfurtherwecanmakeelectronicdice.WewillneedtoarrangetheLED’ssothattheywillappearlikethedotsonthefacesofdice.Wewillalsohavetomakethesoftwarelightupthenumbersforonetosixinthecorrectarrangementstomatchwhatyouwouldfindonthefaceofthedice.

Hereonceagain,whenyouholddowntheswitch,thecounteradvancesandwhenyouletofftheswitchitstopsatwhatevernumberwaslast.Howeverifyoulookcloselythesoftwarethecounterneveractuallystopsrunning!Thatwaythenexttimethatyoupushtheswitchitdoesnotcontinuewhereitleftoff,butcomesup“randomly”.

HereisthearrangementoftheLED’s

ThiswouldbetheorderthattheLED’swouldlightupin:

14

22,6

32,4,6

41,3,5,7

51,3,4,5,7

61,2,3,5,6,7

Page 26: Arduino LED Projects - Robert Davis

Hereisalistofthepartsthatareneededtomakethiswork,besidestheArduinoitself.

1. OneNormallyopenmomentarycontactswitch½inchindiameter.2. SevenLED’scanbeanycolor.3. Seven470ohmresistors

Hereistheschematicdiagramofhowtoconnecteverythingup:

Hereisthesketchorcodetomakeitwork:

//****************************************************////Name:LEDDiceDriver//Author:BobDavis//Date:5March,2013//Version:1.1//****************************************************////PinsfortheLED’sintLED1Pin=1;intLED2Pin=2;intLED3Pin=3;intLED4Pin=4;intLED5Pin=5;

Page 27: Arduino LED Projects - Robert Davis

intLED6Pin=6;intLED7Pin=7;//SetthepinstooutputtotheLED’svoidsetup(){pinMode(LED1Pin,OUTPUT);pinMode(LED2Pin,OUTPUT);pinMode(LED3Pin,OUTPUT);pinMode(LED4Pin,OUTPUT);pinMode(LED5Pin,OUTPUT);pinMode(LED6Pin,OUTPUT);pinMode(LED7Pin,OUTPUT);pinMode(A0,INPUT);digitalWrite(A0,HIGH);}voidloop(){for(intdice=1;dice<8;dice++){if(digitalRead(A0)==LOW){digitalWrite(LED1Pin,LOW);digitalWrite(LED2Pin,LOW);digitalWrite(LED3Pin,LOW);digitalWrite(LED4Pin,LOW);digitalWrite(LED5Pin,LOW);digitalWrite(LED6Pin,LOW);digitalWrite(LED7Pin,LOW);if(dice==1)digitalWrite(LED4Pin,HIGH);if(dice==2){digitalWrite(LED2Pin,HIGH);digitalWrite(LED6Pin,HIGH);}if(dice==3){digitalWrite(LED2Pin,HIGH);digitalWrite(LED4Pin,HIGH);digitalWrite(LED6Pin,HIGH);}

Page 28: Arduino LED Projects - Robert Davis

if(dice==4){digitalWrite(LED1Pin,HIGH);digitalWrite(LED3Pin,HIGH);digitalWrite(LED5Pin,HIGH);digitalWrite(LED7Pin,HIGH);}if(dice==5){digitalWrite(LED1Pin,HIGH);digitalWrite(LED3Pin,HIGH);digitalWrite(LED4Pin,HIGH);digitalWrite(LED5Pin,HIGH);digitalWrite(LED7Pin,HIGH);}if(dice==6){digitalWrite(LED1Pin,HIGH);digitalWrite(LED2Pin,HIGH);digitalWrite(LED3Pin,HIGH);digitalWrite(LED5Pin,HIGH);digitalWrite(LED6Pin,HIGH);digitalWrite(LED7Pin,HIGH);}if(dice==7){digitalWrite(LED1Pin,HIGH);digitalWrite(LED2Pin,HIGH);digitalWrite(LED3Pin,HIGH);digitalWrite(LED4Pin,HIGH);digitalWrite(LED5Pin,HIGH);digitalWrite(LED6Pin,HIGH);digitalWrite(LED7Pin,HIGH);}}

delay(50);}}

Page 29: Arduino LED Projects - Robert Davis

Chapter6

WheelofPrizes

Thiscanphysicallybeaverylargeprojecttobuild.Ihaveuseda4footby4footpieceofplywoodtomakeit.Itcouldbemadefroma2ora3feetsquarepieceofwoodaswell.Theframeismadeoutofsome2x4’swithagrovecutintothemthatiswideenoughtofittheedgeoftheplywood.Basicallyyoumount8brightLED’sinsocketsatequalintervalsaroundacircle.InthemiddleofthecirclethereareprizepanelsthatareeithergluedonorheldinplacebysomeVelcro.Theoperatorhitsthestartbuttonandthecontestantthenhitsthestopbutton.Whateverprizethelightstopson,iswhatthecontestantwins.

Thistimewearegoingtousetwopushbuttons,onetostartitandonetostopit.Youcouldeasilymodifythehardwaredesignandsoftwaretohaveasmanyas10oreven12prizepositionsaroundthecircleifyouwantedtooffermoreprizes.

Hereistheschematicshowinghowtowireitup:

Thisisadrawingofwhatthecompletedprojectmightlooklike:

Page 30: Arduino LED Projects - Robert Davis

Hereisalistoftheneededpartstomakethiswork,besidestheArduinoitself.

1.Twonormallyopenmomentarycontactswitch½inchindiameter.

2.EightLED’scanbeanycolor.

3. Eight470ohmresistors

Hereisthe“Sketch”codetomakeitwork:

//*************************************************///Name:LEDWheelofPrizes//Author:BobDavis//Date:5March,2013//Version:1.1//*************************************************///PinsfortheLED’sintLED1Pin=1;intLED2Pin=2;intLED3Pin=3;intLED4Pin=4;

Page 31: Arduino LED Projects - Robert Davis

intLED5Pin=5;intLED6Pin=6;intLED7Pin=7;intLED8Pin=8;intspin=1;intprize=1;//SetthepinstooutputtotheLED’svoidsetup(){pinMode(LED1Pin,OUTPUT);pinMode(LED2Pin,OUTPUT);pinMode(LED3Pin,OUTPUT);pinMode(LED4Pin,OUTPUT);pinMode(LED5Pin,OUTPUT);pinMode(LED6Pin,OUTPUT);pinMode(LED7Pin,OUTPUT);pinMode(LED8Pin,OUTPUT);pinMode(A0,INPUT);pinMode(A1,INPUT);digitalWrite(A0,HIGH);digitalWrite(A1,HIGH);}voidloop(){if(prize>8)prize=0;if(digitalRead(A0)==LOW)spin=1;if(digitalRead(A1)==LOW)spin=0;if(spin==1){prize++;digitalWrite(LED1Pin,LOW);digitalWrite(LED2Pin,LOW);digitalWrite(LED3Pin,LOW);digitalWrite(LED4Pin,LOW);

Page 32: Arduino LED Projects - Robert Davis

digitalWrite(LED5Pin,LOW);digitalWrite(LED6Pin,LOW);digitalWrite(LED7Pin,LOW);digitalWrite(LED8Pin,LOW);if(prize==1)digitalWrite(LED1Pin,HIGH);if(prize==2)digitalWrite(LED2Pin,HIGH);if(prize==3)digitalWrite(LED3Pin,HIGH);if(prize==4)digitalWrite(LED4Pin,HIGH);if(prize==5)digitalWrite(LED5Pin,HIGH);if(prize==6)digitalWrite(LED6Pin,HIGH);if(prize==7)digitalWrite(LED7Pin,HIGH);if(prize==8)digitalWrite(LED8Pin,HIGH);}

delay(50);}

Page 33: Arduino LED Projects - Robert Davis

Chapter7

BackandForthScanner

YoucanmakeseveralLED’sblinkinsuccessiondownalinewithoutusingamicroprocessor,butcanyoumakethemreversetheirdirectionandgobackandforthwithoutusingamicroprocessor?Idoubtit.Youcouldaddthiscircuittoyourcarandpretendthatitisacomputerizedcar,butactuallyitiscomputerized!

Hereisadrawingoftheideashowinghowitwouldwork.FirstthelitLED’stravelinonedirectionandthentheyreverseandtravelintheoppositedirection.

Thisisapictureofwhatoneofthesescannerslookslike.Thiswasfirstusedina“timemachine”.Theswitchontherightwasanon/offswitch.

BelowisthewiringdiagramofhowtoconnecttheLED’stotheArduino:

Page 34: Arduino LED Projects - Robert Davis

Hereisalistofthepartslisttomakethiswork,besidestheArduinoitself.

1. TenLED’scanbeanycolor.2. Ten470ohmresistors

HereisthesoftwarefortheArduinotomakeitwork:

//***********************************************///Name:LEDBackandForth//Author:BobDavis//Date:5March,2013//Version:1.0//***********************************************///PinsfortheLED’sintLED1Pin=1;intLED2Pin=2;intLED3Pin=3;intLED4Pin=4;intLED5Pin=5;intLED6Pin=6;

Page 35: Arduino LED Projects - Robert Davis

intLED7Pin=7;intLED8Pin=8;intLED9Pin=9;intLED10Pin=10;//SetthepinstooutputtotheLED’svoidsetup(){pinMode(LED1Pin,OUTPUT);pinMode(LED2Pin,OUTPUT);pinMode(LED3Pin,OUTPUT);pinMode(LED4Pin,OUTPUT);pinMode(LED5Pin,OUTPUT);pinMode(LED6Pin,OUTPUT);pinMode(LED7Pin,OUTPUT);pinMode(LED8Pin,OUTPUT);pinMode(LED9Pin,OUTPUT);pinMode(LED10Pin,OUTPUT);}voidloop(){for(intscan=1;scan<19;scan++){digitalWrite(LED1Pin,LOW);digitalWrite(LED2Pin,LOW);digitalWrite(LED3Pin,LOW);digitalWrite(LED4Pin,LOW);digitalWrite(LED5Pin,LOW);digitalWrite(LED6Pin,LOW);digitalWrite(LED7Pin,LOW);digitalWrite(LED8Pin,LOW);digitalWrite(LED9Pin,LOW);digitalWrite(LED10Pin,LOW);if(scan==1)digitalWrite(LED1Pin,HIGH);if(scan==2)digitalWrite(LED2Pin,HIGH);

Page 36: Arduino LED Projects - Robert Davis

if(scan==3)digitalWrite(LED3Pin,HIGH);if(scan==4)digitalWrite(LED4Pin,HIGH);if(scan==5)digitalWrite(LED5Pin,HIGH);if(scan==6)digitalWrite(LED6Pin,HIGH);if(scan==7)digitalWrite(LED7Pin,HIGH);if(scan==8)digitalWrite(LED8Pin,HIGH);if(scan==9)digitalWrite(LED9Pin,HIGH);if(scan==10)digitalWrite(LED10Pin,HIGH);if(scan==11)digitalWrite(LED9Pin,HIGH);if(scan==12)digitalWrite(LED8Pin,HIGH);if(scan==13)digitalWrite(LED7Pin,HIGH);if(scan==14)digitalWrite(LED6Pin,HIGH);if(scan==15)digitalWrite(LED5Pin,HIGH);if(scan==16)digitalWrite(LED4Pin,HIGH);if(scan==17)digitalWrite(LED3Pin,HIGH);if(scan==18)digitalWrite(LED2Pin,HIGH);

delay(500);}}

Page 37: Arduino LED Projects - Robert Davis

Chapter8

QuizPracticeMachine

TheQuizpracticemachineisforpracticingforwhatarecalled“Quizrallies”.TherecanbeuptosixcontestantsatonetimeusingtheArduinoanaloginputpins.Eachcontestanthasahandheldswitchtopushiftheyknowwhattheansweris.ThefirstcontestanttopushhisbuttonwillhavehisLEDlightcomeonandanoptionalbuzzerwillsound.Atthispointalloftheothercontestantsarelockedout.Themoderatorneedstopusha“reset”buttoninorderforthegametocontinue.

ThisisapictureshowingwhataQuizPracticeMachinelookslike.Acrossthefront(thefarside)arethesixLED’s,threeredandthreegreenincolor.Theyaremountedonasmallcircuitboard.InthemiddleistheArduinoprocessor.Acrossthebackarethepowerandresetconnector,thetwosetsofthreewiresfromthepushbuttonsandthebuzzerisontheright.

ThehandheldbuttonsaremadefromfourtofiveinchlongpiecesofhalfinchdiameterPVCpipe.AnalternativetothePVCpipeistousethecenteroutofoldCDspindles.AfourorfivefootpieceoftwoconductorspeakerwireisconnectedtoaswitchthatfitsneatlyinsideofthePVCpipe.Glue,heatshrinkinsulation,orelectricaltapecanbeusedtoholdtheswitchtightlyinplace.TheotherendofthespeakerwiregoestotheArduino

Page 38: Arduino LED Projects - Robert Davis

analoginputpins.Hereisapictureofatypicalinputswitch.

Thisistheschematicdiagram,notetheextra“reset”switchthatislocatedontheright.

Hereisalistoftheneededpartstomakethiswork,besidestheArduinoitself.

1. Sixnormallyopenmomentarycontactswitchesabout½inchindiameter.2. Onenormallyopenmomentarycontactresetswitch3. SixLED’scanbeanycolor.4. Six470ohmresistors.5. Optionalbuzzeronpin11

Thisisthesoftwareorsketchtomakeitwork.NotthatIturnedtheanaloginputsintodigitalinputs.Thatchangegavemuchmorereliableresults.Ithinkthattheanaloginputsignorethebuiltinpullupresistors.

//************************************************/

Page 39: Arduino LED Projects - Robert Davis

//Name:LEDQuizPractice//Author:BobDavis//Date:5March,2013//Version:1.1//************************************************///PinsfortheLED’sintLED1Pin=1;intLED2Pin=2;intLED3Pin=3;intLED4Pin=4;intLED5Pin=5;intLED6Pin=6;intLED7Pin=7;//AnaloginputpinsA0-A5intin0Pin=14;intin1Pin=15;intin2Pin=16;intin3Pin=17;intin4Pin=18;intin5Pin=19;intBeepPin=11;intResSwitch=12;intlatch=0;//SetthepinstooutputtotheLED’svoidsetup(){pinMode(LED1Pin,OUTPUT);pinMode(LED2Pin,OUTPUT);pinMode(LED3Pin,OUTPUT);pinMode(LED4Pin,OUTPUT);pinMode(LED5Pin,OUTPUT);

Page 40: Arduino LED Projects - Robert Davis

pinMode(LED6Pin,OUTPUT);pinMode(LED7Pin,OUTPUT);pinMode(in0Pin,INPUT);pinMode(in1Pin,INPUT);pinMode(in2Pin,INPUT);pinMode(in3Pin,INPUT);pinMode(in4Pin,INPUT);pinMode(in5Pin,INPUT);pinMode(ResSwitch,INPUT);pinMode(BeepPin,OUTPUT);digitalWrite(in0Pin,HIGH);digitalWrite(in1Pin,HIGH);digitalWrite(in2Pin,HIGH);digitalWrite(in3Pin,HIGH);digitalWrite(in4Pin,HIGH);digitalWrite(in5Pin,HIGH);digitalWrite(ResSwitch,HIGH);}voidloop(){if(latch==0){digitalWrite(LED1Pin,LOW);digitalWrite(LED2Pin,LOW);digitalWrite(LED3Pin,LOW);digitalWrite(LED4Pin,LOW);digitalWrite(LED5Pin,LOW);digitalWrite(LED6Pin,LOW);if(digitalRead(in0Pin)==LOW){digitalWrite(LED6Pin,HIGH);latch=1;}if(digitalRead(in1Pin)==LOW){digitalWrite(LED5Pin,HIGH);

Page 41: Arduino LED Projects - Robert Davis

latch=1;}if(digitalRead(in2Pin)==LOW){digitalWrite(LED4Pin,HIGH);latch=1;}if(digitalRead(in3Pin)==LOW){digitalWrite(LED3Pin,HIGH);latch=1;}if(digitalRead(in4Pin)==LOW){digitalWrite(LED2Pin,HIGH);latch=1;}if(digitalRead(in5Pin)==LOW){digitalWrite(LED1Pin,HIGH);latch=1;}

}if(latch==1){digitalWrite(BeepPin,HIGH);delay(50);if(digitalRead(ResSwitch)==LOW){latch=0;digitalWrite(BeepPin,LOW);}

}digitalWrite(LED7Pin,HIGH);//poweronindicator}

Page 42: Arduino LED Projects - Robert Davis

Chapter9

IntroducingLEDArrays

WhenyouneedtousemorethanjustahandfullofLED’sitispossibletogetthempreassembledandprearrangedintoseveraldifferentpatterns.Thefirstcommonarrangementiswhatiscalledthe“sevensegment”numericdisplay.TheydisplaythenumberszerotonineusingsevenLED’s.Thesedisplaysareusedalotinpanelmetersandinsomeoldercalculators.Theyarealsoavailablewithone,two,threeandIhaveseenasmanyassixdigits.BelowisapictureofsometypesofsevensegmentLEDdisplays.

YouwillneedtoknowthatalmostanyLEDarraycanbefoundinboth“CommonCathode”(CC)and“CommonAnode”(CA)arrangements.ThatistosaythatthecommonsideoftheLED’scanbegroundedtoworkinthecommoncathodearrangement.Thecommonsidecanbeconnectedtopowerinthecaseofthecommonanodearrangement.Theterms“cathode”and“anode”arefromtheoldvacuumtubedayswhenthecathodeofatubeemittedtheelectronsandtheanodecollectedtheelectrons.BelowaretwoschematicdiagramsshowingboththecommoncathodeandcommonanodeLEDarrangements.

Page 43: Arduino LED Projects - Robert Davis

IfyoudonotknowthepinconnectionsofanLEDdisplay,itcanbedeterminedbyusinganinevoltbatteryanda1000ohmresistor.Juststartinonecornerwithapplyingpowerthenusetheresistortoapplygroundtoalloftheotherpins.TakenotesonwhatconnectionswillcausewhatLED’stolightup.WiththistrialanderrormethodyoucanquicklyreconstructthewiringofalmostanyLEDarray.AnothersolutionistoGooglethepartnumberandlookforthewiringdiagram.

Anotherimportantconsiderationisthedrivecapacityofmostlogiccircuits.Mostlogiccircuitscanonlydriveabout20ma,or.02ampstoground.ThatisenoughpowertolightoneortwoLED’s.HoweverthecommonconnectiontotheLED’sinacommoncathodeorcommonanodearrangementmustpowersevenoreightLED’s.Thatwouldtakefrom70to80maor.08ampstolightallsevenoreightofthem.ThisamountofpowerwillrequirespecialdriverIC’s,ortheuseofpowertransistors.Forlessthanonehalfofanampyoucanusea2N2222(NPN)ora2N2907(PNP)transistor.Formoreamps,PowerDarlingtontransistorsliketheTIP120(NPN)andTIP125(PNP)arethemostpopulardrivers.Ifspecialdriversarenotused,thenthebrightnessoftheLED’swilldecreaseasthenumberofLED’sthatarelitincreases.TheArduinocansinkorsourceabout40ma.orenoughpowertolightabout4LED’sperpin.

TherearethemanyLEDmatrixtypeofarrangements.ThemostcommonLEDMatrixarrangementsarefivebyseven,fivebyeight,andeightbyeight.BelowisapictureofsometypicalLEDarrays.Theleftmostarrayisaneightbyfivearray,theothertwoareeightbyeightarrays.Thearraysarealsoinsomeofthemorecommonsizes,2and3/8inchesforthelargeronesand1and1/2inchesforthesmallerones.Thesmalleroneontherightisaneightbyeightdualcolorarraylikewhatwewillbeusingforsomeprojectsinthisbook.

Page 44: Arduino LED Projects - Robert Davis

Ledarrayscanalsobefoundinmanycolorsandmanycombinationsofcolors.ThemostcommoncolorsareredandgreenbutsomeLEDarraysareavailableinred,greenandbluesotheycanproduceallofthecolorsoftherainbowbycombiningthosethreecolors.ByusingthreecolorLED’syoucanmakea“TV”likeLEDsign.LEDSignslikethatarepoppingupallovertheplace.

Page 45: Arduino LED Projects - Robert Davis

Chapter10

SevenSegmentDisplayCounter

Inthischapterwewillbeaddingasevensegmentdisplay.ItisessentiallysevenLED’sprearrangedsothatitcanshowthenumberszerotonineusingseven“segments”.TheArduinocandirectlydriveasevensegmentLEDdisplaybutifyoudothat,youwillusesevenpinsperdisplay.Whenyoutrytoaddasecondsevensegmentdisplayyouwillneedatleast14I/Opins.AsyoucantellyouwouldquicklyrunoutofavailableI/Opins.

Therearetwosolutionstothisproblem.Onesolutionistousea7447orsimilarBCDtosevensegmentdecoderdriver.Theothersolutionistouse“multiplexing”.Withmultiplexingyoufirstlightuponesevensegmentdisplayandthentheotherone.Thismultiplexingisdonesorapidlythattheybothappeartobelitallthetime.

Lateronwewillbeusingsome“multiplexing”toswitchbetweenmultiplesevensegmentdisplays.ThatwayonlynineArduinopinscancontroltwosevensegmentLEDarrayssowecandisplayallthenumbersfromzeroto99.Withoutthemultiplexingthatwouldtakeall14digitalI/Opins!MultiplexingwillbeusedalotmorelateronwhenwestartusingLEDarrays.

Hereishowthesegmentsarelabeledandthepinsareconnected(asseenfromabove)foraFND503sevensegmentcommoncathodedisplay.

Todisplaythenumbersweneedtoturnonthecorrectsegmentslikethis.

Page 46: Arduino LED Projects - Robert Davis

0–ABCDEF

1–BC

2–ABGED

3–ABCDG

4–BCFG

5–ACDFG

6–ACDEFG

7–ABC

8–ABCDEFG

9–ABCFG

Hereisalistoftheneededpartstomakethiswork,besidestheArduinoitself.

1. OneCommonCathode,SevenSegmentLEDdisplay.2. Seven470ohmresistors.

Hereistheschematicdiagramforaonedigitcounter,notethatthewirestoFandGmustbecrossed:

Hereisthesoftwaretomakeaonedigitcounter:

Page 47: Arduino LED Projects - Robert Davis

//******************************************///Name:LED7SegmentCounter//Author:BobDavis//Date:12March,2013//Version:1.0//******************************************///PinsfortheSevenSegmentsintSegA=1;intSegB=2;intSegC=3;intSegD=4;intSegE=5;intSegF=6;intSegG=7;intDispA=8;intDispB=9;//SetthepinstooutputtotheLED’svoidsetup(){pinMode(SegA,OUTPUT);pinMode(SegB,OUTPUT);pinMode(SegC,OUTPUT);pinMode(SegD,OUTPUT);pinMode(SegE,OUTPUT);pinMode(SegF,OUTPUT);pinMode(SegG,OUTPUT);pinMode(DispA,OUTPUT);pinMode(DispB,OUTPUT);}voidloop(){

Page 48: Arduino LED Projects - Robert Davis

for(intcount=0;count<10;count++){digitalWrite(SegA,LOW);digitalWrite(SegB,LOW);digitalWrite(SegC,LOW);digitalWrite(SegD,LOW);digitalWrite(SegE,LOW);digitalWrite(SegF,LOW);digitalWrite(SegG,LOW);if(count==1){digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);}if(count==2){digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegE,HIGH);digitalWrite(SegG,HIGH);}if(count==3){digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegG,HIGH);}if(count==4){digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegF,HIGH);digitalWrite(SegG,HIGH);

Page 49: Arduino LED Projects - Robert Davis

}if(count==5){digitalWrite(SegA,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegF,HIGH);digitalWrite(SegG,HIGH);}if(count==6){digitalWrite(SegA,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegE,HIGH);digitalWrite(SegF,HIGH);digitalWrite(SegG,HIGH);}if(count==7){digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);}if(count==8){digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegE,HIGH);digitalWrite(SegF,HIGH);digitalWrite(SegG,HIGH);}if(count==9){

Page 50: Arduino LED Projects - Robert Davis

digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegF,HIGH);digitalWrite(SegG,HIGH);}if(count==0){digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegE,HIGH);digitalWrite(SegF,HIGH);}

delay(1000);

}}

Page 51: Arduino LED Projects - Robert Davis

Chapter11

Meter/TemperatureDisplay

Wecanuseoneoftheanaloginputscombinedwithusingtwosevensegmentdisplaystomakeasimplevoltmeter.Wecanthenusethatmetertomeasurelight,temperature,soundlevels,andjustaboutanythingelsethatcanbemeasuredasavoltage.ForthisprojectwewilladdaLM34temperaturesensoranddisplaytheroomtemperatureonthetwosevensegmentLEDdisplays.

Itispossibletoevenbuildyouownenergymanagementsystemusingthebasicsthatareshowninthischapter.Combiningmultipletemperaturesensorswiththedualsevensegmentdriversyoucannowdisplaytemperaturesrangingfromzeroto99degrees.Ifyouweretoaddrelaysyoucouldthenturntheheatonandoffbasedontheroomtemperature.

Hereisalistoftheneededpartstomakethiswork,besidestheArduinoitself.

1. TwoCommonCathode,SevenSegmentLED’s.2. Seven470ohmresistors.3. OneLM34temperaturesensor.

Hereistheschematicwiringdiagram:

Page 52: Arduino LED Projects - Robert Davis

Hereisapictureofwhatitlookslikewhenitisallwiredupandworking.Notethelm34overontheleftsideofthebreadboard.

HereistheLM34connectiondiagramasviewedfrombelow.TheLM34outputsthetemperatureat.01voltsperdegreeFahrenheit.Thatistosaythat70degreeswouldbeseenas.70voltsoutoftheLM35.

Page 53: Arduino LED Projects - Robert Davis

Hereisthesketchorcodetomakeitwork;

//*****************************************///Name:LEDMeterDriver//Author:BobDavis//Date:12March,2013//Version:1.0//*****************************************///PinsfortheSevenSegmentsintSegA=1;intSegB=2;intSegC=3;intSegD=4;intSegE=5;intSegF=6;intSegG=7;intDispA=8;intDispB=9;bytetemp=0;bytedig1;//leftdigitbytedig2;//rightdigitintpass=0;intdigit;//SetthepinstooutputtotheLED’svoidsetup(){pinMode(SegA,OUTPUT);pinMode(SegB,OUTPUT);pinMode(SegC,OUTPUT);pinMode(SegD,OUTPUT);pinMode(SegE,OUTPUT);pinMode(SegF,OUTPUT);

Page 54: Arduino LED Projects - Robert Davis

pinMode(SegG,OUTPUT);pinMode(DispA,OUTPUT);pinMode(DispB,OUTPUT);}voidloop(){//5V=1024soifyoudivideby2then5V=512//70degreesis.7voltsis71.62temp=analogRead(0)/2;dig1=temp/10;//givesfirsdigitdig2=(temp%10);//givesremainderif(pass==0){digitalWrite(DispA,LOW);digitalWrite(DispB,HIGH);digit=dig1;pass=1;

}else{digitalWrite(DispA,HIGH);digitalWrite(DispB,LOW);digit=dig2;pass=0;

}digitalWrite(SegA,LOW);digitalWrite(SegB,LOW);digitalWrite(SegC,LOW);digitalWrite(SegD,LOW);digitalWrite(SegE,LOW);digitalWrite(SegF,LOW);digitalWrite(SegG,LOW);if(digit==1){digitalWrite(SegB,HIGH);

Page 55: Arduino LED Projects - Robert Davis

digitalWrite(SegC,HIGH);}if(digit==2){digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegE,HIGH);digitalWrite(SegG,HIGH);}if(digit==3){digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegG,HIGH);}if(digit==4){digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegF,HIGH);digitalWrite(SegG,HIGH);}if(digit==5){digitalWrite(SegA,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegF,HIGH);digitalWrite(SegG,HIGH);}if(digit==6){digitalWrite(SegA,HIGH);

Page 56: Arduino LED Projects - Robert Davis

digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegE,HIGH);digitalWrite(SegF,HIGH);digitalWrite(SegG,HIGH);}if(digit==7){digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);}if(digit==8){digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegE,HIGH);digitalWrite(SegF,HIGH);digitalWrite(SegG,HIGH);}if(digit==9){digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegF,HIGH);digitalWrite(SegG,HIGH);}if(digit==0){digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);

Page 57: Arduino LED Projects - Robert Davis

digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegE,HIGH);digitalWrite(SegF,HIGH);}delay(10);}

Page 58: Arduino LED Projects - Robert Davis

Chapter12

ThreeDigitMeterWhathappenswhenthetemperatureinthelastprojectexceeds99degrees?Addingathirddigitwouldsolvethatproblem.ForthisprojectwewilljustaddanothersevensegmentLEDdisplaytothelastproject.Themathgetsalittlebittrickytogetthethreedigitsseparatedouttodisplaythemoneatatime.ThepicturebelowshowsaLM34connectedtothemeter.

WewillalsoaddaUGN3503magneticfieldsensor.BecarefulbecausethegroundandoutputpinsaredifferentthantheLM34temperaturesensor.BelowisaschematicofthepinconnectionsfortheUGN3503

Page 59: Arduino LED Projects - Robert Davis

HereistheschematicdiagramoftheArduinopoweredthreedigitmeter.

Hereisalistoftheneededpartstomakethiswork,besidestheArduinoitself.

4. ThreeCommonCathode,SevenSegmentLED’s.5. Seven470ohmresistors.6. OneUGN3503magneticfieldsensor.

Hereisthecodetomakethethreedigitmeterwork.

//********************************************////Name:LEDMeterDriver3////Author:BobDavis////Date:12March,2013////Version:1.0////********************************************////PinsfortheSevenSegments

Page 60: Arduino LED Projects - Robert Davis

intSegA=1;intSegB=2;intSegC=3;intSegD=4;intSegE=5;intSegF=6;intSegG=7;intDispA=8;intDispB=9;intDispC=10;inttemp=0;bytedig1;bytedig2;bytedig3;intpass=0;intdigit;//SetthepinstooutputtotheLED’svoidsetup(){pinMode(SegA,OUTPUT);pinMode(SegB,OUTPUT);pinMode(SegC,OUTPUT);pinMode(SegD,OUTPUT);pinMode(SegE,OUTPUT);pinMode(SegF,OUTPUT);pinMode(SegG,OUTPUT);pinMode(DispA,OUTPUT);pinMode(DispB,OUTPUT);pinMode(DispC,OUTPUT);}voidloop(){//5V=1024soifyoudivideby2.05then5V=500

Page 61: Arduino LED Projects - Robert Davis

//70degreesis.7voltsis070temp=analogRead(0)/2.05;dig1=(temp%10);//givesrightdigitdig2=(temp/10)%10;//givesmiddledigitdig3=temp/100;//givesleftdigitpass++;if(pass==1){digitalWrite(DispA,LOW);digitalWrite(DispB,HIGH);digitalWrite(DispC,HIGH);digit=dig1;}

if(pass==2){digitalWrite(DispA,HIGH);digitalWrite(DispB,LOW);digitalWrite(DispC,HIGH);digit=dig2;}

if(pass==3){digitalWrite(DispA,HIGH);digitalWrite(DispB,HIGH);digitalWrite(DispC,LOW);digit=dig3;pass=0;}digitalWrite(SegA,LOW);digitalWrite(SegB,LOW);digitalWrite(SegC,LOW);digitalWrite(SegD,LOW);digitalWrite(SegE,LOW);

Page 62: Arduino LED Projects - Robert Davis

digitalWrite(SegF,LOW);digitalWrite(SegG,LOW);if(digit==1){digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);}if(digit==2){digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegE,HIGH);digitalWrite(SegG,HIGH);}if(digit==3){digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegG,HIGH);}if(digit==4){digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegF,HIGH);digitalWrite(SegG,HIGH);}if(digit==5){digitalWrite(SegA,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegF,HIGH);

Page 63: Arduino LED Projects - Robert Davis

digitalWrite(SegG,HIGH);}if(digit==6){digitalWrite(SegA,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegE,HIGH);digitalWrite(SegF,HIGH);digitalWrite(SegG,HIGH);}if(digit==7){digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);}if(digit==8){digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegE,HIGH);digitalWrite(SegF,HIGH);digitalWrite(SegG,HIGH);}if(digit==9){digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegF,HIGH);digitalWrite(SegG,HIGH);

Page 64: Arduino LED Projects - Robert Davis

}if(digit==0){digitalWrite(SegA,HIGH);digitalWrite(SegB,HIGH);digitalWrite(SegC,HIGH);digitalWrite(SegD,HIGH);digitalWrite(SegE,HIGH);digitalWrite(SegF,HIGH);}if(digit>=10){//outofrangedigitalWrite(SegG,HIGH);}delay(5);}

Page 65: Arduino LED Projects - Robert Davis

Chapter13

EightbyEightLEDArray

AneightbyeightLEDdisplaywouldrequire16pinstoconnectittotheArduino.Thereareseveralpossiblesolutions.YoucanuseanyoracombinationofthefollowingsolutionstosaveontheArduinopins.Onesolutionistousetheanaloginputpinsasoutputpins.ThatcanbeaproblemifyouwanttodisplaysomedatafromtheanaloginputsontheLEDarray.Wewilldothatforthisproject.

ToControltheLEDArray’srowsyoucanusea74HC138threetoeightde-multiplexertoallowthreepinsfromtheArduinotoselectbetweentheeightrows.The74HC138hasactivelowoutputsandyouusuallywantactivehighoutputs.ThatproblemcanbefixedbyusingPNPdrivertransistorsorbyusinganICthathaseightinverters.Anothersolutionistousea4017counteranddecoder.The4017decodesto10outputs,withjustaclockinputandhasactivehighoutputs.Howeverthe4017hasalmostnodrivepowersoadriverICortransistorswillbeneeded.

Tocontrolthecolumnsyoucanusea74HC595orsimilarshiftregisters.Thiswillallowtwopinstosendclockanddatatotheshiftregisterthatwillthenbeconnectedtotheeightcolumns.Ashiftregisterisaseriesofeightlatches;everytimeitgetsa“clock”itshiftsthedatadownonepositiontothenextlatch.

Forthisfirstprojectwewillnotbeusinganysupportchips.ThatessentiallylimitsustoaneightbyeightLEDdisplay.Itispossibletousetheanaloginputpinsasoutputs.Youcanaddsupportchipstogetmorethananeightbyeightdisplayworking,andwewilldothatinthenextproject.

UsingLEDmatrixarraysusedtorequiretheuseofsomethingthatwascalleda“CharacterGenerator”.Thecharacterswewillusewillbecreatedinsoftwareusing“binary”values.BasicallyyouselectwhatLED’syouwanttolightup.Thenyoudrawthemonachart.Nextyouadduptheirbinaryvaluesyouwillgetthebinaryvalueforeachrowofthecharacter.

Page 66: Arduino LED Projects - Robert Davis

Belowisachartshowinghowtocreatetheletter“A”,a“@”isalitLEDanda“O”isaLEDthatisnotlit.

168421BinaryValues–––––––––––OO@OO4O@O@O10@OOO@17@@@@@31@OOO@17@OOO@17@OOO@17

Thiswouldshowuplikethisinthesketchcode:

bitmap[8]={0,4,10,17,31,17,17,17};

Sinceweareworkingwitheightbyeightarrays,letsusesomemoreLED’s.Belowisachartshowinghowtocreateasmileyface,againa“@”isalitLED.

1286432168421BinaryValue–––––––––––––—OO@@@@OO60O@OOOO@O66@O@OO@O@165@OOOOOO@129@O@OO@O@165@OO@@OO@153O@OOOO@O66OO@@@@OO60

Thiswouldshowuplikethisinthesketchcode:

Page 67: Arduino LED Projects - Robert Davis

bitmap[8]={60,66,165,129,165,153,66,60};

UpnextisaschematicshowingthepinconnectionsforaLTP14188AeightbyeightdualcolorcommonanodeLEDarraythatwewillbeusingforthenextfewprojects.ItiseasytowireupbecausethepinsareinanicesequentialorderinsteadofalternatinglikemostotherLEDarrays.The“Column”pinsareactivewhenhigh,the“Rows”lightupwhenlow.

Hereisalistoftheneededpartstomakethiswork,besidestheArduinoitself.

1. OnefivebyeightoreightbyeightLEDarray,

TheLTP14188isrecommendedbecauseitiseasytowireup.

2. Eight100ohmresistors.

Notethatwehavechangedtousing100ohmresistors.Nowthatweare“multiplexing”eachLEDisonlyonfor1/8ofthetime.Sotheresistormustbesmallersothatmorecurrentwillflow.

HereistheschematicdiagramforwiringupaneightbyeightgreendisplaywithoutusinganysupportIC’s.Notetheuseoftheanaloginputsasoutputs.Alsotherearestillfourunusedpinsleftoverthatcanbeusedforsomesortofinputsifneeded.

Page 68: Arduino LED Projects - Robert Davis

Hereisapictureofwhatitcouldlooklikewhenitisallwiredup.NotethatthewirescomingfromthesideofthedisplaythatisclosesttoyouareactuallycomingfromunderneaththeLEDarray.ThespacingforthebreadboardmakesthingsalittletootighttoeasilygettobothsidesoftheLEDarray.AlsoIwasusing220ohmresistorsandIwouldrecommendthatyouuse47or100ohmresistorsformorebrightness.

Page 69: Arduino LED Projects - Robert Davis

Hereisthesketchcodetodisplaythesmileyface;

//******************************************///Name:LED8x8arrayDriver//Author:BobDavis//Date:12March,2013//Version:1.0//******************************************///PinsfortheLEDArrayintCol1=1;intCol2=2;intCol3=3;intCol4=4;intCol5=5;intCol6=6;intCol7=7;intCol8=8;intRow4=9;

Page 70: Arduino LED Projects - Robert Davis

intRow3=10;intRow2=11;intRow1=12;intRow8=A1;intRow7=A2;intRow6=A3;intRow5=A4;//arraytomakeasmileyfacebytebitmap[8]={60,66,165,129,165,153,66,60};//Setthepinstooutputtothearrayvoidsetup(){pinMode(Col1,OUTPUT);pinMode(Col2,OUTPUT);pinMode(Col3,OUTPUT);pinMode(Col4,OUTPUT);pinMode(Col5,OUTPUT);pinMode(Col6,OUTPUT);pinMode(Col7,OUTPUT);pinMode(Col8,OUTPUT);pinMode(Row1,OUTPUT);pinMode(Row2,OUTPUT);pinMode(Row3,OUTPUT);pinMode(Row4,OUTPUT);pinMode(Row5,OUTPUT);pinMode(Row6,OUTPUT);pinMode(Row7,OUTPUT);pinMode(Row8,OUTPUT);}voidloop(){for(introw=0;row<8;row++){

Page 71: Arduino LED Projects - Robert Davis

//turneverythingoffdigitalWrite(Col1,HIGH);digitalWrite(Col2,HIGH);digitalWrite(Col3,HIGH);digitalWrite(Col4,HIGH);digitalWrite(Col5,HIGH);digitalWrite(Col6,HIGH);digitalWrite(Col7,HIGH);digitalWrite(Col8,HIGH);digitalWrite(Row1,LOW);digitalWrite(Row2,LOW);digitalWrite(Row3,LOW);digitalWrite(Row4,LOW);digitalWrite(Row5,LOW);digitalWrite(Row6,LOW);digitalWrite(Row7,LOW);digitalWrite(Row8,LOW);//setupthedatainthecolumnsifbitRead(bitmap[row],0)digitalWrite(Col1,LOW);ifbitRead(bitmap[row],1)digitalWrite(Col2,LOW);ifbitRead(bitmap[row],2)digitalWrite(Col3,LOW);ifbitRead(bitmap[row],3)digitalWrite(Col4,LOW);ifbitRead(bitmap[row],4)digitalWrite(Col5,LOW);ifbitRead(bitmap[row],5)digitalWrite(Col6,LOW);ifbitRead(bitmap[row],6)digitalWrite(Col7,LOW);ifbitRead(bitmap[row],7)digitalWrite(Col8,LOW);//turnontheassociatedrowif(row==0)digitalWrite(Row1,HIGH);if(row==1)digitalWrite(Row2,HIGH);if(row==2)digitalWrite(Row3,HIGH);if(row==3)digitalWrite(Row4,HIGH);

Page 72: Arduino LED Projects - Robert Davis

if(row==4)digitalWrite(Row5,HIGH);if(row==5)digitalWrite(Row6,HIGH);if(row==6)digitalWrite(Row7,HIGH);if(row==7)digitalWrite(Row8,HIGH);delay(3);

}}

Page 73: Arduino LED Projects - Robert Davis

Chapter14

DualColorEightbyEightLEDArray

Ifweweretoaddsomeshiftregisterstothepreviousdesignthenwecouldcontrolasecondsetofeightcolumns.Thisadditionallowsforasecondcolortobedisplayedbesidestheusualredcolor.Usuallythesecondcolorisgreen,butwestartedwiththatcolorfirst.WiththeadditionofthetwoshiftregisterswefreeupalotoftheArduinopins,whileaddingmorefeaturestothedisplay.

Hereisalistoftheneededpartstomakethiswork,besidestheArduinoitself.

1. OnedualcoloreightbyeightLEDarray.2. Sixteen100or220ohmresistors.3. Two74HC595shiftregister.

Sinceweareworkingwithdualcolorarrays,weneedtwobitmaps,oneforeachcolor.Belowisachartshowinghowtocreatetheoutsideofthefaceingreen,a“@”isalitLED.

1286432168421BinaryValue–––––––––––––—OO@@@@OO60O@OOOO@O66@OOOOOO@129@OOOOOO@129@OOOOOO@129@OOOOOO@129O@OOOO@O66

Page 74: Arduino LED Projects - Robert Davis

OO@@@@OO60

Thisarraywouldshowupasthisinthesketchcode:

bitmap[8]={60,66,129,129,129,129,66,60};

Belowisachartshowinghowtocreatethemouthandeyesinred,a“@”isalitLED.

1286432168421BinaryValue–––––––––––––—OO@@@@OO60O@OOOO@O66OO@OO@OO36OOOOOOOO0OO@OO@OO36OOO@@OOO24OOOOOOOO0OOOOOOOO0

Thiswouldshowuplikethisinthesketchcode:

bitmap[8]={60,66,36,0,36,24,0,0};

Hereistheschematicforadualcolorversionusing74HC595shiftregisters.

Page 75: Arduino LED Projects - Robert Davis

Hereisthetopviewofthewiringdiagramforthe74HC595.Belowthatiswhatitusuallylookslikeinaschematic.Firstobservethatpinoneisfoundtotheleftofanotchinthechip.Notethattheorientationofthepinsdoesnotusuallymatchtheschematic.ItishardtoshowIC’sinaschematicexactlyliketheyarephysicallywiredup.

Page 76: Arduino LED Projects - Robert Davis

Hereisalistofwhatthe74HC595pinnamesmean:

Q0toQ7aretheshiftregisteroutputpins.

GNDisground.

SOUTisSerialOutputtoanothershiftregisterifused.

MRisMasterResetwhengroundedinput.

CLKistheClocktoadvancetheshiftregisterinput.

RCLisRegisterClocktolatchthecontentsoftheshiftregister.

OEistheOutputEnableinput,itisactivewhenlow.

SERisSerialinput.

5Visthepowersource.

Belowthereisapictureofwhatitmightlooklikeallwiredup.NoticethatIusedaribboncablefortherows.Theresistorsshouldhavehadsomeinsulationontheirleadstokeepthemfromshorting.

Page 77: Arduino LED Projects - Robert Davis

Hereisthecodeforthesketchtomakeitwork:

//********************************************///Name:LED8x8SRarrayDriver//Author:BobDavis//Date:12March,2013//Version:1.0//********************************************///PinsfortheColumnsandRowsintRow1=4;intRow2=3;intRow3=2;intRow4=1;intRow5=5;intRow6=6;intRow7=7;intRow8=8;intGSerial=10;intRSerial=11;intRCLK=12;

Page 78: Arduino LED Projects - Robert Davis

intSCLK=13;//arraystomakearedandgreensmileyfacebyteGbitmap[8]={60,66,129,129,129,129,66,60};byteRbitmap[8]={60,66,36,0,36,24,0,0};//Setthepinstooutputtothearrayvoidsetup(){pinMode(Row1,OUTPUT);pinMode(Row2,OUTPUT);pinMode(Row3,OUTPUT);pinMode(Row4,OUTPUT);pinMode(Row5,OUTPUT);pinMode(Row6,OUTPUT);pinMode(Row7,OUTPUT);pinMode(Row8,OUTPUT);pinMode(GSerial,OUTPUT);pinMode(RSerial,OUTPUT);pinMode(RCLK,OUTPUT);pinMode(SCLK,OUTPUT);}voidloop(){for(introw=0;row<8;row++){//turneverythingoffdigitalWrite(Row1,LOW);digitalWrite(Row2,LOW);digitalWrite(Row3,LOW);digitalWrite(Row4,LOW);digitalWrite(Row5,LOW);digitalWrite(Row6,LOW);digitalWrite(Row7,LOW);digitalWrite(Row8,LOW);//setupthedatainthecolumns

Page 79: Arduino LED Projects - Robert Davis

for(intshiftbit=8;shiftbit>=0;shiftbit—){digitalWrite(GSerial,HIGH);digitalWrite(RSerial,HIGH);ifbitRead(Gbitmap[row],shiftbit)digitalWrite(GSerial,LOW);ifbitRead(Rbitmap[row],shiftbit)digitalWrite(RSerial,LOW);digitalWrite(SCLK,HIGH);digitalWrite(SCLK,LOW);}//latchthedatadigitalWrite(RCLK,HIGH);digitalWrite(RCLK,LOW);//turnontheassociatedrowif(row==0)digitalWrite(Row1,HIGH);if(row==1)digitalWrite(Row2,HIGH);if(row==2)digitalWrite(Row3,HIGH);if(row==3)digitalWrite(Row4,HIGH);if(row==4)digitalWrite(Row5,HIGH);if(row==5)digitalWrite(Row6,HIGH);if(row==6)digitalWrite(Row7,HIGH);if(row==7)digitalWrite(Row8,HIGH);delay(2);

}}

Page 80: Arduino LED Projects - Robert Davis

Chapter15

DualColorEightby15LEDArrayThisprojectwillmakeanArduinopoweredeightby15redandgreenLEDsign.Youcanalsogetorangebyturningonredandgreenatthesametime.Youcanstartwithonearrayforeightbyfiveandthenexpanditasyougeteachsectionworking.Thisprojectdemonstrateschainingshiftregisterstogetherbyconnectingserialout,pinnine,toserialin,pin14,ofthenextshiftregister.

Thistimewewillbeusingseveral1702054eightbyfiveLEDarrays.Ittakesthreeofthemtomakethe8by15sign.ItispossibletotakethisdesignontomakeamuchlargerLEDsign.OnceagainIsetdownwithaninevoltbatteryanda1Kresistorandfiguredoutthepinconnectionsforthisdisplay.Itisactuallyaverycommonarrangement.

HereisthepinoutforoneoftheLEDarrays.Therowsarecalled“Dx”sotheywillnotbeconfusedwiththeredcolumns:

R1D3D1D2D4G3G4R5G5

––––––––––––––—

OOOOO

OOOOO

OOOOO

OOOOO

OOOOO

OOOOO

OOOOO

OOOOO

––––––––––––––—

G1G2R2R3D6D7R4D5D8

Page 81: Arduino LED Projects - Robert Davis

Nowtogetaneightby15displayyouneedtousethreeoftheseLEDarrays,sotheirpindefinitionswillhavetoberearranged.HerearethepindefinitionsforthenexttwoLEDarrays:

R6D3D1D2D4G8G9R10G10

–––––––––––––––—

G6G7R7R8D6D7R9D5D8

R11D3D1D2D4G13G14R15G15

–––––––––––––––––-

G11G12R12R13D6D7R14D5D8

ThisisaschematicdiagramthatshowsallthreeoftheeightbyfiveLEDarrays.Ithadtobeshrunktofitsothelettersmightbealittlehardtoread.

Upnextisaschematicdiagramoftheeightby15LEDdisplaycircuitry.ItistoodifficulttoshowtheLEDarraypinsattheiractualphysicallocations.ThatiswhythereisaseparateschematicoftheLEDarrays.TheArduinohadtobecutoffonthelefttofitthisschematicatitsactualsize.

Page 82: Arduino LED Projects - Robert Davis

Hereiswhatthedisplaylookslikewhenitisassembledandworking.Notethatthepartsjustsofitonstandardseveninchlongbreadboards.ThebiggestproblemIhadwasgettingtheresistorstoalignwiththepinsoftheLEDarrays.TwiceIwasoffbyoneposition.

AlsotheLED’sarenotverybrightbecausetherearenorowdriversandtheArduinohasahardtimesourcingthenecessaryamountofcurrent.EightNPNdrivertransistorssuchasTIP120orTIP121’scanbeusedontherowstomakethedisplayalittlebrighter.TheyarevisibleonasmallerbreadboardbelowtheArduino.Isolderedtheircollectorstogetherandconnectedthemtofivevolts.Ontheleft,theirbasegoestotheArduino.Ontheright,theiremittergoestotheLEDarrays.

Page 83: Arduino LED Projects - Robert Davis

Theserialdataisconvertedtoparallelbyfour74HC595shiftregisters.Oneislocatedateachendofeachbreadboard.Itispossibletohavetheresistorsgodirectlyfromtheshiftregisterstothearrays,butitislessconfusingifjumperwiresareused.ThisprojectusedallofmyjumperwiresandIhadtomakeafewmorejumperstocompleteit.

Hereisasketchtodisplay3lettersasseeninthepictureabove.

//******************************************////Name:Breadboard3digitdualcolor////Author:BobDavis////Date:12January,2014////Version:1.0////******************************************////PinsfortherowdriversintrowPin1=1;introwPin2=2;introwPin3=3;introwPin4=4;introwPin5=5;introwPin6=6;introwPin7=7;introwPin8=8;

Page 84: Arduino LED Projects - Robert Davis

intgdataPin=10;intrdataPin=11;intrclockPin=12;intclockPin=13;//Setthepinstooutputtothesignvoidsetup(){pinMode(rowPin1,OUTPUT);pinMode(rowPin2,OUTPUT);pinMode(rowPin3,OUTPUT);pinMode(rowPin4,OUTPUT);pinMode(rowPin5,OUTPUT);pinMode(rowPin6,OUTPUT);pinMode(rowPin7,OUTPUT);pinMode(rowPin8,OUTPUT);pinMode(rclockPin,OUTPUT);pinMode(clockPin,OUTPUT);pinMode(rdataPin,OUTPUT);pinMode(gdataPin,OUTPUT);}byterbitmap[][8]={//redcharacters{0,0,0,0,0,0,0,0},{0,10,31,31,31,14,14,4},//heart{0,9,9,9,9,9,9,6},//U};bytegbitmap[][8]={//greencharacters{0,14,04,04,04,04,04,14},//I{0,0,0,0,0,0,0,0},{0,9,9,9,9,9,9,6},//U};voidloop(){

Page 85: Arduino LED Projects - Robert Davis

//selecttherow0=toprowfor(introw=0;row<8;row++){//sendserialdatatodisplay3=numberofcharacters//sendtherightmostcharacteroutfirstfor(intcharacter=3;character>-1;character—){//selectthebittoloadintotheshiftregisterfor(intshiftbit=0;shiftbit<5;shiftbit++){digitalWrite(rdataPin,HIGH);digitalWrite(gdataPin,HIGH);ifbitRead(gbitmap[character][row],shiftbit)digitalWrite(gdataPin,LOW);ifbitRead(rbitmap[character][row],shiftbit)digitalWrite(rdataPin,LOW);//advancetheshiftregisterclockdigitalWrite(clockPin,LOW);digitalWrite(clockPin,HIGH);}}//turnoffthedisplaydigitalWrite(rowPin1,LOW);digitalWrite(rowPin2,LOW);digitalWrite(rowPin3,LOW);digitalWrite(rowPin4,LOW);digitalWrite(rowPin5,LOW);digitalWrite(rowPin6,LOW);digitalWrite(rowPin7,LOW);digitalWrite(rowPin8,LOW);//latchthedatadigitalWrite(rclockPin,LOW);digitalWrite(rclockPin,HIGH);//setuprowselectionandturndisplaybackonif(row==0)digitalWrite(rowPin1,HIGH);if(row==1)digitalWrite(rowPin2,HIGH);

Page 86: Arduino LED Projects - Robert Davis

if(row==2)digitalWrite(rowPin3,HIGH);if(row==3)digitalWrite(rowPin4,HIGH);if(row==4)digitalWrite(rowPin5,HIGH);if(row==5)digitalWrite(rowPin6,HIGH);if(row==6)digitalWrite(rowPin7,HIGH);if(row==7)digitalWrite(rowPin8,HIGH);

//Waittoseewhatwesenttothedisplay;delayMicroseconds(500);

}}

Page 87: Arduino LED Projects - Robert Davis

Chapter16

ThreeColorEightbyEightLEDArray

RecentlyIwasabletogetmyhandsonathreecolor,asinred,greenandblue,eightcolumnsbyeightrowLEDarray.ThemodelnumberofthisLEDarrayisCRM-2388ARGB-L.ItisverypopularoneBay.TheLEDarraydidnotcomewithanywiringinstructions,soIsetdownwithaninevoltbatteryanda1Kresistorandfiguredouthowitiswiredup.

Underneathittherearetworowsof16pinseach.Thetoprowdoesnothavethepartnumbernearit.Thebottomrow,leftmostpin,istheclosestpintothepartnumber.Thereisalsoanumber“1”locatednearthispin.ThisiswhatIfoundthepinouttobe.

–—Rows–––––––Green–––––—––Rows–—

R8R7R6R5G1G2G3G4G5G6G7G8R4R3R2R1

32313029282726252423222120191817

––––––––––––––––––––––––––-

00000000

00000000

00000000

00000000

00000000

00000000

00000000

00000000

––––––––––––––––––––––––––-

12345678910111213141516

B1B2B3B4B5B6B7B8R1R2R3R4R5R6R7R8

Page 88: Arduino LED Projects - Robert Davis

–––––Blue–––––—–––––-Red–––––—

NextIwiredupthree74HC595shiftregisterswitheight100ohmresistorseachtothered,greenandbluearraypins.Iwasreusingthedesignthathadpreviouslyworkedforatwocoloreightbyeightledarray.

Fortherowsthereshouldhavebeensomerowdrivertransistors,butIdirectlyconnectedtherowstopinsD1-D8oftheArduinoanditworked.YouwillgetalittlemorebrightnesswithsomeNPNrowdrivertransistors.Howeveritisalreadyverybright.

Hereistheschematicdiagram.IchoppedofftheArduinoonthelefttofittheschematicinatfullsize.

Hereiswhatitlookslikeallwiredupanddisplayingasmileyface.Itishardtotellwhatitlookslikeinblackandwhite.Incolor,itisablueface,witharedmouthandeyes.Thehairatthetopiswhite.

Page 89: Arduino LED Projects - Robert Davis

NextIwroteaquicksketchtooutputdatatothethreeshiftregistersandtoselecttherowsoneatatime.

//********************************************///Name:LED8x8RGBSRarrayDriver//Author:BobDavis//Date:2January,2014//Version:1.0//********************************************///AssignPinsfortheshiftregistersandRowsintRow1=1;intRow2=2;intRow3=3;intRow4=4;intRow5=5;intRow6=6;intRow7=7;intRow8=8;intRSerial=9;

Page 90: Arduino LED Projects - Robert Davis

intBSerial=10;intGSerial=11;intRCLK=12;intSCLK=13;//Setuparrays,selectoneofthesethreethingstodisplay//RGBFlag//byteBbitmap[8]={240,240,240,240,0,0,0,0};//byteGbitmap[8]={15,0,15,0,255,0,255,0};//byteRbitmap[8]={0,15,0,15,0,255,0,255};//BlueFacebyteBbitmap[8]={60,126,255,219,255,219,102,60};byteGbitmap[8]={60,126,129,165,129,129,0,0};byteRbitmap[8]={60,126,0,36,0,36,24,0};//RedCar//byteGbitmap[8]={8,0,0,0,0,0,255,255};//byteBbitmap[8]={255,255,255,131,0,0,0,0};//byteRbitmap[8]={8,0,0,124,255,189,0,0};//Setthepinstooutputtothearrayvoidsetup(){pinMode(Row1,OUTPUT);pinMode(Row2,OUTPUT);pinMode(Row3,OUTPUT);pinMode(Row4,OUTPUT);pinMode(Row5,OUTPUT);pinMode(Row6,OUTPUT);pinMode(Row7,OUTPUT);pinMode(Row8,OUTPUT);pinMode(BSerial,OUTPUT);pinMode(GSerial,OUTPUT);pinMode(RSerial,OUTPUT);

Page 91: Arduino LED Projects - Robert Davis

pinMode(RCLK,OUTPUT);pinMode(SCLK,OUTPUT);}voidloop(){for(introw=0;row<8;row++){//turneverythingoffdigitalWrite(Row1,LOW);digitalWrite(Row2,LOW);digitalWrite(Row3,LOW);digitalWrite(Row4,LOW);digitalWrite(Row5,LOW);digitalWrite(Row6,LOW);digitalWrite(Row7,LOW);digitalWrite(Row8,LOW);//setupthedataintheshiftregistersfor(intshiftbit=0;shiftbit<8;shiftbit++){digitalWrite(BSerial,HIGH);digitalWrite(GSerial,HIGH);digitalWrite(RSerial,HIGH);ifbitRead(Bbitmap[row],shiftbit)digitalWrite(BSerial,LOW);ifbitRead(Gbitmap[row],shiftbit)digitalWrite(GSerial,LOW);ifbitRead(Rbitmap[row],shiftbit)digitalWrite(RSerial,LOW);digitalWrite(SCLK,LOW);digitalWrite(SCLK,HIGH);}//latchthedatadigitalWrite(RCLK,HIGH);digitalWrite(RCLK,LOW);//turnontheassociatedrowif(row==0)digitalWrite(Row1,HIGH);if(row==1)digitalWrite(Row2,HIGH);

Page 92: Arduino LED Projects - Robert Davis

if(row==2)digitalWrite(Row3,HIGH);if(row==3)digitalWrite(Row4,HIGH);if(row==4)digitalWrite(Row5,HIGH);if(row==5)digitalWrite(Row6,HIGH);if(row==6)digitalWrite(Row7,HIGH);if(row==7)digitalWrite(Row8,HIGH);delay(2);

}}

OnceIgoteverythingworking,Iplayedaroundwiththearraystomakeitdisplaydifferentthings.IstartedwiththesmileyfacebecauseIhadalreadydesignedoneforadualcoloreightbyeightarray.Tocreateyourownarray,startwiththefirstnumber,itisthetoprow.Eachnumberinthearraymovesdownonerowinthedisplay.Thenumbersvaluesare128,64,32,16,8,4,2,and1.TolightupthefirstandlastLEDyouwouldjustadd128+1toget129.TolightthemiddletwoLED’syouwouldadd16+8toget24.Usingthispatternyoucancreateyourownarraysormakeminelookevenbetter.

Chapter17

DualColor8by40LEDArrayYoucanbuildyourownLEDsignfromscratch,oryoucanuseanalreadyassembledandtestedmodulesuchastheCadacesCDVO-1thatcanbefoundinoldSignatureElectronicSigns.ThismodulefromCadaceshaseightfivebyeightdualcolorLEDarraysforatotalof40by8LED’s.ThereareTPIC6B595driversforthecolumnsanda74138selectstherowsviasomehighpowertransistordrivers.

EachCadacesmodulecanconsumeovertwoampsatfivevoltswhenalloftheLED’sarelit.HoweverwhenyouarejustdisplayingtextitispossibletorunoneofthesemodulesdirectlyoffthefivevoltsfromtheUSBjackofthecomputer.Formorepower,therearesmallfivevolt2.5ampACadaptersavailablethatwillusuallyruntwoofthesedisplays.

Page 93: Arduino LED Projects - Robert Davis

TheCadacesmoduleshavea10pinconnectorthatconnectstotheArduino.Asecond20pinconnectorisusedtodaisychainuptofourofthesemodulestogetherinseries.Thecolumnsignalsincluderedserialdata,greenserialdata,dataclock,anddatalatch.Therowsignalsaredatazero,dataone,datatwo,androwenable.Whenrowenableisofftheentiresignisoff,itisusedtoblankthedisplaywhileupdatingthecolumns.Theothertwopinsareground.

Hereishowthe10pinconnectoriswireduptotheArduino.Theschematicisshownformaboveasthoughyouwerelookingdownthroughtheadapter.YouwillneedafivepinheadertoconnectonerowoftheribboncabletotheArduino.Theotherrowisconnectedviafourjumperwires.

HereisapictureshowinghowtoconnectthetenpinconnectortoanArduino.ThegreenwiregoingofftheleftsideofthepictureisfivevoltspowergoingtotheredpowerwiretopoweruptheLEDsign.

Page 94: Arduino LED Projects - Robert Davis

ThisisapictureofthetenpinconnectorontheCadacessign.

Letmetranslatesomeofthesignalnames:

TPIC6B595Pins

Pin3-RSeristheRedSerialDatainput.

Pin3-GSeristheGreenSerialDatainput.

Page 95: Arduino LED Projects - Robert Davis

Pin13-ClkistheSerialClockinput.

Pin12-RClkisRegisterClock,or“LatchtheData”.

GNDisGround.

74HCT138Pins

Pin4-RENisRowEnable,or“Turnthedisplayon”.

Pin3-R3isthethirdbitoftherowselector.

Pin2-R2isthesecondbitoftherowselector.

Pin1-R1isthefirstbitoftherowselector.

Youwillneedthefollowingpartstomakethisproject.

1.Arduinoadapteror5pinheaderand4jumperwires.

2.OneortwoCadacesmodules.

3.Oneortworibboncables.

4.OneFivevolt2.5ampACadapter.

Hereiswhatahomemadeadapterlookslikefrombelow.Thatisnottheneatestwiringjobintheworldbutitdoesgetthejobdone.ThisisamodelthatusedanArduinoshield.

HereistheArduinoshieldadapterasitisseenfromabove:

Page 96: Arduino LED Projects - Robert Davis

Hereisthecodeorsketchforadualcolorstationarysign,itiswrittenfortwoCadacesmodulesthatarewiredupinseriesviaribboncables.

//**********************************//Name:CadacesDriver//Author:BobDavis//Date:23February,2013//Version:1.0//**********************************//Pinsfortherowdriversintrow1Pin=2;//R1introw2Pin=7;//R2introw3Pin=1;//R3introwEnable=8;//RENintrclockPin=6;//RCKintclockPin=3;//SCKintdataPin=5;//RSDintgdataPin=4;//GSD//Setthepinstooutputtothesignvoidsetup(){pinMode(row1Pin,OUTPUT);

Page 97: Arduino LED Projects - Robert Davis

pinMode(row2Pin,OUTPUT);pinMode(row3Pin,OUTPUT);pinMode(rowEnable,OUTPUT);pinMode(rclockPin,OUTPUT);pinMode(clockPin,OUTPUT);pinMode(dataPin,OUTPUT);pinMode(gdataPin,OUTPUT);}//===CharacterArray===//CharactersareA,B,C,etc.Onlyuppercase,nosymbols.bytealphabets[][8]={{0,04,10,17,17,31,17,17},//A{0,30,17,17,30,17,17,30},//B{0,14,17,16,16,16,17,14},//C{0,28,18,17,17,17,18,28},//D{0,31,16,16,31,16,16,31},//E{0,31,16,16,31,16,16,16},//F{0,14,17,16,16,19,17,14},//G{0,17,17,17,31,17,17,17},//H{0,14,04,04,04,04,04,14},//I{0,07,02,02,02,02,10,14},//J{0,17,18,20,24,20,18,17},//K{0,16,16,16,16,16,16,31},//L{0,10,21,21,21,17,17,17},//M{0,17,25,25,21,19,19,17},//N{0,14,17,17,17,17,17,14},//O{0,30,17,17,30,16,16,16},//P{0,14,17,17,17,17,19,15},//Q{0,30,17,17,30,20,18,17},//R{0,14,17,16,14,01,17,14},//S{0,31,04,04,04,04,04,04},//T

Page 98: Arduino LED Projects - Robert Davis

{0,17,17,17,17,17,17,14},//U{0,17,17,17,10,10,10,04},//V{0,17,17,17,21,21,21,10},//W{0,17,17,10,04,10,17,17},//X{0,17,10,10,04,04,04,04},//Y{0,31,8,04,02,04,8,31},//Z{0,0,21,14,31,14,21,0},//*};bytenumbers[][8]={{0,04,12,04,04,04,04,14},//1{0,14,17,01,02,04,8,31},//2notethat08isillegal.{0,14,01,01,06,01,01,14},//3{0,17,17,17,31,01,01,01},//4{0,31,16,16,14,01,17,14},//5{0,14,16,16,30,17,17,14},//6{0,31,01,01,02,04,8,16},//7{0,14,17,17,14,17,17,14},//8{0,14,17,17,15,01,01,01},//9{0,14,17,17,17,17,17,14},//0}bytebitmap[][8]={//redcharacters{0,0,0,0,0,0,0,0},{0,4,10,17,17,31,17,17},//A{0,30,17,17,30,20,18,17},//R{0,28,18,17,17,17,18,28},//D{0,0,0,0,0,0,0,0},{0,14,04,04,04,04,04,14},//I{0,17,25,25,21,19,19,17},//N{0,14,17,17,17,17,17,14},//O{0,0,0,0,0,0,0,0},{0,14,17,16,14,01,17,14},//S

Page 99: Arduino LED Projects - Robert Davis

{0,0,0,0,0,0,0,0},{0,14,17,16,16,19,17,14},//G{0,17,25,25,21,19,19,17},//N{0,0,0,0,0,0,0,0},}bytegbitmap[][8]={//greencharacters{0,0,0,0,0,0,0,0},{0,4,10,17,17,31,17,17},//A{0,30,17,17,30,20,18,17},//R{0,0,0,0,0,0,0,0},{0,17,17,17,17,17,17,14},//U{0,14,04,04,04,04,04,14},//I{0,0,0,0,0,0,0,0},{0,14,17,17,17,17,17,14},//O{0,0,0,0,0,0,0,0},{0,14,17,16,14,01,17,14},//S{0,14,04,04,04,04,04,14},//I{0,0,0,0,0,0,0,0},{0,17,25,25,21,19,19,17},//N{0,0,0,0,0,0,0,0},}voidRunSign(){for(introw=7;row>0;row—){//turnoffdisplaydigitalWrite(rowEnable,HIGH);digitalWrite(rclockPin,LOW);//sendserialdatatodisplay14=numberofcharactersfor(intcharacter=0;character<14;character++){for(intshiftbit=5;shiftbit>-1;shiftbit—){digitalWrite(gdataPin,LOW);

Page 100: Arduino LED Projects - Robert Davis

digitalWrite(dataPin,LOW);ifbitRead(gbitmap[character][row],shiftbit)digitalWrite(gdataPin,HIGH);ifbitRead(bitmap[character][row],shiftbit)digitalWrite(dataPin,HIGH);digitalWrite(clockPin,HIGH);digitalWrite(clockPin,LOW);}}//latchthedatadigitalWrite(rclockPin,HIGH);//setup74138rowsesectionandturndisplaybackonifbitRead(row,0)digitalWrite(row1Pin,HIGH);

elsedigitalWrite(row1Pin,LOW);ifbitRead(row,1)digitalWrite(row2Pin,HIGH);

elsedigitalWrite(row2Pin,LOW);ifbitRead(row,2)digitalWrite(row3Pin,HIGH);

elsedigitalWrite(row3Pin,LOW);digitalWrite(rowEnable,LOW);//WaittoseewhatwesenttothedisplaydelayMicroseconds(500);

}}//===LOOP===voidloop(){RunSign();}

Page 101: Arduino LED Projects - Robert Davis

Bibliography

ProgrammingArduinoGettingStartedWithSketches

BySimonMark

Copyright2012bytheMcGraw-HillCompanies

ThisbookgivesathoroughexplanationoftheprogrammingcodefortheArduino.

Howevertheprojectsinthebookareverybasic.ItdoescoverLCD’sandEthernetadapters.

GettingStartedwithArduinoByMassimoBanzi

Copyright2011MassimoBanzi

Thisauthorisaco-founderoftheArduino.

Thisbookhasaquickreferencetotheprogrammingcodeandsomesimpleprojects.