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.

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); | |
} | |
} |
References :
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.