摘要:本文介绍物联网项目设计时常用的传感器之一——温湿度传感器。以SHT30传感器为例。

在阿里云物联网HaaS开发案例中,有一个全自动加湿器,使用的是SHT30温湿度传感器。就是下面这个案例。

该传感器的外观有很多种,最常见的是这种电路板式的。

还有这种带有外壳的,核心的器件都是相同的。驱动也是相同的。

供电电压是2.4V到5.5V,所以常见的arduino控制器以及ESP32等都是可以用的。

引脚定义如下图所示。通信协议选择IIC协议。

通信时序图如下所示。

下面用arduino来测试一下,具体连线如下图所示,供电采用3.3V。用5V也没关系,不会烧掉芯片。请注意用UNO,这个板子上没有专用的SCL SDA接线柱,然后它是用的A4复用为SDA,A5复用为SCL。

arduino版本

驱动直接下载库

编程的源代码如下所示。
- //本程序是用UNO连接温湿度传感器SHT30
- //供电用3.3V
- //传感器的SCL连接UNO的A5
- //传感器的SDA连接UNO的A4
- //串口波特率是9600
- //温湿度传感器的库,选择Adafruit_SHT31
-
- #include
- #include
- #include "Adafruit_SHT31.h"
-
-
- Adafruit_SHT31 sht31 = Adafruit_SHT31();
-
- void setup() {
- Serial.begin(9600);
-
-
- while (!Serial)
- delay(10); // will pause Zero, Leonardo, etc until serial console opens
-
- Serial.println("SHT31 test");
- if (! sht31.begin(0x44)) { // Set to 0x45 for alternate i2c addr
- Serial.println("Couldn't find SHT31");
- while (1) delay(1);
- }
- }
-
- void loop() {
- float t = sht31.readTemperature();
- float h = sht31.readHumidity();
-
- if (! isnan(t)) { // check if 'is not a number'
- Serial.print("Temp *C = "); Serial.println(t);
-
- } else {
- Serial.println("Failed to read temperature");
-
- }
-
- if (! isnan(h)) { // check if 'is not a number'
- Serial.print("Hum. % = "); Serial.println(h);
-
- } else {
- Serial.println("Failed to read humidity");
-
- }
- Serial.println();
- delay(1000);
- }
使用arduino自带的串口显示数据

使用arduino调试,读取出来正确的温湿度之后,这些数据就可以作为参考,下一步使用ESP32来调试,并且将温湿度上传到阿里云物联网平台上。

敬请期待。