Arduino Projects [EP.7] : Arduino รับค่าอุณหภูมิและความชื้นจาก DH11 แสดงผลผ่าน OLED

ในEP นี้เราจะนำ Arduino มารับค่าอุณหภูมิและความชื้นจาก DH11 แสดงผลผ่าน OLED กันครับ โดยเราเปลี่ยนบรรยากาศกันบ้างโดยในEPนี้เราจะใช้ Arduino Nano มาใช้นะครับ โดยข้อดีของ Arduino Nano รุ่นนี้คือมีขนาดเล็กและราคาไม่สูงมากครับ

อุปกรณ์
1. Arduino Nano
2. OLED
3. DH11
4. ตัวต้านทาน 10k Ohm

DH11 คืออะไร?
DH11 คือ TEMPERATURE-HUMIDITY SENSOR หรือเซ็นเซอร์วัดความชื้นและอุณหภูมิครับ ชื่อมันก็บอกอยู่แล้วเนอะ ในการสื่อสารกับ Arduino จะใช้ 1-Wire Protocol ในการสื่อสารครับ(ไม่ได้ลงรายละเอียดไว้นะครับแต่ถ้าสงสัยถามได้ครับ) การใช้งานไม่ยากครับเพราะเรามี Library ให้ครับ โดยขาจะมี 4ขาครับ ขาแรกต่อ VCC ขา 2เป็นขา Data เอาไว้สื่อสารกับ Arduino ขาที่ 3 ปล่อยว่าง ขาที่ 4 ต่อ Ground ครับ โดยต้อง pull-up ขาData ไว้ด้วยตัวต้าน 10 k ohm ด้วยนะครับ

OLED คืออะไร?
ชื่อเต็ม (Organic Light-Emitting Diode)  หลักการ… เอาเป็นว่ามันคือจอแสดงผลคล้ายๆ LCD ทั่วไปครับแต่มันเจ๋งตรงที่ มันมีขนาด 128×64 Pixel ครับ แถวใช้สายแค่2 เส้นในการส่งข้อมูล โดยเราจะใช้ i2c bus(อ่านว่าไอสแควร์ซี) ในการคุยกับ Arduino ครับ

การต่อใช้งาน

Arduino nano DH11 OLED

ขาSCL ของ OLED เข้าขา A5 Arduino
ขาSDA ของ OLED เข้าขา  A4 Arduino
ขาDATA ของ DH11 เข้า D5 Arduino Pull-Up 10kohm ตามรูปด้วยนะครับ

VCC Ground ต่อตามรูปเลยครับ

Source Code

// Read DHT11 humidity/temperature sensors
// display on 0.96 inch 128X64 I2C OLED

#include "DHT.h"
#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);

#define DHTPIN 2     
#define DHTTYPE DHT11   // DHT 11 

DHT dht(DHTPIN, DHTTYPE, 6);

char str[10];

void drawTest(void) {
  u8g.setFont(u8g_font_unifont);
  u8g.drawStr( 0, 20, "Arduino Projects!");
}

void setup() {
  Serial.begin(9600); 
  Serial.println("DHTxx test!");
 
  dht.begin();
  u8g.firstPage();  
  do {
    drawTest();
  } while( u8g.nextPage() );
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index
  // Must send in temp in Fahrenheit!
  float hi = dht.computeHeatIndex(f, h);

  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  
  Serial.print("Heat index: ");
  Serial.print(hi);
  Serial.println(" *F");
  
  // picture loop
  u8g.firstPage();  
  do {
    u8g.setFont(u8g_font_helvB08);
    
    u8g.drawStr( 0, 15, "Humidity:");
    u8g.drawStr( 80, 15, dtostrf(h, 5, 2, str));
    u8g.drawStr( 120, 15, "%");
    
    u8g.drawStr( 0, 30, "Temperature:");
    u8g.drawStr( 80, 30, dtostrf(t, 5, 2, str));
    u8g.drawStr( 120, 30, "\260C");
    
    u8g.drawStr( 80, 45, dtostrf(f, 5, 2, str));
    u8g.drawStr( 120, 45, "\260F");
    
    u8g.drawStr( 0, 60, "Heat index:");
    u8g.drawStr( 80, 60, dtostrf(hi, 5, 2, str));
    u8g.drawStr( 120, 60, "\260F");
    
  } while( u8g.nextPage() );
}
Facebook Comments