@[TOC](
【Arduino】温湿度传感器(DHT11)控制设计 四位数码管,LCD1602,带蜂鸣器、LED灯报警,串口输入设定阈值
)
一、实验器材
UNO控制板:1块共阳极四位数码管:1个面包板:1块面包板跳线:若干220Ω电阻:4个有源蜂鸣器:1个红色led灯-1黄色led灯-1DHT11:1个
二、连线示意图
注意:A4接LCD1602的SDA、A5接LCD1602的SCL
三、代码如下
#include <Wire.h>#include <LiquidCrystal_I2C.h>#include <dht11.h>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或0x27LiquidCrystal_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 0lcd.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 0lcd.print(humidity);lcd.setCursor(11,1); // line 1, pos 0lcd.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日
参考链接
/qq_5294/article/details/123685337?spm=1001..3001.5501
【Arduino】温湿度传感器(DHT11)控制设计 四位数码管 LCD1602 带蜂鸣器 LED灯报警 串口输入设定阈值