ESP8266 - Temperature Humidity Sensor - OLED
This tutorial instructs you how to use ESP8266 to read the temperature and humidity from DHT11/DHT22 sensor and displaying it on an OLED.
You can use DHT22 sensor instead of DHT11 sensor.
Or you can buy the following sensor kits:
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.
If you are unfamiliar with OLED display, DHT11 and DHT22 temperature humidity sensor (including pinout, functionality, programming, etc.), the following tutorials can help you:
This image is created using Fritzing. Click to enlarge image
See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.
This image is created using Fritzing. Click to enlarge image
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
#define DHT_PIN 2
#define DHT_TYPE DHT11
Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1);
DHT dht(DHT_PIN, DHT_TYPE);
String temperature;
String humidity;
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(3);
oled.setTextColor(WHITE);
oled.setCursor(0, 10);
dht.begin();
temperature.reserve(10);
humidity.reserve(10);
}
void loop() {
float humi = dht.readHumidity();
float tempC = dht.readTemperature();
if (isnan(humi) || isnan(tempC)) {
temperature = "Failed";
humidity = "Failed";
} else {
temperature = String(tempC, 1);
temperature += char(247);
temperature += "C";
humidity = String(humi, 1);
humidity += "%";
}
Serial.print(tempC);
Serial.print("°C | " );
Serial.print(humi);
Serial.println("%");
oledDisplayCenter(temperature, humidity);
}
void oledDisplayCenter(String temperature, String humidity) {
int16_t x1;
int16_t y1;
uint16_t width_T;
uint16_t height_T;
uint16_t width_H;
uint16_t height_H;
oled.getTextBounds(temperature, 0, 0, &x1, &y1, &width_T, &height_T);
oled.getTextBounds(temperature, 0, 0, &x1, &y1, &width_H, &height_H);
oled.clearDisplay();
oled.setCursor((SCREEN_WIDTH - width_T) / 2, SCREEN_HEIGHT / 2 - height_T - 5);
oled.println(temperature);
oled.setCursor((SCREEN_WIDTH - width_H) / 2, SCREEN_HEIGHT / 2 + 5);
oled.println(humidity);
oled.display();
}
To get started with ESP8266 on Arduino IDE, follow these steps:
Wire the components as shown in the diagram.
Connect the ESP8266 board to your computer using a USB cable.
Open Arduino IDE on your computer.
Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
Click to the Libraries icon on the left bar of the Arduino IDE.
Search for “SSD1306” and locate the SSD1306 library from Adafruit.
Then, press the Install button to complete the installation.
You will be prompted to install additional library dependencies.
To install all of them, click the Install All button.
Copy the code and open it with the Arduino IDE.
Click the Upload button in the Arduino IDE to compile and upload the code to the ESP8266.
Place the sensor in hot and cold water, or hold it in your hand.
Check out the result on the OLED display and in the Serial Monitor.
※ NOTE THAT:
The code in question will center the text both horizontally and vertically on an OLED display.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define OLED_WIDTH 128
#define OLED_HEIGHT 64
#define DHT_PIN 2
#define DHT_TYPE DHT22
Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1);
DHT dht(DHT_PIN, DHT_TYPE);
String temperature;
String humidity;
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(3);
oled.setTextColor(WHITE);
oled.setCursor(0, 10);
dht.begin();
temperature.reserve(10);
humidity.reserve(10);
}
void loop() {
float humi = dht.readHumidity();
float tempC = dht.readTemperature();
if (isnan(humi) || isnan(tempC)) {
temperature = "Failed";
humidity = "Failed";
} else {
temperature = String(tempC, 1);
temperature += char(247);
temperature += "C";
humidity = String(humi, 1);
humidity += "%";
Serial.print(tempC);
Serial.print("°C | " );
Serial.print(humi);
Serial.println("%");
}
oledDisplayCenter(temperature, humidity);
}
void oledDisplayCenter(String temperature, String humidity) {
int16_t x1;
int16_t y1;
uint16_t width_T;
uint16_t height_T;
uint16_t width_H;
uint16_t height_H;
oled.getTextBounds(temperature, 0, 0, &x1, &y1, &width_T, &height_T);
oled.getTextBounds(temperature, 0, 0, &x1, &y1, &width_H, &height_H);
oled.clearDisplay();
oled.setCursor((SCREEN_WIDTH - width_T) / 2, SCREEN_HEIGHT / 2 - height_T - 5);
oled.println(temperature);
oled.setCursor((SCREEN_WIDTH - width_H) / 2, SCREEN_HEIGHT / 2 + 5);
oled.println(humidity);
oled.display();
}
※ NOTE THAT:
The code for the DHT11 and DHT22 is the same, apart from a single line. The library for both of these is the same too.