【Arduino】温湿度传感器(DHT11)控制设计 四位数码管,LCD1602,带蜂鸣器、LED灯报警,串口输入设定阈值

@[TOC](
【Arduino】温湿度传感器(DHT11)控制设计 四位数码管,LCD1602,带蜂鸣器、LED灯报警,串口输入设定阈值
)


一、实验器材

UNO控制板:1块
共阳极四位数码管:1个
面包板:1块
面包板跳线:若干
220Ω电阻:4个
有源蜂鸣器:1个
红色led灯-1
黄色led灯-1
DHT11:1个

二、连线示意图

【Arduino】温湿度传感器(DHT11)控制设计 四位数码管,LCD1602,带蜂鸣器、LED灯报警,串口输入设定阈值_第1张图片
【Arduino】温湿度传感器(DHT11)控制设计 四位数码管,LCD1602,带蜂鸣器、LED灯报警,串口输入设定阈值_第2张图片注意:A4接LCD1602的SDA、A5接LCD1602的SCL

三、代码如下

#include 
#include 
#include 
dht11 dht11; 
char signal;
int dhtpin = A3;//DHT11温湿度传感数据读取端口  
int buzzerPin = A0;//蜂鸣器端口
int LED1 = A2;
int LED2 = A1;
 //设置初始温度湿度
int temperatureset=22;
int humidityset=20;
//设置阴极接口
int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
int p = 9;
 //设置阳极接口
int d4 = 12;
int d3 = 13;
int d2 = 11;
int d1 = 10;
 //设置变量

float humidity;
float temperature;
int num4;
int num3;
int num2;
int num1;
// I2C地址, 一般为0x3F, 0x20或0x27
LiquidCrystal_I2C lcd(0x27,16,2);

void setup(){            
lcd.init();
lcd.backlight(); // 打开背光
 Serial.begin(9600);
 pinMode(d1, OUTPUT);
 pinMode(d2, OUTPUT);
 pinMode(d3, OUTPUT);
 pinMode(d4, OUTPUT);
 pinMode(a, OUTPUT);
 pinMode(b, OUTPUT);
 pinMode(c, OUTPUT);
 pinMode(d, OUTPUT);
 pinMode(e, OUTPUT);
 pinMode(f, OUTPUT);
 pinMode(g, OUTPUT);
 pinMode(p, OUTPUT);
 pinMode(buzzerPin,OUTPUT);
 pinMode(LED1,OUTPUT);//设置led引脚为输出引脚
 pinMode(LED2,OUTPUT);//设置led引脚为输出引脚
  lcd.setCursor(0,1); // line 1, pos 0
  lcd.print("H:     % T:");
  delay(1000);
}

void pickDigit(int x)  //定义pickDigit(x),其作用是开启dx端口
{
  digitalWrite(d1, LOW);
  digitalWrite(d2, LOW);
  digitalWrite(d3, LOW);
  digitalWrite(d4, LOW);
  
  switch(x)
  {
  case 1:
    digitalWrite(d1, HIGH);
    break;
  case 2:
    digitalWrite(d2, HIGH);
    break;
  case 3:
    digitalWrite(d3, HIGH);
    break;
  default:
    digitalWrite(d4, HIGH);
    break;
  }
}



void dispDec(int x)  //设定开启小数点
{
  digitalWrite(p, LOW);
}

void clearLEDs()  //清屏
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(p, HIGH);
}

void Num_0()  //定义数字0时阴极那些管脚开关
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void Num_1()  //定义数字1时阴极那些管脚开关
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void Num_2()  //定义数字2时阴极那些管脚开关
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}

void Num_3()  //定义数字3时阴极那些管脚开关
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}

