อ่านค่าเซ็นเซอร์ DHT22 ด้วย Arduino UNO R3 บนจอ Oled0.96



 อ่านค่าเซ็นเซอร์ DHT22  ด้วย Arduino UNO R3 บนจอ Oled0.96


Arduino Nano: เป็นบอร์ดไมโครคอนโทรลเลอร์ที่นิยมใช้สำหรับงานอิเล็กทรอนิกส์พื้นฐาน

อุปกรณ์ที่จำเป็น

1.บอร์ด Arduino UNO R3 

2.จอ oled0.96 128x64 pixels  I2C

3.Breadboard (ถ้ามี)

4.สาย Jumper

5.เซ็นเซอร์ DHT22

6.แหล่งจ่ายไฟ (ถ้าไม่ใช้จาก Arduino power) usb ต่อคอมพิวเตอร์


จอ OLED 0.96 นิ้ว เป็นจอแสดงผลขนาดเล็กที่นิยมใช้ในอุปกรณ์อิเล็กทรอนิกส์ต่าง ๆ 

เช่น นาฬิกาอัจฉริยะ, เซ็นเซอร์, และอุปกรณ์ IoT เนื่องจากมีขนาดกะทัดรัด 

ใช้พลังงานน้อย และให้ภาพที่คมชัด สีสันสดใส


OLED คืออะไร?

OLED ย่อมาจาก Organic Light-Emitting Diode หมายถึงไดโอดที่ปล่อยแสงได้เอง 

เมื่อมีกระแสไฟฟ้าไหลผ่าน โดยใช้วัสดุอินทรีย์เป็นส่วนประกอบหลัก


หลักการทำงาน

พิกเซล: จอ OLED ประกอบด้วยพิกเซลเล็กๆ จำนวนมาก เมื่อมีการส่งสัญญาณไฟฟ้าไปยังพิกเซลนั้น 

พิกเซลก็จะเปล่งแสงออกมาได้เอง 

สี: แต่ละพิกเซลสามารถควบคุมความสว่างได้อิสระ ทำให้สามารถแสดงสีต่าง ๆ ได้หลากหลาย 

โดยการผสมสีหลักคือ แดง เขียว และน้ำเงิน (RGB)

ไม่มีแบ็คไลท์: จุดเด่นของ OLED คือไม่จำเป็นต้องใช้แสงแบ็คไลท์เหมือนจอ LCD ทำให้สีดำมีความดำสนิท

และใช้พลังงานน้อยกว่า


เชื่อมต่อ Oled 0.96 กับ Arduino UNO R3

Vin ---------------> 5V

GND ---------------> GND

SCL ---------------> SCL or A4

SDA ---------------> SDA or A5


ติดตั้ง ไลบราลี่ที่จำเป็น 

1. Open your Arduino IDE and go to Sketch > Include Library > Manage Libraries

2. Type “SSD1306” in the search box and install the SSD1306 library from Adafruit.

3. Type “GFX” in the search box and install the library.


ขั้นตอนการต่อสาย

เชื่อมต่อ DHT22 กับ Arduino UNO R3

VCC ----> +5V  

GND ----> - GND 

DATA----> D2



ติดตั้ง ไลบราลี่ใน Arduio IDE 

Tools->Manage Libraries->DHT เลือก DHT sensor library by Adafruit


เริ่มต้นเขียนโปรแกรม เชื่อมต่อกับ จอ OLED 0.96 ก่อนหรือหลังหรือพร้อมกันก็ได้แล้วแต่ความถนัด 

ใช้โค้ด Example โดยไปที่ 

File->Examples->Adafruit SSD1306 ->ssd1306_128x32_i2C


1. ประกาศส่วนหัวของโปรแกรม

#include <SPI.h>

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels

#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


2.กำหนดให้พิมพ์ข้อความส่วนแรก

void setup() {

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))  { // Address 0x3D for 128x64

    Serial.println(F("SSD1306 allocation failed"));

//    for(;;);

  }

  display.display(); //Display logo

  delay(1000); 

  display.clearDisplay();

}

  

