用Arduino连接DHT11传感器通过1602(IIC方式连接)显示温湿度
用Arduino连接DHT11传感器通过1602(IIC方式连接)显示温湿度
[@(用Arduino连接DHT11传感器通过1602(IIC方式连接)显示温湿度)]
本文分四部分:
一:硬件连接:
DHT11接数字口8.
arduino 5v和GND分别接到面包板上的+,-两极,然后连接DHT11上的正负极。(为了简便,我自己没有用面包板,而是引出了排线,直接插线,具体看照片)。
Lcd 1602使用I2C方式与Arduino连接
Scl,sda与Arduino上的对应连接,vcc接5V,GND接GND。
二:软件部分:
下载dht11库文件(论坛上搜到的,此处鸣谢!):http://pan.baidu.com/s/1ntv9QA1
将dht11压缩文件解压到arduino安装文件夹的libraryies中
LiquidCrystal_I2C库文件下载地址为:
https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads/
三:代码部分:
代码如下:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <dht11.h>
#define DHT11PIN 8
dht11 DHT11;
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup() {
pinMode(DHT11PIN,OUTPUT);
// set up the LCD’s number of columns and rows:
lcd.init(); // initialize the lcd
lcd.backlight();
}
void loop() {
int chk = DHT11.read(DHT11PIN);
// Print a message to the LCD.
lcd.setCursor(0, 0);
lcd.print("Tep: ");
lcd.print((float)DHT11.temperature, 2);
lcd.print(“C”);
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(“Hum: “);
lcd.print((float)DHT11.humidity, 2);
lcd.print(”%”);
delay(800);
}
四:实验效果:
请见附图
插入链接与图片
与常用的温湿度计对比结果,如上图。
后记
此为学习贴,欢迎大家交流指教,谢谢!