Get Started in Electronics #9 - Using the DHT11 Humidity & Temp Sensor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello 3d printing and electronics friends today on the bb 3d channel we're going to see how to use the humidity and temperature sensor with the arduino uno or in this case the lagoo uno stick around and we'll get into it right after this i'm brian and you are watching bv 3d hi welcome back hey if you're new here and you're wanting to learn about cool 3d printer upgrades 3d modeling and other 3d printing and occasionally electronics related stuff start now by subscribing and clicking the bell so you don't miss anything okay so this is the ninth episode in the getting started with electronic series in which we're using the elegoo super starter kit for uno this inexpensive kit features elegu's version of the arduino uno microcontroller board it includes the uno board a breadboard for building circuits and a huge assortment of sensors electronics components and connecting wires check the description for a link to the kit if you're interested in it today we're going to see how to use the dht11 humidity and temperature sensor now to help keep things organized for each project if you have a 3d printer or you know someone who does you can download and print this project organizer that i designed it's free to download and as with many things there is a link in the description for it it holds the uno and the breadboard and it has a tray up front to help keep the project parts all in one place now last time we learned how to get distance information from an ultrasonic sensor using some code that we wrote and uploaded to the uno and to make that easier we used a code library which makes the task of getting data from the sensor super easy this time our code will let us make use of the humidity and temperature sensor module and like last time we'll use a code library to talk to the sensor the great thing about code libraries is that they take care of the complicated code that's sometimes necessary to get to uno to talk to a sensor so using them makes sense since they let you find out uncensored what the sensor is sensing but we'll get to the code in a little bit first i want to talk about the sensor itself and how it works and then we can get this project wired up so the dht11 sensor and i think dht is digital humidity and temperature is not just a single sensor inside the case there is a small circuit board on one side of the board there are two separate sensors a humidity sensor and a temperature sensor on the other side of the board there is a chip that reads the sensors and handles the analog to digital conversion to store the readings as digital information and also takes care of serial communication so we can use a single data pin to ask for and receive its readings so the humidity sensor has a moisture sensitive material sandwiched between two electrodes as the amount of moisture in the air increases so too does the amount of moisture in the sensor and this changes the resistance between the electrodes in a predictable way in other words whenever the humidity in the air is at 50 percent the resistance between the electrodes is always the same value so whenever the chip on the board reads the sensor and it gets that resistance value it knows the humidity is 50 and the temperature sensor works the same way more or less this sensor is a thermistor a thermally variable resistor therm from thermal and ester from resistor therm ester thermistor so as the temperature increases the thermistor's resistance decreases so at a temperature of say 30 degrees c for example it's always the same value so whenever the chip on the board reads the thermistor and gets that value it knows the temperature is 30 degrees c well now that we have some idea of how this little thing works let's gather up the parts for the project and make a few connections in addition to the dht-11 sensor itself we need the uno and the breadboard and we'll need some wires to connect the two i'm going to use a short red and black wire for the power and ground and i'm going to use a longer green wire for the data connection looking at the sensor from the front the three pins on it are data power and ground on the uno we're going to be using pin 2 for data so let's start by connecting the green wire from pin 2 on the uno to a spot on the breadboard then we need power and ground so let's connect the short red wire to the 5 volt pin on the uno and put it right here next to the green pin and then for ground we'll plug the black wire into one of the ground pins on the uno and the other side of it right next to the red wire so when you're done you should see them like this green red and black now we can plug the sensor in just make sure the green wire goes to the first pin here and we'll just press the sensors pins into the breadboard like this and we're done with the wiring and now it's time to write some code as i mentioned we're going to be using a code library to make talking to the sensor so much easier as always we'll be using the arduino integrated development environment or ide if you don't have it it's free to download and use it's easy to install and there's a link in the description so open up the ide and you should be presented with a new blank sketch if not click the file menu then click new to get one and first thing let's save the sketch as dht sensor saving now saves us from being asked to save later when we upload it like anything else involving computers the motto save early save often applies this is also good financial advice now right up front i need to tell you that we are not using the code sample included with the kit for this sensor it's a little overly complicated in a few areas and we're also not using the code library included with the kit for the sensor instead we'll be using the adafruit dht sensor library to install it click the tools menu then click manage libraries in the search field here type dht and the one we want is dht sensor library by adafruit so let's install that as soon as you click the install button you'll probably see a message like this indicating that the dht sensor library depends on another library to do its job since we need this to actually work we'll click the install all button the entire installation literally only takes a few seconds so once it's done click the close button to dismiss the library manager window with the dht sensor library installed we can start writing some code to save a little time on this one i'll be pasting the code in a little at a time and i'll talk about it as we go along i have adapted the code from the adafruit sample code for this library to get it to the bare minimum for what we want to accomplish so the first thing we need to do is tell the ide that we want to use the adafruit dht sensor library in this sketch so we'll do that with pound include dht.h note the quotation marks around dht.h and also note the lack of a semicolon at the end of the line next we'll define the arduino pin we're using for communication with the sensor we connected the sensor's data pin to pin 2 on the uno so that's what we're specifying here with pound define dht pin 2 and again no semicolon at the end of the line then we're specifying the sensor type there are multiple kinds of dht sensors and the one that we're using is a dht11 so that's what we're specifying here with pound define dht type dht11 and yet again there's no semicolon at the end of the line it's like we're breaking all the rules but really we aren't these lines beginning with pound signs are compiler directives and they're like instructions for the ide rather than commands for the uno and then we need to initialize the sensor library getting a dht object that's configured as a dht 11 communicating with pin 2 on the uno and that's done with dht dht dht pin comma dht type and a semicolon at the end of the line this time it is uno code so that's why we've got the semicolon so down here in the setup function we need to set up serial communication we'll be using the serial monitor window in the ide to see what the uno is telling us about the humidity and temperature so first we'll set it up to use a data rate of 9 600 bits per second with serial.begin 9600 semicolon and then we'll use serial.printline to send the text humidity and temperature to the serial monitor don't forget the semicolon at the end and the last thing we need to do in setup is to start the sensor so we'll do that with dht.begin open and close parenthesis and a semicolon at the end and that's it for setup so let's get down into the loop function the dht11 can handle being asked for humidity and temperature data once per second but we'll actually wait two seconds every time through the loop just so we're not pestering it all the time so delay 2000 and the semicolon will tell the uno to two thousand milliseconds which is two seconds then we need to ask the sensor for the current relative humidity and we'll do that by using a floating point variable named h so float h equals dht dot read humidity open and close parenthesis semicolon will take care of that remember up in setup where we said dht dot begin that dht represents the sensor in code and that's the object that we're interacting with so anywhere that we have a lowercase dht followed by a dot followed by something else that something else is how we're telling the sensor to do something next we'll ask the sensor for the current temperature the sensor returns a value in degrees c and we'll store that in a floating point variable named t so float t equals dht dot read temperature open and close parenthesis semicolon gets us the temperature now we've got variables h and t holding the current humidity and temperature values the next thing we need to do is send these values back to the ide so that we can see them and we'll do that with the serial.print and serial.printline commands the difference between the two is that serial.printline adds a return to the end of what it prints out so that whatever comes next goes on the next line serial.print doesn't do this so you can use serial.print to put multiple items on the same line so here's a big block of code and that's all the commands that we're using to send the humidity and temperature back to the ide for display we've got serial.print humidity to print the word humidity we've got serial.print h to print the value of the humidity variable we've got serial.print percent and then temperature this is so the percent sign follows the humidity and then we have the temperature label and we've got dot print t to print the value of the temperature variable and we've got serial dot print line degree symbol c which prints exactly that and returns the cursor to the start of the next line so two seconds from now when we've read the sensor again and we want to send the current humidity and temperature to the ide it looks nice make sure you've got all the lines typed as shown above and note the parentheses quotes spaces and the semicolons at the ends of the lines okay code's done let's make sure the ide knows what we're working with boardwise and how it's connected first connect the computer and the uno together with a usb cable then click the tools menu then go to board avr boards and make sure arduino uno is selected now this grouping of board types like avr boards is something new in the arduino ide if you're on an older version of the ide the board menu may look a little different but the important thing is that you've got the correct board selected after you've made sure the arduino uno board is selected click tools again then go to port and make sure you've selected the port that the uno is connected to on mine i'm using the port that actually gets tagged as arduino uno i don't know if that's specific to the mac version of the arduino ide or if the windows and linux versions also tag the port like that but that's the port that i'm using now that we've got that set up let's check for errors by clicking the check mark button here at the top left corner of the window if everything's okay great if not check the error messages to see where you might have a problem usually what you'll run into is that you've forgotten a semicolon at the end of a line or mistyped a word if that happens to you don't worry just look carefully at the error and check your typing i don't feel bad either it happens to me too okay assuming everything's great let's open the serial monitor window remember the uno is reporting humidity and temperature data back to the ide and this is how we'll be able to see what it's saying so click the tools menu and pick serial monitor down here along the bottom of that window i have the line endings set to new line i also have the speed set to 9600 baud which matches what we specified in the setup function in our sketch now to get the code to the uno click the upload button and that's the one with the arrow on it right next to the check mark button that'll compile the sketch and send the code to the uno it should only take a couple of seconds when it's done the uno will begin reporting the humidity and temperature in the serial monitor window now of course clicking the upload button brought the sketch window to the front so we can go to tools and pick serial monitor again to see what the uno is saying so here at bv3d studios in fabulous downtown podunk texas we have about 39 relative humidity and a pleasant 25-ish degrees c and just to test the sensor if i blow on it the humidity and temperature will go up a bit and then return back to normal right now this has somewhat limited use because we have to be tethered to the computer to be able to see the temperature and humidity but in a future electronics episode we'll be able to interface with an lcd module to display data on its screen and that'll be pretty cool when we get to that one you can do a mashup with this episode or with the ultrasonic sensor episode to be able to display data from those sensors on the lcd but that's going to be three or four episodes from now because we still have several interesting sensors to go through well 3d printing and electronics friends that's about all the time we have for this episode and now that we're at the end if you're doing these projects on the laptop why not move the computer and the project into other rooms in your house or even outside to see differences in humidity and temperature that could be cool or it could be warm if you're not sure this project will tell you hey real quick before you go i wanted to say thanks for being one of these super awesome people who sticks around all the way to the end and thanks for all the likes comments and shares and an especially big thanks to those who directly support what i do you're all wonderful for doing that and i really appreciate it if you liked this episode a thumbs up would be great and if you'd like to help support the channel check the description for ways you can do exactly that oh i've got some other videos here that you might want to take a look at too and if nothing else please consider subscribing if you haven't already done so it's absolutely free and it's an excellent way to help keep me making these videos for you well that's it for this one thanks again and i'll see you next time here on the bb 3d channel
Info
Channel: BV3D: Bryan Vines
Views: 36,768
Rating: undefined out of 5
Keywords: arduino tutorial, arduino project, arduino programming, arduino uno, arduino tutorial for beginners, arduino programming basics, arduino uno projects, arduino uno r3, STEM, STEAM, STEM Projects, STEAM Projects, arduino projects for beginners, arduino projects, elegoo super starter kit, arduino code, arduino coding, electronics projects, how to, dht11 arduino, dht11 sensor arduino, dht11 sensor, dht11 arduino code, dht11 temperature and humidity sensor arduino code
Id: bB1RChYdR5A
Channel Id: undefined
Length: 15min 32sec (932 seconds)
Published: Mon Oct 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.