#include <LiquidCrystal.h>
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 8
// 实例化一个名为lcd的LiquidCrysta类型对象,并初始化相关引脚
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
volatile unsigned int temp;
int dht_12_gettemperature() {
// 得到DHT11检测到的温度
int value = DHT11.temperature;
return value;
}
void setup()
{
// 初始化温度
temp = 0;
// 设置引脚模式,7号为输出
pinMode(7, OUTPUT);
// 设置LCD有几列几行,1602LCD为16列2行
lcd.begin(16, 2);
// 串口设置为9600
Serial.begin(9600);
}
void loop()
{
// 温度超过22蜂鸣器报警
if (temp >= 20) {
tone(7,10,1000);//参数一:引脚号,参数二:频率,参数三:时间(可选:默认无限长)
}else if (temp < 20) {
noTone(7);// 停止输出方波 参数引脚号
}
// 读取传感器数据
int chk = DHT11.read(DHT11PIN);
Serial.print("Read sensor: ");
// 检测数据是否正常接收
switch (chk)
{
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
// 在串口监视器上输出湿度与温度信息
Serial.print("Humidity (%): ");
Serial.println(DHT11.humidity);
Serial.print("Temperature (℃): ");
Serial.println(DHT11.temperature);
// 将光标设置在列 0, 行 1
lcd.setCursor(0, 0);
// 在LCD上输出湿度信息
lcd.print("Humidity (%): ");
lcd.println(DHT11.humidity);
// 将光标设置在列 0, 行 1
lcd.setCursor(0, 1);
// 在LCD上输出温度信息
lcd.print("Temperature : ");
lcd.println(DHT11.temperature);
delay(1000);
}