传感器虚拟实验:DHT22湿温度传感器

作者: liufeisheng

创建时间: 2024-11-25 09:29:18


  • 完成温湿度传感器虚拟实验

    • 实验目标:

    • 实验步骤:

      1. 认真观察实验过程

      2. 查看实验文档,了解DHT22ATTINY85元件分别有什么用途

      3. 登录https://wokwi.com/projects/,创建新项目 。 根据实验指导,完成电路的设计和连接。

      4. 运行实验,观察实验结果。

      5. 分析实验结果,总结电路原理和电子元件的作用。

运行效果如下

接线图

image.png

完成部分功能

初始化显示内容

代码如下

#include <dht.h>
#include <TinyWireM.h>
#include <Tiny4kOLED.h>

#define DHT22_PIN PB1

const unsigned char  img_heart_small[] PROGMEM = {
  0x00, 0x00, 0xc0, 0xe0, 0xe0, 0xe0, 0xc0, 0x80, 0x80, 0x80, 0xc0, 0xe0, 0xe0, 0xe0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00, 0x00, 0x00
};

const unsigned char  img_heart_big[] PROGMEM = {
  0xe0, 0xf0, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0xe0, 0xe0, 0xe0, 0xf0, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0xe0, 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00
};

const unsigned char  img_thermometer[] PROGMEM = {
  0x00, 0xfe, 0x03, 0xfe, 0x50,
  0x00, 0xff, 0x00, 0xff, 0x55,
  0x60, 0x9f, 0x80, 0x9f, 0x65,
};

dht DHT;

void splash() {
  oled.clear();
  oled.setCursor(15, 1);
  oled.print(F("ATtiny85+SSD1306"));

  oled.setCursor(42, 3);
  oled.print(F("Example"));

  oled.setCursor(35, 7);
  oled.print(F("wokwi.com"));
}

void setup(){
  pinMode(DHT22_PIN, INPUT);

  oled.begin(128,64,sizeof(tiny4koled_init_128x64br),tiny4koled_init_128x64br);
  oled.setFont(FONT6X8);
  oled.clear();
  oled.on();

  splash();
  delay(3000);
  // prepareDisplay();
}

void loop(){

}

image.png

实验代码

#include <dht.h>
#include <TinyWireM.h>
#include <Tiny4kOLED.h>

#define DHT22_PIN PB1

const unsigned char  img_heart_small[] PROGMEM = {
  0x00, 0x00, 0xc0, 0xe0, 0xe0, 0xe0, 0xc0, 0x80, 0x80, 0x80, 0xc0, 0xe0, 0xe0, 0xe0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00, 0x00, 0x00
};

const unsigned char  img_heart_big[] PROGMEM = {
  0xe0, 0xf0, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0xe0, 0xe0, 0xe0, 0xf0, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0xe0, 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00
};

const unsigned char  img_thermometer[] PROGMEM = {
  0x00, 0xfe, 0x03, 0xfe, 0x50,
  0x00, 0xff, 0x00, 0xff, 0x55,
  0x60, 0x9f, 0x80, 0x9f, 0x65,
};

dht DHT;

void splash() {
  oled.clear();
  oled.setCursor(15, 1);
  oled.print(F("ATtiny85+SSD1306"));

  oled.setCursor(42, 3);
  oled.print(F("Example"));

  oled.setCursor(35, 7);
  oled.print(F("wokwi.com"));
}

void heartBeat(){
  static char big = 1;
  static long startTime = 0;
  long currentTime;

  currentTime = millis();

  if((currentTime - startTime)>200){
    startTime = currentTime;
    big = 1- big;

    if(big){
      oled.bitmap(20,4,37,6,img_heart_big);      
    }else
      oled.bitmap(20,4,37,6,img_heart_small);   
  }
}

void prepareDisplay(){
  unsigned int i,k;
  unsigned char ch[5];

  oled.clear();
  oled.begin();

  oled.setCursor(20,1);
  oled.print(F("ATtiny85+SSD1306"));
  oled.setCursor(3,2);
  oled.print(F("temperature|humidity"));

  oled.bitmap(105,4,110,7,img_thermometer);
  oled.setCursor(57,4);
  oled.print(F("24.0℃"));
  oled.setCursor(57,5);
  oled.print(F("40.0%"));

  oled.bitmap(10,5,17,2,img_heart_small);
}

float getTemperature(){
  return DHT.temperature;
}

float getHumidity(){
  return DHT.humidity;
}

void setup(){
  pinMode(DHT22_PIN, INPUT);

  oled.begin(128,64,sizeof(tiny4koled_init_128x64br),tiny4koled_init_128x64br);

  oled.setFont(FONT6X8);

  oled.clear();
  oled.on();

  splash();


  delay(3000);
  prepareDisplay();
}


void loop(){
  static long startTime = 0;
  long currentTime;


  DHT.read22(DHT22_PIN);

  currentTime = millis();

  if((currentTime-startTime)>1000){
    startTime = currentTime ;

    float temperature = getTemperature();

    oled.setCursor(57,4);

    oled.print(temperature,1);
    oled.print("℃");

    float humidity = getHumidity();

    oled.setCursor(57,5);

    oled.print(humidity,1);
    oled.print("% ");

    oled.bitmap(105,4,110,7,img_thermometer);

    oled.setCursor(20,7);
    oled.print("Liu Feisheng ");

    heartBeat();
  }
}

