Display Temperature and Humidity on OLED using DHT22 and Arduino

The air is very hot and we have to take a very good care for ourself especially during haze. With this simple project, you can check the temperature, humidity and heat index.

Introduction

The DHT22 is a basic, low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin (no analog input pins needed). It’s easy to use, but requires careful timing to grab data. The only real downside of this sensor is you can only get new data from it once every 2 seconds, so when using our library, sensor readings can be up to 2 seconds old.

Video

This video will show you how to Display Temperature and Humidity on OLED using DHT22 and Arduino

Hardware Preparation

This is the hardware connection and the components needed.

Dhtoled Bb
Figure 1 : Hardware connection.

Sample Code

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.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 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define DHTPIN 2
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
#define PIEZO 8
#define ALARM 1000
int Sound[] = {ALARM, ALARM};
int SoundNoteDurations[] = {4, 4};
#define playSound() playMelody(Sound, SoundNoteDurations, 2)
char inChar;
String inString;
void setup() {
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.clearDisplay();
dht.begin();
}
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 (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
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(F("Failed to read from DHT sensor!"));
return;
}
// 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);
if (hic >= 41) {
displayOled();
playSound();
}
else {
displayOled();
noTone(PIEZO);
}
}
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();
}
void playMelody(int *melody, int *noteDurations, int notesLength)
{
pinMode(PIEZO, OUTPUT);
for (int thisNote = 0; thisNote < notesLength; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(PIEZO, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(PIEZO);
}
}
view raw DHT22Oled.ino hosted with ❤ by GitHub

References :

  1. DHT 22 Temperature and Humidity Sensor

Thank you

Thank you for reading this tutorial and we hope it helps your project development. If you have any technical inquiry, please post at Cytron Technical Forum.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

Ultrasonic HC-SR04P Using Raspberry Pi Pico
Object Sense With Servo Using Maker Pi RP2040
Build Otto DIY Robot Using Maker Pi RP2040
Raspberry Pi RP2040 vs STM32F1 vs SAMD21G18 vs ESP32-S2
The Easiest Way to Print Temperature Tower Using Cura 4.9.1 Plugins
Tutorials of Cytron Technologies Scroll to Top