Interface BMP180 with STM32
The BMP180 is a very ‘simple to use’ Pressure sensor, which senses the atmospheric Pressure. This Pressure can be used to calculate the Altitude from the sea level. BMP180 also have an integrated Temperature Sensor. It uses the I2C for the communication, and delivers the uncompensated values of pressure and temperature. For more information on the sensor, please read it’s datasheet.
The measurement Flow chart is given below
Connecting BMP180 is very simple. Just connect the SCL to SCL and SDA to SDA and you are good to go.
The I2C address of BMP180 is 0xEE as per it’s datasheet. It is shown below
How to actually do the measurement
Well the answer for every “How to interface this” is in the device datasheet. If you look at page number 15 of the BMP180 Datasheet, there is an algorithm given to do all the necessary calculations. Below is the picture.
Here, first of all we need to read the calibration values from the EEPROM with starting address from 0xAA.
Every value i.e. AC1, AC2, AC3 etc is 16 bit in size. These values varies for every BMP180 sensor, but there are some defaults given in the datasheet.
Whatever calibration values you have, they should be somewhat around these default values.
They are as shown below
- Once the Calibration is read, we will start the reading for UT (Uncompensated Temperature). To do so, we need to read the Registers 0xF6, and 0xF7.
- Next in line is UP (Uncompensated Pressure). To read the UP, we need to read 0xF6, 0xF7 and 0xF8 registers.
- And finally we do the calculations as per the datasheet to get the Temperature and the Pressure.
Some Insight into the CODE
- As per the instructions in the datasheet, we will read the calibrations data first.
- The start address for the data to be read is 0xAA
- We need to read 22 Bytes totally
- The values for 16 bit AC1, AC2 etc.. are calculated by shifting the 8 bit values that we read
- HAL_I2C_Mem_Read function reads the entered number of registers from the given start address
In order to calculate the Temperature, we need to get the UT (Uncompensated Temperature) first. To do so, the following are the steps
- Write 0x2E (DATA) in the 0xF4 (Address)
- Now read the 2 Bytes of Data from the address 0xF4.
- convert these 2 Bytes into a single 16 bit value, and that will be the UT value
- Using this UT value, we will do the calculation for the Temperature as shown in the above code
- The formulas used are provided in the datasheet
In this part we will calculate the Pressure. The steps are mentioned below
- Just like Temperature, here also we need to calculate the value of UP (Uncompensated Pressure) first
- Write 0x34+(oss<<6) into the Register 0xF4. Here oss is OverSampling Setting, you can read about it in the datasheet.
- Now we need to wait depending on what oss value you choose. I have used the oss as 0 in this code
- Then read 3 bytes of data starting from 0xF6
- Rearranging these Bytes gives the UP value
- Using the UP from the first function, we will calculate the Pressure
- The calculation shown above is as provided in the BMP180 datasheet.
Here we need to define the Standard Atmospheric Pressure first, as it will be needed in the calculation
The Altitude can be calculated from the Pressure using the formula as shown below
Let’s take a look the Calibration variables first
As you can see in the picture above, that the calibration results are almost the same as the default ones. I have too much variation in AC1 and AC2 but that’s okay.
you can see above the rest of the variables values and based on these values, the Pressure, Temperature and Altitude is calculated.
. . . . . DOWNLOAD . . . . .
You can buy me acontrollerstech
coffeeSensor by clicking DONATE OR Just click DOWNLOAD to download the code