Today, we have an intriguing project in store for you—integrating the DHT22 temperature sensor with the Arduino Nano. By the end of this tutorial, you’ll have the skills to create your own temperature and humidity monitoring system. Let’s dive in!
Understanding the DHT22 Temperature Sensor
The DHT22 is a digital sensor renowned for its accuracy in measuring temperature and humidity. It operates using a 1-wire protocol and communicates with the Arduino Nano through a single data line. If you’re new to the Arduino Nano, we recommend watching our “Getting Started with Arduino Nano” video before proceeding. You’ll find the link in the description. This will provide you with a solid foundation and help you better understand the concepts and steps involved in working with the Arduino Nano.

Components Required
Before we begin, let’s gather the necessary components:
• Arduino Nano Board
• DHT22 Temperature and Humidity Sensor
• Breadboard
• Connecting Wires
Hardware Setup
Now let’s proceed with the hardware setup:
1. Power Connections:
• Connect the VCC (positive) pin of the DHT22 sensor to the 5V pin of the Arduino Nano.
• Connect the GND (negative) pin of the DHT22 sensor to the GND pin of the Arduino Nano.
2. Data Connection:
• Connect the Data (middle) pin of the DHT22 sensor to any digital pin of the Arduino Nano. In this tutorial, we’ll use Digital Pin 2.

Installing the Required Libraries
To work with the DHT22 sensor, we need to install the appropriate Arduino libraries. Follow these steps:
1. Open Arduino IDE: If you haven’t already installed it, download it from the Arduino website.
2. Install DHT Sensor Library:
• Go to Sketch > Include Library > Manage Libraries.
• In the Library Manager, search for “DHT” and locate the DHT sensor library by Adafruit.
• Click Install to add the library to your Arduino IDE.
Alternatively, you can download the library from the link below and install it manually:
• Go to Sketch > Include Library > Add .ZIP Library.
• Browse the downloaded file from your computer and install it.
Coding the Arduino Nano
Now let’s proceed to coding the Arduino Nano. The following steps will guide you through the process:
1. Include Necessary Libraries: The code begins by including the necessary libraries and defining the DHT sensor pin and model.
2. Setup Function: In the setup() function, we initialize serial communication and the DHT sensor.
3. Loop Function: The loop() function reads the temperature and humidity values using the readTemperature() and readHumidity() functions, respectively, and prints them to the serial monitor. A delay of two seconds is added between readings to avoid overwhelming the sensor.
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#include <DHT.h>
#define DHT_PIN 12 // Pin connected to the DHT sensor
#define DHT_TYPE DHT22 // DHT sensor type
DHT dht(DHT_PIN, DHT_TYPE);
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
dht.begin();
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
}
void loop() {
delay(2000); // Wait for 2 seconds between readings
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read data from DHT sensor!");
return;
}
display.clearDisplay();
display.setCursor(0,10);
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0); // Start at top-left corner
Serial.println(temperature);
// Display temperature on line 1
display.print("T: ");
display.print(temperature);
display.println(" C");
// Display humidity on line 2
display.setCursor(0,30);
display.print("H: ");
display.print(humidity);
display.println(" %");
display.display(); // Show the updated display content
}
Uploading the Code to the Arduino Nano
After writing the code, the next step is to upload it to the Arduino Nano:
1. Connect Your Arduino Nano:
• Connect your Arduino Nano to your computer using a Mini USB cable.
2. Select the Appropriate Board:
• In the Arduino IDE, go to Tools > Board > Arduino AVR Boards.
• Select Arduino Nano from the dropdown menu.
3. Select the Relevant Port:
• Go to Tools > Port and select the port to which the Nano board is connected.
4. Select the Processor:
• From Tools, select the processor—ATmega328P (Old Bootloader) or ATmega328P depending on your Nano board.
5. Upload the Code:
• Finally, click on the Upload button to upload the code to the Arduino Nano. If you face any issues uploading the code, first watch our tutorial on the “Beginner’s Guide to Programming Arduino Nano”.
Testing the Setup
Once the code is uploaded successfully, open the Serial Monitor by clicking on the magnifying glass icon at the top right corner of the Arduino IDE. Set the baud rate to 9600, and you should start seeing temperature and humidity readings displayed in the Serial Monitor.
Congratulations!
You have successfully set up a DHT22 temperature sensor with the Arduino Nano. You can now use this setup as a foundation for various temperature monitoring and control projects. Feel free to experiment further by integrating the sensor with other components or adding additional functionality to the code. With the DHT22 sensor and Arduino Nano, the possibilities are endless.
If you have any questions or suggestions, please leave them in the comments section below. Happy tinkering!