void loop() {

  delay(1000);

  display.clearDisplay();

  display.setTextSize(1);

  display.setTextColor(WHITE);

  display.setCursor(0, 0);   

  display.print("Temperature");  

  display.setCursor(0, 15);     

  display.print("30.15");    

  display.print(" C");    

  display.setCursor(50, 15);       

  display.print("80.00");    

  display.print(" F");    

  display.setCursor(0, 30);       

  display.print("Humidity: ");    

  display.print("57.01");    

  display.print(" %");    

  display.setCursor(0, 45);         

  display.print("39.15");    

  display.print(" C");    

  display.setCursor(50, 45);           

  display.print("89.15");    

  display.print(" F");    

  display.display();

}



3.โปรแกรมเชื่อมต่อกับ DHT22

#include "DHT.h"

#define DHTPIN 2     // Digital pin connected to the DHT sensor

#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

DHT dht(DHTPIN, DHTTYPE);


void setup() {

  Serial.begin(9600);

  Serial.println(F("DHT22 test!"));

  dht.begin();

}


void loop() {

  delay(2000);

  float h = dht.readHumidity();

  float t = dht.readTemperature();

  float f = dht.readTemperature(true);

  if (isnan(h) || isnan(t) || isnan(f)) {

    Serial.println(F("Failed to read from DHT sensor!"));

    return;

  }


  float hif = dht.computeHeatIndex(f, h);

  float hic = dht.computeHeatIndex(t, h, false);


  Serial.print(F("Humidity: "));

  Serial.print(h);

  Serial.print(F("%  Temperature: "));

  Serial.print(t);

  Serial.print(F("°C "));

  Serial.print(f);

  Serial.print(F("°F  Heat index: "));

  Serial.print(hic);

  Serial.print(F("°C "));

  Serial.print(hif);

  Serial.println(F("°F"));

}







//Complete Code

#include <SPI.h>

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels

#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


#include "DHT.h"

#define DHTPIN 2     // Digital pin connected to the DHT sensor

#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

DHT dht(DHTPIN, DHTTYPE);


void setup() {

  Serial.begin(9600);

  Serial.println(F("DHT22 test!"));

  dht.begin();

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))  { // Address 0x3D for 128x64

    Serial.println(F("SSD1306 allocation failed"));

//    for(;;);

  }


}


void loop() {

  delay(2000);

  float h = dht.readHumidity();

  float t = dht.readTemperature();

  float f = dht.readTemperature(true);

  if (isnan(h) || isnan(t) || isnan(f)) {

    Serial.println(F("Failed to read from DHT sensor!"));

    return;

  }


  float hif = dht.computeHeatIndex(f, h);

  float hic = dht.computeHeatIndex(t, h, false);


  delay(1000); 

  display.clearDisplay();

  display.setTextSize(1);

  display.setTextColor(WHITE);

  display.setCursor(0, 0);   

  display.print("Temperature");  

  display.setCursor(0, 15);     

  display.print(t);    

  display.print(" C");    

  display.setCursor(50, 15);       

  display.print(f);    

  display.print(" F");    

  display.setCursor(0, 30);       

  display.print("Humidity: ");    

  display.print(h);    

  display.print(" %");    

  display.setCursor(0, 45);         

  display.print(hic);    

  display.print(" C");    

  display.setCursor(50, 45);           

  display.print(hif);    

  display.print(" F");    

  display.display();

}

//////////////////////////////////


Relate Link:

https://randomnerdtutorials.com/guide-for-oled-display-with-arduino/

ของขวัญจากก้อนดิน - เจ้าหญิงแห่งบรูไน..ทรงร้องเพลง "พ่อแห่งแผ่นดิน"

(ต้นฉบับ เบิร์ด ธงไชย)

คำร้อง/ทำนอง : นิติพงษ์ ห่อนาค

เรียบเรียงเปียโน : ST. Music





ความคิดเห็น