0:07
hello everyone welcome to controllers
0:11
tech today in this video I am going to
0:15
show you how to work with the
0:16
temperature sensors that only uses one
0:19
wire to communicate in this video I will
0:23
cover two HD 11-day HD 22 and ds1 8b
0:27
$0.20 errs the first part of the video
0:30
will be common to all three sensors as I
0:33
am going to use the same pin for all
0:35
three of them let's start by creating a
0:39
project in stm32 cube ide i am using
0:44
stm32f4 for 6r ii controller give the
0:51
name to the project and click finish
0:58
in the cube MX first of all I am setting
1:01
external crystal for the clock I am
1:10
using timer six for creating delay in
1:12
microseconds pin pa1 will be the data
1:17
pin for the sensors and i2c is for
1:24
connecting the LCD 1602 let's set the
1:35
clock now I have eight megahertz
1:40
I want the controller to run at fifty
1:55
8pv one clock is also running at 50
1:58
megahertz and that's where my timer 6 is
2:02
I chose low frequency for the HCL K
2:06
because some of you complained that the
2:08
microsecond delay is not working at
2:10
lower frequencies I am setting timer to
2:16
create delay in microseconds if you
2:19
don't understand it check out the video
2:21
of the top right corner click Save to
2:31
I am including i2c LCD related files in
3:00
if you are not using stm32f4 you need to
3:05
change it accordingly if you are using
3:14
any other eye to see change it here I
3:23
will build this code once to check for
3:38
this is 4s print f function we need to
3:50
write some functions first delay is to
3:54
create delay in microseconds basically
3:58
set the counter to zero and then wait
4:01
for the counter to reach the entered
4:02
value these are some variables that we
4:13
are going to use throughout these are
4:16
functions to set the pin as input or
4:18
output based on the requirement you can
4:27
change to pull up or no pull if you are
4:30
not getting the data from the pin
4:46
now the DHD 11 specific cart starts
4:50
let's take a look at the datasheet
4:53
this is the overall communication of the
5:02
it starts with sending a start signal to
5:06
the day HD 11 MCU sends the start signal
5:10
by pulling the line low for 18
5:12
milliseconds and then high for 20 to 40
5:27
I am defining the pin and the port for
5:31
the dht11 sensor this is the function to
5:39
send the start signal first the MCU
5:46
we'll pull the pin low and then keep it
5:49
low for 18 milliseconds then MCU will
5:53
pull the pin high and keep it high for
5:55
20 to 40 micro seconds after that MCU
6:03
will release the pin after receiving the
6:09
start signal dht11 will send a response
6:12
to the MCU to do so it will pull the
6:18
line low for 80 micro seconds and then
6:21
high for 80 micro seconds this is the
6:29
function to check response we first wait
6:33
for 40 microseconds that means we are
6:39
here on the timeline then we check for
6:42
the pin to be low which is actually low
6:45
so we will wait for eighty microseconds
6:52
basically we reach here on the timeline
6:55
now we will check if the pin is high if
6:59
it is that means the sensor is present
7:02
or else the sensor is not present
7:12
finally we will wait for the pin to go
7:24
now after transmitting the response the
7:27
dht11 will start transmitting data
7:30
before transmitting any bit
7:33
it keeps the line low for 50
7:35
microseconds which is followed by a high
7:38
for 26 to 28 microseconds if the bit is
7:42
a zero or a high for 70 microseconds if
7:54
this is the function to read the data
7:56
from dht11 first we will wait for the
8:00
pin to go high which means we will be
8:07
here on the time line
8:15
now we will wait for 40 microseconds if
8:25
a zero was received we will be here on
8:27
the timeline and the pin would be low at
8:30
this point or if a one was received we
8:34
will be here and the pin would be high
8:37
based on the result we store the data at
8:40
the respective position let's start by
8:48
starting the timer first initialize the
8:52
LCD i am sending this string to the LCD
9:00
wait for some time clear the display
9:10
now start the day HD 11 record the
9:14
response from the sensor sensor will
9:24
send 40 bits of data which includes
9:26
integral RH data decimal RH data
9:30
integral temperature data decimal
9:32
temperature data and the checksum so we
9:36
will read 5 bytes of this data
9:43
temperature data is from the integral
9:46
part and Rh data is also from the
9:59
also these are the functions to display
10:02
the temperature and humidity data on the
10:16
I am writing display functions in the
10:19
beginning so that they can be common to
10:21
all other sensors also
10:57
and in the end give some delay
11:10
there is the floating point error
11:12
because I am trying to convert the float
11:14
into character go to project setting C++
11:22
build setting tool setting and select
11:41
the project builds successfully let's
11:56
let's see the result on the LCD itself
11:59
you can see the data being displayed on
12:23
notice the increase in temperature when
12:26
I am touching the sensor this is it for
12:31
the day HT 11 the next part of this
12:34
video will cover the day HD 22 when than
12:37
dsb 1 8 b 20 you can download the code
12:41
from the link in the description
12:54
first we need to define the pin for the
13:07
eh t20 to start sends the start signal
13:11
to the sensor let's take a look at the
13:14
dht22 datasheet to transmit a start
13:18
signal the MCU needs to pull the data
13:21
line low for at least one milliseconds
13:23
and then it needs to pull the line high
13:26
for around twenty to forty microseconds
13:29
the pin must be released after that on
13:45
receiving start signal sensor we'll send
13:48
the response it will pull the line low
13:55
for eighty microseconds and then high
13:57
for another eighty microseconds now we
14:12
will first wait for 40 microseconds
14:15
which means that we are here on the time
14:17
line and the pin should read low here
14:28
if it does we will wait for another 80
14:31
microseconds that brings us here on the
14:35
timeline the pin should read high here
14:42
and that indicate the sensors presence
14:51
after that we will wait for the pin to
15:03
to transmit data dht22 will pull the
15:08
line low for 50 microseconds followed by
15:10
pulling it high for 26 to 28
15:12
microseconds in case the bit is zero to
15:21
transmit a one day HT 22 will pull the
15:24
line low for 50 microseconds followed by
15:27
pulling it high for 70 microseconds to
15:42
read the data we will wait for the pin
15:44
to go high this brings us here on the
16:02
we will wait for 40 microseconds then if
16:12
the bit was a zero we would be here
16:14
after 40 microseconds and the pin would
16:18
read low if the bit was a 1 we would be
16:22
here and the pin would be high we will
16:25
record the bit at the respective
16:27
position and wait for the pin to go low
16:29
before reading next bit
16:54
inside the main function first start the
16:57
timer initialize the LCD i am sending
17:02
this string to the display wait for some
17:06
time and clear the display here are the
17:09
functions to display the temperature and
17:11
humidity on the LCD first of all send
17:26
the start signal to the sensor read the
17:29
response from the sensor sensor will
17:32
send 40 bits of data which includes
17:35
integral RH data decimal RH data
17:38
integral temperature data decimal
17:41
temperature data and the checksum so we
17:45
will read 5 bytes of this data
17:58
now we will combine the integral and
18:01
decimal part of the data finally convert
18:05
the values to the proper float
18:25
let's build and debug
18:42
as you can see here the values of
18:45
temperature and humidity are being
18:48
displayed this is it for the day HD 22
18:52
sensor the next part of this video is
18:55
about ds1 8b 20 the code from the link
19:00
in the description here is the function
19:04
to create delay in microseconds these
19:08
functions are used to display
19:10
temperature and humidity values on the
19:12
LCD functions to set the pin as output
19:35
first of all I am defining the data pin
19:39
for the ds1 8b 28 is connected to pin
19:43
pa1 start function is to send the start
19:53
signal to the sensor it start by setting
20:03
the pin in this output pull the pin low
20:06
and hold it low for 480 microseconds as
20:10
you can see in the datasheet the master
20:14
have to pull the data line low for 480
20:17
microseconds after that we will release
20:21
the pin by setting it as input
20:31
on receiving the start signal sensor
20:35
will send the response to indicate its
20:37
presence so we will wait for eighty
20:40
microseconds we should be here on the
20:49
time line now we will read the pin if
20:53
the pin is low the sensor is present as
20:56
it's the one which pulled the line low
20:59
now we will wait another 400 seconds to
21:02
complete the response cycle
21:14
this will complete the initialization of
21:33
next is the right function to write data
21:35
to the sensor to write a one we must
21:39
first set the pin as output then pull
21:43
the pins low and hold it low for 1
21:46
microseconds you can see that here after
21:58
1 microseconds we must release the pin
22:00
by setting it as input and wait for
22:04
around 60 microseconds for this cycle to
22:26
to write a zero we have to again set the
22:29
pin in his output then pull the pins low
22:32
and hold it low for around 60 micro
22:35
seconds and finally release the pin by
22:37
setting it as input re function reads
22:48
the data from the sensor to read the
22:51
data we must set the pin as output first
22:55
pull the pin low and hold it low for 2
22:58
microseconds now we will set the pin as
23:07
input and read the data pin the
23:10
microcontroller is going to sample the
23:13
data here and if the pin would be high
23:16
which means that we are here on the time
23:18
line and the data read would be a 1 if
23:21
the pin would be low the data read must
23:24
be a zero and at last we have to wait
23:28
for 60 microseconds for the cycle to get
23:49
inside the main function first start the
23:52
timer initialize the LCD display some
23:56
string on the LCD wait for some time and
23:58
clear the display now inside the while
24:02
loop let's take an example from the
24:22
first send the start signal and look for
24:25
the presence of the sensor next write
24:35
the skip ROM command and write scratch
24:38
pad with convert temperature command
24:56
again send the start read the response
24:59
and skip ROM command and read scratch
25:17
read the nine bits of data and here we
25:25
finally calculate the temperature
25:34
let's build this and this time I am
25:37
directly running it you can see the
25:43
temperature being displayed on the LCD
25:52
when I hold the sensor there is a rise
26:16
if you are having issues with the
26:18
temperature values for example if the
26:23
you can change this delay here don't
26:27
decrease it too much I am reducing it to
26:30
50 microseconds as the time for this
26:33
cycle is 60 microseconds so try to keep
26:37
the delay close to this value
26:49
you can also change this pull-up - no
26:52
Paul this is it guys I hope you
26:56
understood the video you can download
26:59
the code from the link in the
27:01
description keep watching