• Arduino and BMP180 Pressure Sensor Interfacing( cảm biến áp suất Arduino và BMP180).

QC

Arduino and BMP180 Pressure Sensor Interfacing( cảm biến áp suất Arduino và BMP180).

 

Arduino and BMP180 Pressure Sensor Interfacing( cảm biến áp suất Arduino và BMP180).

Cảm biến nhiệt độ BMP180 là gì?
Cảm biến BMP180 là cảm biến áp suất kỹ thuật số có độ chính xác cao thuộc dòng BMPXXX được sử dụng cho cả mục đích giáo dục và thương mại. Nó dựa trên công nghệ điện trở áp để đạt được độ bền của EMC, độ chính xác và độ tuyến tính cao cũng như độ ổn định lâu dài. Nó được sử dụng để đo áp suất khí quyển hoặc áp suất khí quyển. Áp suất khí quyển hay còn gọi là áp suất khí quyển là áp suất do trọng lượng của không khí ở trên chúng ta tạo nên. Cảm biến BMP180 đo áp suất đó và xuất ra dưới dạng tín hiệu kỹ thuật số có thể được đọc thêm bởi bất kỳ vi điều khiển nào như Arduino UNO bằng cách giao tiếp cảm biến bằng bus I2C. Để có được kết quả đo áp suất chính xác cao, nó sử dụng một cảm biến đo nhiệt độ có sẵn. Vì vậy, BMP180 cũng có thể được sử dụng như một cảm biến đo nhiệt độ rất tốt. Ở đây, chúng ta sẽ giao tiếp cảm biến này với Arduino UNO bằng thư viện. Hơn nữa, chúng tôi sẽ viết một đoạn mã để tạo ra một máy đo độ cao.

Các tính năng của cảm biến BMP180
· Phạm vi áp suất: 300-1000hPa (-500m đến + 9000m so với mực nước biển)

· Điện áp cung cấp (cảm biến standlone): 1.8-3.6V DC

· Dòng tiêu thụ: 1000µA (Đỉnh).

· Sử dụng giao diện I2C

· Hiệu chỉnh đầy đủ

· Cảm biến đo nhiệt độ tích hợp

Các ứng dụng của cảm biến BMP180
·       Dự báo thời tiết

·       Đồng hồ thông minh

·       Điện thoại di động

·       Định vị toàn cầu

· Chỉ báo vận tốc dọc
Các chế độ sử dụng cảm biến BPM180
Có tổng cộng bốn chế độ sử dụng BMP180 có thể được chọn bằng biến oversampling_setting (0, 1, 2, 3) trong mã C. Các chế độ này là;

· Chế độ tiêu thụ điện năng cực thấp

·       Chế độ căn bản
· Chế độ cao

· Chế độ độ phân giải siêu cao

Mỗi chế độ có các giá trị khác nhau về lấy mẫu, thời gian chuyển đổi và mức tiêu thụ hiện tại. Giống như, chế độ đầu tiên là chế độ công suất cực thấp. Ở chế độ này, mức tiêu thụ hiện tại rất thấp nhưng thời gian chuyển đổi nhiều hơn. Đây là giá trị cho tất cả các chế độ khác.



ModeCodeInternal no. of samplesConversion time(ms)Current consumption(max)
Ultra-low power014.53
Standard127.55
High2413.57
Ultra-high resolution3825.512


Làm thế nào để giao tiếp cảm biến BMP180 với bảng Arduino UNO?

Để giao tiếp với cảm biến BMP180, chúng tôi sẽ sử dụng mô-đun cảm biến BMP180 bao gồm cảm biến BMP180 và một số linh kiện điện tử cần thiết. Hơn nữa, chúng tôi sẽ sử dụng thư viện Arduino để đọc dữ liệu từ cảm biến bằng giao diện I2C.

Sơ đồ chân của mô-đun cảm biến BMP180





Chân VCC được sử dụng để kết nối 3.3-5V với mô-đun.
Chân GND được sử dụng để kết nối GND với mô-đun.
Các chân SCL và SDA được sử dụng cho giao tiếp I2C.


Giao diện mô-đun cảm biến BMP180 với bảng Arduino UNO


Chúng tôi cần một thư viện Arduino cho việc này. Tải xuống thư viện từ liên kết này https://github.com/sparkfun/BMP180_Breakout_Arduino_Library và cài đặt nó trong Arduino IDE. Nếu bạn chưa biết cách cài đặt thư viện trong Arduino IDE thì hãy truy cập liên kết này (Đưa link bài viết khác dựa trên cách cài đặt thư viện trong Arduino IDE). Sau khi cài đặt xong thư viện, bạn có thể tiến hành thêm. Theo mạch này.

Sơ đồ dòng đo áp suất không khí, nhiệt độ và độ cao






#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.

Output Screen






















Nap Code vào PY32F003 dùng Stlink

 Nap Code vào PY32F003 dùng Stlink Bước 1: Cài đặt  KeilC v5.39 theo link sau ( chú ý 5.39 keil c mới nạp ok). https://edge07.111.ir.cdn.ir/...