Arduino and BMP180 Pressure Sensor Interfacing( cảm biến áp suất Arduino và BMP180).
Mode | Code | Internal no. of samples | Conversion time(ms) | Current consumption(max) |
Ultra-low power | 0 | 1 | 4.5 | 3 |
Standard | 1 | 2 | 7.5 | 5 |
High | 2 | 4 | 13.5 | 7 |
Ultra-high resolution | 3 | 8 | 25.5 | 12 |
#include <SFE_BMP180.h>
#include <Wire.h>
SFE_BMP180 pressure;
#define ALTITUDE 1655.0 //replace 1655.0 with your current city altitude
void setup()
{
Serial.begin(9600);
Serial.println("REBOOT");
if (pressure.begin())
Serial.println("BMP180 init success");
else
{
Serial.println("BMP180 init fail\n\n");
while(1);
}
}
void loop()
{
char status;
double T,P,p0,a;
Serial.println();
Serial.print("Provided Altitude: ");
Serial.print(ALTITUDE,0);
Serial.print(" meters, ");
Serial.print(ALTITUDE*3.28084,0);
Serial.println(" feet");
status = pressure.startTemperature();
if (status != 0)
{
delay(status);
status = pressure.getTemperature(T);
if (status != 0)
{
// Print out the measurement:
Serial.print("Temperature: ");
Serial.print(T,2);
Serial.print(" °C, ");
Serial.print((9.0/5.0)*T+32.0,2);
Serial.println(" °F");
status = pressure.startPressure(3);
if (status != 0)
{
delay(status);
status = pressure.getPressure(P,T);
if (status != 0)
{
Serial.print("Absolute Pressure: ");
Serial.print(P,2);
Serial.print(" mb, ");
Serial.print(P*0.0295333727,2);
Serial.println(" inHg");
p0 = pressure.sealevel(P,ALTITUDE);
Serial.print("Relative Pressure(Sea Level): ");
Serial.print(p0,2);
Serial.print(" mb, ");
Serial.print(p0*0.0295333727,2);
Serial.println(" inHg");
a = pressure.altitude(P,p0);
Serial.print("Altitude: ");
Serial.print(a,0);
Serial.print(" meters, ");
Serial.print(a*3.28084,0);
Serial.println(" feet");
}
else Serial.println("error retrieving pressure measurement\n");
}
else Serial.println("error starting pressure measurement\n");
}
else Serial.println("error retrieving temperature measurement\n");
}
else Serial.println("error starting temperature measurement\n");
delay(5000);
}
Working of code
#include <SFE_BMP180.h>
#include <Wire.h>
First, include the library for the BMP180 sensor and the library for the I2C interface. These two libraries will include all the necessary functions in order to interface the sensor with Arduino UNO.
SFE_BMP180 pressure;
create an object of class SFE_BMP180. We will be using this object to access all the functions of the library.
#define ALTITUDE 1655.0
Define a constant of name ALTITUDE to store the current altitude of your city. Replace 16655.0 with your city altitude. You can find your city altitude on google.
void setup()
{
Serial.begin(9600);
Start the setup() function and first define the baud rate for serial communication. We will be using this serial communication in order to receive sensor data from the Arduino UNO and display it on the Serial monitor.
if (pressure.begin())
Serial.println("BMP180 init success");
else
{
Serial.println("BMP180 init fail\n\n");
while(1); // Pause forever.
}
}
Using the if statement we will initiate the BMP180 sensor. If it is successfully initiated then you will see a message “BMP180 init success”. Otherwise, you will see the message “BMP180 init fail”. The setup() function is finished here.
void loop()
{
char status;
double T,P,p0,a;
Start loop() function. First, define all the variables needed for calculation. ‘status’ for storing the status of the sensor, ‘T’ for temperature, ‘P’ for pressure, ‘p0’ for relative pressure, and ‘a’ for altitude.
Serial.println();
Serial.print("provided altitude: ");
Serial.print(ALTITUDE,0);
Serial.print(" meters, ");
Serial.print(ALTITUDE*3.28084,0);
Serial.println(" feet");
First, we will print Altitude provide by you in meters and then in inches.
status = pressure.startTemperature();
Start measuring the temperature using the startTemperature() function and store the status in ‘status’ variable. If the measurement is started then ‘5’ will be stored in the ‘status’ variable and if the measurement is not started ‘0’ will be stored in the ‘status’ variable.
status = pressure.startTemperature();
Check whether the status is equal to 0 or not. If the status is not equal to 0 then the code inside the if statement will be executed. Otherwise, “error starting temperature measurement” will be printed on the serial monitor.
delay(status);
If the status is not 0 then will give a delay equal to the status that is 5 ms because the value of status is 5. This is the time taken by the sensor to calculate the temperature.
status = pressure.getTemperature(T);
Get temperature value from the sensor using the getTemperature() function and store it in the variable ‘T’. And store the status in the ‘status’ variable. I have already mentioned above what will be the value of status.
if (status != 0)
{
Serial.print("temperature: ");
Serial.print(T,2);
Serial.print(" deg C, ");
Serial.print((9.0/5.0)*T+32.0,2);
Serial.println(" deg F");
If the status is not equal to 0 then first print the temperature in °Celsius and then print the temperature in °Fahrenheit.
status = pressure.startPressure(3);
Start measuring pressure using the startPressure() function, where 3 is the mode in which you are using the sensor. 3 means you are using the sensor in ultra-high-resolution mode. The status is also stored in the ‘status variable’. If measuring is not started then the value stored in ‘status’ is 0. Otherwise, it will be 5 for mode 0, 8 for mode 1, 14 for mode 2 and 26 for mode 3.
if (status != 0)
{
delay(status);
If the status is not equal to 0 execute all the statements inside the if. First, give a delay equal to the status value. We are using mode 3 so, the value of delay will be 24. This is the time taken by the sensor to calculate the pressure.
status = pressure.getPressure(P,T);
Get the pressure data from the sensor using the getPressure() function and store the pressure data in the variable ‘P’. In order to get the accurate value of pressure, we have also passed the value of temperature inside the function by using the ‘T’ variable.
if (status != 0)
{
Serial.print("absolute pressure: ");
Serial.print(P,2);
Serial.print(" mb, ");
Serial.print(P*0.0295333727,2);
Serial.println(" inHg");
If the status is not equal to 0 then first print the pressure value in ‘mb’ millibar and then print the pressure value in ‘inHg’ inch of mercury.
p0 = pressure.sealevel(P,ALTITUDE);
Calculate the pressure with respect to sea level by using the selevel() function and store the value in the ‘p0’ variable. In order to do so, you have to pass the value of pressure and altitude given by you in the function. For passing the value we are using the variable ‘P’ and ‘ALTITUDE’.
Serial.print("relative (sea-level) pressure: ");
Serial.print(p0,2);
Serial.print(" mb, ");
Serial.print(p0*0.0295333727,2);
Serial.println(" inHg");
Print the relative pressure readings on the serial monitor in ‘mb’ then prin the values in inHG.
a = pressure.altitude(P,p0);
Get the altitude reading by using the altitude() function. In order to get the altitude reading, you have to both pressure(P) and absolute pressure(p0) values in the function.
Serial.print("computed altitude: ");
Serial.print(a,0);
Serial.print(" meters, ");
Serial.print(a*3.28084,0);
Serial.println(" feet");
First, print the altitude in meters and then print the altitude in inch by multiplying meters with 3.28084.
else
Serial.println("error retrieving pressure measurement\n");
}
else
Serial.println("error starting pressure measurement\n");
}
else
Serial.println("error retrieving temperature measurement\n");
}
else
Serial.println("error starting temperature measurement\n");
You the sensor get stuck at any of the stage, you will get the error depending on the stage.
delay(5000);
In last give a delay of 5000ms.