display tempearture and humidity using oled display
STEPS:
connect circuit as per ciecuit diagram
CIRCUIT DIAGRAM:
LIBRARY LINK:
adafruit library link:
gfx:
VIDEO:
CODE:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET LED_BUILTIN // 4 for arudino Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(OLED_RESET);
#define DHTPIN 2 //gpio 4
#define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
displayOled();
}
void displayOled() {
// Read humidity
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(5, 15);
display.print("Humidity:");
display.setCursor(80, 15);
display.print(h);
display.print("%");
display.setCursor(5, 30);
display.print("Temperature:");
display.setCursor(80, 30);
display.print(t);
display.print((char)247); // degree symbol
display.print("C");
display.setCursor(5, 45);
display.print("Heat Index:");
display.setCursor(80, 45);
display.print(hic);
display.print((char)247); // degree symbol
display.print("C");
display.display();
}
No comments: