Skip to playerSkip to main content
In this tutorial from ControllersTech, you'll learn how to connect the BME280 sensor to an STM32 microcontroller using the I²C interface. We walk you through wiring, sensor configuration, raw data reading, and compensation calculations to deliver accurate readings for temperature (°C), pressure (hPa/Pa), and humidity (%RH). Ideal for building DIY weather stations and IoT environmental monitoring systems.

► Download resources & code : [https://controllerstech.com/bme280-with-stm32/]
► Download BME280 Datasheet : [https://controllerstech.com/wp-content/uploads/2022/01/bst-bme280-ds002.pdf]

📺 Related Videos:
👉STM32 Weather Station using BME280 : https://youtu.be/aj4JGlvtznk

STM32 Playlist :::: https://www.youtube.com/playlist?list=PLfIJKC1ud8gga7xeUUJ-bRUbeChfTOOBd

#BME280 #STM32 #I2C #EnvironmentalSensor #IoT #WeatherStation #Microcontroller
________________________________________________________________________________________________

Facebook Page : https://www.facebook.com/controllerstech

Telegram Group : https://t.me/controllerstechdiscuss

Instagram : https://www.instagram.com/controllerstech/

For more info, visit https://controllerstech.com

Category

🤖
Tech
Transcript
00:00Hello, and welcome to Controllers Tech. Today in this video, we will see how to interface BME
00:17280 sensor with STM32. This sensor can measure the temperature, pressure, and relative humidity.
00:25I have written a library for it, which I will upload on the GitHub, and you can get it from
00:31there. As we progress along the video, I will also explain the code, and how you can write
00:37one yourself using the datasheet. The library covers a lot of things, but there are still
00:43few things which you need to manually implement. So watch the video carefully, as you might
00:49need to make changes in the library, based on what requirements you have from the sensor.
00:55This is the datasheet for the device. Here I have highlighted few important things, that
01:03I will cover in today's video. I will leave the link to this datasheet in the description.
01:10Let's start with cube ID, and create a new project. I am using STM32F103 controller. Give
01:22some name to the project, and click finish. First of all I am enabling the external crystal
01:34for the clock. The blue pill have 8 MHz crystal on board, and I want the system to run at maximum
01:4272 MHz clock. Enable the serial wire debug. The sensor can use both the I2C, and SPI for communication.
01:54You can use either of those, but I am going to go with the I2C. Enable the I2C interface, and
02:02leave everything to default. We have the two pins, for data and clock. Before going any further
02:09in the video, let's see the sensor and the connection with blue pill. Here is the BME280, and as you
02:17can see it have the pin out for both SPI and I2C. Here I am connecting it with the blue pill.
02:27It's powered with 3.3 volts, and there are two pull up resistors, each 4700 ohms, connected
02:34between the clock and data pins and the 3.3 volts. Pull up resistors must be used while using
02:41the I2C communication. Also one very important thing, I have grounded the SDO pin. Keep this
02:50in mind, as it will be used in the addressing of the device. Now connect the PB6 to the clock
02:57pin, and PB7 to the SDI pin, that is data pin. That completes the connection. Let's generate
03:06the project now. First thing we will do is, copy the library files into our project. So copy
03:17the C file into the source directory, and header file into the include directory. Let's take
03:27a look at the source file. Here first we have to define the I2C we are using. As I set up
03:39the I2C one, so I am leaving it unchanged. The next thing is the 64 bit support. If your configuration
03:48supports 64 bit integers, then leave this as 1, or else to use the 32 bit integers, uncomment
03:56the 32 bit support and comment out 64 bit. The next is the address of the device.
04:06As mentioned in the datasheet, the 7 bits of the address are these. Here X depends on the SDO pin,
04:14and if you remember I grounded that pin, and therefore the X is 0 in my case. The slave address
04:21will consist of these 7 address bits, along with the read or write bit. So the address will be 1 1 1 0,
04:301 1 0 0, which makes up 0 cross EC.
04:34These variables will store the corresponding values, and they are externally defined here. So you should
04:51define them in the main file. The rest of the code should be unchanged for default configuration.
04:58Let me explain how this works. The sensor can work with 3 different modes. In sleep mode no measurements
05:10are performed. But the registers are accessible, and therefore you can wake the sensor and perform
05:17the measurement. Then comes the forced mode. Here the sensor performs a single measurement and goes into
05:25the sleep mode. For the next measurement, you need to wake the sensor again. I have added a function for this.
05:36You need to call this wake up function before doing the measurement. This is useful in situations like
05:43weather monitoring, where the data does not need to be read very frequently. Basically, it will measure
05:50all 3 parameters, and then go back to sleep mode. The next is the normal mode. Here the sensor does the
05:59measurement and goes into the standby. The data rate depends on the measurement time and standby time.
06:07The current consumption will obviously be higher, but it allows you to continuously monitor the data.
06:13In this tutorial, I will be using the normal mode. There are few important things to note about the
06:22measurements. The humidity measurement have a fixed resolution of 16 bits. The resolution for the temperature
06:30and pressure depends on the fact that if you are using the IIR filter or not. If using the filter, then the
06:37resolution will be 20 bits. Otherwise, it depends on the oversampling setting as shown here.
06:46IIR filter can be used to avoid the fluctuations in the pressure and temperature measurements.
06:52I will be using the IIR filter in this tutorial, and this library does not supports the measurements
06:58without filter, at least for now. Then we have some examples for the settings, which we will see later.
07:15Let's check the source file again.
07:21Here the first function is the trim read. This reads the trimming values, that are stored in the
07:28non-volatile memory of the sensor. Every sensor comes pre-programmed with these values, and they don't
07:34change with reset or anything. We need to read these values and then use them in the calculations ahead.
07:42As shown in the datasheet, we must read the values from these registers, and I am going to name them
07:49same as it's named here. Also note that some of these are unsigned, and others are signed values.
07:55So that's why I have defined them separately. Then we start reading from 0 by 88 address,
08:04and we burst read, 25 registers. This means we read up to 0 x A1, which is dig H1.
08:17Again we have to read from E1 to E7. This is done here, we are reading 7 bytes from E1.
08:25And finally we will arrange the data, just how it's arranged in the datasheet.
08:30Then comes the configuration, which we will see later. Next is the reading of raw data.
08:53It's mentioned in the datasheet that we must burst read the data in order to avoid the possible mix-ups
09:03between the measurements. We will read the registers F7 to FE.
09:08This is done here, we are reading from this register. It is defined in the header file,
09:19and its address is F7. And we will read 8 bytes from here, which will include the registers up to FE.
09:27As I mentioned in the beginning, this library is using the filter, and this is why the pressure and
09:33temperature are 20 bits in resolution. We will calculate the raw values for all three parameters
09:39using the registers we just read.
09:46After this we have the compensation formulas on page 25 of the datasheet.
09:52These formulas uses the raw values for temperature, pressure and humidity,
09:57and gives us the refined results. We need to use them exactly in the same way.
10:08So this is what that is. I just copied them from the datasheet, and put them here.
10:14Note that the pressure uses the 64-bit integer here. But in case your machine doesn't support it,
10:23there is a 32-bit alternative also. I have included that in the library, so all you need to do is,
10:31define the support as I mentioned in the beginning. Alright now we will take a look at the registers.
10:38The first register is the ID register. It's a read-only register, and it returns the ID of the device,
10:46which should be 0 by 60. I have included it in the code.
10:53Before reading the raw values, the code checks for the ID. If the ID is 0 by 60,
11:01only then it goes for the measurement. The next register is reset. If we write 0xb6 to this register,
11:12the device will soft reset. The next register is for the humidity control. Here we need to select
11:20the oversampling for the humidity. If you want to skip the humidity calculation, just set the oversampling to
11:270. This configuration is controlled by the BME280 config function. Here the parameters are the
11:39oversampling settings for all three parameters, then we have the mode, the standby time, and finally the
11:46filter configuration. First of all we will read the trimming parameters, as it only needs to be done
11:55once. Then I am performing the soft reset. Then write the oversampling data for the humidity.
12:09These oversampling parameters are defined in the header file, and you can use them instead of
12:20writing a hexadecimal value. After writing the data, we will read the same register to make sure the
12:31changes were done in the register. The next register is control measure register. It controls the oversampling
12:45of temperature and pressure, along with the mode of the sensor. Here I am shifting the temperature data by
12:545, the pressure data by 2, and the first 2 bits are for the mode.
13:10The next register is the config register. It controls the standby time for the normal mode, along with the
13:18filter coefficients for the IIR filter. You can also use 3-wire SBI, and enable it from here. Below are the tables for
13:28both standby time, and filter coefficients. I have defined them in the header file also.
13:34And at last we have the data registers, where we read the data from the pressure, temperature and humidity.
13:50As mentioned here, this is how the 20-bit data is arranged in these registers.
13:57This is enough explaining I hope, let's write the code now.
14:00I have defined these configurations as per my setup. I am using I2C1, and also my system supports 64-bit variables.
14:17Let's copy this. In the main file, we will include the BME280 header file. Now define the variables to
14:26store the data. Inside the main function, we will call the BME280 config function. There are some examples
14:35provided in the datasheet. For example, for weather monitoring, we can use the forced mode, with one
14:43sample per minute. The pressure, temperature, and humidity over sampling all should be set to one.
14:50Similarly there are other examples, but in this tutorial, I will use the indoor navigation.
14:57Here I will use the normal mode, with standby time of 0.5 milliseconds. The pressure over sampling is 16,
15:06and that for temperature is 2, and 1 for humidity. So let's set the temperature over sampling to 2,
15:14pressure to 16, and humidity to 1.
15:23The mode should be set to normal mode. The standby time is set to 0.5. And at last, the filter coefficient
15:32is 16. With this configuration, the output data rate is 25 hertz. Inside the while loop,
15:40we will call the BME280 measure function. This will handle all the measurement, and store the values
15:48values in the variables that we defined earlier. Alright everything is done, now build the code,
15:58and debug it. Let's run it. You can see the values of temperature in degree celsius, pressure in pascals,
16:09and relative humidity as a percentage. I am going to put my finger on the sensor.
16:15See the value of the temperature rising.
16:27I kept the sensor in the refrigerator for a while, and see the temperature and humidity values.
16:42The temperature is rising, and humidity is decreasing. They both are slowly tending towards the room
16:50condition. This is it for this video. I hope you understood how I wrote each function, and also how
17:02to use them. I will post this library on the github, so that I can modify it in the near future, and also
17:10include some other parameters like altitude measurement. The link to github is in the description.
17:17This is all for today. Leave comments in case of any doubt. Keep watching, and have a nice day ahead.
Be the first to comment
Add your comment

Recommended