Index
Introduction
The DHT11 module is a popular temperature and humidity sensor module that can measure the air temperature and relative humidity. It outputs a digital signal and is simple to connect to an Arduino, making it ideal for basic weather station projects, home automation, and environmental monitoring.
Required Components
- Arduino UNO R3 SMD (or any other Arduino board)
- DHT11 Temperature and Humidity Module (with built-in pull-up resistor)
- Jumper wires
- Breadboard
Pinout
Circuit Diagram / Wiring
Follow these steps to connect the DHT11 module to the Arduino:
- DHT11 VCC → 5V (Arduino)
- DHT11 GND → GND (Arduino)
- DHT11 SIGNAL → Pin A0 (Arduino)
Arduino Code / Programming
- Go to the “Libraries” tab on the left-hand side of the screen.
- Click on the “Library Manager” button (book icon) at the top of the Libraries tab.
- In the Library Manager window, type “SimpleDHT” in the search bar.
- Locate the “SimpleDHT” library click on the “Install” button next to it.
- Wait for the library to be installed, and you’re ready to use the SimpleDHT library in your projects.
Use the following code to read temperature and humidity data from the DHT11 module:
#include <SimpleDHT.h>
// Define the pin connected to DHT11
#define DHTPIN A0
SimpleDHT11 dht11;
void setup() {
Serial.begin(9600);
Serial.println("DHT11 Example");
}
void loop() {
int err;
byte temperature = 0;
byte humidity = 0;
// Read the DHT11 sensor
if ((err = dht11.read(DHTPIN, &temperature, &humidity, NULL)) != 0) {
Serial.print("Read DHT11 failed, err=");
Serial.println(err);
delay(1000);
return;
}
// Print temperature and humidity
Serial.print("Humidity: ");
Serial.print((int)humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print((int)temperature);
Serial.println(" °C");
delay(2000); // Wait a few seconds between readings
}
Explanation of the Code
- DHT Library: The code uses the
SimpleDHT
library to handle communication with the DHT11 sensor. - Setup: The
setup()
function initializes the serial communication and the DHT sensor. - Loop: In the
loop()
, the sensor readings for temperature and humidity are taken every 2 seconds and displayed on the Serial Monitor. - Error Handling: If the readings fail, an error message is printed.
Testing and Troubleshooting
- Check Connections: Ensure the wiring is correct and secure.
- Power Supply: Make sure the module is powered properly (5V).
- Code Errors: Double-check that you have included the
DHT
library in your code.
Arduino Projects:
Arduino Project 1 – Weather Monitor with OLED Display and DHT11 Sensor
This Arduino project combines the capabilities of the DHT11 temperature and humidity sensor with an SSD1306 OLED display to create a compact and user-friendly weather monitoring system. The DHT11 sensor measures the surrounding environment’s temperature and humidity, while the SSD1306 OLED display provides a clear and visually appealing representation of the data. Additionally, a bitmap image is displayed to enhance the visual experience, making it suitable for educational and hobbyist applications.
Required Components
- Arduino UNO R3 SMD (or any other Arduino board)
- DHT11 Temperature and Humidity Module (with built-in pull-up resistor)
- SSD1306 OLED Display
- Jumper wires
- Breadboard
Circuit Diagram / Wiring
- DHT11 Sensor with Arduino
- DHT11 VCC → 5V (Arduino)
- DHT11 GND → GND (Arduino)
- DHT11 SIGNAL → Pin A0 (Arduino)
- OLED Display with Arduino
- OLED VCC → 5V (Arduino)
- OLED GND → GND (Arduino)
- OLED SCK → Pin A5 (Arduino)
- OLED SDA → Pin A4 (Arduino)
Arduino Code
- Open Arduino IDE and go to the Libraries tab.
- Click on the Library Manager button (book icon).
- Search and install the DHT sensor library by Adafruit.
- Search and install the Adafruit GFX Library.
- Search and install the Adafruit SSD1306 Library.
- Search and install the Adafruit Unified Sensor Library.
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMonoBold18pt7b.h>
#include "DHT.h"
#define DHTPIN A0
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
int h;
int t;
#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 bitmap_height 128
#define bitmap_width 64
static const unsigned char PROGMEM logo_bmp[] =
{ 0x00, 0x00, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x3F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x78, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x80, 0x08, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x80, 0x78, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x80, 0x08, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0xC0, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0xC0, 0x78, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0xC0, 0x78, 0x07, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x03, 0xC0, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x03, 0xE0, 0x08, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x07, 0xE0, 0x78, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x07, 0xF0, 0x08, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x0F, 0xF0, 0x08, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x0F, 0xF8, 0x78, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x1F, 0xF8, 0x08, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x1F, 0xF8, 0x08, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x3F, 0xFC, 0x78, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x3F, 0xFE, 0x08, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7F, 0xFE, 0x0F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7F, 0xFF, 0x7F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xFF, 0xFF, 0x0F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xFF, 0xFF, 0x0F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0xFF, 0xFF, 0x7F, 0x80, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xFF, 0xFF, 0x0F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xFF, 0xFF, 0x0F, 0x87, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xFF, 0xFF, 0x1F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x07, 0xFF, 0xFF, 0x7F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0F, 0xFF, 0xFF, 0x0F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0F, 0xFF, 0xFF, 0x0F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x1F, 0xFF, 0xFF, 0x7F, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x1F, 0xFF, 0xFF, 0x0F, 0x8F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x1F, 0x0F, 0xF3, 0xEF, 0x83, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x3E, 0x03, 0xE7, 0xCF, 0xC0, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x3C, 0x61, 0xC7, 0x9F, 0xE0, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7C, 0x71, 0xCF, 0xBF, 0xE3, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7C, 0x71, 0xCF, 0xBF, 0xF3, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7C, 0x71, 0x9F, 0xBF, 0xE3, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFC, 0x71, 0x3F, 0xBF, 0xE3, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFC, 0x71, 0x38, 0x1F, 0xE7, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFC, 0x02, 0x20, 0x0F, 0x87, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFE, 0x06, 0x46, 0x03, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFF, 0x8C, 0xC7, 0x18, 0x1F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFC, 0xC7, 0x1C, 0x7F, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xF9, 0xC7, 0x1C, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xF9, 0xC7, 0x1E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7F, 0xF3, 0xC6, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7F, 0xE3, 0xC0, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7F, 0xE7, 0xE0, 0x7E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x3F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x1F, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x1F, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x07, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x7F, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x1F, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x03, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void setup() {
Serial.begin(9600);
dht.begin();
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
printText();
delay(1500);
}
void loop() {
h = dht.readHumidity();
t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
showBitmap();
printText();
display.display();
delay(500);
display.clearDisplay();
}
void printText() {
display.setFont(&FreeMonoBold18pt7b);
display.setTextColor(WHITE); // Draw white text
display.setCursor(45, 28); // Start at top-left corner
display.print(t);
display.drawCircle(92, 8, 3, WHITE);
display.setCursor(100, 27);
display.print("C");
display.setCursor(45, 62);
display.print(h);
display.print("%");
}
void showBitmap(void) {
display.drawBitmap(0, 0, logo_bmp, bitmap_height, bitmap_width, WHITE);
//display.display();
}
Explanation
- Library and Hardware Setup: Includes libraries for OLED and DHT11, defines pins for the DHT11 sensor and OLED reset, and initializes their objects.
- Display Configuration: Sets up the OLED display resolution and prepares to display graphics or text using the Adafruit SSD1306 library.
- Bitmap Data: Defines a custom bitmap (
logo_bmp
) as a graphic to be displayed on the OLED. - Sensor Data Display: (Not shown in provided code but implied) Reads temperature (
t
) and humidity (h
) values from DHT11 and displays them or graphics on the OLED.
Troubleshooting
- Library Installation Errors: Ensure you’re connected to the internet and restart the Arduino IDE if needed.
- No Display Output (SSD1306): Check I2C connections and ensure the correct display address is set.
- Sensor Not Responding (DHT, Accelerometer): Verify wiring and check if correct pin and voltage are used.
- Compilation Errors: Select the correct board and port, and make sure all libraries are updated and installed properly.