To connect a humidity sensor to an Arduino Uno, you’ll need a humidity sensor module such as the DHT11 or DHT22, which includes both a humidity sensor and a temperature sensor. Here are the steps to connect and read data from a DHT11 or DHT22 humidity sensor:
Components Needed:
Arduino Uno
DHT11 or DHT22 humidity and temperature sensor module
Jumper wires
Breadboard (optional)
Connections:
DHT11/DHT22 – Arduino Connections:
- Connect the positive (red) wire from the DHT sensor to the 5V pin on the Arduino.
- Connect the negative (black) wire from the DHT sensor to the GND (ground) pin on the Arduino.
- Connect the data (white/yellow) wire from the DHT sensor to a digital pin on the Arduino (e.g., D2).
Here’s an example wiring:
DHT11/DHT22 VCC (red) -> Arduino 5V
DHT11/DHT22 GND (black) -> Arduino GND
DHT11/DHT22 Data (white/yellow) -> Arduino D2 (or any other digital pin)
Arduino Code:
You’ll need to install the DHT sensor library if you haven’t already. To install it, open the Arduino IDE, go to Sketch -> Include Library -> Manage Libraries, and search for “DHT.” Install the “DHT sensor library by Adafruit.”
Here’s a sample Arduino code to read temperature and humidity data from the DHT sensor and print it to the serial monitor:
#include <DHT.h>
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT11 or DHT22, depending on your sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000); // 2-second delay between readings
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println(“Failed to read from DHT sensor!”);
} else {
Serial.print(“Humidity: “);
Serial.print(humidity);
Serial.print(” %\t”);
Serial.print(“Temperature: “);
Serial.print(temperature);
Serial.println(” Β°C”);
}
}
Upload the code to your Arduino Uno, and then open the Arduino IDE’s Serial Monitor (Tools -> Serial Monitor). You should see the humidity and temperature readings being printed to the Serial Monitor.
Make sure you select the correct sensor type (DHT11 or DHT22) by setting the DHTTYPE variable in the code to match the sensor you are using.
With this setup, your Arduino Uno can now read humidity and temperature data from the DHT sensor, which is useful for various environmental monitoring and control applications.





