Arduino MKR WiFi 1010 - DHT22

Measure temperature and humidity with precision! The DHT22 sensor (also known as AM2302) is a digital sensor that accurately measures temperature and humidity. This tutorial shows you how to interface the DHT22 with your Arduino MKR WiFi 1010 and read environmental data through the Serial Monitor.

What You'll Learn:

Real-World Applications:

Arduino MKR WiFi 1010 DHT22 Temperature and humidity Sensor

Hardware Preparation

1×Arduino MKR WiFi 1010
1×Micro USB Cable
1×DHT22 Temperature Humidity Sensor Module
1×10 kΩ Resistor
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack

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 DHT22 Temperature and Humidity Sensor

The DHT22 is a precision digital sensor for measuring temperature and humidity. It uses a capacitive humidity sensor and thermistor to measure the surrounding environment, then outputs a calibrated digital signal.

DHT22 Specifications

DHT22
Operating Voltage3 to 5V
The humidity range 0% to 100%
The humidity accuracy ± 2% to 5%
The temperature range -40°C to 80°C
The temperature accuracy ± 0.5°C
The reading rate 0.5Hz (one time per 2 seconds)

Key Features:

  • High accuracy: ±0.5°C for temperature, ±2% for humidity
  • Wide operating range: -40°C to 80°C temperature, 0-100% humidity (vs. DHT11's 0-50°C)
  • Fine resolution: 0.1°C and 0.1% humidity readings (10× better than DHT11)
  • Sampling rate: Can read every 2 seconds (0.5Hz)
  • Digital interface: Simple one-wire communication protocol
  • Low power: Minimal current draw during operation
  • Factory calibrated: Pre-calibrated for accuracy out of the box
  • Low cost: Affordable precision sensor (~$4-6)

Understanding the Specifications:

Temperature Accuracy (±0.5°C):

If the sensor reads 25.0°C, the actual temperature is between 24.5°C and 25.5°C. This is excellent for most applications - incubating eggs, climate control, or environmental monitoring.

Humidity Accuracy (±2%):

Reading 70% humidity means actual humidity is between 68% and 72%. Perfect for applications like cigar humidors (which require 70% ±2% RH).

Sampling Rate (Every 2 Seconds):

The sensor needs at least 2 seconds between readings to take accurate measurements. Faster polling will return error values or stale data.

DHT22 vs. DHT11 Comparison

If you're deciding between DHT22 and DHT11, here's how they compare:

Feature DHT11 DHT22
**Temperature Range** 0 to 50°C **-40 to 80°C**
**Temperature Accuracy** ±2°C **±0.5°C** (4× better!)
**Humidity Range** 20-80% **0-100%** (full range)
**Humidity Accuracy** ±5% **±2%** (2.5× better!)
**Resolution** 1°C / 1% **0.1°C / 0.1%** (10× finer)
**Price** ~$2 ~$4-6
**Best For** Learning, casual monitoring Precision applications

When to Use DHT22:

  • Precision applications (incubators, climate control)
  • Low-temperature monitoring (below 0°C)
  • Full humidity range needed (wine cellars, humidors)
  • Data logging requiring accuracy
  • Professional or commercial projects

When DHT11 is Sufficient:

  • Learning and educational projects
  • Casual home monitoring
  • Budget-constrained hobby projects

DHT22 Pinout

The DHT22 comes in two versions: bare sensor (4 pins) and module (3 pins with built-in resistor).

DHT22 sensor module Pinout

DHT22 Bare Sensor (4-Pin Version):

  • VCC pin: Connect to 3.3V or 5V power (5V recommended for stability)
  • DATA pin: Digital communication line - connects to Arduino digital pin
  • NC pin: Not Connected - leave this pin unconnected
  • GND pin: Connect to ground (0V)

Important: The bare sensor requires a 10kΩ pull-up resistor between DATA and VCC pins for reliable communication.

DHT22 Module (3-Pin Version) - Recommended:

  • VCC (+): Connect to 5V power
  • DATA (OUT or S): Digital communication line to Arduino
  • GND (-): Connect to ground

Advantages of Module Version:

  • Built-in pull-up resistor (no external resistor needed)
  • Easier wiring (only 3 pins)
  • More robust PCB mounting
  • Better signal quality
  • Beginner-friendly

We strongly recommend using the DHT22 module for simpler wiring and fewer potential connection issues.

Wiring Diagram between DHT22 and Arduino MKR WiFi 1010

This section shows how to connect the DHT22 sensor to your Arduino MKR WiFi 1010. The wiring is the same for both sensor and module versions, with one important difference: the bare sensor requires an external pull-up resistor.

Why Pull-up Resistor?

The DHT22 uses a single-wire communication protocol that requires the data line to be "pulled high" to 5V by default. The 10kΩ resistor ensures reliable communication. Most DHT22 modules include this resistor built-in.

Arduino MKR WiFi 1010 - DHT22 Sensor Wiring

If using the bare DHT22 sensor (4 pins), you must add a 10kΩ pull-up resistor between the DATA and VCC pins.

The wiring diagram between Arduino MKR WiFi 1010 DHT22 Temperature and humidity Sensor

This image is created using Fritzing. Click to enlarge image

Connection Details:

  • DHT22 VCCArduino 5V
    • Provides stable power to the sensor
    • Can use 3.3V, but 5V is recommended for better performance
  • DHT22 DATAArduino D2 pin
    • Digital communication line
    • Also connect a 10kΩ resistor between DATA and VCC
  • DHT22 NC → Leave unconnected
    • No Connection pin - don't wire anything here
  • DHT22 GNDArduino GND
    • Common ground reference

    Arduino MKR WiFi 1010 - DHT22 Module Wiring

    If using the DHT22 module (3 pins), no external resistor is needed - the module includes it built-in. This is the recommended option for beginners!

    The wiring diagram between Arduino MKR WiFi 1010 DHT22 Temperature and humidity Module

    This image is created using Fritzing. Click to enlarge image

    Connection Details:

    • DHT22 VCC (+)Arduino 5V
      • Module power supply
    • DHT22 DATA (OUT or S)Arduino D2 pin
      • Digital communication line
      • Built-in pull-up resistor already on module
    • DHT22 GND (-)Arduino GND
      • Ground connection

      Wiring Tips:

      • Use short jumper wires (under 50cm) for reliable communication
      • Ensure solid connections in breadboard holes
      • Double-check polarity - reversed VCC/GND can damage the sensor
      • The code defines DATA pin as D2, but you can use any digital pin
      • Keep the sensor away from heat sources for accurate readings

      Power Supply:

      For This Project: Simply plug the Arduino into your computer's USB port - it provides enough power for the DHT22 sensor.

Arduino MKR WiFi 1010 Code - DHT22

This code reads temperature and humidity from the DHT22 sensor every 2 seconds and displays the values in the Serial Monitor. Temperature is shown in both Celsius and Fahrenheit.

What the Code Does:

  • Initializes the DHT22 sensor on pin D2
  • Reads sensor data every 2 seconds (sensor's minimum sampling interval)
  • Displays humidity percentage, temperature in Celsius, and temperature in Fahrenheit
  • Checks for sensor communication errors
/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-dht22 */ #include "DHT.h" #define DHT22_PIN 2 DHT dht22(DHT22_PIN, DHT22); void setup() { Serial.begin(9600); dht22.begin(); // initialize the DHT22 sensor } void loop() { // wait a few seconds between measurements. delay(2000); // read humidity float humi = dht22.readHumidity(); // read temperature as Celsius float tempC = dht22.readTemperature(); // read temperature as Fahrenheit float tempF = dht22.readTemperature(true); // check if any reads failed if (isnan(humi) || isnan(tempC) || isnan(tempF)) { Serial.println("Failed to read from DHT22 sensor!"); } else { Serial.print("DHT22# Humidity: "); Serial.print(humi); Serial.print("%"); Serial.print(" | "); Serial.print("Temperature: "); Serial.print(tempC); Serial.print("°C ~ "); Serial.print(tempF); Serial.println("°F"); } }

Detailed Instructions

If you are new to the Arduino MKR WiFi 1010, be sure to check out our Getting Started with Arduino MKR WiFi 1010 tutorial first. Then, follow these steps:

Hardware Setup:

  • Connect the DHT22 to the Arduino MKR WiFi 1010 board as shown in the wiring diagram above
    • If using bare sensor, don't forget the 10kΩ pull-up resistor!
    • If using module, connect directly (resistor built-in)
  • Use a USB cable to connect the Arduino MKR WiFi 1010 board to your computer

Software Setup:

  • Launch the Arduino IDE on your computer
  • Select the Arduino MKR WiFi 1010 board and choose its corresponding COM port
  • Open the Library Manager by clicking on the Libraries icon on the left sidebar
  • Type DHT in the search box
  • Find the DHT sensor library by Adafruit
  • Click the Install button
Arduino MKR WiFi 1010 DHT sensor library
  • A window will appear asking if you want to install additional dependencies
  • Click Install All to install the required Adafruit Unified Sensor library
Arduino MKR WiFi 1010 Adafruit Unified sensor library

Upload and Test:

  • Copy the code above and paste it into the Arduino IDE
  • Click the Upload button to compile and upload the code to your Arduino MKR WiFi 1010
  • Open the Serial Monitor (Tools → Serial Monitor or press Ctrl+Shift+M)
  • Set the baud rate to 9600 in the Serial Monitor
  • You should see temperature and humidity readings updating every 2 seconds

Test the Sensor:

  • Try changing the sensor's temperature - place it near a hot cup of coffee or breathe on it gently
  • Watch the temperature values increase in the Serial Monitor
  • For humidity testing, breathe on the sensor (your breath is humid) - humidity percentage should rise

Expected Output:

COM6
Send
Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 32.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 31.00% | Temperature: 29.00°C ~ 84.20°F Humidity: 32.00% | Temperature: 29.00°C ~ 84.20°F Humidity: 31.00% | Temperature: 29.00°C ~ 84.20°F
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Troubleshooting Common Issues

Problem: "Failed to read from DHT sensor!" Error

Possible Causes:

  • Incorrect wiring connections
  • Missing pull-up resistor (if using bare sensor)
  • Faulty sensor
  • Wrong pin number in code
  • Insufficient power supply

Solutions:

  1. Check wiring carefully:

- VCC to 5V

- GND to GND

- DATA to D2

- If bare sensor: 10kΩ resistor between DATA and VCC

  1. Verify pin definition in code:
#define DHTPIN D2 // Make sure this matches your wiring
  1. Try different DHT22 sensor - sensors can occasionally be defective
  2. Check sensor orientation - ensure you're looking at pinout correctly
  3. Use USB hub with external power if your USB port provides low current

Problem: Readings Seem Inaccurate

DHT22 Accuracy Notes:

  • Temperature: ±0.5°C tolerance is normal
  • Humidity: ±2% variation is within spec
  • Sensor needs 10-30 seconds to stabilize after power-on
  • Keep sensor away from direct sunlight, heat sources, and airflow

Calibration Check:

  • Compare with a reliable thermometer
  • DHT22 is factory-calibrated and generally accurate
  • If consistently off by same amount, you can add offset in code:
float tempC = dht.readTemperature() + 0.5; // Adjust offset as needed

Problem: No Serial Monitor Output

Solutions:

  1. Verify Serial Monitor baud rate is set to 9600
  2. Check correct COM port is selected
  3. Close and reopen Serial Monitor
  4. Press Reset button on Arduino after opening Serial Monitor

Problem: Values Don't Update

Causes:

  • Sensor polling too fast (must wait 2 seconds between reads)
  • delay(2000) removed or changed in code
  • Sensor frozen or damaged

Solution:

Ensure your code has proper delay:

delay(2000); // Wait 2 seconds between readings

Understanding Pull-up Resistor Issues

If you're using a bare DHT22 sensor without the pull-up resistor, you'll see intermittent failures or all readings as "Failed to read". The pull-up resistor is not optional for bare sensors - it's required for the communication protocol to work reliably.

Quick Test: If you have a DHT22 module with built-in resistor, try that first to rule out wiring issues.

Challenge Yourself - Creative Extensions

Once you have the basic temperature and humidity monitoring working, try these enhancements:

1. Data Logging to SD Card:

Add an SD card module to record temperature and humidity over time:

// Log to SD card with timestamp File dataFile = SD.open("climate.txt", FILE_WRITE); dataFile.println(String(tempC) + "," + String(humi)); dataFile.close();

2. LCD Display:

Show readings on a 16x2 LCD instead of Serial Monitor (see our DHT22 LCD tutorial).

3. Temperature Alerts:

Add visual or audio alerts when temperature exceeds thresholds:

if (tempC > 30) { digitalWrite(LED_PIN, HIGH); // Turn on warning LED tone(BUZZER_PIN, 1000); // Sound alarm }

4. Relay Control:

Automatically control a fan or heater based on temperature (see our DHT22 Relay tutorial).

5. Web Server:

Create a web interface to view readings remotely using the Arduino MKR WiFi 1010's WiFi capabilities.

6. Min/Max Tracking:

Keep track of highest and lowest values:

static float maxTemp = -999; static float minTemp = 999; if (tempC > maxTemp) maxTemp = tempC; if (tempC < minTemp) minTemp = tempC; Serial.print("Min: "); Serial.print(minTemp); Serial.print(" | Max: "); Serial.println(maxTemp);

7. Multiple Sensors:

Connect several DHT22 sensors on different pins to monitor multiple locations.

8. Heat Index Calculation:

Calculate "feels like" temperature combining temperature and humidity (search for heat index formula).

Experiment with these ideas to create your own custom environmental monitoring system!

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!