Skip to content

Instantly share code, notes, and snippets.

@soap
Created April 29, 2025 15:26
Show Gist options
  • Save soap/98c0d17790b649dad0110826d646095a to your computer and use it in GitHub Desktop.
Save soap/98c0d17790b649dad0110826d646095a to your computer and use it in GitHub Desktop.
Measure temperature and humidity with OLED display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h> // Adafruit library
#define dhtPin 4
#define buzzerPin 19
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
DHT dht11(dhtPin, DHT11); // pin and type of sensor
float humid, temp_c;
void setup() {
Serial.begin(115200); // ใช้ความเร็วสูงขึ้น
dht11.begin();
if( !oled.begin(SSD1306_SWITCHCAPVCC, 0x3C) ) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
oled.clearDisplay();
oled.setTextSize(1);
oled.setTextColor(SSD1306_WHITE);
oled.setCursor(5, 10); // x=5, y=10
oled.println("Ready");
oled.display();
delay(1000);
}
void loop() {
temp_c = dht11.readTemperature();
humid = dht11.readHumidity();
// แสดงบนจอ OLED
oled.clearDisplay();
oled.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SSD1306_WHITE);
oled.setCursor(5,5);
oled.print("T : ");
oled.print(temp_c, 1);
oled.print(" ");
// oled.print((char)247);
// วาดวงกลมเล็กแทนองศา
int16_t x = oled.getCursorX(); // X หลังจากพิมพ์เลข
int16_t y = oled.getCursorY(); // Y หลังจากพิมพ์เลข
oled.drawCircle(x + 3, y + 2, 2, SSD1306_WHITE); // วาดวงกลมเล็ก
oled.setCursor(x + 8, y); // เลื่อนไปต่อ
oled.print("C");
oled.setCursor(5, 20);
oled.print("H : ");
oled.print(humid, 1);
oled.println("%");
oled.display();
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment