lingzhiLab 发表于 2025-7-9 18:48

零知开源——STM32F407VET6驱动SHT41温湿度传感器完整教程

​✔零知开源是一个真正属于国人自己的开源软硬件平台,在开发效率上超越了Arduino平台并且更加容易上手,大大降低了开发难度。零知开源在软件方面提供了完整的学习教程和丰富示例代码,让不懂程序的工程师也能非常轻而易举的搭建电路来创作产品,测试产品。快来动手试试吧!

✔访问零知开源平台,获取更多实战项目和教程资源吧!
www.lingzhilab.com

项目概述
      本项目基于STM32F407VET6零知增强板,实现SHT41高精度温湿度传感器的驱动和数据采集。SHT41是Sensirion推出的新一代数字温湿度传感器,具有±0.2°C的温度精度和±1.8%RH的湿度精度,采用I2C接口通信。本教程将展示如何通过STM32F407的硬件I2C接口驱动SHT41,并实现实时温湿度数据的采集与显示。
一、硬件连接1.1 硬件清单         >主控芯片:STM32F407VET6零知增强板
         >传感器:SHT41温湿度传感器 (I2C地址:0x44)
         >杜邦线若干

1.2 接线硬件表
SHT41引脚STM32F407VET6引脚功能说明
VCC3.3V电源正极
GNDGND电源地
SCL21/SCLI2C时钟线
SDA20/SDAI2C数据线
注意:I2C总线需要4.7kΩ上拉电阻(开发板通常已内置)
1.3 接线实物图

二、软件环境配置2.1 安装开发工具          零知IDE 3.8.0
         选择开发板零知增强板

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;
if (!i2c_dev->read(readbuffer, 6)) return false;

// CRC校验
if (readbuffer != crc8(readbuffer, 2) ||
      readbuffer != crc8(readbuffer + 3, 2))
    return false;

// 原始数据转换
uint16_t temp_raw = (readbuffer << 8) | readbuffer;
uint16_t hum_raw = (readbuffer << 8) | readbuffer;

// 转换为实际值
_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)
[*]避免传感器暴露在气流中

五、结果显示成功运行后,串口监视器将输出以下格式的数据:


资源链接
      SHT41数据手册
      STM32F407VET6参考手册
      Adafruit SHT4x库GitHub


通过本教程,开发者可以快速构建稳定可靠的环境监测系统,点击了解更多零知开发教程:
零知实验室--零知开源




页: [1]
查看完整版本: 零知开源——STM32F407VET6驱动SHT41温湿度传感器完整教程