void Num_4()  //定义数字4时阴极那些管脚开关
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void Num_5()  //定义数字5时阴极那些管脚开关
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void Num_6()  //定义数字6时阴极那些管脚开关
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void Num_7()  //定义数字7时阴极那些管脚开关
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void Num_8() //定义数字8时阴极那些管脚开关
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void Num_9()  //定义数字9时阴极那些管脚开关
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}
void dispose(int show){
  num4=show%10;
  show=show/10;
  num3=show%10;
  show=show/10;
  num2=show%10;
  show=show/10;
  num1=show%10;
}
void pickNumber(int n)   //定义pickNumber(x),其作用是显示数字n
{

 switch(n)
  {
   default:
   case 0:Num_0();
   break;
   case 1:Num_1();
   break;
   case 2:Num_2();
   break;
   case 3:Num_3();
   break;
   case 4:Num_4();
   break;
   case 5:Num_5();
   break;
   case 6:Num_6();
   break;
   case 7:Num_7();
   break;
   case 8:Num_8();
   break;
   case 9:Num_9();
   break;

  }
}
void tone1(){
  for(int i=0;i<100;i++){
    digitalWrite(buzzerPin,HIGH);
    delay(1);
    digitalWrite(buzzerPin,LOW);
    delay(3);
  }
}
void tone2(){
  for(int i=0;i<100;i++){
    digitalWrite(buzzerPin,HIGH);
    delay(6);
    digitalWrite(buzzerPin,LOW);
    delay(8);
  }
}
String inString="";
void loop()
{
  Serial.println("请输入字符'H'或'T'分别修改湿度和温度阈值:");
   if(Serial.available()>0){
    Serial.print("输入字符:");
    char signal=Serial.read();
    Serial.println(signal);
    delay(5000);
    if(signal=='H'){//修改湿度阈值
      if(Serial.available()>0){       
        int i = Serial.parseInt();  
        Serial.print("修改湿度阈值:");
        Serial.println(i);
        humidityset=i;
      }
    }
    if(signal=='T'){//修改温度阈值
      if(Serial.available()>0){
        int i = Serial.parseInt();  
        Serial.print("修改温度阈值:");
        Serial.println(i);
        temperatureset=i;
      }
     }
  }
  dht11.read(dhtpin);  // 读取 DHT11 传感器
  // 读取温度或湿度
  humidity=dht11.humidity;
  temperature=dht11.temperature;

  int show=humidity*100;
  dispose(show);
  clearLEDs();
  Serial.print("当前湿度:");
  Serial.print(humidity);
  Serial.println(" %\t");
  for(int k=2000;k>0;k--){
  clearLEDs();
  pickDigit(1);
  pickNumber(num1);
  clearLEDs();
  pickDigit(2);
  dispDec(2);
  pickNumber(num2);
  clearLEDs();
  pickDigit(3);
  pickNumber(num3);
  clearLEDs();
  pickDigit(4);
  pickNumber(num4); 
  }     
  Serial.print("当前温度:");
  Serial.print(temperature);
  Serial.println(" °C ");
  show=temperature*100;
  clearLEDs();
  dispose(show);
  for(int k=2000;k>0;k--){
  clearLEDs();
  pickDigit(1);
  pickNumber(num1);
  clearLEDs();
  pickDigit(2);
  dispDec(2);
  pickNumber(num2);
  clearLEDs();
  pickDigit(3);
  pickNumber(num3);
  clearLEDs();
  pickDigit(4);
  pickNumber(num4); 
  }      
  clearLEDs();
  if(temperature>temperatureset){
    digitalWrite(LED1,HIGH);
    tone1();
    Serial.println("温度超标,黄色报警灯亮");   //黄灯亮
  }else{
    digitalWrite(LED1,LOW);
  }
  if(humidity>humidityset){                //红灯亮
    digitalWrite(LED2,HIGH);
    tone2();
    Serial.println("湿度超标,红色报警灯亮");  
  }else{
    digitalWrite(LED2,LOW);
    }
  lcd.setCursor(0,0);
  lcd.print("S-H:  %");
  lcd.setCursor(4,0);
  lcd.print(humidityset);
  lcd.setCursor(8,0);
  lcd.print("S-T:  'C");
  lcd.setCursor(12,0);
  lcd.print(temperatureset);
  lcd.setCursor(2,1); // line 1, pos 0
  lcd.print(humidity);
  lcd.setCursor(11,1); // line 1, pos 0
  lcd.print(temperature);
//  delay(2000);
//移动
//      for (int positionCounter = 0; positionCounter < 13; positionCounter++)
//      {
//        lcd.scrollDisplayLeft();
//        delay(1000);
//      }
//      for (int positionCounter = 0; positionCounter < 29; positionCounter++)
//      {
//        lcd.scrollDisplayRight();
//        delay(1000);
//      }
//      for (int positionCounter = 0; positionCounter < 16; positionCounter++)
//      {
//        lcd.scrollDisplayLeft();
//        delay(1000);
//      }
//      //  下次移动的延迟
//      delay(10);
}

2.实物展示如下:

4月21日

参考链接
https://blog.csdn.net/qq_52201194/article/details/123685337?spm=1001.2014.3001.5501

你可能感兴趣的:(arduino,单片机)