1 quickstart - 30 arduino projects for the evil genius_ second edition
DESCRIPTION
211968634-TRANSCRIPT
-
CHAPTER 1
QuickstartTHISISACHAPTERfortheimpatientEvilGenius.YournewArduinoboardhasarrived,andyouareeagertohaveitdosomething.
So,withoutfurtherado
Powering UpWhenyoubuyanArduinoUnoorLeonardoboard,itisusuallypreinstalledwithasampleBlinkprogramthatwillmakethelittlebuiltinLEDflash.Figure11showsapairofArduinoboards.
PREVIntroduction
NEXT2 A Tour of Arduino
30 Arduino Projects for the Evil Genius: Second Edition Recent
Topics
Tutorials
Highlights
Settings
Feedback(http://community.safaribooksonline.com)
Sign Out
Settings
10 days left in your trial. Subscribe.
Feedback(http://community.safaribooksonline.com/)
Sign Out
Enjoy Safari? Subscribe Today
-
Figure11 ArduinosUnoandLeonardo.
Thelightemittingdiode(LED)markedLiswireduptooneofthedigitalinputoutputsocketsontheboard.Itisconnectedtodigitalpin13.Thisreallylimitspin13tobeingusedasanoutput,buttheLEDonlyusesasmallamountofcurrent,soyoucanstillconnectotherthingstothatconnector.
AllyouneedtodotogetyourArduinoupandrunningissupplyitwithsomepower.TheeasiestwaytodothisistopluginitintotheUniversalSerialBus(USB)portonyourcomputer.ForanArduinoUno,youwillneedatypeAtotypeBUSBlead.Thisisthesametypeofleadthatisnormallyusedtoconnectacomputertoaprinter.ForaLeonardo,youwillneedamicroUSBconnector.Youmaygetsomemessagesfromyouroperatingsystemaboutfindingnewdevicesorhardware.Ignorethesefornow.
Ifeverythingisworkingokay,theLEDshouldblinkonceeverytwoseconds.ThereasonthatnewArduinoboardshavethisBlinksketchalreadyinstalledistoverifythattheboardworks.ClickingtheResetbuttonshouldcausetheLEDtoflickermomentarily.Ifthisisthecase,buttheLEDdoesnotflash,thenitmayjustbethattheboardhasnotbeenprogrammedwiththeBlinksketchbutdonotdespair,asonceeverythingisinstalled,wearegoingtomodifyandinstallthatscriptanywayasourfirstproject.
Installing the SoftwareNowthatwehaveourArduinoworking,letsgetthesoftwareinstalledsothatwecanaltertheBlinkprogramandsenditdowntotheboard.Theexactproceduredependsonwhatoperatingsystemyouuseonyourcomputer.Butthebasicprincipleisthesameforall.
-
InstalltheArduinodevelopmentenvironment,whichistheprogramthatyourunonyourcomputerthatenablesyoutowritesketchesanddownloadthemtotheArduinoboard.
InstalltheUSBdriverthatallowsthecomputertotalktotheArduinosUSBport.Itusesthisforprogrammingandsendingmessages.
TheArduinowebsite(www.arduino.cc(http://www.arduino.cc))containsthe
latestversionofthesoftware.InthisbookwehaveusedArduino1.0.2.
Installation on Windows
TheinstructionsthatfollowareforinstallingonWindows7.TheapproachismuchthesameforVistaandXP.Theonlypartthatcanbealittledifficultisinstallingthedrivers.
FollowthedownloadlinkontheArduinohomepage(www.arduino.cc(http://www.arduino.cc)),andselectthedownloadfor
Windows.ThiswillstartthedownloadoftheZiparchivecontainingtheArduinosoftware,asshowninFigure12.Youmaywellbedownloadingamorerecentversionofthesoftwarethantheversion1.0.2shown(misleadingly,theArduinoteamhavenotgotaroundtorenamingtheZipfileyet).
-
Figure12 DownloadingtheArduinosoftwareforWindows.
TheArduinosoftwaredoesnotdistinguishbetweendifferentversionsofWindows.Thedownloadshouldworkforallversions,fromWindowsXPonward.ThefollowinginstructionsareforWindows7.
SelecttheSaveoptionfromthedialog,andsavetheZipfileontoyourdesktop.ThefoldercontainedintheZipfilewillbecomeyourmainArduinodirectory,sonowunzipitontoyourDesktop.Youcanmoveitsomewhereelselaterifyouwish.
YoucandothisinWindowsbyrightclickingtheZipfiletoshowthemenuinFigure13andselectingtheExtractAlloption.ThiswillopentheExtractionWizard,showninFigure14.
Figure13 TheExtractAllmenuoptioninWindows.
-
Figure14 ExtractingtheArduinofileinWindows.
ExtractthefilestoyourDesktop.
ThiswillcreateanewdirectoryforthisversionofArduino(inthiscase,1.0.2)onyourDesktop.Youcan,ifyouwish,havemultipleversionsofArduinoinstalledatthesametime,eachinitsownfolder.UpdatesofArduinoarefairlyinfrequentandhistoricallyhavealwayskeptprettygoodcompatibilitywithearlierversionsofthesoftware.Sounlessthereisanewfeatureofthesoftwarethatyouwanttouse,oryouhavebeenhavingproblems,itisbynomeansessentialtokeepupwiththelatestversion.
NowthatwehavegottheArduinofolderintherightplace,weneedtoinstalltheUSBdrivers.Ifyouhavenotalreadydoneso,plugyourLeonardoorUnointoyourcomputer.DependingonyourversionofWindows,theremaybesomehalfheartedattemptbytheoperatingsystemtoinstalldrivers.Justcancelthisattheearliestopportunityitisunlikelytowork.Instead,youneedtoopentheDeviceManager.ThisisaccessedindifferentwaysdependingonyourversionofWindows.InWindows7,youfirsthavetoopentheControlPanel,thenselecttheoptiontoviewIcons,andyoushouldfindtheDeviceManagerinthelist.
UnderthesectionOtherDevices,youshouldseeaniconforUnknownDevicewithalittleyellowwarningtrianglenexttoit.ThisisyourArduino(Figure15).
-
Figure15 TheWindowsDeviceManager.
RightclickontheUnknownDeviceandselecttheoptionUpdateDriverSoftware.YouwillthenbepromptedtoeitherSearchautomaticallyforupdateddriversoftwareorBrowsemycomputerfordriversoftware.Selecttheoptiontobrowseandnavigatetothearduino1.0.2windows\arduino1.0.2\drivers(Figure16).ChangetheversionnumbersifyouareusingadifferentversionofArduino.
-
Figure16 BrowsingfortheUSBdrivers.
ClickNext,andyoumaygetasecuritywarningifso,allowthesoftwaretobeinstalled.Oncethesoftwarehasbeeninstalled,youwillgetaconfirmationmessageliketheoneinFigure17.AlthoughthemessagewillbedifferentforaLeonardo,theprocedureisidentical.
-
Figure17 TheUSBdriverInstalledsuccessfully.
TheDeviceManagershouldnowlisttherightnamefortheArduino(Figure18).
-
Figure18 TheDeviceManagershowingtheArduino.
Thisisaoneoffprocessfromnowon,wheneveryoupluginyourArduinoboard,itsUSBdriverswillautomaticallybeloaded,andtheArduinowillbereadyforaction.
Installation on Mac OS X
TheprocessforinstallingtheArduinosoftwareontheMacisaloteasierthanonthePC.
Asbefore,thefirststepistodownloadthefile.InthecaseoftheMac,itisaZipfile.Oncedownloaded,doubleclickontheZipfile,whichwillextractasinglefilecalledArduino.app.ThisisthewholeArduinoapplicationjustdragitintoyourApplicationsfolder.
YoucannowfindandlaunchtheArduinosoftwareinyourApplicationsfolder.Asyouaregoingtouseitfrequently,youmaywishtorightclickitsiconinthedockandsetittoKeepinDock.
Installation on LINUX
TherearemanydifferentLINUXdistributions,andtheinstructionsforeachdistributionarealittledifferent.TheArduinocommunityhasdoneagreatjobofputtingtogethersetsofinstructionsforeachdistribution.Sofollowthelinkbelowandselectoneoftheten(atthetimeofwriting)distributionsonoffer.
Configuring Your Arduino Environment
-
Whatevertypeofcomputeryouuse,youshouldnowhavetheArduinosoftwareinstalledonit.Younowneedtomakeafewsettings.YouneedtospecifytheserialportthatisconnectedtotheArduinoboard,andweneedtospecifythetypeofArduinoboardthatweareusing.Butfirst,youneedtoconnectyourArduinotoyourcomputerusingtheUSBleadoryouwillnotbeabletoselecttheserialport.
Next,starttheArduinosoftware.InWindows,thismeansopeningtheArduinofolderandclickingontheArduinoicon(selectedinFigure19).YoumayprefertomakeashortcutfortheDesktop.
Figure19 StartingArduinoinWindows.
TheserialportissetfromtheToolsmenu,asshowninFigure110fortheMacandinFigure111forWindows7thelistofportsforLINUXissimilartotheMac.
-
Figure110 SettingtheserialportontheMac.
-
Figure111 SettingtheserialportonWindows.
IfyouusemanyUSBorBluetoothdeviceswithyourMac,youarelikelytohavequiteafewoptionsinthislist.Selecttheiteminthelistthatbeginswithdev/tty.usbserial.
OnWindows,theserialportcanjustbesettoCOM3orCOM4,whichevershowsup.
FromtheToolsmenu,wecannowselecttheboardthatwearegoingtouse,asshowninFigure112.
-
Figure112 Settingtheboard.
Downloading the Project SoftwareThesketches(asprogramsarecalledintheArduinoworld)usedinthebookareavailableasasingleZipfiledownload.Thewholedownloadislessthanamegabyte,soitmakessensetodownloadthesoftwareforalloftheprojects,evenifyouonlyintendtouseafew.Todownloadthem,browsetowww.arduinoevilgenius.com(http://www.arduinoevilgenius.com)and
followthedownloadlinksforthesecondeditionofthisbook.
Whateveryourplatform,theArduinosoftwareexpectstofindallyoursketchesinyourDocumentsfolder,containedwithinafoldercalledArduino,whichtheArduinosoftwarewillcreatethefirsttimeitisrun.SoplacethecontentsoftheZipfileintothatfolder.
Notethateachsketchcomesinitsownfolders,andthesketchesarenumberedbyproject.
Project 1Flashing LEDHavingassumedthatwehavesuccessfullyinstalledthesoftware,wecannowstartonourfirstexcitingproject.Actually,itsnotthatexciting,butweneedtostartsomewhere,andthiswillensurethatwehaveeverythingsetupcorrectlytouseourArduinoboard.
-
WearegoingtomodifytheexampleBlinksketchthatcomeswithArduino.WewillincreasethefrequencyoftheblinkingandtheninstallthemodifiedsketchonourArduinoboard.Ratherthanblinkslowly,ourboardwillflashitsLEDquickly.WewillthentaketheprojectastagefurtherbyusingabiggerexternalLEDandresistorratherthanthetinybuiltinLED.
COMPONENTSANDEQUIPMENT
Software
First,weneedtoloadtheBlinksketchintotheArduinosoftware.TheBlinksketchisincludedasanexamplewhenyouinstalltheArduinoenvironment.SowecanloaditusingtheFilemenu,asshowninFigure113.
-
Figure113 LoadingtheexampleBlinksketch.
Thesketchwillopeninaseparatewindow(Figure114)youcan,ifyoulike,nowclosetheemptywindowthatopenedwhenArduinostarted.
-
Figure114 TheBlinksketch.
Themajorityofthetextinthissketchisintheformofcomments.Commentsarenotactuallypartoftheprogrambutexplainwhatisgoingonintheprogramtoanyonereadingthesketch.
Commentscanbesinglelinecommentsthatstartaftera//andcontinuetotheendoftheline,ortheycanbemultilinecommentsthatstartwitha/*andendsomelineslaterwitha*/.
Ifallthecommentsinasketchweretoberemoved,itwouldstillworkinexactlythesameway,butweusecommentsbecausetheyareusefultoanyonereadingthesketchtryingtoworkoutwhatitdoes.
Beforewestart,alittlewordaboutvocabularyisrequired.TheArduinocommunityusesthewordsketchinplaceofprogram,sofromnowonIwillrefertoourArduinoprogramsassketches.OccasionallyImayrefertocode.Codeisprogrammerspeakforasectionofaprogramorevenagenerictermforwhatiswrittenwhencreatingaprogram.Sosomeonemightsay,Iwroteaprogramtodothat,ortheycouldsay,Iwrotesomecodetodothat.
-
TomodifytherateatwhichtheLEDwillblink,weneedtochangethevalueofthedelaysointhetwoplacesinthesketchwherewehave
changethevalueintheparenthesesto200sothatitappearsas
ThisischangingthedelaybetweenturningtheLEDonandofffrom1000milliseconds(1second)to200milliseconds(1/5thofasecond).InChapter3wewillexplorethissketchfurther,butfornow,wewilljustchangethedelayanddownloadthesketchtotheArduinoboard.
Withtheboardconnectedtoyourcomputer,clicktheUploadbuttonontheArduino.ThisisshowninFigure115.Ifeverythingisokay,therewillbeashortpause,andthenthetworedLEDsontheboardwillstartflashingawayfuriouslyasthesketchisuploadedontotheboard.Thisshouldtakearound5to10seconds.
Figure115 UploadingthesketchtotheArduinoboard.
Ifthisdoesnothappen,checktheserialportandboardtypesettingsasdescribedintheprevioussections.
Whenthecompletedsketchhasbeeninstalled,theboardwillautomaticallyreset,andifeverythinghasworked,youwillseetheLEDfordigitalport13starttoflashmuchmorequicklythanbefore.
Hardware
Atthemoment,thisdoesntreallyseemlikerealelectronicsbecausethehardwareisallcontainedontheArduinoboard.InthissectionwewilladdanexternalLEDtotheboard.
-
LEDscannotsimplyhavevoltageappliedtothemtheymusthaveacurrentlimitingresistorattached.Bothpartsarereadilyavailablefromanyelectronicssuppliers.ThecomponentordercodesforanumberofsuppliersaredetailedintheAppendix.
TheArduinoboardconnectorsaredesignedtoattachshieldpluginboards.However,forexperimentationpurposes,theyalsoallowwiresorcomponentleadstobeinserteddirectlyintothesockets.
Figure116showstheschematicdiagramforattachingtheexternalLED.
Figure116 SchematicdiagramforanLEDconnectedtotheArduinoboard.
Thiskindofschematicdiagramusesspecialsymbolstorepresenttheelectroniccomponents.TheLEDappearsratherlikeanarrow,whichindicatesthatlightemittingdiodes,incommonwithalldiodes,onlyallowthecurrenttoflowinonedirection.ThelittlearrowsnexttotheLEDsymbolindicatethatitemitslight.
Theresistorisjustdepictedasarectangle.Resistorsarealsooftenshownasazigzagline.Therestofthelinesonthediagramrepresentelectricalconnectionsbetweenthecomponents.Theseconnectionsmaybelengthsofwireortracksonacircuitboard.Inthiscase,theywilljustbethewiresofthecomponents.
WecanconnectthecomponentsdirectlytotheArduinosocketsbetweenthedigitalpin12andtheGNDpin,butfirstweneedtoconnectoneleadoftheLEDtooneleadoftheresistor.
ItdoesnotmatterwhichleadoftheresistorisconnectedtotheLEDhowever,theLEDmustbeconnectedthecorrectway.TheLEDwillhaveoneleadslightlylongerthantheother,anditisthelongerleadthatmustbeconnectedtodigitalpin12andtheshorterleadthatshouldbeconnectedtotheresistor.LEDsandsomeothercomponentshavetheconventionofmakingthepositiveleadlongerthanthenegativeone.
-
ToconnecttheresistortotheshortleadoftheLED,gentlyspreadtheleadsapart,andtwisttheshortleadaroundoneoftheresistorleads,asshowninFigure117.
Figure117 AnLEDconnectedtoaserialresistor.
ThenpushtheLEDslongleadintothedigitalpin12andthefreeleadoftheresistorintooneofthetwoGNDsockets.ThisisshowninFigure118.Sometimesithelpstobendaslightkinkintotheendoftheleadsothatitfitsmoretightlyintothesocket.
-
Figure118 AnLEDconnectedtotheArduinoboard.
WecannowmodifyoursketchtousetheexternalLEDthatwehavejustconnected.Allweneedtodoischangethesketchsothatitusesdigitalpin12insteadof13fortheLED.Todothis,wechangetheline
toread
NowuploadthesketchbyclickingtheUploadtoIOBoardbuttoninthesamewayasyoudidwhenmodifyingtheflashrate.
BreadboardTwistingtogetherafewwiresisnotpracticalforanythingmuchmorethanasingleLED.Abreadboardallowsustobuildcomplicatedcircuitswithouttheneedforsoldering.Infact,itisagoodideatobuildallcircuitsonabreadboardfirsttogetthedesignrightandthencommitthedesign
-
tosolderonceeverythingisworking.
Abreadboardcomprisesaplasticblockwithholesinit,withsprungmetalconnectionsbehind.Electroniccomponentsarepushedthroughtheholesatthefront.
Underneaththebreadboardholes,therearestripsofconnectors,soalltheholesinastripareconnectedtogether.Thestripshaveagapbetweenthemsothatintegratedcircuitsindualinlinepackagingcanbeinsertedwithoutleadsonthesamerowbeingshortedtogether.
Wecanbuildthisprojectonabreadboardratherthanwithtwistedwires.Figure119showsaphotographofthis.Figure120makesitalittleeasiertoseehowthecomponentsarepositionedandconnectedtogether.
Figure119 Project1onbreadboard.
Figure120 Project1breadboardlayout.
Youwillnoticethatattheedgesofthebreadboard(topandbottom)therearetwolonghorizontalstrips.Theconnectionsonthebackoftheselongstripsrunatrightanglestothenormalstripsofconnectionsandareused
-
Recommended / Queue / Recent / Topics / Tutorials / Settings / Blog(http://blog.safaribooksonline.com) / Feedback(http://community.safaribooksonline.com/) / Sign Out 2015 Safari(http://www.safaribooksonline.com/). Terms of Service / Privacy Policy
toprovidepowertothecomponentsonthebreadboard.Normally,thereisoneforground(0VorGND)andoneforthepositivesupplyvoltage(usually5V).
Inadditiontoabreadboard,youwillneedsomejumperwires(seetheAppendix).Theseareshortleadsofafewinchesinlengthofdifferentcolors.TheyareusedtomakeconnectionsbetweentheArduinoandthebreadboard.Alternatively,youcanusesolidcorewireandsomewirestrippersorplierstocutandremovetheinsulationfromtheendsofthewire.Itisagoodideatohaveatleastthreedifferentcolors:redforallwiresconnectedtothepositivesideofthesupply,blackfornegative,andsomeothercolor(orangeoryellow)forotherconnections.Thismakesitmucheasiertounderstandthelayoutofthecircuit.Youcanalsobuypreparedshortlengthsofsolidcorewireinavarietyofcolors.Notethatitisnotadvisabletousemulticorewirebecauseitwilltendtobunchupwhenyoutrytopushitintothebreadboardholes.
WecanstraightenoutthewiresofourLEDandresistorandplugthemintoabreadboard.Thebreadboardusedisoftenreferredtoasahalfsizebreadboardandhas30rowsofstrips,eachstripbeingfiveholes,thenagap,thenanotherfiveholes.Wewillbeusingthisbreadboardalotinthisbook,soifyoucanfindsomethingassimilaraspossible,itwillmakelifeeasier.TheactualboardusedwassuppliedbyAdaFruit(seetheAppendix),butitisaverycommonsizeandlayout.
SummaryWehavecreatedourfirstproject,albeitaverysimpleone.InChapter2wewillgetabitmorebackgroundontheArduinobeforemovingontosomemoreinterestingprojects.