✔零知开源是一个真正属于国人自己的开源软硬件平台,在开发效率上超越了Arduino平台并且更加容易上手,大大降低了开发难度。零知开源在软件方面提供了完整的学习教程和丰富示例代码,让不懂程序的工程师也能非常轻而易举的搭建电路来创作产品,测试产品。快来动手试试吧!
项目概述
本项目基于STM32F407VET6零知增强板,实现SHT41高精度温湿度传感器的驱动和数据采集。SHT41是Sensirion推出的新一代数字温湿度传感器,具有±0.2°C的温度精度和±1.8%RH的湿度精度,采用I2C接口通信。本教程将展示如何通过STM32F407的硬件I2C接口驱动SHT41,并实现实时温湿度数据的采集与显示。
一、硬件连接1.1 硬件清单
>主控芯片:STM32F407VET6零知增强板
>传感器:SHT41温湿度传感器 (I2C地址:0x44)
>杜邦线若干
1.2 接线硬件表| SHT41引脚 | 零知增强板引脚 | 功能说明 | | VCC | 3.3V | 电源正极 | | GND | GND | 电源地 | | SCL | 21/SCL | I2C时钟线 | | SDA | 20/SDA | I2C数据线 |
注意:I2C总线需要4.7kΩ上拉电阻(开发板通常已内置)
1.3 接线实物图
二、软件环境配置2.1 安装开发工具
2.2 依赖库
- Adafruit SHT4x Library
- Adafruit_I2CDevice
- Adafruit BusIO
三、核心代码实现3.1 零知IDE代码驱动
- #include "Adafruit_SHT4x.h"
- Adafruit_SHT4x sht4 = Adafruit_SHT4x();
- void setup() {
- Serial.begin(115200);
- while (!Serial) delay(10); // 等待串口初始化
- // 初始化传感器
- if (!sht4.begin()) {
- Serial.println("Sensor not found!");
- while (1) delay(1);
- }
-
- // 设置高精度模式
- sht4.setPrecision(SHT4X_HIGH_PRECISION);
- // 关闭加热器(节能模式)
- sht4.setHeater(SHT4X_NO_HEATER);
- }
- void loop() {
- sensors_event_t humidity, temp;
- sht4.getEvent(&humidity, &temp); // 读取数据
- Serial.print("Temperature: ");
- Serial.print(temp.temperature);
- Serial.println(" ℃");
-
- Serial.print("Humidity: ");
- Serial.print(humidity.relative_humidity);
- Serial.println("% rH");
- delay(1000); // 每秒读取一次
- }
复制代码
3.2 核心库函数解析 3.2.1 Adafruit_SHT4x库
- bool Adafruit_SHT4x::begin(TwoWire *theWire) {
- i2c_dev = new Adafruit_I2CDevice(SHT4x_DEFAULT_ADDR, theWire);
- if (!i2c_dev->begin()) return false;
- return reset(); // 发送复位命令
- }
复制代码
- 功能:初始化I2C通信并复位传感器
- 参数:I2C接口指针(默认使用Wire)
- 返回值:初始化成功返回true,失败返回false
- bool Adafruit_SHT4x::getEvent(sensors_event_t *humidity,
- sensors_event_t *temp) {
- // 根据精度和加热器设置选择命令
- uint8_t cmd = SHT4x_NOHEAT_HIGHPRECISION;
- uint16_t duration = 10;
-
- // 发送测量命令
- if (!i2c_dev->write(&cmd, 1)) return false;
-
- delay(duration); // 等待测量完成
-
- // 读取6字节数据(温度+CRC, 湿度+CRC)
- uint8_t readbuffer[6];
- if (!i2c_dev->read(readbuffer, 6)) return false;
-
- // CRC校验
- if (readbuffer[2] != crc8(readbuffer, 2) ||
- readbuffer[5] != crc8(readbuffer + 3, 2))
- return false;
-
- // 原始数据转换
- uint16_t temp_raw = (readbuffer[0] << 8) | readbuffer[1];
- uint16_t hum_raw = (readbuffer[3] << 8) | readbuffer[4];
-
- // 转换为实际值
- _temperature = -45 + 175 * temp_raw / 65535.0;
- _humidity = -6 + 125 * hum_raw / 65535.0;
-
- // 填充传感器事件
- if (temp) fillTempEvent(temp, millis());
- if (humidity) fillHumidityEvent(humidity, millis());
- return true;
- }
复制代码
- 功能:获取温湿度数据并填充到事件结构体
- 参数:指向温湿度事件结构体的指针
- 返回值:成功返回true,失败返回false
- static uint8_t crc8(const uint8_t *data, int len) {
- const uint8_t POLYNOMIAL(0x31);
- uint8_t crc(0xFF);
-
- for (int j = len; j; --j) {
- crc ^= *data++;
- for (int i = 8; i; --i) {
- crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1);
- }
- }
- return crc;
- }
复制代码
- 功能:计算CRC8校验码
- 参数:数据指针和长度
- 返回值:CRC8校验值
3.2.2 Adafruit_Sensor库
- void Adafruit_SHT4x::fillTempEvent(sensors_event_t *temp, uint32_t timestamp) {
- memset(temp, 0, sizeof(sensors_event_t));
- temp->version = sizeof(sensors_event_t);
- temp->sensor_id = _sensorid_temp;
- temp->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
- temp->timestamp = timestamp;
- temp->temperature = _temperature;
- }
复制代码
temp:目标结构体指针
timestamp:时间戳
- void Adafruit_SHT4x::fillHumidityEvent(sensors_event_t *humidity,
- uint32_t timestamp) {
- memset(humidity, 0, sizeof(sensors_event_t));
- humidity->version = sizeof(sensors_event_t);
- humidity->sensor_id = _sensorid_humidity;
- humidity->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
- humidity->timestamp = timestamp;
- humidity->relative_humidity = _humidity;
- }
复制代码
humidity:目标结构体指针
timestamp:时间戳
3.2.3 Adafruit_I2CDevice库
- bool Adafruit_I2CDevice::write_then_read(const uint8_t *write_buffer,
- size_t write_len,
- uint8_t *read_buffer,
- size_t read_len,
- bool stop) {
- if (!write(write_buffer, write_len, stop)) {
- return false;
- }
- return read(read_buffer, read_len);
- }
复制代码
功能:先写后读的I2C操作(常用模式)
参数:
- write_buffer:写入数据缓冲区
- write_len:写入数据长度
- read_buffer:读取数据缓冲区
- read_len:读取数据长度
- stop:是否在写操作后发送停止条件
- bool Adafruit_I2CDevice::read(uint8_t *buffer, size_t len, bool stop) {
- size_t pos = 0;
- while (pos < len) {
- size_t read_len = min(len - pos, _maxBufferSize);
- bool read_stop = (pos + read_len >= len) ? stop : false;
-
- if (!_read(buffer + pos, read_len, read_stop))
- return false;
-
- pos += read_len;
- }
- return true;
- }
复制代码
功能:从I2C设备读取数据
参数:
- buffer:数据缓冲区
- len:读取长度
- stop:是否发送停止条件
四、常见问题解答Q1:编译时报错"Adafruit_SHT4x.h: No such file or directory" A:解决方法
检查是否安装了依赖库(Adafruit BusIO)
Q2:传感器检测失败("Couldn't find SHT4x") A:排查步骤
- 检查硬件连接(VCC、GND、SCL、SDA)
- 确认I2C地址正确(SHT41默认为0x44)
- 使用I2C扫描工具确认设备地址
- 检查上拉电阻(4.7kΩ)
Q3:数据读取不稳定或CRC校验失败A:解决方案
- 降低I2C时钟速度
- 缩短I2C总线长度
- 尝试不同的精度模式
Q4:如何提高测量精度?A:优化方法
- 使用高精度模式:sht4.setPrecision(SHT4X_HIGH_PRECISION)
- 启用加热器:sht4.setHeater(SHT4X_MED_HEATER_100MS)
- 避免传感器暴露在气流中
五、结果显示成功运行后,串口监视器将输出以下格式的数据:
|
|
|
|
|
|