FREE SHIPPING ON ALL ORDERS OVER £20

60 DAY HASSLE-FREE RETURNS

将BMP280与Raspberry Pi一起使用

Using the BMP280 with the Raspberry Pi

这 BMP280 是一个 气压和温度传感器 这提供了准确的大气压力和温度读数。通常用于 天气监测,海拔跟踪和环境感应。本指南将带您进行设置,并使用 BMP280带有覆盆子Pi 使用 Python和I2C/SPI通信.


你需要什么

  1. 覆盆子pi (任何具有GPIO支持的模型,例如PI 3,PI 4)
  2. BMP280传感器模块
  3. 面包板和跳线电线
  4. 安装了Python 在覆盆子pi上

步骤1:将BMP280接线到Raspberry Pi

这 BMP280支持I2C和SPI通信.

对于I2C模式(推荐)

BMP280针 覆盆子Pi Pin
VCC 3.3V(引脚1)
gnd 地面(引脚6)
SDA SDA(引脚3,GPIO2)
SCL SCL(引脚5,GPIO3)

对于SPI模式

BMP280针 覆盆子Pi Pin
VCC 3.3V(引脚1)
gnd 地面(引脚6)
SCK SCLK(引脚23,GPIO11)
SDI MOSI(引脚19,GPIO10)
SDO 味o(引脚21,GPIO9)
CS CE0(引脚24,GPIO8)

笔记: BMP280在 3.3V. 不要 将其连接到5V,因为它可能会损坏模块。


步骤2:在Raspberry Pi上启用I2C或SPI

用于I2C通信

  1. 打开Raspberry Pi配置工具:
    sudo raspi-config
    
  2. 导航到 接口选项> I2C 并启用它。
  3. 重新启动覆盆子Pi:
    sudo reboot
    
  4. 验证 检测到BMP280 在I2C公共汽车上:
    sudo i2cdetect -y 1
    
    这 BMP280应该出现在地址 0x76 或者 0x77.

用于SPI通信

  1. 启用S​​PI使用:
    sudo raspi-config
    
  2. 导航到 接口选项> SPI 并启用它。
  3. 重新启动覆盆子Pi:
    sudo reboot
    
  4. 使用以下方式验证SPI接口:
    ls /dev/spidev*
    
    你应该看 /dev/spidev0.0 和 /dev/spidev0.1.

步骤3:安装所需库

  1. 更新您的Raspberry Pi的包装列表:
    sudo apt update && sudo apt upgrade -y
    
  2. 安装所需的 I2C/SPI工具 和Python图书馆:
    sudo apt install -y python3-smbus python3-spidev python3-pip
    pip3 install adafruit-circuitpython-bmp280
    

步骤4:从BMP280读取数据

I2C模式的Python代码

import time
import board
import busio
import adafruit_bmp280

# Initialize I2C bus and BMP280 sensor
i2c = busio.I2C(board.SCL, board.SDA)
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)

# Set sea level pressure (optional, for altitude calculations)
bmp280.sea_level_pressure = 1013.25  

try:
    while True:
        print(f"Temperature: {bmp280.temperature:.2f} °C")
        print(f"Pressure: {bmp280.pressure:.2f} hPa")
        print(f"Altitude: {bmp280.altitude:.2f} m")
        print("------------------------")
        time.sleep(2)
except KeyboardInterrupt:
    print("Exiting...")

SPI模式的Python代码

import time
import board
import busio
import digitalio
import adafruit_bmp280

# Initialize SPI bus and BMP280 sensor
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs = digitalio.DigitalInOut(board.D8)  # Use GPIO8 (Pin 24) for CS
bmp280 = adafruit_bmp280.Adafruit_BMP280_SPI(spi, cs)

try:
    while True:
        print(f"Temperature: {bmp280.temperature:.2f} °C")
        print(f"Pressure: {bmp280.pressure:.2f} hPa")
        print(f"Altitude: {bmp280.altitude:.2f} m")
        print("------------------------")
        time.sleep(2)
except KeyboardInterrupt:
    print("Exiting...")

步骤5:BMP280与Raspberry Pi的应用

  1. 天气监测 - 收集温度和压力数据进行实时天气分析。
  2. 高度测量 - 使用压力数据估计高度变化。
  3. 物联网项目 - 将传感器数据发送到云平台以进行远程监视。
  4. 家庭自动化 - 根据环境读数调整通风和空调。

故障排除

  1. BMP280未检测到(i2cdetect 不显示 0x76 或者 0x77)

    • 确保 SDA/SCL引脚 正确连接。
    • 验证这一点 I2C已启用 使用 sudo raspi-config.
  2. 读数不正确

    • 检查 连接松动 或者 电源问题.
    • 确保传感器为 不暴露于突然的温度变化.
  3. SPI通信问​​题

    • 确保启用SPI(ls /dev/spidev* 应该列出设备)。
    • 核实 正确选择CS PIN 在Python脚本中。

结论

这 BMP280传感器 提供 准确的气压压力,温度和高度测量值,使其理想 气象站,物联网应用和环境监控。通过遵循本指南,您可以轻松地将BMP280集成到您的 Raspberry Pi项目 用于实时数据收集和分析! 🚀

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.

发表评论