[Arduino资料] 零知开源——STM32F407VET6驱动SHT41温湿度传感器完整教程

[复制链接]
 楼主| lingzhiLab 发表于 2025-7-9 18:48 | 显示全部楼层 |阅读模式
​✔零知开源是一个真正属于国人自己的开源软硬件平台,在开发效率上超越了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引脚
功能说明
VCC
3.3V
电源正极
GND
GND
电源地
SCL
21/SCL
I2C时钟线
SDA
20/SDA
I2C数据线
注意:I2C总线需要4.7kΩ上拉电阻(开发板通常已内置)

1.3 接线实物图

c1700a0b1c5f4c2bb9471d8c95ef6deb.png



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

2.2 依赖库
  • Adafruit SHT4x Library
  • Adafruit_I2CDevice
  • Adafruit BusIO


三、核心代码实现3.1 零知IDE代码驱动
  1. #include "Adafruit_SHT4x.h"
  2. Adafruit_SHT4x sht4 = Adafruit_SHT4x();

  3. void setup() {
  4.   Serial.begin(115200);
  5.   while (!Serial) delay(10);  // 等待串口初始化

  6.   // 初始化传感器
  7.   if (!sht4.begin()) {
  8.     Serial.println("Sensor not found!");
  9.     while (1) delay(1);
  10.   }
  11.   
  12.   // 设置高精度模式
  13.   sht4.setPrecision(SHT4X_HIGH_PRECISION);
  14.   // 关闭加热器(节能模式)
  15.   sht4.setHeater(SHT4X_NO_HEATER);
  16. }

  17. void loop() {
  18.   sensors_event_t humidity, temp;
  19.   sht4.getEvent(&humidity, &temp);  // 读取数据

  20.   Serial.print("Temperature: ");
  21.   Serial.print(temp.temperature);
  22.   Serial.println(" ℃");
  23.   
  24.   Serial.print("Humidity: ");
  25.   Serial.print(humidity.relative_humidity);
  26.   Serial.println("% rH");
  27.   delay(1000);  // 每秒读取一次
  28. }


3.2 核心库函数解析 3.2.1 Adafruit_SHT4x库
  1. bool Adafruit_SHT4x::begin(TwoWire *theWire) {
  2.   i2c_dev = new Adafruit_I2CDevice(SHT4x_DEFAULT_ADDR, theWire);
  3.   if (!i2c_dev->begin()) return false;
  4.   return reset(); // 发送复位命令
  5. }
  • 功能:初始化I2C通信并复位传感器
  • 参数:I2C接口指针(默认使用Wire)
  • 返回值:初始化成功返回true,失败返回false


  1. bool Adafruit_SHT4x::getEvent(sensors_event_t *humidity,
  2.                               sensors_event_t *temp) {
  3.   // 根据精度和加热器设置选择命令
  4.   uint8_t cmd = SHT4x_NOHEAT_HIGHPRECISION;
  5.   uint16_t duration = 10;
  6.   
  7.   // 发送测量命令
  8.   if (!i2c_dev->write(&cmd, 1)) return false;
  9.   
  10.   delay(duration); // 等待测量完成
  11.   
  12.   // 读取6字节数据(温度+CRC, 湿度+CRC)
  13.   uint8_t readbuffer[6];
  14.   if (!i2c_dev->read(readbuffer, 6)) return false;
  15.   
  16.   // CRC校验
  17.   if (readbuffer[2] != crc8(readbuffer, 2) ||
  18.       readbuffer[5] != crc8(readbuffer + 3, 2))
  19.     return false;
  20.   
  21.   // 原始数据转换
  22.   uint16_t temp_raw = (readbuffer[0] << 8) | readbuffer[1];
  23.   uint16_t hum_raw = (readbuffer[3] << 8) | readbuffer[4];
  24.   
  25.   // 转换为实际值
  26.   _temperature = -45 + 175 * temp_raw / 65535.0;
  27.   _humidity = -6 + 125 * hum_raw / 65535.0;
  28.   
  29.   // 填充传感器事件
  30.   if (temp) fillTempEvent(temp, millis());
  31.   if (humidity) fillHumidityEvent(humidity, millis());
  32.   return true;
  33. }
  • 功能:获取温湿度数据并填充到事件结构体
  • 参数:指向温湿度事件结构体的指针
  • 返回值:成功返回true,失败返回false


  1. static uint8_t crc8(const uint8_t *data, int len) {
  2.   const uint8_t POLYNOMIAL(0x31);
  3.   uint8_t crc(0xFF);
  4.   
  5.   for (int j = len; j; --j) {
  6.     crc ^= *data++;
  7.     for (int i = 8; i; --i) {
  8.       crc = (crc & 0x80) ? (crc << 1) ^ POLYNOMIAL : (crc << 1);
  9.     }
  10.   }
  11.   return crc;
  12. }

  • 功能:计算CRC8校验码
  • 参数:数据指针和长度
  • 返回值:CRC8校验值


3.2.2 Adafruit_Sensor库
  1. void Adafruit_SHT4x::fillTempEvent(sensors_event_t *temp, uint32_t timestamp) {
  2.   memset(temp, 0, sizeof(sensors_event_t));
  3.   temp->version = sizeof(sensors_event_t);
  4.   temp->sensor_id = _sensorid_temp;
  5.   temp->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
  6.   temp->timestamp = timestamp;
  7.   temp->temperature = _temperature;
  8. }
  • 功能:填充温度事件结构体
  • 参数:
        temp:目标结构体指针
        timestamp:时间戳
  1. void Adafruit_SHT4x::fillHumidityEvent(sensors_event_t *humidity,
  2.                                       uint32_t timestamp) {
  3.   memset(humidity, 0, sizeof(sensors_event_t));
  4.   humidity->version = sizeof(sensors_event_t);
  5.   humidity->sensor_id = _sensorid_humidity;
  6.   humidity->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
  7.   humidity->timestamp = timestamp;
  8.   humidity->relative_humidity = _humidity;
  9. }
  • 功能:填充湿度事件结构体
  • 参数:
        humidity:目标结构体指针
        timestamp:时间戳

3.2.3 Adafruit_I2CDevice库

  1. bool Adafruit_I2CDevice::write_then_read(const uint8_t *write_buffer,
  2.                                         size_t write_len,
  3.                                         uint8_t *read_buffer,
  4.                                         size_t read_len,
  5.                                         bool stop) {
  6.   if (!write(write_buffer, write_len, stop)) {
  7.     return false;
  8.   }
  9.   return read(read_buffer, read_len);
  10. }
功能:先写后读的I2C操作(常用模式)
参数:
  • write_buffer:写入数据缓冲区
  • write_len:写入数据长度
  • read_buffer:读取数据缓冲区
  • read_len:读取数据长度
  • stop:是否在写操作后发送停止条件

  1. bool Adafruit_I2CDevice::read(uint8_t *buffer, size_t len, bool stop) {
  2.   size_t pos = 0;
  3.   while (pos < len) {
  4.     size_t read_len = min(len - pos, _maxBufferSize);
  5.     bool read_stop = (pos + read_len >= len) ? stop : false;
  6.    
  7.     if (!_read(buffer + pos, read_len, read_stop))
  8.       return false;
  9.    
  10.     pos += read_len;
  11.   }
  12.   return true;
  13. }
功能:从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)
  • 避免传感器暴露在气流中

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

企业微信截图_17518827746926.png
资源链接
        SHT41数据手册
        STM32F407VET6参考手册
        Adafruit SHT4x库GitHub


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




您需要登录后才可以回帖 登录 | 注册

本版积分规则

22

主题

33

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部