BMP280 温度/气压传感器驱动 - MicroPython版本
目录
简介
BMP280 是 Bosch 出品的高精度数字气压/温度传感器,支持 I2C 接口。本驱动为 MicroPython 实现,提供温度和气压的补偿读取,适用于 ESP32、RP2040 等主流 MicroPython 平台。驱动采用依赖注入设计,外部传入 I2C 实例,初始化时自动加载出厂校准系数并配置测量模式。
主要功能
- 支持 I2C 接口(地址 0x76 / 0x77)
- 自动加载出厂校准系数,输出补偿后温度(℃)和气压(Pa)
- 提供原始 ADC 值读取接口
read_raw() - 支持软复位
reset() - 依赖注入设计,不在类内创建总线对象
- 可选调试日志输出(
debug=True)
硬件要求
推荐测试硬件: ESP32
| 引脚 | 功能描述 |
|---|---|
| VCC | 电源正极(1.8V–3.6V,推荐 3.3V) |
| GND | 电源负极 |
| SCL | I2C 时钟线 → ESP32 GPIO 22 |
| SDA | I2C 数据线 → ESP32 GPIO 21 |
| SDO | I2C 地址选择(接 GND → 0x76,接 VCC → 0x77) |
| CSB | 接 VCC 选择 I2C 模式 |
软件环境
| 项目 | 版本 |
|---|---|
| MicroPython 固件 | v1.23.0 及以上 |
| 驱动版本 | v1.0.0 |
| 依赖库 | 无(仅使用 MicroPython 内置模块) |
文件结构
├── bmp280.py # 核心驱动
├── main.py # 测试示例
└── README.md # 说明文档
文件说明
- bmp280.py:BMP280 传感器核心驱动,包含
BMP280类,提供温度、气压读取及软复位功能。 - main.py:测试示例,演示 I2C 扫描、芯片 ID 验证、传感器初始化及各 API 调用,含边界与异常参数测试。
- README.md:本说明文档。
快速开始
步骤一: 将 bmp280.py 复制到设备根目录或 /lib 目录。
步骤二: 按下表接线(ESP32):
| BMP280 引脚 | ESP32 引脚 |
|---|---|
| VCC | 3.3V |
| GND | GND |
| SCL | GPIO 22 |
| SDA | GPIO 21 |
步骤三: 运行 main.py:
# Python env : MicroPython v1.23.0
# -*- coding: utf-8 -*-
# @Time : 2026/04/26
# @Author : leezisheng
# @File : main.py
# @Description : 测试BMP280驱动类的代码
# @License : MIT
# ======================================== 导入相关模块 =========================================
import time
import micropython
from machine import I2C, Pin
from bmp280 import BMP280
# ======================================== 全局变量 ============================================
# BMP280 芯片 ID 期望值(0x60 表示 BMP280)
BMP280_EXPECTED_ID = micropython.const(0x60)
# 芯片 ID 寄存器地址
REG_CHIP_ID = micropython.const(0xD0)
# BMP280 可能的 I2C 地址列表
BMP280_ADDRS = (0x76, 0x77)
# 上次打印时间戳
last_print_time = 0
# 打印间隔(ms)
print_interval = 2000
# ======================================== 功能函数 ============================================
def print_raw_data():
"""打印原始 ADC 数据(低频,自动执行)"""
# 读取原始温度和气压 ADC 值
raw_temp, raw_press = sensor.read_raw()
print("Raw temp: %d, Raw press: %d" % (raw_temp, raw_press))
def test_reset():
"""软复位传感器(模式切换,注释调用,可 REPL 手动触发)"""
# 发送软复位命令,传感器重启后需重新初始化
sensor.reset()
print("Sensor reset done")
def test_boundary_and_exception():
"""边界与异常参数测试(初始化后调用一次)"""
# 异常参数:addr 超出合法范围
try:
BMP280(i2c, addr=0xFF)
print("ERROR: should have raised ValueError")
except ValueError as e:
print("Expected ValueError (addr out of range): %s" % str(e))
# 异常参数:addr 类型错误
try:
BMP280(i2c, addr="bad")
print("ERROR: should have raised ValueError")
except ValueError as e:
print("Expected ValueError (addr wrong type): %s" % str(e))
# 异常参数:i2c 非法对象
try:
BMP280("not_i2c")
print("ERROR: should have raised ValueError")
except ValueError as e:
print("Expected ValueError (invalid i2c): %s" % str(e))
# 边界参数:addr 最大合法值 0x7F
try:
tmp = BMP280(i2c, addr=0x7F)
print("Boundary addr=0x7F accepted")
except Exception as e:
print("Boundary addr=0x7F raised: %s" % str(e))
# ======================================== 自定义类 ============================================
# ======================================== 初始化配置 ==========================================
# 等待硬件上电稳定
time.sleep(3)
print("FreakStudio: Testing BMP280 driver")
# 初始化 I2C 总线
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
# 扫描 I2C 总线
print("START I2C SCANNER")
devices_list = i2c.scan()
# 若总线为空则直接抛出异常终止程序
if len(devices_list) == 0:
raise RuntimeError("No I2C device found")
print("I2C devices found: %d" % len(devices_list))
# 遍历设备列表,查找 BMP280 地址
sensor_addr = None
for device in devices_list:
print("I2C hexadecimal address: %s" % hex(device))
if device in BMP280_ADDRS:
sensor_addr = device
# 未找到目标地址则抛出异常
if sensor_addr is None:
raise RuntimeError("Device not found at expected address")
print("BMP280 found at address: %s" % hex(sensor_addr))
# 读取芯片 ID 寄存器验证设备型号
_id_buf = bytearray(1)
i2c.readfrom_mem_into(sensor_addr, REG_CHIP_ID, _id_buf)
if _id_buf[0] == BMP280_EXPECTED_ID:
print("Device found: chip_id=0x%02X" % _id_buf[0])
else:
print("Device not found: unexpected chip_id=0x%02X" % _id_buf[0])
# 实例化 BMP280 驱动,开启调试日志
sensor = BMP280(i2c, addr=sensor_addr, debug=True)
# 执行边界与异常参数测试
test_boundary_and_exception()
# 记录初始时间戳
last_print_time = time.ticks_ms()
# ======================================== 主程序 ===========================================
try:
while True:
current_time = time.ticks_ms()
# 每隔 print_interval ms 执行一次低频数据采集
if time.ticks_diff(current_time, last_print_time) >= print_interval:
# 读取补偿后温度
temp = sensor.get_temp()
print("Temperature: %.2f C" % temp)
# 读取补偿后气压
press = sensor.get_pressure()
print("Pressure: %.2f Pa" % press)
# 读取原始 ADC 值
print_raw_data()
# 更新时间戳
last_print_time = current_time
# test_reset() # 软复位,注释默认执行,可 REPL 手动触发
# 主循环节拍
time.sleep_ms(10)
except KeyboardInterrupt:
print("Program interrupted by user")
except OSError as e:
print("Hardware communication error: %s" % str(e))
except Exception as e:
print("Unknown error: %s" % str(e))
finally:
print("Cleaning up resources...")
# 将传感器置于睡眠模式
sensor.deinit()
# 释放驱动对象
del sensor
print("Program exited")
注意事项
| 类别 | 说明 |
|---|---|
| 工作电压 | 1.8V–3.6V,不可接 5V |
| 温度测量范围 | -40℃ ~ +85℃ |
| 气压测量范围 | 300 hPa ~ 1100 hPa |
| I2C 地址 | SDO 接 GND → 0x76;SDO 接 VCC → 0x77 |
| 初始化顺序 | 必须先创建 I2C 实例再传入驱动 |
| 软复位后 | 需重新实例化 BMP280 对象以重新加载校准系数 |
| 兼容性 | 不兼容 BME280(BME280 多湿度通道,寄存器布局不同) |
版本记录
| 版本号 | 日期 | 作者 | 修改说明 |
|---|---|---|---|
| v1.0.0 | 2026-04-24 | leezisheng | 初始版本,支持温度/气压读取、软复位、依赖注入 |
联系方式
- GitHub:leezisheng
许可协议
MIT License
Copyright (c) 2026 leezisheng
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.