My name is Ahmad Shamshiri and I am presenting this tutorial from Canada.
Hi welcome to Robojax. In this video I'm going to show you how you can use this HDT22
humidity and temperature sensor using Arduino . and also I'm gonna provide you the code so you
can read the temperature something like this and degrees Celsius and Fahrenheit also humidity and
temperature index. and also I'm gonna provide you the code the original library code and the code
that I've customized to print and display or get the values easily. you can get the codes for this
by clicking at the link of the description below or you can go directly to
robojax.com/learn/arduino Let's get started with this. actually the sensor AM2302. so once they put it on
that PCB module they call it dht22 by Adafruit. so this is produced by Aosong Guang Zhao and here
are the specifications. so it can work with three point three to five volts and the measuring range
for temperature is minus -40 to plus 80 degrees Celsius which is -40 to 176
Fahrenheit. so that's very important to know it goes very low in negative direction. the positive
it goes a little up with comparing to the DHT11 . so the measuring range for the humidity is 0 to
99 percent accuracy is 0.5 percent in Celsius which is very good and then humidity accuracy
s plus minus 2 percent at 25 degrees Celsius but the long-term drift every year it will be
less than 0.1 Celsius and also the humidity will be 0.5 and 1 year so as it gets older it loses
sensitivity so to keep that in mind. and then the resolution is 0.1 celsius. meaning it cannot show
zero point zero something it can jump one tenth of a siliceous tenth of a percent in humidity
sensor type capacitive sensors so the type of sensor is used. and it's the material and weight
is also mentioned. so the transmission line can be up to 20 meters that's very important that's long
enough with that single wire it can communicate. so this actual sensor AM3302 is now
is here as a as dht22 so this is sold with this module with 3 pins though it has four pins. so
for our purpose we just need one + and then minus (-) so this can be connected to five volts
and then the mother one is for communication so that's the output. and that's serial communication
it can be extended up to 20 meter according to the specification so the wire that's long enough
for industrial application or project that you guys have so you can extend the wire up to 20
meter it will not affect the communication. but than that so there is a screw that you can put
it here or maybe screw this on somewhere to hold it stable. cover toll anything so this should be
able to easily access the flow of care so it can measure the temperature and humidity. the width
is 15 point five millimeter so the length of the device is thirty seven point five millimeter and
thickness is nine point three millimeter. so here I am connecting the this is will be connected to
five volts that this will be connected to ground and this is connected to pin 2 and this code.
so here of an Arduino so that if the 5 volts have been connected here the red
the black with here and then the pin 2 is connected for data reading from the sensor.
Downloading the library
I will provide the link so you can come here and download the library also this library
will be provided directly on Robojax.com so you can get it in case it's changed or moved
so once you arrive at this page just click download and select the location I'm going
inside the download and then library and then saving it after it's finished just go open it
so this is the files now right-click you can get 7-zip which is free from www.7-zip.org extract here it
will create a folder with the same name. there's a file DHT sensor library master. because there
are a lot of files here that creates problem and I've tested it there are so many issues so
I have removed this unified and unified for this purpose because it has some code that interferes
so I've deleted this from here so just delete then right-click copy and then go to your do we know in
my case it's and C:\program files in your case it might be C:\program file\Arduino and my case
is directly inside the download so once you open the main folder go inside so this is I mean like
or do you know that you're running go to library folder and then right-click somewhere I pasted once you
pastes you will have this folder with these two files dot CPP and dot H. so these are the important
the rest are just complementary files that are not necessarily giving you errors or anything so these
two must exist here click on file go example and then scroll down until you see DHT sensor library
and then select the second one tester. so this is the main code for that is provided by other
Code explained
good provided by Adafruit. so let me explain it at the first line
so let me explain at the first line it says include dht.h this was a file that I just
showed you. so this was this file that is needed and inside this file we have reference did that
dot cpp so these two must be together here and then here we are defining that the hv the pin
so i'm using pintwo you can do pin two up to thirteen whichever pin you like and this is a
pin that the pin out will be connected and then we have this three different why aligns with dht11
dht22 and DHT twenty one which has different module the one that we are using dht22 is here
so this might be something like this commented. so remove the comment so this comment means they are
just disabling this and disabling this only this one is active. with a hash tag at the beginning so
that is done and then we're initializing it here with a pin and the type the type that are defining
here twenty two so these two will be passed to those files to do the job for us. and then this
is for initializing the serial monitor and this is just bracele printing the text and then the begin
so setup is done. so this will run only once after that we have
two thousand millisecond delay so this waits between the measurement s necessary. and these
are all comments so the I'm not gonna explain this so DHT is now an object here so this is an
object have been initialized so this as we call it an object and this is a method so this method
belongs to this object object read humidity DHT Daughtrey humidity when we put this
it will get the humidity and then read temperature it will get
the temperature in Celsius and store it in T this will get the temperature and put it in Fahrenheit
we got here humidity temperature in Fahrenheit here we are
checking it if humidity this means all centigrade temperature or Fahrenheit if there are if they
don't exist which means it was they were not able to get it it says failed to read censored so this
will print the text for us so we are passing the temperature in Fahrenheit and humidity to this
method to calculate humidity and the heat index and Fahrenheit and here heat index and Celsius
so these are all the functions or methods that are getting the value and these are the texts so this
H is actual text the value this T is actual value F is the actual value each I see as the actual
value and these are the texts the rest inside the double quotation or the text so these are you can
change it whatever you want so let me upload the code and then also show the serial monitor here
so this is the way it it shows so it shows the humidity and shows temperature in Celsius this
this means Celsius so this is Fahrenheit and then heat index and Celsius and heat index and
Fahrenheit with this heat index is related to humidity as well so that is being calculated
and displayed now this is this has too much cluttering and I have simplified and written
a different project for this using the same method so let me show you that one so here is the custom
code that I made so from here nothing has changed and I have made the changes inside the loop so and
I've removed all the other stuff so the only thing that you need to get the temperature is this get
temp and inside the quotation just put C so this will give you the centigrade so this is a value
so the see if you want if you need Fahrenheit just make it F if you need heat index and siliceous put
heat index HIC and HOF for Fahrenheit and then if you just need humidity just put H and as you
can see here they are running so the get temp is a method that I've written and it's at the
bottom here get temp all the values are here and here I've just put a condition from here
to here and also added the Calvin calculation when temperature is T plus this value 273 0.15
and then if the person has selected so this is a variable that you are sending so that variable
inside the quotation that you are sending see letters or work so it will return the value F
at C it will give you the T the temperature if it's F it will return the Fahrenheit the
Fahrenheit and all of this has been calculated here from original code so then heating index
and Fahrenheit hif and hidden index in C it will return it and if nothing is found it will return
zero zero zero so so this is the code that that is printing this here on the screen in
Kelvin here 297 Kelvin I got a heat gun and let me just increase the temperature and see how at
this place so pay attention for this Celsius as you're interested or Fahrenheit the other one
so already reached 70 degrees Celsius 68 degrees and thus 162
so it's now dropping back 16 percent and if you just blow your breath and
you will see immediately that that 16 or 14 percent will change so let me do that
as you can see it increase 21:22 so to show you how it's sample it is you see you just
need to use just get temp and inside the double quotation put see it will give you
the Celsius and Method below will do that calculation so here it will be very clean
if you just wanna need Fahrenheit just make it F and it will print the Fahrenheit for you so
so now now that's in Fahrenheit if you need it in Kelvin the same way just do
in Kelvin put K and if you need heating with index just make it heat index and Fahrenheit
and heat index and Celsius or if you need just humidity just type age oh now this
type plants humidity but if you want to type and another value or text after that just type here
let's say you wanna put percent or any other text but just remove this Ln so it will be
just in one line this Ln mean new line so this will not create new line so this percent will
be beside that humidity what I space so and let's say for example if the temperature in
the room goes above some value or below some value and you want to do something so you do
F and then use this one get temp Celsius if you want to look at the Celsius and you want to say
if destined pressure of Celsius is bigger than twenty six point five degrees and then here this
is the beginning this is the end so you want to put something here some pen should be high
let's say you wanna turn on the air conditioner if the temperature goes above twenty six degrees
or if it is below some value you want to turn on the heater so you can do all those here so
this was how we you use this dht22 module I will have another video that shows the temperature in
a display so in case in your application you need to use it without computer so make sure
to subscribe and also some of the video share that I appreciate it thank you for watching
you