Arduino - Temperature Humidity Sensor - OLED Display
In this tutorial, we are going to learn:
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.
If you do not know about OLED display, DHT11 and DHT22 temperature humidity sensor (pinout, how it works, how to program ...), learn about them in the following tutorials:

Image is developed using Fritzing. Click to enlarge image

Image is developed using Fritzing. Click to enlarge image
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define DHTPIN 2
#define DHTTYPE DHT11
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
DHT dht(DHTPIN, DHTTYPE);
String displayString;
void setup() {
Serial.begin(9600);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(2000);
oled.clearDisplay();
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(0, 10);
dht.begin();
displayString.reserve(10);
}
void loop() {
float humi = dht.readHumidity();
float tempC = dht.readTemperature();
if (isnan(humi) || isnan(tempC)) {
displayString = "Failed";
} else {
displayString = String(tempC, 1);
displayString += "°C";
displayString += String(humi, 1);
displayString += "%";
}
Serial.println(displayString);
oledDisplayCenter(displayString);
}
void oledDisplayCenter(String text) {
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
oled.clearDisplay();
oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2);
oled.println(text);
oled.display();
}
Open Arduino IDE, Go to Tools Manage Libraries
Search “SSD1306”, then find the SSD1306 library by Adafruit
Click Install button to install the library.
You will be asked for intalling some other library dependencies
Click Install All button to install all library dependencies.
Search “DHT”, then find the DHT sensor library by Adafruit
Click Install button to install the library.
You will be asked for intalling some other library dependencies
Click Install All button all library dependencies.
Copy the above code and open with Arduino IDE
Click Upload button on Arduino IDE to upload code to Arduino
Put the sensor on hot and cold water, or grasp the sensor by your hand
See the result on OLED and Serial Monitor
※ NOTE THAT:
The about code automatically horizontal and vertical center aligns the text on OLED display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define DHTPIN 2
#define DHTTYPE DHT22
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
DHT dht(DHTPIN, DHTTYPE);
String displayString;
void setup() {
Serial.begin(9600);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(2000);
oled.clearDisplay();
oled.setTextSize(2);
oled.setTextColor(WHITE);
oled.setCursor(0, 10);
dht.begin();
displayString.reserve(10);
}
void loop() {
float humi = dht.readHumidity();
float tempC = dht.readTemperature();
if (isnan(humi) || isnan(tempC)) {
displayString = "Failed";
} else {
displayString = String(tempC, 1);
displayString += "°C";
displayString += String(humi, 1);
displayString += "%";
}
Serial.println(displayString);
oledDisplayCenter(displayString);
}
void oledDisplayCenter(String text) {
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
oled.clearDisplay();
oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2);
oled.println(text);
oled.display();
}
※ NOTE THAT:
Code for DHT11 and DHT22 are identical except for one line of code. Library for DHT11 and DHT22 are the same.
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.
※ OUR MESSAGES
You can share the link of this tutorial anywhere. Howerver, please do not copy the content to share on other websites. We took a lot of time and effort to create the content of this tutorial, please respect our work!
Follow Us
Share with your friends to help us spread the tutorial!