งานปฏิบัติ 15
งานปฏิบัติ 15
การอ่านค่าอุณหภูมิและค่าความชื้น
DHT11
Code 1
#include <DHT11.h>
int pin=4;
DHT11 dht11(pin);
LedControl lc=LedControl(8,10,9,1);
// Pin 8->DIN, 10->CLK, 9->CS(LOAD), 1 = No.of devices
void show2digit(int h,int t)
{
int seg1,seg2,seg3,seg4;
seg1 = h%10;
seg2 = h/10;
lc.setDigit(0,4,seg1,false);
lc.setDigit(0,5,seg2,false);
seg3 = t%10;
seg4 = t/10;
lc.setDigit(0,0,seg3,false);
lc.setDigit(0,1,seg4,false);
delay(300);
}
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
lc.shutdown(0,false);
lc.setIntensity(0,5);
lc.clearDisplay(0);
}
void loop()
{
int err;
float temp, humi;
if((err=dht11.read(humi, temp))==0)
{
Serial.print("temperature:");
Serial.print(temp);
Serial.print(" humidity:");
Serial.print(humi);
Serial.println();
}
else
{
Serial.println();
Serial.print("Error No :");
Serial.print(err);
Serial.println();
}
lc.clearDisplay(0);
show2digit(humi,temp);
}
/////////////////////////////////////////////////////////////////////////
Code 2
#include <LiquidCrystal_I2C.h>
#include <DHT11.h>
int pin=4;
DHT11 dht11(pin);
LiquidCrystal_I2C lcd(0x27,16,2);
void setup()
{
lcd.begin();
lcd.setCursor(0,0);lcd.print("H = %RH");
lcd.setCursor(0,1);lcd.print("T = C");
}
void loop()
{
int err;
float temp, humi;
if((err=dht11.read(humi, temp))==0)
{
Serial.print("temperature:");
Serial.print(temp);
Serial.print(" humidity:");
Serial.print(humi);
Serial.println();
}
else
{
Serial.println();
Serial.print("Error No :");
Serial.print(err);
Serial.println();
}
lcd.setCursor(4,0);
lcd.print(humi);
lcd.setCursor(4,1);
lcd.print(temp);
delay(300);
}
ความคิดเห็น
แสดงความคิดเห็น