本文分为三部分:
硬件讲解代码实现ds18b20替代dht111.硬件讲解 材料:
dht11温湿度传感器*1arduino uno*1lcd*1杜邦线* n面包板*1电位计*1引脚说明: 连接图: dht11的DATA引脚采用单总线
2.代码实现
#include <LiquidCrystal.h> #include <DHT.h> #define DHTPIN 8 #define DHTTYPE DHT11 LiquidCrystal lcd(12,11,5,4,3,2); DHT dht(DHTPIN,DHTTYPE); void setup() { // put your setup code here, to run once: Serial.begin(9600); dht.begin(); lcd.begin(16,2); } void loop() { // put your main code here, to run repeatedly: delay(3000);//dht需要反应时间 lcd.clear(); float t=dht.readTemperature(); float h=dht.readHumidity(); lcd.print("temperature is"); lcd.setCursor(0,1); lcd.print(t); lcd.print((char)223); lcd.print("C"); delay(3000); lcd.clear(); lcd.print("humidity is"); lcd.setCursor(0,1); lcd.print(h); lcd.print("%"); //加入了湿度测量 }3.ds18b20替代dht11 它长这样 引脚说明:正对字面从左到右GND,DATA,VCC
连接图: 代码:`
#include <OneWire.h> #include <DallasTemperature.h> #include <LiquidCrystal.h> #define ONE_WIRE_BUS 8 OneWire temp(ONE_WIRE_BUS); DallasTemperature sensors(&temp); LiquidCrystal lcd(12,11,2,3,4,5); float temperature = 0; void setup() { // put your setup code here, to run once: Serial.begin(9600); sensors.begin(); lcd.begin(16,2); lcd.print("hello,world!"); delay(1000); } void loop() { // put your main code here, to run repeatedly: sensors.requestTemperatures(); temperature = sensors.getTempCByIndex(0); delay(100); temperature = (temperature*10); lcd.clear(); lcd.print("temperature is"); lcd.setCursor(0,1); lcd.print((long)temperature/10); lcd.print("."); lcd.print((long)temperature%10); lcd.print((char)223); lcd.print("C"); delay(1000); }lcd显示温度为-127则是报错,一般是接线出问题了
小结 在测温方面,ds18b20完全可以和dht11平起平坐,同时ds18吧、20体积小接线灵活,可操作性强,其实这些温度传感器无非就是一些能随温度变化而改变的物品,只要一个物品能够感知外界变化并反馈出电信号,那么它就可以被制成传感器了。