SHT31数字温湿度传感器模块 I2C GY-SHT31-D环境检测

一、简介

SHT31数字温湿度传感器模块是新一代温湿度传感器,可以宽电压2.4~5.5V供电,采用IIC通讯,通信速度高达1MHz,湿度精确度为±2 %RH ,温度精确度为0.3 ℃,工作电流为800µA 。
在这里插入图片描述

原理框图
名称 描述
VCC 电源2.4~5.5V
GND 接地
SCL IIC串行时钟线/SPI串行时钟端口
SDA IIC串行数据线/SPI串行数据输入

二、使用前准备

在这里插入图片描述

SHT31数字温湿度传感器模块 I2C GY-SHT31-D环境检测

在这里插入图片描述

原装 uno r3开发板 编程 Atmega328P AVR 8位单片机

在这里插入图片描述

USB2.0打印机数据线高速方口连接转接线 A公对B公

在这里插入图片描述

杜邦线
SHT31数字温湿度传感器模块 I2C GY-SHT31-D环境检测 1个
原装 uno r3开发板 编程 Atmega328P AVR 8位单片机 1个
USB2.0打印机数据线高速方口连接转接线 A公对B公 1条
杜邦线 若干

三、 测试方法

用USB2.0打印机数据线高速方口连接转接线A公对B公与原装 uno r3开发板 编程 Atmega328P AVR 8位单片机连接在一起。原装 uno r3开发板 编程 Atmega328P AVR 8位单片机和SHT31数字温湿度传感器模块连接,如下图所示在这里插入图片描述

5V —— VCC
GND —— GND
SCL —— SCL
SDA —— SDA
安装Arduino IDE,打开ArduinoIDE,点击【项目】再点击【加载库】,在库管理器搜索SHT31,然后点击【安装】,如下图所示在这里插入图片描述
在这里插入图片描述
点击【文件】,点击【示例】,再点击【Adafruit SHT31 Library】和【SHT31test】,最后点击【上传】,如下图所示
在这里插入图片描述
在这里插入图片描述
代码如下

/*************************************************** 
  This is an example for the SHT31-D Humidity & Temp Sensor

  Designed specifically to work with the SHT31-D sensor from Adafruit
  ----> https://www.adafruit.com/products/2857

  These sensors use I2C to communicate, 2 pins are required to  
  interface
 ****************************************************/
 
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"

bool enableHeater = false;
uint8_t loopCnt = 0;

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);
  }

  Serial.print("Heater Enabled State: ");
  if (sht31.isHeaterEnabled())
    Serial.println("ENABLED");
  else
    Serial.println("DISABLED");
}


void loop() {
  float t = sht31.readTemperature();
  float h = sht31.readHumidity();

  if (! isnan(t)) {  // check if 'is not a number'
    Serial.print("Temp *C = "); Serial.print(t); Serial.print("\t\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");
  }

  delay(1000);

  // Toggle heater enabled state every 30 seconds
  // An ~3.0 degC temperature increase can be noted when heater is enabled
  if (loopCnt >= 30) {
    enableHeater = !enableHeater;
    sht31.heater(enableHeater);
    Serial.print("Heater Enabled State: ");
    if (sht31.isHeaterEnabled())
      Serial.println("ENABLED");
    else
      Serial.println("DISABLED");

    loopCnt = 0;
  }
  loopCnt++;
}

四、实验现象

在这里插入图片描述

程序下载进去之后,显示温度30℃,湿度47%RH。

五、总结

波特率要选择正确,否则不会有有读数显示出来的。

猜你喜欢

转载自blog.csdn.net/qq_42250136/article/details/124156669