Arduino Mega - DHT11

This guide shows how to use the DHT11 temperature and humidity sensor with the Arduino Mega. We will cover the details:

Arduino Mega and DHT11 sensor module

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×DHT11 Temperature Humidity Sensor Module
1×10 kΩ Resistor
1×Breadboard
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of DHT11 Temperature and Humidity Sensor

DHT11
Operating Voltage3 to 5V
Temperature Range 0°C to 50°C
Temperature Accuracy ± 2°C
Humidity Range 20% to 80%
Humidity Accuracy 5%
Reading Rate 1Hz (once every second)

Pinout

DHT11 has two kinds: a sensor and a module.

DHT11 temperature and humidity sensor Pinout

The DHT11 sensor has 4 pins.

  • GND pin: connect to ground (0V)
  • VCC pin: connect to power (5V or 3.3V)
  • DATA pin: used to send and receive data between the sensor and the Arduino Mega
  • NC pin: not connected, can be ignored

The DHT11 module has three pins.

  • Ground pin: connect to ground (0 volts).
  • Power pin: connect to the power supply (5 volts or 3.3 volts).
  • Data pin: used to send and receive data between the sensor and the Arduino Mega.

Some makers sell the DHT11 sensor as a small module with three pins: GND, VCC, and DATA (or sometimes -, +, and OUT).

Wiring Diagram

To connect the DHT11 sensor to the Arduino Mega, use a resistor of about 5k to 10k ohms. This resistor keeps the data line high, so the sensor and the Arduino can talk properly.

Arduino Mega - DHT11 Sensor Wiring

The wiring diagram between Arduino Mega DHT11 Temperature and humidity Sensor

This image is created using Fritzing. Click to enlarge image

Arduino Mega - DHT11 Module Wiring

Most DHT11 sensor modules already have a resistor built in, so you don’t need to add one. This makes wiring or soldering easier.

The wiring diagram between Arduino Mega DHT11 Temperature and humidity Module

This image is created using Fritzing. Click to enlarge image

How To Program For DHT11 Temperature Sensor

Coding for both the sensor and the module is the same.

  • Add the library.
#include <DHT.h>
  • Choose the Arduino Mega pin that connects to the DHT sensor.
#define DHT11_PIN 2
  • Make a DHT11 sensor.
DHT dht11(DHT11_PIN, DHT11);
  • Turn on the sensor:
dht11.begin();
  • Check the humidity level
float humi = dht11.readHumidity();
  • Read the temperature in Celsius.
float tempC = dht11.readTemperature();
  • Check the temperature in Fahrenheit.
float tempF = dht11.readTemperature(true);

Arduino Mega Code - DHT11

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-dht11 */ #include <DHT.h> #define DHT11_PIN 2 // The Arduino Mega pin connected to DHT11 DHT dht11(DHT11_PIN, DHT11); void setup() { Serial.begin(9600); dht11.begin(); // initialize the sensor } void loop() { // wait a few seconds between measurements. delay(3000); // read humidity float humidity = dht11.readHumidity(); // read temperature as Celsius float tempC = dht11.readTemperature(); // read temperature as Fahrenheit float tempF = dht11.readTemperature(true); // check if any reads failed if (isnan(humidity) || isnan(tempC) || isnan(tempF)) { Serial.println("Failed to read from DHT11 sensor!"); } else { Serial.print("DHT11# Humidity: "); Serial.print(humidity); Serial.print("%"); Serial.print(" | "); Serial.print("Temperature: "); Serial.print(tempC); Serial.print("°C ~ "); Serial.print(tempF); Serial.println("°F"); } }

Detailed Instructions

Do these steps one by one.

  • Connect the parts following the diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the Arduino Mega board and the correct COM port.
  • Click the Libraries icon on the left side of the IDE.
  • In the search box, type DHT and find the Adafruit DHT library.
  • Click Install to add the library.
Arduino Mega DHT sensor library
  • You need to install more software packages.
  • Click the Install All button to install all the needed libraries.
Arduino Mega Adafruit Unified sensor library
  • Copy the code for your sensor and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to upload the code to the Arduino Mega.
  • Change the temperature near the sensor to be warmer or cooler.
  • See the results in the Serial Monitor.
COM6
Send
DHT11# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT11# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT11# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT11# Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F DHT11# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT11# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT11# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT11# Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F DHT11# Humidity: 32.00% | Temperature: 28.00°C ~ 82.40°F DHT11# Humidity: 31.00% | Temperature: 29.00°C ~ 84.20°F DHT11# Humidity: 32.00% | Temperature: 29.00°C ~ 84.20°F DHT11# Humidity: 31.00% | Temperature: 29.00°C ~ 84.20°F
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!