BMP180 Pressure Sensor
The BMP180 is a high-precision, small-size, low-power pressure sensor that can be easily applied in mobile devices. When measuring altitude, the traditional method is to measure the atmospheric pressure at a certain height and then convert it to obtain altitude data. The BMP180 can not only measure atmospheric pressure in real time, but also measure real-time temperature. At the same time, it has an IIC bus interface for easy access by the microcontroller. In addition, it is very convenient to use; without too many operations, you can read the pressure and measurement data.
Module Source
Purchase link: https://detail.tmall.com/item.htm?abbucket=0&id=521331189285&ns=1&spm=a21n57.1.0.0.71d8523cm3qMhZ Materials download link: https://pan.baidu.com/s/1miTIphm
Specifications
Operating voltage: 1.8~3.6V Operating current: 0.1~1000uA Temperature accuracy: ±1℃ Temperature range: 0~65℃ Pressure range: 300~1100 hPa Pressure accuracy: 1 hPa Output method: IIC Number of pins: 3 Pin
View Materials
The BMP180 has four operating modes, each with different parameters such as sampling count, conversion speed, and noise. The mode can be set by writing to the ctrl_meas register. The default is the first one: ultra low power.
The BMP180's pressure and temperature values cannot be read directly. Each different sensor has its own unique calibration values stored in the built-in E2PROM memory. After the microprocessor reads the raw temperature and pressure values from the sensor, it must convert them according to the calibration values in the E2PROM to obtain the actual temperature and pressure data. The storage location of each calibration value is shown below; the microprocessor reads the calibration values through these addresses.
Like all IIC bus devices, the BMP180 also has a fixed device address. According to its data sheet, the default slave address of the BMP180 at the factory is 0xEE (write direction) or 0xEF (read direction).
The following are the steps to read temperature and pressure:
- Read the 16-bit calibration values into the microcontroller. There are 11 values in total. Note that the high bits are stored at the MSB address and the low bits at the LSB address. For example, for the value AC1, the high 8 bits are stored at address 0xAA, and the low 8 bits are stored at address 0xAB.
- Steps to read the initial temperature value: (1) Write 0x2e to register 0xf4, wait 4~5ms; (2) Read registers 0xf6 (high 8 bits) and 0xf7 (low 8 bits); (3) Calculate: UT=MSB <<8 +LSB.
- Steps to read the initial pressure value: (1) Write 0x34 to register 0xf4 (if not using the default operating mode, add the result of oss shifted left by six bits; oss is set by bits bit7 and bit6 of register 0xf4 for the operating mode), wait 4~5ms; (2) Read registers 0xf6 (bits 16-23), 0xf7 (bits 8-15), and 0xf8 (bits 0-7); (3) Calculate: UP=MSB <<16 + LSB<<8 + XLSB >> (8-oss (same as for the initial temperature value read)).
- Based on the calibration coefficients read in step 1 and the UT and UP read in step 2, perform the conversion to finally obtain T (temperature, each value represents 0.1 degrees Celsius) and p (pressure, each value represents 1 pascal).
Porting Process
Pin Selection
Port to Project
Our goal is to port the example to the ESP32-S3 dev board. Complete driver code has been provided for you. Follow the steps below to complete the porting. For detailed instructions on creating folders and new .c and .h files, refer to section 1.4.2 in the [DHT11 Temperature and Humidity Sensor] chapter; we will not repeat it here. The only difference is that we replace the file names bsp_dht11.c and bsp_dht11.h with bsp_bmp180.c and bsp_bmp180.h, and rename the folder to BMP180.
Write Code
In the file bsp_bmp180.c, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-04 LCKFB-lp first version
*/
#include "bsp_bmp180.h"
#include "stdio.h"
#include "math.h"
typedef struct _BMP180_STRUCT{
short AC1;
short AC2;
short AC3;
uint16_t AC4;
uint16_t AC5;
uint16_t AC6;
short B1;
short B2;
short MB;
short MC;
short MD;
}_BMP180_PARAM_;
_BMP180_PARAM_ param={0};
long B5 = 0;
void delay_ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_us(unsigned int us)
{
ets_delay_us(us);
}
/******************************************************************
* Function Name: BMP180_GPIO_Init
* Function Description: BMP180 pin initialization
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void BMP180_GPIO_Init(void)
{
gpio_config_t lll_config = {
.pin_bit_mask = (1ULL<<BMP180_SCL_PIN)|(1ULL<<BMP180_SDA_PIN), // Configure pins
.mode =GPIO_MODE_OUTPUT, // Output mode
.pull_up_en = GPIO_PULLUP_ENABLE, // Enable pull-up
.pull_down_en = GPIO_PULLDOWN_DISABLE, // Disable pull-down
.intr_type = GPIO_INTR_DISABLE // Disable pin interrupt
};
gpio_config(&lll_config);
}
/******************************************************************
* Function Name: IIC_Start
* Function Description: IIC start sequence
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void IIC_Start(void)
{
SDA_OUT();
SDA(1);
delay_us(5);
SCL(1);
delay_us(5);
SDA(0);
delay_us(5);
SCL(0);
delay_us(5);
}
/******************************************************************
* Function Name: IIC_Stop
* Function Description: IIC stop signal
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void IIC_Stop(void)
{
SDA_OUT();
SCL(0);
SDA(0);
SCL(1);
delay_us(5);
SDA(1);
delay_us(5);
}
/******************************************************************
* Function Name: IIC_Send_Ack
* Function Description: Master sends ACK or NACK signal
* Function Parameters: 0=send ACK 1=send NACK
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void IIC_Send_Ack(unsigned char ack)
{
SDA_OUT();
SCL(0);
SDA(0);
delay_us(5);
if(!ack) SDA(0);
else SDA(1);
SCL(1);
delay_us(5);
SCL(0);
SDA(1);
}
/******************************************************************
* Function Name: I2C_WaitAck
* Function Description: Wait for slave acknowledge
* Function Parameters: None
* Function Return: 0=ACK received 1=timeout, no ACK
* Author: LC
* Notes: None
******************************************************************/
unsigned char I2C_WaitAck(void)
{
char ack = 0;
unsigned char ack_flag = 50;
SCL(0);
SDA(1);
SDA_IN();
delay_us(5);
SCL(1);
delay_us(5);
while( (SDA_GET()==1) && ( ack_flag ) )
{
ack_flag--;
delay_us(1);
}
if( ack_flag <= 0 )
{
IIC_Stop();
return 1;
}
else
{
SCL(0);
SDA_OUT();
}
return ack;
}
/******************************************************************
* Function Name: Send_Byte
* Function Description: Write one byte
* Function Parameters: dat=data to write
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void Send_Byte(uint8_t dat)
{
int i = 0;
SDA_OUT();
SCL(0);//Pull clock low to start data transfer
for( i = 0; i < 8; i++ )
{
SDA( (dat & 0x80) >> 7 );
delay_us(1);
SCL(1);
delay_us(5);
SCL(0);
delay_us(5);
dat<<=1;
}
}
/******************************************************************
* Function Name: Read_Byte
* Function Description: IIC read sequence
* Function Parameters: None
* Function Return: Data read
* Author: LC
* Notes: None
******************************************************************/
unsigned char Read_Byte(void)
{
unsigned char i,receive=0;
SDA_IN();//Set SDA as input
for(i=0;i<8;i++ )
{
SCL(0);
delay_us(5);
SCL(1);
delay_us(5);
receive<<=1;
if( SDA_GET() )
{
receive|=1;
}
delay_us(5);
}
SCL(0);
return receive;
}
/******************************************************************
* Function Name: BMP180_Write_Cmd
* Function Description: Write one byte of data to BMP180
* Function Parameters: regaddr=register address cmd=data to write
* Function Return: None
* Author: LC
* Notes: regaddr=0xf4, cmd=0X2E
******************************************************************/
void BMP180_Write_Cmd(uint8_t regaddr,uint8_t cmd)
{
IIC_Start();//Start signal
Send_Byte(0XEE);//Device address + write
if( I2C_WaitAck() == 1 ) printf("Write_Cmd NACK -1\r\n");
Send_Byte(regaddr);
if( I2C_WaitAck() == 1 ) printf("Write_Cmd NACK -2\r\n");
Send_Byte(cmd);
if( I2C_WaitAck() == 1 ) printf("Write_Cmd NACK -3\r\n");
IIC_Stop();
}
/******************************************************************
* Function Name: BMP180_Read16
* Function Description: Read BMP180 data
* Function Parameters: regaddr=read address len=read length
* Function Return: Data read
* Author: LC
* Notes: None
******************************************************************/
uint16_t BMP180_Read16(uint16_t regaddr,uint8_t len)
{
int timeout = 0;
uint16_t dat[3] = {0};
int i =0;
for( i = 0; i < len; i++ )
{
IIC_Start();//Start signal
Send_Byte(0XEE);//Device address + write
if( I2C_WaitAck() == 1 ) printf("Read_Reg NACK -1\r\n");
Send_Byte(regaddr+i);
if( I2C_WaitAck() == 1 ) printf("Read_Reg NACK -2\r\n");
do{
timeout++;
delay_ms(1);
IIC_Start();//Start signal
Send_Byte(0XEF);//Device address + read
}while(I2C_WaitAck() == 1 && (timeout < 5) );
dat[i] = Read_Byte();
IIC_Send_Ack(1);
IIC_Stop();
delay_ms(1);
}
if( len == 2 ) return ( (dat[0]<<8) | dat[1] );
if( len == 3 ) return (( (dat[0]<<16) | (dat[1]<<8) | (dat[2]) ) >> 8);
return 0;
}
/******************************************************************
* Function Name: BMP180_Get_Temperature
* Function Description: Read temperature in ℃
* Function Parameters: None
* Function Return: Temperature
* Author: LC
* Notes: None
******************************************************************/
float BMP180_Get_Temperature(void)
{
long UT = 0;
long X1 = 0, X2 = 0;
BMP180_Write_Cmd(0XF4, 0x2E);
delay_ms(6);
UT = BMP180_Read16(0xf6,2);
// X1 = ((long)UT - param.AC6) * param.AC5 / 32768.0;
X1 = ((long)UT - param.AC6) * param.AC5 >> 15;
// X2 = ((long)param.MC * 2048.0) / ( X1 + param.MD );
X2 = ((long)param.MC << 11) / ( X1 + param.MD );
B5 = X1 + X2;
// return ((B5+8)/16.0)*0.1f;
return ((B5+8) >> 4)*0.1f;
}
/******************************************************************
* Function Name: BMP180_Get_Pressure
* Function Description: Read pressure, unit Pa
* Function Parameters: None
* Function Return: Current pressure, unit Pa
* Author: LC
* Notes: None
******************************************************************/
float BMP180_Get_Pressure(void)
{
long UP = 0;
uint8_t oss = 0;
long X1 = 0, X2 = 0;
// BMP180_Get_Temperature();
BMP180_Write_Cmd(0XF4, (0XB4+(oss<<6)));
delay_ms(10);
UP = BMP180_Read16(0xf6,3);
int32_t B6 = B5 - 4000;
X1 = (B6 * B6 >> 12) * param.B2 >> 11;
X2 = param.AC2 * B6 >> 11;
int32_t X3 = X1 + X2;
int32_t B3 = (((param.AC1 << 2) + X3) + 2) >> 2;
X1 = param.AC3 * B6 >> 13;
X2 = (B6 * B6 >> 12) * param.B1 >> 16;
X3 = (X1 + X2 + 2) >> 2;
uint32_t B4 = param.AC4 * (uint32_t)(X3 + 32768) >> 15;
uint32_t B7 = ((uint32_t)UP - B3) * 50000;
int32_t p;
if(B7 < 0x80000000){
p = (B7 << 1) / B4;
}else{
p = B7/B4 << 1;
}
X1 = (p >> 8) * (p >> 8);
X1 = (X1 * 3038) >> 16;
X2 = (-7375 * p) >> 16;
p = p + ((X1 + X2 + 3791) >> 4);
return p;
}
/******************************************************************
* Function Name: BMP180_Get_Altitude
* Function Description: Calculate altitude
* Function Parameters: p=current pressure
* Function Return: Altitude
* Author: LC
* Notes: None
******************************************************************/
float BMP180_Get_Altitude(float p)
{
// #define PRESSURE_OF_SEA 101325.0f // Reference sea level pressure
float altitude = 0;
altitude = 44330*(1 - pow(((double)p)/ (double)101325.0, (double)1.0 / (double)5.255));
printf("altitude = %.2f\r\n",altitude);
return altitude;
}
/******************************************************************
* Function Name: BMP180_Get_param
* Function Description: Get factory calibration values
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void BMP180_Get_param(void)
{
param.AC1 = BMP180_Read16(0xAA,2);
param.AC2 = BMP180_Read16(0xAC,2);
param.AC3 = BMP180_Read16(0xAE,2);
param.AC4 = BMP180_Read16(0xB0,2);
param.AC5 = BMP180_Read16(0xB2,2);
param.AC6 = BMP180_Read16(0xB4,2);
param.B1 = BMP180_Read16(0xB6,2);
param.B2 = BMP180_Read16(0xB8,2);
param.MB = BMP180_Read16(0xBA,2);
param.MC = BMP180_Read16(0xBC,2);
param.MD = BMP180_Read16(0xBE,2);
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
In the file bsp_bmp180.h, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-04 LCKFB-lp first version
*/
#ifndef _BSP_BMP180_H_
#define _BSP_BMP180_H_
#include "driver/i2c.h"
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "freertos/queue.h"
#include <inttypes.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "rom/ets_sys.h"
#include "esp_system.h"
#include "driver/gpio.h"
//Port porting
#define BMP180_SCL_PIN 1
#define BMP180_SDA_PIN 2
//Set SDA output mode
#define SDA_OUT() gpio_set_direction(BMP180_SDA_PIN,GPIO_MODE_OUTPUT)
//Set SDA input mode
#define SDA_IN() gpio_set_direction(BMP180_SDA_PIN,GPIO_MODE_INPUT)
//Get SDA pin level change
#define SDA_GET() gpio_get_level(BMP180_SDA_PIN)
//SDA and SCL output
#define SDA(x) gpio_set_level(BMP180_SDA_PIN, (x?1:0))
#define SCL(x) gpio_set_level(BMP180_SCL_PIN, (x?1:0))
void delay_us(unsigned int us);
void delay_ms(unsigned int ms);
void BMP180_GPIO_Init(void);
float BMP180_Get_Temperature(void);
float BMP180_Get_Pressure(void);
void BMP180_Write_Cmd(uint8_t regaddr,uint8_t cmd);
void BMP180_Get_param(void);
float BMP180_Get_Altitude(float p);
#endif2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Porting Verification
In the main function of your own project, write the following.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-04 LCKFB-lp first version
*/
#include <stdio.h>
#include "bsp_bmp180.h"
void app_main(void)
{
BMP180_GPIO_Init();
BMP180_Get_param();
printf("start\r\n");
while(1)
{
float Temperature = BMP180_Get_Temperature();
float Pressure = BMP180_Get_Pressure();
printf("temp = %.2f\r\n", Temperature);
printf("Pressure = %.2f\r\n", Pressure);
BMP180_Get_Altitude(Pressure);
Temperature = 0;
Pressure = 0;
delay_ms(1000);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Power-on effect:
Driver code:
File Download
📌 Materials Download Center (click to jump)
📌 In the Materials Download Center -> Module Porting Materials Download, inside the compressed package of this chapter.