0:09
hello and welcome to controllers tech
0:12
today we will see how to interface the
0:14
AHT20 temperature and humidity sensor
0:20
aht20 is small in size and is relatively
0:23
easy to interface compared to other
0:25
temperature sensors we have interfaced
0:27
before temperature resolution is 0.01°
0:32
C whereas the humidity resolution is
0:38
the sensor works with I2C hence the
0:40
connection is very straightforward we
0:43
will write the entire code from scratch
0:45
using the data sheet of the sensor let's
0:48
start the Q ID and create a new project
0:53
i am using SDM32F103C8
0:57
give some name to the project and click
1:00
finish let's start with the clock
1:03
configuration first i am selecting the
1:06
external high-speed crystal for the
1:08
clock the board has 8 MHz crystal on it
1:12
and we will run the system at maximum 72
1:16
go to CIS debug and enable serial wire
1:21
now enable the I2C in the connectivity
1:23
section i am leaving the configuration
1:26
to default and you can see the pins PB6
1:29
and PB7 are configured as the clock and
1:31
data pins that is all we need to
1:34
configure in the CubeMX click save to
1:37
generate the project let's take a look
1:40
at the connection diagram the sensor is
1:43
powered with 3.3 volts from the MCU
1:46
itself the clock pin from the sensor is
1:50
connected to the pin PB6 and the data
1:52
pin is connected to PB7
1:55
that is all for the connection let's
1:57
write the code now as mentioned in the
2:00
data sheet the 7 bit I2C device address
2:03
is 38 hex this 7 bit address does not
2:07
include the read or write bit we are
2:10
going to use the HALI 2C functions and
2:13
these functions require an 8- bit slave
2:15
address including the read or write bit
2:19
let's define the AHT20 device address
2:22
first here we will shift the 7 bit
2:25
address 38 hex by one position to the
2:28
left this will convert the address to 8
2:31
bit format with the read or write bit
2:34
set to zero now let's see the sensor
2:39
first of all we need to wait for 40
2:43
then we need to check the calibration
2:45
enable bit which is bit 3 in the status
2:48
register we can read the status register
2:51
by reading the register 71 hex here you
2:55
can see the calibration bit in the
2:57
status register if this bit is not set
3:01
to one we need to send the
3:02
initialization command we will do this
3:06
taking one step at a time let's create a
3:10
function to initialize the AHT20
3:13
inside this function define a variable
3:16
to store the data from the status
3:18
register now we will read the register
3:21
71 hex and store the one byte received
3:24
data in the status variable i forgot to
3:28
set the 40 milliseconds weight time in
3:30
the beginning the data from the status
3:33
register is now stored in the status
3:35
variable we will check bit three of this
3:38
status data if this bit is zero which
3:42
means that the calibration is not done
3:44
and hence we need to send the
3:46
initialization command the calibration
3:51
by the two parameters 08 hex and 0 hex
3:56
let's define an array of three bytes to
3:59
store the initialization commands here
4:03
we will store the main command followed
4:05
by the two parameters
4:07
now call the function I2C master
4:10
transmit to transmit these three bytes
4:12
to the slave device after sending the
4:15
initialization sequence wait for 10
4:19
this completes the device initialization
4:22
now we will measure the data the command
4:25
AC hex triggers the measurement this
4:28
command also has two parameters 33 hex
4:32
and 0 hex after triggering the
4:35
measurement we need to wait for 80
4:37
milliseconds for the measurement to
4:38
complete let's create another function
4:41
to measure the data here we will create
4:44
an array to store the trigger commands
4:48
transmit these commands to the slave
4:50
device and wait for 80 milliseconds for
4:52
the measurement to complete we have
4:55
covered this much part let's proceed
4:57
with the next section next we need to
5:00
again read the status register the bit
5:03
seven of this register is set to one if
5:05
the device is busy in measurement
5:07
otherwise this bit is reset to zero then
5:11
we can read the data bytes we need to
5:14
read seven bytes in total that includes
5:17
one state bite two bytes of humidity
5:19
data one bite of combined humidity and
5:22
temperature data two bytes of
5:24
temperature data and one bite of CC data
5:27
let's write the code for this part we
5:31
need to continuously read the status
5:33
register until the busy bit resets to
5:37
here I have set a 100 milliseconds delay
5:40
between successive reading of the status
5:44
once the seven bit of the status
5:46
register the busy indication bit resets
5:49
to zero this dowh loop will exit then we
5:53
will read seven data bytes from the
5:55
device and store them in the RX data
6:00
note that there are two different read
6:02
functions being used here the I2C mem
6:06
read function is used to read the data
6:08
starting from a fixed register inside
6:10
the slave memory here we are reading
6:13
just one bite from the address 71 hex
6:19
whereas the I2C master receive simply
6:21
requests the required number of data
6:23
bytes from the slave device
6:26
it does not specify the register inside
6:29
the memory as per the data sheet we do
6:32
not need to read the data from a
6:36
we can simply request it and the device
6:39
will automatically send the temperature
6:43
now that we have received the data bytes
6:46
we will convert them to readable
6:48
temperature and humidity values
6:51
the first received bite is the status
6:53
register we will leave it the humidity
6:56
data is stored in the second bite third
6:59
bite and half of the fourth bite the
7:02
higher four bits of this combined data
7:04
bytes store the humidity information
7:07
while the lower four bits store the
7:10
temperature information therefore we
7:13
have a total of 20 bytes for each
7:15
humidity and temperature first we will
7:18
combine these 20 bytes let's define a
7:22
32bit variable to store the 20 bytes
7:24
humidity data here I am combining the
7:28
second third and fourth bytes to make a
7:31
24-bit humidity data these 24 bits also
7:35
consist of four bits of temperature data
7:38
so we will shift the humidity data by
7:41
four places to the right to remove the
7:43
lower four bits of the combined data now
7:46
we have 20 bits of the humidity data
7:49
here is the formula to convert the data
7:52
into the relative humidity
7:55
let's define the float variables to
7:57
store the temperature and humidity
8:00
include the math header file so that we
8:03
can use the power function now write the
8:06
formula as per the data sheet to convert
8:08
the humidity data to the relative
8:10
humidity value the calculated value will
8:14
be stored in the humidity variable next
8:17
we will combine the fourth fifth and
8:20
sixth bytes of the RX data array to make
8:22
a 24-bit temperature data the 24-bit
8:26
data consist of four humidity bits too
8:29
but they are stored at the most
8:30
significant positions in the temperature
8:32
data we need to extract the first 20
8:36
bits of the temperature data so we will
8:38
perform an end operation to do so here
8:41
is another formula to calculate the
8:44
temperature in degrees C
8:47
let's use this formula to calculate the
8:49
temperature the calculated temperature
8:52
will be stored in the temperature
8:54
variable that is it now initialize the
8:58
AHT20 sensor inside the main function
9:02
and inside the while loop we will
9:04
measure the data every 2 seconds
9:07
let's build the project and debug it add
9:11
the variables in the live expression
9:14
run the debugger now the humidity data
9:17
seems accurate but the temperature is
9:20
definitely wrong actually the
9:22
temperature formula is still incorrect
9:26
it should have been 200 here as
9:28
mentioned in the data sheet all right
9:31
let's build and debug the project again
9:34
the temperature and humidity values seem
9:37
accurate now I am holding the sensor
9:40
with my finger and you can see the rise
9:42
in temperature and humidity as well so
9:46
everything works as expected the sensor
9:49
is responding fine and the temperature
9:52
and humidity values are also accurate we
9:55
wrote a very small code compared to
9:58
other similar sensors and still got an
10:00
accurate output from the sensor this is
10:03
it for the video i hope everything was
10:06
explained clearly you can download the
10:09
project from the link in the description
10:12
leave comments in case of any doubt keep
10:15
watching and have a nice day ahead