0:10
hello and welcome to controllers tech
0:13
today in this video we will see how to
0:16
interface bme280 sensor with stm32
0:20
this sensor can measure the temperature
0:23
pressure and relative humidity
0:25
i have written a library for it which i
0:28
will upload on the github and you can
0:32
as we progress along the video i will
0:34
also explain the code and how you can
0:36
write one yourself using the data sheet
0:40
the library covers a lot of things but
0:42
there are still few things which you
0:44
need to manually implement
0:46
so watch the video carefully as you
0:48
might need to make changes in the
0:50
library based on what requirements you
0:56
this is the data sheet for the device
0:59
here i have highlighted few important
1:02
things that i will cover in today's
1:05
i will leave the link to this data sheet
1:11
let's start with cube id and create a
1:22
give some name to the project and click
1:31
first of all i am enabling the external
1:34
crystal for the clock
1:37
the blue pill have eight megahertz
1:38
crystal on board and i want the system
1:41
to run at maximum 72 megahertz clock
1:46
enable the serial wire debug
1:49
the sensor can use both the i2c and sbi
1:54
you can use either of those but i am
1:57
going to go with the i2c
1:59
enable the i2c interface and leave
2:02
everything to default
2:04
we have the two pins for data and clock
2:08
before going any further in the video
2:10
let's see the sensor and the connection
2:16
bme280 and as you can see it have the
2:18
pin out for both sbi and i2c
2:23
here i am connecting it with the blue
2:26
it's powered with 3.3 volts and there
2:29
are two pull-up resistors each
2:32
4700 ohms connected between the clock
2:35
and data pins and the 3.3 volts
2:39
pull up resistors must be used while
2:41
using the i2c communication
2:44
also one very important thing i have
2:47
grounded the sdo pin keep this in mind
2:51
as it will be used in the addressing of
2:55
now connect the pv6 to the clock pin and
2:58
pb7 to the sdi pin that is data pin
3:02
that completes the connection
3:05
let's generate the project now
3:10
first thing we will do is copy the
3:12
library files into our project
3:17
so copy the c file into the source
3:19
directory and header file into the
3:26
let's take a look at the source file
3:33
here first we have to define the i-2-c
3:38
as i set up the i-2-c-1 so i am leaving
3:43
the next thing is the 64-bit support
3:47
if your configuration supports 64-bit
3:50
integers then leave this as 1 or else to
3:53
use the 32-bit integers uncomment the
3:56
32-bit support and comment out 64-bit
4:00
the next is the address of the device
4:06
as mentioned in the data sheet the 7
4:08
bits of the address are these
4:11
here x depends on the sdo pin and if you
4:14
remember i grounded that pin and
4:16
therefore the x is zero in my case
4:19
the slave address will consist of these
4:21
seven address bits along with the read
4:27
so the address will be 1 1 1 0 1 1 0 0.
4:32
which makes up zero cross e c
4:44
these variables will store the
4:46
corresponding values and they are
4:48
externally defined here
4:50
so you should define them in the main
4:54
the rest of the code should be unchanged
4:56
for default configuration
5:00
let me explain how this works
5:05
the sensor can work with three different
5:08
in sleep mode no measurements are
5:12
but the registers are accessible and
5:14
therefore you can wake the sensor and
5:16
perform the measurement
5:18
then comes the forced mode
5:21
here the sensor performs a single
5:23
measurement and goes into the sleep mode
5:27
for the next measurement you need to
5:29
wake the sensor again
5:31
i have added a function for this
5:36
you need to call this wake up function
5:39
before doing the measurement
5:41
this is useful in situations like
5:43
weather monitoring where the data does
5:45
not need to be read very frequently
5:48
basically it will measure all 3
5:51
parameters and then go back to sleep
5:55
the next is the normal mode
5:58
here the sensor does the measurement and
6:00
goes into the standby
6:02
the data rate depends on the measurement
6:05
time and standby time
6:07
the current consumption will obviously
6:09
be higher but it allows you to
6:11
continuously monitor the data
6:14
in this tutorial i will be using the
6:19
there are few important things to note
6:21
about the measurements
6:23
the humidity measurement have a fixed
6:25
resolution of 16 bits
6:28
the resolution for the temperature and
6:30
pressure depends on the fact that if you
6:32
are using the iir filter or not
6:35
if using the filter then the resolution
6:40
otherwise it depends on the over
6:43
sampling setting as shown here
6:45
i-ir filter can be used to avoid the
6:48
fluctuations in the pressure and
6:50
temperature measurements
6:52
i will be using the iir filter in this
6:55
tutorial and this library does not
6:57
supports the measurements without filter
7:04
then we have some examples for the
7:06
settings which we will see later
7:15
let's check the source file again
7:21
here the first function is the trim read
7:24
this reads the trimming values that are
7:27
stored in the non-volatile memory of the
7:31
every sensor comes pre-programmed with
7:33
these values and they don't change with
7:37
we need to read these values and then
7:39
use them in the calculations ahead
7:42
as shown in the data sheet we must read
7:45
the values from these registers and i am
7:48
going to name them same as its named
7:51
also note that some of these are
7:53
unsigned and others are signed values
7:57
so that's why i have defined them
8:00
then we start reading from zero by 88
8:03
address and we burst read 25 registers
8:07
this means we read up to zero x-a-1
8:17
again we have to read from e-1 to e-7
8:21
this is done here we are reading 7 bytes
8:23
from e 1. and finally we will arrange
8:27
the data just how it's arranged in the
8:47
then comes the configuration which we
8:51
next is the reading of raw data
8:57
it's mentioned in the data sheet that we
8:59
must first read the data in order to
9:01
avoid the possible mix ups between the
9:04
we will read the registers f 7 to f e
9:12
this is done here we are reading from
9:16
it is defined in the header file and its
9:21
and we will read 8 bytes from here which
9:24
will include the registers up to f e
9:27
as i mentioned in the beginning this
9:29
library is using the filter and this is
9:31
why the pressure and temperature are 20
9:36
we will calculate the raw values for all
9:38
three parameters using the registers we
9:46
after this we have the compensation
9:48
formulas on page 25 of the data sheet
9:52
these formulas uses the raw values for
9:54
temperature pressure and humidity and
9:57
gives us the refined results
9:59
we need to use them exactly in the same
10:08
so this is what that is
10:10
i just copied them from the data sheet
10:16
note that the pressure uses the 64-bit
10:21
but in case your machine doesn't support
10:23
it there is a 32-bit alternative also
10:27
i have included that in the library so
10:29
all you need to do is define the support
10:32
as i mentioned in the beginning
10:34
all right now we will take a look at the
10:38
the first register is the id register
10:41
it's a read-only register and it returns
10:44
the id of the device which should be
10:48
i have included it in the code
10:53
before reading the raw values the code
10:58
if the id is zero by 60 only then it
11:01
goes for the measurement
11:06
the next register is reset
11:09
if we write zero x b6 to this register
11:12
the device will soft reset
11:15
the next register is for the humidity
11:18
here we need to select the over sampling
11:23
if you want to skip the humidity
11:24
calculation just set the over sampling
11:31
this configuration is controlled by the
11:33
b m e 2 80 config function
11:38
here the parameters are the over
11:39
sampling settings for all three
11:41
parameters then we have the mode the
11:44
standby time and finally the filter
11:50
first of all we will read the trimming
11:52
parameters as it only needs to be done
11:57
then i am performing the soft reset
12:05
then write the over sampling data for
12:14
these over sampling parameters are
12:16
defined in the header file and you can
12:19
use them instead of writing a
12:27
after writing the data we will read the
12:29
same register to make sure the changes
12:31
were done in the register
12:39
the next register is control measure
12:43
it controls the over sampling of
12:45
temperature and pressure along with the
12:52
here i am shifting the temperature data
12:54
by 5 the pressure data by 2 and the
12:57
first 2 bits are for the mode
13:10
the next register is the config register
13:13
it controls the standby time for the
13:16
normal mode along with the filter
13:18
coefficients for the iir filter
13:21
you can also use 3y sbi and enable it
13:26
below the tables for both standby time
13:29
and filter coefficients
13:31
i have defined them in the header file
13:42
and at last we have the data registers
13:45
where we read the data from the pressure
13:47
temperature and humidity
13:50
as mentioned here this is how the 20-bit
13:52
data is arranged in these registers
13:56
this is enough explaining i hope let's
14:03
i have defined these configurations as
14:07
i am using i2c1 and also my system
14:11
supports 64-bit variables
14:19
in the main file we will include the
14:24
now define the variables to store the
14:28
inside the main function we will call
14:30
the b m e 2 80 config function
14:34
there are some examples provided in the
14:38
for example for weather monitoring we
14:40
can use the forced mode with 1 sample
14:44
the pressure temperature and humidity
14:47
over sampling all should be set to 1.
14:50
similarly there are other examples but
14:53
in this tutorial i will use the indoor
14:57
here i will use the normal mode with
15:00
standby time of 0.5 milliseconds
15:04
the pressure over sampling is 16 and
15:06
that for temperature is 2 and 1 for
15:10
so let's set the temperature over
15:12
sampling to 2 pressure to 16 and
15:23
the mode should be set to normal mode
15:26
the standby time is set to 0.5
15:30
and at last the filter coefficient is
15:34
with this configuration the output data
15:39
inside the while loop we will call the b
15:41
m e 2 80 measure function
15:44
this will handle all the measurement and
15:47
store the values in the variables that
15:55
all right everything is done now build
15:57
the code and debug it
16:04
you can see the values of temperature in
16:06
degree celsius pressure in pascals and
16:09
relative humidity as a percentage
16:12
i am going to put my finger on the
16:15
see the value of the temperature rising
16:36
i kept the sensor in the refrigerator
16:38
for a while and see the temperature and
16:43
the temperature is rising and humidity
16:47
they both are slowly tending towards the
16:56
this is it for this video
16:58
i hope you understood how i wrote each
17:00
function and also how to use them i will
17:04
post this library on the github so that
17:07
i can modify it in the near future and
17:09
also include some other parameters like
17:11
altitude measurement
17:14
the link to github is in the description
17:17
this is all for today
17:19
leave comments in case of any doubt
17:22
keep watching and have a nice day ahead