What's the purpose of this project?
The purpose of this project is to interface the MS5611 barometric pressure sensor with an Arduino to measure and monitor atmospheric pressure and temperature in various environments. The sensor provides highly accurate pressure and temperature readings, making it ideal for applications such as altitude measurement, weather monitoring, and navigation systems. By reading real-time data, users can design systems that dynamically respond to changes in pressure and temperature, enhancing functionality and reliability in outdoor and environmental applications.
What are we going to learn in this tutorial?
In this tutorial, you’ll learn how to:
- Connect the MS5611 sensor to Arduino and establish I2C or SPI communication.
- Use an existing library or write custom code to interface with the sensor, enhancing your understanding of data acquisition protocols.
- Measure atmospheric pressure and temperature using the sensor, interpreting real-time data for practical applications.
- Develop projects like altitude measurement, weather monitoring, and navigation systems, gaining hands-on experience in creating responsive systems based on environmental conditions.
This hands-on guide provides insights into sensor integration and real-time data monitoring with Arduino.
What do we need to start this project?
As you probably know, we need some hardware and software to do this project. The titles of these hardware and software are provided to you in the table below and you can prepare/download by clicking on each of them and get ready to start.
Required hardware
|
Required software
|
---|---|
Arduino UNO
|
|
First as shown in the image below, we connect the GebraBit MS5611 module to the Arduino UNO as follows:
Then download and add the GebraBit MS5611 library to your Arduino IDE.
Required Library
|
---|
If you don’t know how to add GebraBit libraries to Arduino IDE, refer to the tutorial link below.
MS5611 library and driver
In addition to the modular design of various sensors and ICs, GebraBit tries to provide variety of structured and hardware-independent libraries in C language for the ease of users in setting up and developing software.
For this purpose, after preparing each GebraBit module, the users can refer to the “tutorial” section of the desired module and download the dedicated library, which contains the “ .h” and “ .c” file (Header and Source) and a sample training program under “GebraBit STM32F303”, “GebraBit ATMEGA32A” or “Arduino” development boards.
All the defined functions and structures in the library are commented in full detail and all the received parameters in the arguments of the functions and their return values, are briefly explained. Since the libraries are hardware independed, the user can easily add the library in any of their favorite compilers and develop it by desired microcontroller and development board.
GebraBit MS5611.h header file
In this file, based on the datasheet of the sensor or IC, all address registers, the values of each register are defined in the form of “Enumeration”. Also, the casing of the MS5611 sensor and the configurations related to each of the MS5611 sensor internal blocks are defined in the form of a “STRUCT” with the name GebraBit_ MS5611. Finally, in the Debug Session environment, all the configurations related to each block can be seen in real time.
USER REGISTER MAP
The registry map or sensor commands are defined in this section:
/************************************************
* USER REGISTER MAP *
***********************************************/
#define MS5611_RESET (0x1E)
#define MS5611_PRESSURE_SAMPLING_START (0x40)
#define MS5611_TEMPERATURE_SAMPLING_START (0x50)
#define MS5611_ADC_READ (0x00)
#define MS5611_PROM_READ (0xA0)
/*----------------------------------------------*
* USER REGISTER MAP End *
*----------------------------------------------*/
MS5611_Output_Sample_Rate Enum
The values of this enum are used to select the OSR of the sensor:
1. typedef enum Output_Sample_Rate
2. {
3. OSR_256 = 0x00 ,
4. OSR_512 = 0x02 ,
5. OSR_1024 = 0x04 ,
6. OSR_2048 = 0x06 ,
7. OSR_4096 = 0x08
8. } MS5611_Output_Sample_Rate;
MS5611 struct
1. typedef struct MS5611
2. {
3. uint8_t Register_Cache1;
4. MS5611_Output_Sample_Rate PRESSURE_SAMPLE_RATE;
5. MS5611_Output_Sample_Rate TEMPERATURE_SAMPLE_RATE;
6. uint8_t PROM_DATA[PROM_DATA_BUFFER_SIZE];
7. uint16_t FACTORY_DATA;
8. uint16_t C1;
9. uint16_t C2;
10. uint16_t C3;
11. uint16_t C4;
12. uint16_t C5;
13. uint16_t C6;
14. uint16_t CRC_SERIAL_CODE;
15. uint8_t ADC_DATA[ADC_DATA_BUFFER_SIZE];
16. uint32_t ADC_RAW_PRESSURE;
17. uint32_t ADC_RAW_TEMPERATURE;
18. int32_t DT;
19. int64_t T2;
20. int64_t OFF2;
21. int64_t SENS2;
22. int64_t OFF;
23. int64_t SENS;
24. float TEMPERATURE;
25. float PRESSURE;
26. double ALTITUDE;
27. }GebraBit_MS5611;
Declaration of functions
At the end of this file, all the functions for reading and writing in MS5611 registers, sensor configuration and receiving data from the sensor are declared:
1. /********************************************************
2. *Declare Read&Write MS5611 Register Values Functions *
3. ********************************************************/
4. extern uint8_t GB_MS5611_Burst_Read(uint8_t regAddr,uint8_t *data, uint16_t byteQuantity);
5. extern uint8_t GB_MS5611_Write_Reg_Data(uint8_t regAddr);
6. /********************************************************
7. * Declare MS5611 Configuration Functions *
8. ********************************************************/
9. extern void GB_MS5611_Soft_Reset ( GebraBit_MS5611 * MS5611 );
10. extern void GB_MS5611_Read_PROM ( GebraBit_MS5611 * MS5611 );
11. extern void GB_MS5611_Pressure_Sample_Rate(GebraBit_MS5611 * MS5611 , MS5611_Output_Sample_Rate rate);
12. extern void GB_MS5611_Temperature_Sample_Rate(GebraBit_MS5611 * MS5611 , MS5611_Output_Sample_Rate rate);
13. extern void GB_MS5611_Start_Pressure_Sampling(GebraBit_MS5611 * MS5611);
14. extern void GB_MS5611_Start_Temperature_Sampling(GebraBit_MS5611 * MS5611);
15. extern void GB_MS5611_Read_ADC ( GebraBit_MS5611 * MS5611 ) ;
16. extern void GB_MS5611_Read_ADC_Raw_Pressure(GebraBit_MS5611* MS5611);
17. extern void GB_MS5611_Read_ADC_Raw_Temperature(GebraBit_MS5611* MS5611);
18. extern void GB_MS5611_initialize( GebraBit_MS5611 * MS5611 );
19. extern void GB_MS5611_Calculate_Temperature(GebraBit_MS5611* MS5611);
20. extern void GB_MS5611_Calculate_Temperature_Compensated_Pressure(GebraBit_MS5611* MS5611);
21. extern void GB_MS5611_Altitude(GebraBit_MS5611 * MS5611);
GebraBit_MS5611.c source file
In this file, which is written in C language, all the functions are commented in full detail, and all the parameters received in the arguments of the functions and their return values are clearly explained so we confine to these explanations and invite users to check this file directly for more information.
Sample program in Arduino
After connecting the module to Arduino and adding the library to the IDE, go to the following path: File > Examples > GebraBit_MS5611 > Temp-Press-Alt
Description of Sample file
Enums and functions required by GebraBit MS5611 module have been added to the structures. In the next part, a variable named MS5611 of the GebraBit_MS5611 structure type (this structure is in the GebraBit_MS5611 header and is explained in the GebraBit_MS5611 library description section) is defined for the configuration of the GebraBit MS5611 module:
GebraBit_MS5611 MS5611;
In the next part of the written code, using the GB_MS5611_initialize (&MS5611) we set the GebraBit MS5611 module and finally, in the loop part of the program, the data is read from the sensor and the Sensor values are continuously received:
void setup() {
Serial.begin(9600);
SPI.begin();
pinMode(SPI_CS_Pin, OUTPUT);
digitalWrite(SPI_CS_Pin, HIGH);
GB_MS5611_initialize(&MS5611);
Serial.println("MS5611 Initialized");
}
void loop() {
GB_MS5611_Calculate_Temperature(&MS5611);
Serial.print("Temperature: ");
Serial.print(MS5611.TEMPERATURE);
Serial.println(" °C");
GB_MS5611_Calculate_Temperature_Compensated_Pressure(&MS5611);
Serial.print("Pressure: ");
Serial.print(MS5611.PRESSURE);
Serial.println(" hPa");
GB_MS5611_Altitude(&MS5611);
Serial.print("Altitude: ");
Serial.print(MS5611.ALTITUDE);
Serial.println(" meters");
delay(1000);
}
The Sample file code text:
#include "GebraBit_MS5611.h"
GebraBit_MS5611 MS5611;
void setup() {
Serial.begin(9600);
SPI.begin();
pinMode(SPI_CS_Pin, OUTPUT);
digitalWrite(SPI_CS_Pin, HIGH);
GB_MS5611_initialize(&MS5611);
Serial.println("MS5611 Initialized");
}
void loop() {
GB_MS5611_Calculate_Temperature(&MS5611);
Serial.print("Temperature: ");
Serial.print(MS5611.TEMPERATURE);
Serial.println(" °C");
GB_MS5611_Calculate_Temperature_Compensated_Pressure(&MS5611);
Serial.print("Pressure: ");
Serial.print(MS5611.PRESSURE);
Serial.println(" hPa");
GB_MS5611_Altitude(&MS5611);
Serial.print("Altitude: ");
Serial.print(MS5611.ALTITUDE);
Serial.println(" meters");
delay(1000);
}
Connect your arduino to computer and select your Arduino Board
Then Verify and Upload the Sample code
After uploading the code, open the serial monitor and you can see the luminosity values.
In the following, you can download the GebraBit_MS5611 Library, the schematic of the modules and the “MS5611 datasheet”.
Program output video
The video of the module operation will be uploaded soon