完成实验报告填写

要求:1.完成以上接线

2.在屏幕的最后一行打上自己的名字拼音

扩展练习:

  1. 加上温度超过一定值时,红灯闪烁功能

扩展练习-加红灯闪烁功能-接线图

image.png

扩展练习代码

#include <dht.h>
#include <TinyWireM.h>
#include <Tiny4kOLED.h>

#define DHT22_PIN PB1
#define LED PB3

const unsigned char  img_heart_small[] PROGMEM = {
  0x00, 0x00, 0xc0, 0xe0, 0xe0, 0xe0, 0xc0, 0x80, 0x80, 0x80, 0xc0, 0xe0, 0xe0, 0xe0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00, 0x00, 0x00
};

const unsigned char  img_heart_big[] PROGMEM = {
  0xe0, 0xf0, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0xe0, 0xe0, 0xe0, 0xf0, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0xe0, 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00
};

const unsigned char  img_thermometer[] PROGMEM = {
  0x00, 0xfe, 0x03, 0xfe, 0x50,
  0x00, 0xff, 0x00, 0xff, 0x55,
  0x60, 0x9f, 0x80, 0x9f, 0x65,
};

dht DHT;

void splash() {
  oled.clear();
  oled.setCursor(15, 1);
  oled.print(F("ATtiny85+SSD1306"));

  oled.setCursor(42, 3);
  oled.print(F("Example"));

  oled.setCursor(35, 7);
  oled.print(F("wokwi.com"));
}

void heartBeat(){
  static char big = 1;
  static long startTime = 0;
  long currentTime;

  currentTime = millis();

  if((currentTime - startTime)>200){
    startTime = currentTime;
    big = 1- big;

    if(big){
      oled.bitmap(20,4,37,6,img_heart_big);      
    }else
      oled.bitmap(20,4,37,6,img_heart_small);   
  }
}

void prepareDisplay(){
  unsigned int i,k;
  unsigned char ch[5];

  oled.clear();
  oled.begin();

  oled.setCursor(20,1);
  oled.print(F("ATtiny85+SSD1306"));
  oled.setCursor(3,2);
  oled.print(F("temperature|humidity"));

  oled.bitmap(105,4,110,7,img_thermometer);
  oled.setCursor(57,4);
  oled.print(F("24.0℃"));
  oled.setCursor(57,5);
  oled.print(F("40.0%"));

  oled.bitmap(10,5,17,2,img_heart_small);
}

float getTemperature(){
  return DHT.temperature;
}

float getHumidity(){
  return DHT.humidity;
}

void setup(){
  pinMode(DHT22_PIN, INPUT);
  pinMode(LED, OUTPUT);

  oled.begin(128,64,sizeof(tiny4koled_init_128x64br),tiny4koled_init_128x64br);

  oled.setFont(FONT6X8);

  oled.clear();
  oled.on();

  splash();


  delay(3000);
  prepareDisplay();
}


void loop(){
  static long startTime = 0;
  long currentTime;


  DHT.read22(DHT22_PIN);

  currentTime = millis();

  if((currentTime-startTime)>1000){
    startTime = currentTime ;

    float temperature = getTemperature();

    oled.setCursor(57,4);

    oled.print(temperature,1);
    oled.print("℃");

    if(temperature>40){
      digitalWrite(LED, HIGH); 
    }
    delay(500);
    digitalWrite(LED, LOW);
    delay(500);

    float humidity = getHumidity();

    oled.setCursor(57,5);

    oled.print(humidity,1);
    oled.print("% ");

    oled.bitmap(105,4,110,7,img_thermometer);

    oled.setCursor(20,7);
    oled.print("Liu Feisheng ");

    heartBeat();
  }





}

扩展练习2:更换芯片和LCD显示屏

接线图

image.png

代码

#include <dht.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);

#define DHT22_PIN A0
#define LED 2


dht DHT;


float getTemperature(){
  return DHT.temperature;
}

float getHumidity(){
  return DHT.humidity;
}



void setup(){
  pinMode(DHT22_PIN, INPUT);
  pinMode(LED, OUTPUT);

  Serial.begin(9600);
  lcd.init();
  lcd.backlight();

  lcd.clear();
  lcd.setCursor(1, 0);
  lcd.print("ATtiny85");

  lcd.setCursor(1, 2);
  lcd.print("Welcome!");
  delay(3000);

}

void loop(){
  static long startTime = 0;
  long currentTime;

  DHT.read22(DHT22_PIN);

  currentTime = millis();

  if((currentTime - startTime )>1000){
    startTime = currentTime;

    float temperature = DHT.temperature;

    if(temperature>40){
      digitalWrite(LED, HIGH);  
      // 如果温度高于40,红灯闪烁报警
    }
    String temperature1 = (String)temperature;
    lcd.setCursor(1,1);
    lcd.print("temperature:");

    lcd.print(temperature1);
    lcd.print(" ℃ ");

    String humidity = (String)DHT.humidity;
    lcd.setCursor(1,2);
    lcd.print("humidity:");
    lcd.print(humidity);
    lcd.print(" % ");


    lcd.setCursor(2,3);
    lcd.print("Liu Feisheng");


    delay(500);
    digitalWrite(LED, LOW);

  }


}