#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
DHT dht(13, DHT22);
float temperature ;
float humidity;
int counter;
// create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(2000); // wait two seconds for initializing
// display on OLED
}
void loop() {
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
// Read temperature and humidity values
float temperatureC = dht.readTemperature();
float temperatureF = dht.readTemperature(true);
float humidity = dht.readHumidity();
// Print temperature in Celsius and Fahrenheit
oled.setCursor(0, 0);
oled.print("Temp: ");
oled.print(temperatureC);
oled.println(" C");
oled.setCursor(0, 10);
oled.print(temperatureF);
oled.println(" F");
// Print humidity
oled.setCursor(0, 20);
oled.print("Humidity: ");
oled.print(humidity);
oled.println("%");
oled.display();
counter++;
delay(1000);
}
Loading
ssd1306
ssd1306