
正文
Arduino I2C + 气压传感器LPS25H
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
LPS25H是ST生产的MEMS数字气压传感器。主要特性有:
- 测量范围:260 ~ 1260 hPa绝对气压
- 分辨率:均方根1 Pa
- 工作电压:1.7 ~ 3.6 V
- 功耗:4μA(低分辨率模式)~25μA(高分辨率模式)
- 数据刷新频率:1 ~ 25 Hz可选择
- 接口:I2C,三线制/四线制SPI
- 内置温度补偿
- 内置24位ADC
- 内置先入先出(FIFO)存储器
- 封装:2.5 x 2.5 x 1.0 mm HCLGA-10L
管脚定义
- VDD:电源,1.7~3.6V
- GND:地
- VDD_IO:IO口供电脚,电压小于等于VDD
- GND_IO:IO口接地脚,需与GND相连
- Reserved:与GND相连
- CS:I2C接口(CS=1)选择,或SPI接口片选脚(CS=0)
- SCL/SPC:I2C接口或SPI接口的时钟线,通过CS脚选择
- SDA/SDI/SDIO:I2C接口或SPI接口的数据线
- SA0/SDO:I2C接口地址选择脚,或者SPI接口的数据线
- INT1:中断输出脚,可编程其触发条件,参见CTRL_REG4寄存器设置
与Arduino的连接
因为传感器IC大多工作在3.3V附近,因此干脆用工作于3.3V/8MHz版本的Arduino Pro Mini进行调试,避免了用UNO时接口电平转换的麻烦。采用I2C接口进行通讯。未利用INT1和FIFO的功能。
LPS25H Pro Mini 3.3V/8MHz
VDD <------> 3.3V
GND <------> GND
SCL <------> A5 (SCL)
SDA <------> A4 (SDA)
测试代码
/*
Barometer based on LPS25H sensor and Arduino Pro Mini(3.3V)
*/
#include <Wire.h>
#define ADDRESS_LPS25H 0x5D
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define PRESS_OUT_XL 0x28
byte buffer[];
boolean ready = false;
int tempOut;
long presOut;
float tempVal;
float presVal;
void setup()
{
Wire.begin();
Serial.begin();
//power down the device (clean start)
Wire.beginTransmission(ADDRESS_LPS25H);
Wire.write(CTRL_REG1);
Wire.write(0x00);
Wire.endTransmission();
//turn on the sensor, set the one-shot mode, and set the BDU bit
Wire.beginTransmission(ADDRESS_LPS25H);
Wire.write(CTRL_REG1);
Wire.write(0x84);
Wire.endTransmission();
}
void loop()
{
//run one-shot measurement
Wire.beginTransmission(ADDRESS_LPS25H);
Wire.write(CTRL_REG2);
Wire.write(0x01);
Wire.endTransmission();
//wait until the measurement is completed
while (ready == false)
{
delay(); //conversion time: ~37ms
Wire.beginTransmission(ADDRESS_LPS25H);
Wire.write(CTRL_REG2);
Wire.endTransmission();
Wire.requestFrom(ADDRESS_LPS25H, );
if (Wire.read() == 0x00)
{
ready = true;
}
// Serial.println("waiting...");
}
//read the result
Wire.beginTransmission(ADDRESS_LPS25H);
Wire.write(PRESS_OUT_XL | 0x80); //read multiple bytes
Wire.endTransmission();
Wire.requestFrom(ADDRESS_LPS25H, );
if (Wire.available() >= )
{
for (int i = ; i < ; i++)
{
buffer[i] = Wire.read();
}
}
ready = false;
//calculation
presOut = (long(buffer[]) << ) | (long(buffer[]) << ) | long(buffer[]);
presOut = (presOut << ) >> ; //PRESS_OUT_H/_L/_XL and is represented as 2’s complement
presVal = presOut/4096.0;
tempOut = (buffer[] << ) | buffer[];
tempVal = 42.5 + tempOut/480.0;
Serial.print(presVal); Serial.print(" hPa\t");
Serial.print(tempVal); Serial.println(" `C");
delay();
}
/*
Barometer based on LPS25H sensor and Arduino Pro Mini(3.3V)
*/ #include <Wire.h> #define ADDRESS_LPS25H 0x5D
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define PRESS_OUT_XL 0x28 byte buffer[]; boolean ready = false;
int tempOut;
long presOut;
float tempVal;
float presVal; void setup()
{
Wire.begin();
Serial.begin(); //power down the device (clean start)
Wire.beginTransmission(ADDRESS_LPS25H);
Wire.write(CTRL_REG1);
Wire.write(0x00);
Wire.endTransmission(); //turn on the sensor, set the one-shot mode, and set the BDU bit
Wire.beginTransmission(ADDRESS_LPS25H);
Wire.write(CTRL_REG1);
Wire.write(0x84);
Wire.endTransmission();
} void loop()
{
//run one-shot measurement
Wire.beginTransmission(ADDRESS_LPS25H);
Wire.write(CTRL_REG2);
Wire.write(0x01);
Wire.endTransmission(); //wait until the measurement is completed
while (ready == false)
{
delay(); //conversion time: ~37ms
Wire.beginTransmission(ADDRESS_LPS25H);
Wire.write(CTRL_REG2);
Wire.endTransmission();
Wire.requestFrom(ADDRESS_LPS25H, );
if (Wire.read() == 0x00)
{
ready = true;
}
// Serial.println("waiting...");
} //read the result
Wire.beginTransmission(ADDRESS_LPS25H);
Wire.write(PRESS_OUT_XL | 0x80); //read multiple bytes
Wire.endTransmission(); Wire.requestFrom(ADDRESS_LPS25H, );
if (Wire.available() >= )
{
for (int i = ; i < ; i++)
{
buffer[i] = Wire.read();
}
}
ready = false; //calculation
presOut = (long(buffer[]) << ) | (long(buffer[]) << ) | long(buffer[]);
presOut = (presOut << ) >> ; //PRESS_OUT_H/_L/_XL and is represented as 2’s complement
presVal = presOut/4096.0; tempOut = (buffer[] << ) | buffer[];
tempVal = 42.5 + tempOut/480.0; Serial.print(presVal); Serial.print(" hPa\t");
Serial.print(tempVal); Serial.println(" `C"); delay();
}
MCU每隔两秒测量一次气压和温度数据,并通过串口打印结果。
参考资料
LPS25H - STMicroelectronics






