DHT11 and DHT22 Temp & Humidity Sensors on Raspberry PI and Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this week on maker's digest we're gonna implement dht11 and 22 temperature and humidity sensors on the raspberry pi and the arduino all the parts all the code come on in [Music] welcome back to makers digest today we're gonna look at sensors these are the dht11 and dht22 temperature and humidity sensors they're both inexpensive tiny and come packaged in a plastic enclosure to protect the individual components inside they're basically the same package but have different specs and when I say basically the same I mean they operate identically they can be interchanged and all you have to do is swap out one line of code the pins are exactly the same this one is the dht11 and this one is the dht22 they both operate on three to five volts and consume about 2.5 million at max current the 11 can sense humidity between 20 and 80 percent with a 5 percent accuracy while the 22 can sense humidity between zero and a hundred percent with 2 to 5 percent accuracy on the temperature side the 11 is good for 0 to 50 degrees Celsius with an accuracy of plus minus 2 degrees and the 22 is good for negative 40 to 80 degrees Celsius with accuracy of plus minus point zero five degrees the 11 can be sampled once per second but the 22 takes is a hit on the sample rate for accuracy and can only be sampled once every two seconds as usual we're gonna connect both of these units to an Arduino and a Raspberry Pi let's jump over to the workbench and get started and here we go with the Arduino these sensors are pretty simple to connect to the Arduino because they pull such a small amount of current we can use the onboard power so we only need the three pins that this thing uses let's take a look at what pins are what so if you hold the sensor with the mesh side up or the grid on here you can see the back is flat this has the grid side we're gonna look at this is pin 1 2 3 & 4 and pin 1 is power in pin 2 is the digital signal and 3 is unused not just for this Apple tation but it seems like it's just completely unused and pen 4 is ground what we're gonna need to connect this is the sensor itself a pull-up resistor and you should use a 10k resistor and some of you out there probably read these bands and say hey that's not a 10k resistor no it's not it's a three point 3k resistor and the reason I'm using that is I don't have a 10k resistor three point three is gonna be fine it'll work in fact these will work without the pull-up resistor but it's good to have it in there the last thing that we need are three male-to-male jumper wires let's get into it we'll start with a dht11 so we can connect this to any of the pins here Bend that back a little bit so you can see it take our ground from ground on the Arduino to pin four take our power from five volts on the Arduino to pin one on the sensor take our signal wire we're gonna run it from digital pin 2 to pin two on the sensor there's no correlation there I just like using pen two on the Arduino the last thing is to put our pull-up resistor between pen one and two on the sensor side let's get this in there there it is pretty simple let's take a look at the code so as usual I wrote the code on my time not on yours we're gonna go through this line by line briefly and we'll run the example after that so first things first we import the DHT library that is the Adafruit DHT library that we're going to use and there's instructions on the makers digest github page on how to install that for Arduino it's not not hard but there's just a couple of steps you have to follow we're using our DHT pen we're using pen two that's digital pen we have a delay of 2 seconds 2000 milliseconds here we define what type of sensor we're going to use so since this is the dht11 we have to uncomment that and comment out the dht22 when we pop the dht22 and all we have to do is change that here we instantiate the DHT object from the library set it with our what pin we're on and the type that we're using in the setup portion all we have to do is say DHT begin down here in our loop we've got we introduce our delay of 2,000 milliseconds and a couple of notes here you can pass true to DHT read temperature to return the value in Fahrenheit if you leave that blank or set it to false it will return in Celsius note too there's a bunch of other functions that the our Adafruit DHT library will do I'm not going to cover all of them here but if you go to this page on github you can look through the source code and find all of the cool other functions and methods that are there here's where we read in the actual temperature and humidity so they're both float humidity we do DHT read humidity float temperature equals DHT read temperature and then we want to check and see if those have values as long as this is working they're both going to return a value so we can check if humidity or temperature is NaN if either one of those are true it's going to give us an error on the serial console and then return so that return will just kick it back up to the loop it'll keep going right through here and not get to this point if this does pass it's going to continue through the loop and we're gonna print out our humidity and temperature so let's fire it up and see what happens blinky blinky on running and here we go usually this first number is a little goofy and then it stables out and starts giving us a more accurate reading so how is it every two seconds it's going to give us a value this will read once every second we're just doing every two so if you want to change that in the code up here to one second you can to swap over to the dht22 this is very simple we're going to stop our code here we're just going to upload a blank sketch so we don't have any output here and since they have the exact same pin out this is power digital unused and ground we can just pull that out pop this one in go over here change the type by commenting out the dht11 and uncommenting dht22 load it up load blink blink running and here we go again we get our walkie first value and then stabilizes and we have our other values and it is a different value because these two temperature sensors and humidity sensors have a different accuracy so if you remember this is plus minus 0.5 degrees Celsius on the temperature so that's why this temperature is dramatically different than the other one I mean a couple of degrees is pretty dramatic did you remember this has plus minus of 2 degrees Celsius and that's it let's move over to the raspberry pi for the raspberry pi it is also a very simple setup in connection as it's only 3 wires if you just skip the Arduino and got right over here to the Raspberry Pi we'll look at the pins again if you'd look at the top where the grid is on the back there's no grid we're looking at pin 1 2 3 & 4 pin 1 is voltage in and 2 is digital out pin 3 is unused and pin 4 is ground well we're going to need to connect this to the Raspberry Pi is the sensor itself a pull-up resistor and you're going to want to use a 10k resistor this is a three point 3k resistor because I'm out of 10k it works fine in fact you can run this without the pull-up resistor and it'll work it's just good to have it in there we also need three male two female jumper wires let's get it going take the sensor pop it into your breadboard bend mine back a little bit so you can see it will take ground off the Raspberry Pi connect that to pin two sorry 10-4 it's going into one two three four power we're gonna connect it to five volts on the Raspberry Pi in two pin one and our signal we're going to use GPIO pin four which just happens to be the fourth one down on the row furthest from me and this one will go to pin to our pull-up resistor we'll go between pin 1 and 2 on the sensor side there it is it's that simple now let's take a look at the code as usual wrote all the code on my time not yours we'll go through this briefly line by line so you can see what's happening we import the cysts module sleep from the time module and we're using the Adafruit DHT library for this in the makers digest github page for this episode there are instructions on how to install this there's a couple of different ways you can install it take your pick have the install it's pretty easy we're running on pen 4 we have a delay of 2 seconds and our sensor type is either 22 or 11 and for this one it is 11 so we're just gonna change that out down here in our loop we introduce our delay of 2 seconds and this line is where we read the temperature and humidity in so the method for that is Adafruit underscore DHT dot read underscore retry and we put in our sensor type and what pen we're working with this method doesn't have a way to return Fahrenheit so down here we're going to do some pretty basic math to do that conversion manually here we check to see if the values and they're not set to none so if humidity is not none or temperature is not none then we're gonna print out our values elsewise we will print an error and run through the loop again so let's run this see what it looks like sudo python hi we're getting some wonky numbers here I wonder why all right let's try that again apparently with my keyboard at a weird angle I didn't save it when we change it to the eleven in there aha now we're getting something a little more sane so it's showing 77 degrees Fahrenheit with 41 percent humidity go 40 okay make a liar out of me again easy to swap out for the dht22 because we have this exact same pin out let's stop this pull that pop the 22 back in now we'll go in and change this to sensor type of 22 I'll save this correctly this time and run it again there we go we get kind of a wonky reading out of the first one but it stabilizes you can see the values are different here and that's because this sensor is a little more accurate it's on the humidity side it's two to five percent accuracy so zero to 100% humidity with plus minus two to five percent and on a temperature side it's a minus forty to eighty degrees Celsius plus minus 0.5 degrees Celsius and so it's a little bit more accurate we can only pull this one once every two seconds so that's the cost of being a little bit more accurate so there you have it we've got both sensors running on an Arduino and a Raspberry Pi so now you have learned how to do this go be creative and then share with me what you've made I want to see it I can be found on Twitter and Instagram Facebook YouTube patreon all the usual business and if you like this video hit the subscribe button and ring that little bell see you get notified when we have a new video available until then thanks for watching and we'll see you next time
Info
Channel: Maker's Digest
Views: 14,980
Rating: undefined out of 5
Keywords: Maker, Maker Project, Makers Digest, makersdigest, digest, DIY, Build
Id: D1Acq_LUC40
Channel Id: undefined
Length: 16min 17sec (977 seconds)
Published: Tue Dec 18 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.