特别感谢文章作者:无垠的广袤 / 李金磊,辛苦制作的教程,非常适合新人及树莓派爱好者学习使用!
本文介绍了树莓派 CM0 Dev kit 单板计算机结合 SPI 通信协议 驱动 LCD 彩色显示屏、结合 IIC 协议驱动 AHT20 和 BMP280 传感器,进而实现环境状态实时监测的项目设计。本次使用的系统是 EDATEC Industrial OS,如果需要使用这个系统可以查看下面这个文章。系统异常断电导致树莓派“变砖”?这个问题终于被我解决了!
项目介绍
项目包括驱动 LCD 彩屏、驱动 AHT20 & BMP280 模块、综合搭建环境监测仪三部分。
LCD 彩屏:模块介绍、硬件连接、环境搭建、关键代码、效果演示;
AHT20 和 BMP20:模块介绍、硬件连接、软件包部署、代码、效果演示;
环境监测仪:硬件连接、工程代码、效果演示;
LCD 彩屏
结合 SPI 接口驱动 ST7735 协议的 LCD 彩色显示屏。
LCD 模块
使用 OpenMV LCD 彩色屏幕模块,基于 ST7735R 通信协议。
引脚定义
LCD 模块:https://openmv.io/products/lcd-shield
驱动库:
https://github.com/adafruit/Adafruit_CircuitPython_RGB_Display/
语法:
https://docs.circuitpython.org/projects/rgb_display/en/latest/api.html
驱动源码:
https://docs.circuitpython.org/projects/rgb_display/en/latest/_modules/adafruit_rgb_display/st7735.html#ST7735R.init
硬件连接
详见:
https://learn.adafruit.com/adafruit-1-14-240x135-color-tft-breakout/python-wiring-and-setup
树莓派40Pin引脚定义:https://pinout.xyz/
安装软件包
终端执行
python3 -m venv .venvsource .venv/bin/activatesudo apt-get install python3 python3-pip python3-pil libjpeg-dev zlib1g-dev libfreetype6-dev liblcms2-dev libopenjp2-7 libtiff6 -ypip install psutil luma.lcd st7735
详见:https://pypi.org/project/adafruit-circuitpython-rgb-display/
代码
终端执行执行指令 touch lcd_display.py 新建程序文件,并添加如下代码
from luma.core.interface.serial import spifrom luma.lcd.device import st7735from luma.core.render import canvasfrom PIL import ImageFontimport timeimport osimport psutil# 初始化 SPI 接口serial = spi(port=0,device=0,gpio_DC=24,gpio_RST=25,gpio_LIGHT=18)# 初始化 ST7735 屏幕device = st7735(serial,width=160,height=128,rotate=0,h_offset=0,v_offset=0)# 字体try:font_large = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 18)font_small = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 14)except OSError:font_large = ImageFont.load_default()font_small = font_large# 主循环try:while True:with canvas(device) as draw:# 设置背景颜色为灰色draw.rectangle(device.bounding_box, fill="gray")# 绘制文本内容draw.text((10, 10), "Raspberry Pi", fill="black", font=font_large)draw.text((50, 40), "CM0", fill="red", font=font_large)draw.text((10, 70), "Test ST7735R", fill="blue", font=font_large)time.sleep(2)except KeyboardInterrupt:passfinally:print("Exiting....")
保存代码。
效果
终端执行指令 python lcd_diaplay.py 运行程序;
LCD 显示彩色文字
AHT20 & BMP280
使用 IIC 协议驱动 AHT20 温湿度传感器和 BMP280 气压传感器,并终端打印数据。
模块
该模块由奥松数字温湿度传感器 AHT20 与数字气压传感器 BMP280 组成。
直流 3.3V 供电,体积小巧,便于集成。
两个传感器的通信引脚接至同一 IIC 总线, 板载 4.7K 上拉电阻。
参数特点
AHT20
BMP280
硬件连接
安装软件包
终端执行如下指令,安装所需软件包
sudo apt install python3-smbus2 python3-smbus
终端执行指令 i2cdetect -y 1 获取传感器地址
其中 0x38 对应 aht20,0x77 对应 bmp280 .
代码
终端执行指令 touch aht20_bmp280_print.py 新建程序文件,并添加如下代码
from aht20 import AHT20from bmp280 import BMP280import timetry:while True:sensor = AHT20(bus=1, address=0x38)temp, hum = sensor.read()bmp280 = BMP280(bus=1, address=0x77)temperature, pressure = bmp280.get_temperature_and_pressure()print("Temperature: {:.2f} °C Humidity: {:.2f} %RH Pressure: {:.3f} kPa".format(temp, hum, pressure/1000))time.sleep(2)except KeyboardInterrupt:passfinally:print("Exiting....")
保存代码。
驱动文件详见:上海晶珩睿莓 1 单板计算机 - 物联网环境监测终端.
https://blog.csdn.net/qq_36654593/article/details/156518435
效果
终端执行指令 python aht20_bmp280_print.py 运行程序;
终端连续打印温湿度和气压数据;
环境监测仪
结合前面驱动 AHT20 和 BMP280 模块的程序以及 LCD 屏幕驱动程序,实现 LCD 显示实时温湿度和气压数据的环境监测仪。
硬件连接
保持 LCD 屏幕和树莓派 CM0 连接。
保持 AHT20&BMP280 模块和树莓派 CM0 连接。
代码
终端执行执行指令 touch lcd_aht20_bmp280.py 新建程序文件,并添加如下代码
from luma.core.interface.serial import spifrom luma.lcd.device import st7735from luma.core.render import canvasfrom PIL import ImageFontimport timeimport osimport psutilfrom aht20 import AHT20from bmp280 import BMP280# ========== 初始化 LCD 屏幕 ==========serial = spi(port=0,device=0,gpio_DC=24,gpio_RST=25,gpio_LIGHT=18)device = st7735(serial,width=160,height=128,rotate=0,h_offset=0,v_offset=0)# ========== 初始化传感器 ==========aht20 = AHT20(bus=1, address=0x38)bmp280 = BMP280(bus=1, address=0x77)# ========== 加载字体 ==========try:font_title = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 14)font_data = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 16)font_small = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 10)except OSError:font_title = ImageFont.load_default()font_data = font_titlefont_small = font_title# ========== 主程序 ==========try:print("环境监测仪启动...")print("按 Ctrl+C 停止程序")while True:# 读取传感器数据temp_aht, humidity = aht20.read()temp_bmp, pressure = bmp280.get_temperature_and_pressure()avg_temp = (temp_aht + temp_bmp) / 2 # 计算平均温度pressure_kpa = pressure / 1000 # 转换气压 kPaprint("Temp: {:.1f}°C | Humi: {:.1f}% | ATM: {:.2f} kPa".format(avg_temp, humidity, pressure_kpa)) # 控制台输出# LCD 显示with canvas(device) as draw:draw.rectangle(device.bounding_box, fill="black") # 黑色背景# 标题栏 - 深灰色背景draw.rectangle((0, 0, 160, 20), fill="#333333")draw.text((3, 3), "Environmental Monitor", fill="white", font=font_title)# 分隔线draw.line((10, 22, 150, 22), fill="gray", width=1)# 温度显示 - 橙色 (第1行)draw.text((10, 28), "Temp:", fill="#FFA500", font=font_data)draw.text((70, 28), "{:.1f}°C".format(avg_temp), fill="white", font=font_data)# 湿度显示 - 青色 (第2行)draw.text((10, 50), "Humi:", fill="#00CED1", font=font_data)draw.text((70, 50), "{:.1f}%".format(humidity), fill="white", font=font_data)# 气压显示 - 绿色 (第3行)draw.text((10, 72), "ATM:", fill="#90EE90", font=font_data)draw.text((70, 72), "{:.2f}".format(pressure_kpa), fill="white", font=font_data)draw.text((135, 75), "kPa", fill="#90EE90", font=font_small)# 底部状态栏draw.line((10, 100, 150, 100), fill="gray", width=1)draw.text((25, 105), "Raspberry Pi CM0", fill="gray", font=font_small)# 更新时间指示draw.text((120, 105), "Live", fill="red", font=font_small)time.sleep(2) # 每2秒更新一次except KeyboardInterrupt:print("n程序中断")except Exception as e:print(f"n发生错误: {e}")finally:# 清屏显示退出信息try:with canvas(device) as draw:draw.rectangle(device.bounding_box, fill="black")draw.text((30, 50), "System Stopped", fill="white", font=font_title)except:passprint("程序已退出")
保存代码。
效果
终端执行指令 python lcd_aht20_bmp280.py 运行程序;
LCD 显示实时温湿度和气压数据,同时终端连续打印温湿度和气压数据;
总结
本文介绍了树莓派 CM0 Dev 单板计算机结合 SPI 通信协议驱动 LCD 彩色显示屏、结合 IIC 协议驱动 AHT20 和 BMP280 传感器,进而实现环境状态实时监测的项目设计,为相关产品在工业物联网领域的快速开发和应用设计提供了参考。
官方网站:https://edatec.cn/zh/cm0
淘宝店铺:https://edatec.taobao.com/
180