Professional Documents
Culture Documents
NEXT
PREV
1. Building a Weat
Creating a Tembo
Recent
Topics
Tutorials
Highlights
Settings
Feedback(http://community.safaribooksonline.com)
hardwareisworkingcorrectly.Wearegoingtomakethecorrect
hardwareconnectionsbetweenthedifferentcomponentsandwritea
simpleArduinosketchtotestallthesesensorsindividually.Bydoingthis,
Sign Out
wewillensurethatyoumakeallthehardwareconnectionscorrectly,and
thiswillhelpalotifyouencounterproblemsinthenextsectionsofthis
chapterthatusemorecomplexArduinosketches.
Thehardwareconnectionsrequiredforourprojectareactuallyquite
Settings
10 days left in your trial.
simple.WehavetoconnecttheDHT11sensorandthenthepart
responsibleforthelightlevelmeasurementwiththephotocellby
performingthefollowingsteps:
1. First,weconnecttheArduinoYnboard's+5Vpintotheredrailon
thebreadboardandthegroundpintothebluerail.
2. Then,weconnectpinnumber1oftheDHT11sensortotheredrail
onthebreadboardandpinnumber4tothebluerail.Also,connect
pinnumber2ofthesensortopinnumber8oftheArduinoYn
board.
3. TocompletetheDHT11sensorconnections,clampthe4.7kOhm
resistorbetweenpinnumbers1and2ofthesensor.
Forthephotocell,firstplacethecellinserieswiththe10kOhmresistor
onthebreadboard.Thispulldownresistorwillensurethatduringthe
operation,ifthereisnolightatall,thevoltageseenbytheArduinoboard
willbe0V.Then,connecttheotherendofthephotocelltotheredrailon
Enjoy Safari? Subscribe Today
thebreadboardandtheendoftheresistortotheground.Finally,connect
thecommonpintotheArduinoYnboardanalogpinA0.
ThefollowingimagemadeusingtheFritzingsoftwaresummarizesthe
hardwareconnections:
Nowthatthehardwareconnectionsaredone,wewillworkontestingthe
sensorswithoutuploadinganythingtotheWeb.Let'sgothroughthe
importantpartsofthecode.
First,wehavetoimportthelibraryfortheDHT11sensor,asfollows:
#include"DHT.h"
Then,weneedtodeclareacoupleofvariablesthatwillstorethe
measurements,asshowninthefollowingcode.Thesevariablesare
declaredasfloatsbecausetheDHTsensorlibraryreturnsfloatnumbers.
intlightLevel
floathumidity
floattemperature
Also,wecandefinethesensorpinandsensortypeasfollows:
Subscribe.
Feedback(http://community.safaribooksonline.com/)
Sign Out
#defineDHTPIN8
#defineDHTTYPEDHT11
CreatetheDHTinstanceasfollows:
DHTdht(DHTPIN,DHTTYPE)
Now,inthesetup()partofthesketch,weneedtostarttheserial
connection,asfollows:
Serial.begin(115200)
Next,inordertoinitializetheDHTsensor,wehavethefollowing:
dht.begin()
Intheloop()part,wearegoingtoperformthedifferentmeasurements.
First,wewillcalculatethetemperatureandhumidity,asfollows:
floathumidity=dht.readHumidity()
floattemperature=dht.readTemperature()
Then,measurethelightlevel,asfollows:
intlightLevel=analogRead(A0)
Finally,weprintallthedataontheserialmonitor,asshowninthe
followingcode:
Serial.print("Temperature:")
Serial.println(temperature)
Serial.print("Humidity:")
Serial.println(humidity)
Serial.print("Lightlevel:")
Serial.println(lightLevel)
Serial.println("")
Repeatthisevery2seconds,asshown:
delay(2000)
Thecompletesketchforthispartcanbefoundat
https://github.com/openhomeautomation/geekyprojects
yun/tree/master/chapter1/sensors_test.
Nowit'stimetotestthesketchanduploadittotheArduinoboard.Then,
opentheserialmonitorandyoushouldhavethedatathatcomesfromthe
sensorsbeingdisplayed,asshowninthefollowingscreenshot:
Ifyoucanseethedifferentmeasurementsbeingdisplayedasinthe
previousscreenshot,itmeansthatyouhavemadethecorrecthardware
connectionsonyourbreadboardandthatyoucanproceedtothenext
sectionsofthischapter.
Ifitisnotthecase,pleasecheckalltheconnectionsagainindividuallyby
followingtheinstructionsinthissection.Pleasemakesurethatyou
haven'tforgottenthe4.7kOhmresistorwiththeDHTsensor,asthe
measurementsfromthissensorwon'tworkwithoutit.
Tip
Downloadingtheexamplecode
YoucandownloadtheexamplecodefilesforallPacktbooks
youhavepurchasedfromyouraccountat
http://www.packtpub.com(http://www.packtpub.com).Ifyou
purchasedthisbookelsewhere,youcanvisit
http://www.packtpub.com/support(http://www.packtpub.com/support)
andregistertohavethefilesemaileddirectlytoyou.
Alltheuptodatecodeforthefourprojectsofthisbookcan
alsobefoundat
https://github.com/openhomeautomation/geekyprojectsyun.