Arduino BMP085/BMP180气压传感器实验

 

BPM180/BPM085 用于气压测量

 

这次示例没有使用了库,如果需要库文件的可以参考(GitHub : https://github.com/adafruit/Adafruit-BMP085-Library

 

 

实验效果

 

BOM表

Arduino Uno                                     *1

BMP180 气压传感器(高度计)   *1

跳线若干

 

引脚说明

VIN     电源输入3.3v

GND  接地

SCL   时钟线

SAD   数据线

 

接线图

程序开源代码

网上找到的不用库的代码,

在精度和修正方面还需要调整算法,继续学习,先用作参考

程序打包下载:https://u16460183.ctfile.com/fs/16460183-296051401

文件夹说明:

Adafruit-BMP085-Library-master ——BMP085/BMP180气压传感器库文件

LS_BMP180                                   ——主程序

 

/**************************************************************************************/
           /**********************DFRobot.com*****************************/
/***write by Tom Riddler          Jun.16.14***/
/***if you got any progream,please contact me terminaterfxy@hotmail.com***/

#include <Wire.h>
#define BMP180ADD 0x77  // I2C address of BMP180  
                           //write is (0xEE)     read is (0xEF)       
unsigned char OSS;                            
/**********************MSB      LSB******/
int ac1;           // 0xAA     0xAB
int ac2;           // 0xAC     0xAD
int ac3;           // 0xAE     0xAE
unsigned int ac4;  // 0xB0     0xB1
unsigned int ac5;  // 0xB2     0xB3
unsigned int ac6;  // 0xB4     0xB5
int b1;            // 0xB6     0xB7
int b2;            // 0xB8     0xB9
int mb;            // 0xBA     0xBB
int mc;            // 0xBC     0xBD
int md;            // 0xBE     0xBF
float temperature;  
double pressure;   
double pressure2;
long b5;          
double altitude;  



void setup()
{
  Serial.begin(9600);
  Wire.begin();
  OSS = 2;  // Oversampling Setting           0: single    1: 2 times    2: 4 times   3: 8 times 
  BMP180start();
}

void loop()
{
  calculate();
  show();
  delay(1000);
}

/** calculate centure **/
void calculate()
{
  temperature = bmp180GetTemperature(bmp180ReadUT());
  temperature = temperature*0.1;
  pressure = bmp180GetPressure(bmp180ReadUP());
  pressure2 = pressure/101325;
  pressure2 = pow(pressure2,0.19029496);
  altitude = 44330*(1-pressure2);                            //altitude = 44330*(1-(pressure/101325)^0.19029496);
}

/** print reslut **/
void show()
{
  Serial.print("Temperature: ");
  Serial.print(temperature, 1);                            //10 hexadecimal
  Serial.println(" C");
  Serial.print("Pressure: ");
  Serial.print(pressure, 0);                               //10 hexadecimal
  Serial.println(" Pa");
  Serial.print("altitude:");
  Serial.print(altitude);
  Serial.println("m");
}

/**BMP180 satrt program**/
void BMP180start()
{                     /*MSB*/
  ac1 = bmp180ReadDate(0xAA);                      //get full data
  ac2 = bmp180ReadDate(0xAC);  
  ac3 = bmp180ReadDate(0xAE);  
  ac4 = bmp180ReadDate(0xB0);  
  ac5 = bmp180ReadDate(0xB2);  
  ac6 = bmp180ReadDate(0xB4);  
  b1  = bmp180ReadDate(0xB6);  
  b2  = bmp180ReadDate(0xB8);  
  mb  = bmp180ReadDate(0xBA);  
  mc  = bmp180ReadDate(0xBC);  
  md  = bmp180ReadDate(0xBE);
}

/***BMP180 temperature Calculate***/
short bmp180GetTemperature(unsigned int ut)
{
  long x1, x2;
  x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;  //x1=((ut-ac6)*ac5)/(2^15)
  x2 = ((long)mc << 11)/(x1 + md);                //x2=(mc*2^11)/(x1+md)
  b5 = x1 + x2;                                   //b5=x1+x2
  return ((b5 + 8)>>4);                           //t=(b5+8)/(2^4)
}

/***BMP180 pressure Calculate***/

long bmp180GetPressure(unsigned long up)
{
  long x1, x2, x3, b3, b6, p;
  unsigned long b4, b7;
  
  b6 = b5 - 4000;

  x1 = (b2 * (b6 * b6)>>12)>>11;
  x2 = (ac2 * b6)>>11;
  x3 = x1 + x2;
  b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
  
  x1 = (ac3 * b6)>>13;
  x2 = (b1 * ((b6 * b6)>>12))>>16;
  x3 = ((x1 + x2) + 2)>>2;
  b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
  
  b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  if (b7 < 0x80000000)
    p = (b7<<1)/b4;
  else
    p = (b7/b4)<<1;
    
  x1 = (p>>8) * (p>>8);
  x1 = (x1 * 3038)>>16;
  x2 = (-7357 * p)>>16;
  p += (x1 + x2 + 3791)>>4;
  
  return p;
}


/*** Read 1 bytes from the BMP180  ***/

int bmp180Read(unsigned char address)
{
  unsigned char data;
  
  Wire.beginTransmission(BMP180ADD);
  Wire.write(address);
  Wire.endTransmission();
  
  Wire.requestFrom(BMP180ADD, 1);
  while(!Wire.available());
    
  return Wire.read();
}

/*** Read 2 bytes from the BMP180 ***/
int bmp180ReadDate(unsigned char address)
{
  unsigned char msb, lsb;
  Wire.beginTransmission(BMP180ADD);
  Wire.write(address);
  Wire.endTransmission();
  Wire.requestFrom(BMP180ADD, 2);
  while(Wire.available()<2);
  msb = Wire.read();
  lsb = Wire.read();
  return (int) msb<<8 | lsb;
}

/*** read uncompensated temperature value ***/
unsigned int bmp180ReadUT()
{
  unsigned int ut;
  Wire.beginTransmission(BMP180ADD);
  Wire.write(0xF4);                       // Write 0x2E into Register 0xF4
  Wire.write(0x2E);                       // This requests a temperature reading
  Wire.endTransmission();  
  delay(5);                               // Wait at least 4.5ms
  ut = bmp180ReadDate(0xF6);               // read MSB from 0xF6 read LSB from (16 bit)
  return ut;
}

/*** Read uncompensated pressure value from BMP180 ***/
unsigned long bmp180ReadUP()
{
  unsigned char msb, lsb, xlsb;
  unsigned long up = 0;
  
  Wire.beginTransmission(BMP180ADD);
  Wire.write(0xF4);                        // Write 0x34+(OSS<<6) into register 0xF4
  Wire.write(0x34 + (OSS<<6));             // 0x34+oss*64
  Wire.endTransmission(); 
  delay(2 + (3<<OSS));                     // Wait for conversion, delay time dependent on OSS
  
  Wire.beginTransmission(BMP180ADD);
  Wire.write(0xF6);                        // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
  Wire.endTransmission();
  
  Wire.requestFrom(BMP180ADD, 3); 
  while(Wire.available() < 3);             // Wait for data to become available
  msb = Wire.read();
  lsb = Wire.read();
  xlsb = Wire.read();
  up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);//16 to 19 bit
  return up;
}

 

 

 

 

 

 

  • 4
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
基于BMP180气压传感器测量大气压和海拔高度的步骤如下: 1. 连接BMP180传感器至单片机的I2C总线,并将I2C地址设置为0x77。 2. 向BMP180传感器发送启动气压测量命令,并等待测量完成。 3. 读取BMP180传感器返回的气压数据,并进行处理转换为实际大气压值。 4. 向BMP180传感器发送启动温度测量命令,并等待测量完成。 5. 读取BMP180传感器返回的温度数据,并进行处理转换为实际温度值。 6. 根据大气压和温度计算出海平面上的大气压。 7. 根据当前大气压和海平面大气压计算出当前海拔高度。 下面是一个简单的基于BMP180传感器测量大气压和海拔高度的示例代码(使用Arduino编写): ```c #include <Wire.h> #include <Adafruit_BMP085.h> Adafruit_BMP085 bmp; void setup() { Serial.begin(9600); if (!bmp.begin()) { Serial.println("Could not find a valid BMP085 sensor, check wiring!"); while (1) {} } } void loop() { float temperature = bmp.readTemperature(); float pressure = bmp.readPressure(); float altitude = bmp.readAltitude(1013.25); // 海平面上的标准气压 Serial.print("Temperature = "); Serial.print(temperature); Serial.println(" *C"); Serial.print("Pressure = "); Serial.print(pressure/100.0F); // 单位转换为hPa Serial.println(" hPa"); Serial.print("Altitude = "); Serial.print(altitude); Serial.println(" m"); Serial.println(); delay(1000); } ``` 这个示例代码会读取BMP180传感器的温度、气压和海拔高度,并将其输出至串口监视器。注意,在使用这个示例代码之前,您需要先安装Adafruit_BMP085库。
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值