1.先说明BMP180的特点
注意了该传感器是I2C接口的,因此在后续步骤要稍加注意。
2. 接线
I2C 有两个总线:SCL 为时钟信号,和 SDA 为双向数据传输。 每个 I2C 器件采用独特的 7 位地址,这意味着你可以有超过 120 个独特的 I2C 器件共享总线, 并且可以同时控制这些器件一起工作。岩绝
将 BMP180 的 VCC 引脚用红色跳线连接树莓派的 3V3 ( 3.3 伏电源)。将 BMP180 的 SDA 引脚 接入树莓派的 SDA 引脚并将 BMP180 SCL 引脚接入树莓派的 SCL 引脚,该引脚提供一个规律的 时钟信号。SDA 传递数据信号。BMP180 的 GND 引脚档渣通过黑色跳线连接树莓派的接地( GND) 引脚。 在上电前,一定多检查两次接线的准确性。
3. 环境和程序部分
配置文件 /boot/config.txt:
dtparam=i2c_arm=on
重启树莓派,用如下命令查看传感器是否接上:
i2cdetect -y 1
如果接上了的话会显示下粗蠢姿图:
编写 BMP180.py:
然后编写调用函数bmp180_example.py:
执行./bmp180_example.py就可以在终端看到实时的温度/气压输出。
第二个structstudent是定义了一个student结构体,这个迟正明白或知吧。
第一个是用typedef把struct
student这个结构体类型名字重新定义为student,也就是说struct
student和student表示同一个事物,都是一个类型的标识符,比如
typedef
int
zhengshu
就是你把整型int重命名为zhengshu,下码团悔面定义:int
i
和
zhengshu
i
两句就是等价的了
以Arduino Uno为例:VCC 接3.3v,GND接GND,SCL接A5,郑兆SDA接A4
代码如下
#include <SFE_BMP180.h>
#include <喊态租Wire.h>
SFE_BMP180 pressure// 创建一个气压计对象
double baseline// 基准气压
void setup()
{
Serial.begin(9600)
Serial.println("REBOOT")
// 初始化传感器
if (pressure.begin())
Serial.println("BMP180 init success")
else
{
// 糟糕,气压计出问题了,闭薯多半是连线有问题
Serial.println("BMP180 init fail (disconnected?)\n\n")
while(1)// 暂停
}
//获得基准气压
baseline = getP()
Serial.print("baseline pressure: ")
Serial.print(baseline)
Serial.println(" hPa")
}
void loop()
{
double a,p,t
p = getP()// 获得一个气压值
a = pressure.altitude(p,baseline)//获得基于基准气压的高度值
Serial.print("relative altitude: ")
if (a >= 0.0) Serial.print(" ")// 调整正数显示格式
Serial.print(a,1)
Serial.print(" meters ")
t = getT()// 获得一个温度值
Serial.print("temperature: ")
Serial.print(t,1)
Serial.println(" degrees")
delay(500)//刷新率
}
double getP()
{
char status
double T,P,p0,a
// You must first get a temperature measurement to perform a pressure reading.
// Start a temperature measurement:
// If request is successful, the number of ms to wait is returned.
// If request is unsuccessful, 0 is returned.
status = pressure.startTemperature()
if (status != 0)
{
// Wait for the measurement to complete:
delay(status)
// Retrieve the completed temperature measurement:
// Note that the measurement is stored in the variable T.
// Use '&T' to provide the address of T to the function.
// Function returns 1 if successful, 0 if failure.
status = pressure.getTemperature(T)
if (status != 0)
{
// Start a pressure measurement:
// The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
// If request is successful, the number of ms to wait is returned.
// If request is unsuccessful, 0 is returned.
status = pressure.startPressure(3)
if (status != 0)
{
// Wait for the measurement to complete:
delay(status)
// Retrieve the completed pressure measurement:
// Note that the measurement is stored in the variable P.
// Use '&P' to provide the address of P.
// Note also that the function requires the previous temperature measurement (T).
// (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
// Function returns 1 if successful, 0 if failure.
status = pressure.getPressure(P,T)
if (status != 0)
{
return P
}
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")
}
double getT()
{
char status
double T,p0
status = pressure.startTemperature()
if (status != 0)
{
delay(status)
status = pressure.getTemperature(T)
if (status != 0)
{
status = pressure.startPressure(3)
return T
}
else Serial.println("error retrieving temperature measurement\n")
}
else Serial.println("error starting temperature measurement\n")
}
注意:串口会同时输出两组数据,A较精确
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)