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)
测试代码
1 /* 2 Barometer based on LPS25H sensor and Arduino Pro Mini(3.3V) 3 */ 4 5 #include <Wire.h> 6 7 #define ADDRESS_LPS25H 0x5D 8 #define CTRL_REG1 0x20 9 #define CTRL_REG2 0x21 10 #define PRESS_OUT_XL 0x28 11 12 byte buffer[5]; 13 14 boolean ready = false; 15 int tempOut; 16 long presOut; 17 float tempVal; 18 float presVal; 19 20 void setup() 21 { 22 Wire.begin(); 23 Serial.begin(9600); 24 25 //power down the device (clean start) 26 Wire.beginTransmission(ADDRESS_LPS25H); 27 Wire.write(CTRL_REG1); 28 Wire.write(0x00); 29 Wire.endTransmission(); 30 31 //turn on the sensor, set the one-shot mode, and set the BDU bit 32 Wire.beginTransmission(ADDRESS_LPS25H); 33 Wire.write(CTRL_REG1); 34 Wire.write(0x84); 35 Wire.endTransmission(); 36 } 37 38 void loop() 39 { 40 //run one-shot measurement 41 Wire.beginTransmission(ADDRESS_LPS25H); 42 Wire.write(CTRL_REG2); 43 Wire.write(0x01); 44 Wire.endTransmission(); 45 46 //wait until the measurement is completed 47 while (ready == false) 48 { 49 delay(5); //conversion time: ~37ms 50 Wire.beginTransmission(ADDRESS_LPS25H); 51 Wire.write(CTRL_REG2); 52 Wire.endTransmission(); 53 Wire.requestFrom(ADDRESS_LPS25H, 1); 54 if (Wire.read() == 0x00) 55 { 56 ready = true; 57 } 58 // Serial.println("waiting..."); 59 } 60 61 //read the result 62 Wire.beginTransmission(ADDRESS_LPS25H); 63 Wire.write(PRESS_OUT_XL | 0x80); //read multiple bytes 64 Wire.endTransmission(); 65 66 Wire.requestFrom(ADDRESS_LPS25H, 5); 67 if (Wire.available() >= 5) 68 { 69 for (int i = 0; i < 5; i++) 70 { 71 buffer[i] = Wire.read(); 72 } 73 } 74 ready = false; 75 76 //calculation 77 presOut = (long(buffer[2]) << 16) | (long(buffer[1]) << 8) | long(buffer[0]); 78 presOut = (presOut << 8) >> 8; //PRESS_OUT_H/_L/_XL and is represented as 2’s complement 79 presVal = presOut/4096.0; 80 81 tempOut = (buffer[4] << 8) | buffer[3]; 82 tempVal = 42.5 + tempOut/480.0; 83 84 Serial.print(presVal); Serial.print(" hPa\t"); 85 Serial.print(tempVal); Serial.println(" `C"); 86 87 delay(2000); 88 }
MCU每隔两秒测量一次气压和温度数据,并通过串口打印结果。