MS5611 Pressure Sensor
Module Source
Purchase link: https://item.taobao.com/item.htm?spm=a230r.1.14.18.c2c52122lnx328&id=522572301522&ns=1&abbucket=19#detail Materials download link: https://pan.baidu.com/s/1QOrpiggCE6mBpqabJXUufg Extraction code: c2pp
Specifications
Operating voltage: 1.8~3.6V Operating current: 0.25~23uA Temperature precision: 0.8℃ Temperature range: -40~85℃ Pressure range: 10~1200 mbar Pressure precision: 1.5 mbar Output method: IIC Number of pins: 3 Pin
View Materials
When the PS pin is connected to high level, the sensor is in IIC mode; when the PS pin is connected to low level, the sensor is in SPI mode. In the schematic diagram, the PS pin is connected to high level through a pull-up resistor, so the default is IIC mode.
Device address = 0XEE When the CSB pin is connected to high level, the address is 1110 110+(read/write bit) When the CSB pin is connected to low level, the address is 1110 111+(read/write bit)
Flow for reading pressure and temperature: Start -> Read factory calibration values C1 to C6 -> Read raw data of pressure D1 and temperature D2 -> Substitute D2 and C1-C6 into formulas to calculate dT and TEMP, where TEMP is the temperature data -> Substitute dT and C1 to C6 into formulas to calculate OFF, SENS, and P, where P is the pressure data.
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. However, here we change the file names bsp_dht11.c and bsp_dht11.h to bsp_ms5611.c and bsp_ms5611.h, and rename the folder to MS5611.
Write Code
In the file bsp_ms5611.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_ms5611.h"
#include "stdio.h"
// Factory calibration values
//Cal_C1_6[0] = Factory information
//Cal_C1_6[1] ~ Cal_C1_6[6] = Calibration values
//Cal_C1_6[7] = CRC checksum of calibration values
uint16_t Cal_C1_6[8];
void delay_ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_us(unsigned int us)
{
ets_delay_us(us);
}
/******************************************************************
* Function Name: MS5611_GPIO_Init
* Function Description: Pin initialization for MS5611
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void MS5611_GPIO_Init(void)
{
gpio_config_t lll_config = {
.pin_bit_mask = (1ULL<<MS5611_SCL_PIN)|(1ULL<<MS5611_SDA_PIN), // Configure pin
.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 timing
* 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 acknowledge or non-acknowledge signal
* Function Parameters: 0 sends acknowledge 1 sends non-acknowledge
* 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 acknowledged 1 timeout no acknowledge
* Author: LC
* Notes: None
******************************************************************/
unsigned char I2C_WaitAck(void)
{
char ack = 0;
unsigned char ack_flag = 10;
SCL(0);
SDA(1);
SDA_IN();
delay_us(5);
SCL(1);
delay_us(5);
while( (SDA_GET()==1) && ( ack_flag ) )
{
ack_flag--;
delay_us(5);
}
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 transmission
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 timing
* 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: MS5611_Reset
* Function: Reset MS5611
* Parameters: None
* Function Return: 0=reset successful 1=device address error 2=command no response
* Author: LC
* Notes: None
**********************************************************/
char MS5611_Reset(void)
{
IIC_Start();// Start signal
Send_Byte(0xee|0); // Device address + write
if( I2C_WaitAck() == 1 )return 1;
Send_Byte(0x1e); // Reset command
if( I2C_WaitAck() == 1 )return 2;
IIC_Stop();
return 0;
}
//C1-C6 16 bits 6 addresses each address 16 bits
/**********************************************************
* Function Name: MS5611_Read_PROM
* Function: Read factory calibration values
* Parameters: None
* Function Return: None
* Author: LC
* Notes: None
**********************************************************/
void MS5611_Read_PROM(void)
{
uint8_t data_H=0,data_L=0;
uint8_t i = 0;
for( i = 0; i < 8; i++ )
{
IIC_Start();// Start signal
Send_Byte(0xee|0); // Device address + write
I2C_WaitAck();
Send_Byte( 0xA0 + i * 2 ); // Register address
I2C_WaitAck();
IIC_Stop();
delay_us(200);
IIC_Start();// Start signal
Send_Byte(0xee|1); // Device address + read
I2C_WaitAck();
data_H = Read_Byte();// High 8 bits of read data
IIC_Send_Ack(0);
data_L = Read_Byte();// Low 8 bits of read data
IIC_Send_Ack(1);
IIC_Stop();
// Save factory calibration data
Cal_C1_6[i] = (data_H<<8) | data_L;
}
}
/**********************************************************
* Function Name: MS5611_Read_D1_D2
* Function: Read raw data of pressure D1 and temperature D2
* Parameters: regaddr=0x48 or 0x58
* Function Return: Returns the 24-bit data integrated after reading
* Author: LC
* Notes:
* regaddr= 0x48 Read D1 data (OSR=4096)
* regaddr= 0x58 Read D2 data (OSR=4096)
**********************************************************/
uint32_t MS5611_Read_D1_D2(uint8_t regaddr)
{
uint32_t dat = 0;
uint8_t buff[3] ={0};
IIC_Start();// Start signal
Send_Byte(0xee|0); // Device address + write
if( I2C_WaitAck() == 1 )printf("D1 NACK -1\r\n");
Send_Byte(regaddr); // OSR = 4096
if( I2C_WaitAck() == 1 )printf("D1 NACK -2\r\n");
IIC_Stop();
delay_ms(10);
IIC_Start();// Start signal
Send_Byte(0xee|0); // Device address + write
if( I2C_WaitAck() == 1 )printf("D1 NACK -3\r\n");
Send_Byte(0X00);
if( I2C_WaitAck() == 1 )printf("D1 NACK -4\r\n");
IIC_Stop();
delay_ms(10);
IIC_Start();// Start signal
Send_Byte(0xee|1); // Device address + read
if( I2C_WaitAck() == 1 )printf("D1 NACK -5\r\n");
buff[0] = Read_Byte();
IIC_Send_Ack(0);
buff[1] = Read_Byte();
IIC_Send_Ack(0);
buff[2] = Read_Byte();
IIC_Send_Ack(1);
IIC_Stop();
dat = (((buff[0]<<16) | ( buff[1]<<8)) | buff[2]);
return dat;
}
uint32_t D1 = 0, D2 = 0, dT = 0;
/**********************************************************
* Function Name: Get_TEMP
* Function: Convert temperature
* Parameters: None
* Function Return: Temperature data without decimal point
* Author: LC
* Notes: None
**********************************************************/
float Get_TEMP(void)
{
float dat = 0;
long long TEMP = 0;
D1 = MS5611_Read_D1_D2(0x48);
delay_ms(10);
D2 = MS5611_Read_D1_D2(0x58);
dT = D2 - (Cal_C1_6[5] * 256.0);
TEMP = 2000 + (((float)dT * Cal_C1_6[6]) / 8388608.0);
// printf("temp = %lld%lld.%lld%lld\r\n",TEMP/1000, TEMP/100%10,TEMP/10%10,TEMP%10);
// Temperature without decimal
dat = (((TEMP/1000)*10) + (TEMP/100%10)) ;
return dat;
}
/**********************************************************
* Function Name: Get_pressure
* Function: Convert pressure data
* Parameters: None
* Function Return: Returns pressure, unit (HPa)
* Author: LC
* Notes: None
**********************************************************/
float Get_pressure(void)
{
long long SENS = 0;
long long P =0;
long long OFF = 0;
Get_TEMP();
OFF = Cal_C1_6[2] * 65536.0 + Cal_C1_6[4] * dT / 128;
SENS = (Cal_C1_6[1] * 32768.0) + ((Cal_C1_6[3] * dT ) / 256.0);
P = (D1 * SENS / 2097152.0 - OFF) / 32768.0;
return (P/100.0);
}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
In the file bsp_ms5611.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_MS5611_H_
#define _BSP_MS5611_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 MS5611_SCL_PIN 1
#define MS5611_SDA_PIN 2
// Set SDA output mode
#define SDA_OUT() gpio_set_direction(MS5611_SDA_PIN,GPIO_MODE_OUTPUT)
// Set SDA input mode
#define SDA_IN() gpio_set_direction(MS5611_SDA_PIN,GPIO_MODE_INPUT)
// Get the level change of SDA pin
#define SDA_GET() gpio_get_level(MS5611_SDA_PIN)
// SDA and SCL output
#define SDA(x) gpio_set_level(MS5611_SDA_PIN, (x?1:0))
#define SCL(x) gpio_set_level(MS5611_SCL_PIN, (x?1:0))
void delay_us(unsigned int us);
void delay_ms(unsigned int ms);
void MS5611_GPIO_Init(void);
char MS5611_Reset(void);
void MS5611_Read_PROM(void);
float Get_TEMP(void);
float Get_pressure(void);
#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
Porting Verification
In the main function of your own project, 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 <stdio.h>
#include "bsp_ms5611.h"
void app_main(void)
{
MS5611_GPIO_Init();
MS5611_Reset();// Device reset
delay_ms(300);// Wait for initialization to complete;
MS5611_Read_PROM();// Read factory calibration values
printf("start\r\n");
while(1)
{
// Output temperature
printf("temp = %.0f℃\r\n",Get_TEMP() );
// Output pressure
printf("pressure = %.2fHPa\r\n",Get_pressure() );
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
32
33
34
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.