Education

Education

Humidity & Temperature Sensor

DHT11 Sensor

The DHT11 is a small and powerful sensor that can measure temperature and humidity in the air!

It works like a mini weather station inside your project! Whether it’s hot, cold, dry, or humid, the DHT11 sensor can tell you!

Measures temperature in °C (Celsius) or °F (Fahrenheit)
Measures humidity (how much water is in the air)
Uses only one pin to send data to Arduino

The Science Behind DHT11!

The DHT11 sensor has two parts inside that help it measure temperature and humidity:

1️⃣ Temperature Sensor: Measuring Heat

  • Inside the DHT11, there is a tiny thermistor (a type of resistor that changes with temperature).

  • When the temperature changes, the resistance inside the thermistor changes too!

  • The sensor measures this resistance and converts it into temperature data!

2️⃣ Humidity Sensor: Measuring Moisture

  • The DHT11 also has a special layer that absorbs water from the air.

  • When there is more water in the air (humid conditions), this layer stores more charge.

  • The sensor measures this change and tells us the humidity level (0% to 100%)!

🔹 Example:
If it’s raining, the humidity is high (80-100%)
If you are in a desert, the humidity is low (10-20%)

Real-Life Uses of the DHT11 Sensor

1. Smart Home Systems – Your air conditioner can adjust automatically based on temperature & humidity!

2. Plant Monitoring – Helps keep plants in the right temperature & moisture conditions!

3. Weather Stations – Used in DIY weather stations to report local conditions!

4. Medical Devices – Some hospitals use it to maintain room conditions for patients!

5. IoT Smart Gadgets – Works with WiFi & Bluetooth to send data to smartphones

How to connect DHT11 Sensor on the Blue Elixer Board

Code Breakdown: DHT11 Sensor with Arduino!

Now, let’s write a program to read temperature & humidity from the DHT11 sensor and display it on the serial monitor!

#include <dht11.h>  // Include the DHT11 sensor library

#define DHT11_PIN 11  // Connect the DHT11 data pin to pin 11

dht11 DHT;  // Create a DHT object

void setup() {
  Serial.begin(9600);  // Start serial communication
}

void loop() {
  int chk = DHT.read(DHT11_PIN);  // Read temperature & humidity from DHT11

  Serial.print("Temperature: ");
  Serial.print(DHT.temperature);
  Serial.println("°C");

  Serial.print("Humidity: ");
  Serial.print(DHT.humidity);
  Serial.println("%");

  delay(2000);  // Wait 2 seconds before reading again
}

Code Breakdown (How It Works!)

1️⃣ Including the DHT11 Library

#include <dht11.h
  • This library helps the Arduino communicate with the DHT11 sensor easily.

2️⃣ Defining the Sensor Pin

#define DHT11_PIN 11
  • The DHT11 sensor is connected to pin 11 on the Arduino.

  • We use #define so we can easily change the pin later if needed.

3️⃣ Creating the DHT Object

dht11 DHT;
  • This creates a sensor object called DHT so we can read data from the sensor.

4️⃣ Setting Up Serial Communication in setup()

void setup() {
  Serial.begin(9600);  
}
  • This allows the Arduino to send sensor data to your computer’s Serial Monitor!

5️⃣ Reading Temperature & Humidity in loop()

int chk = DHT.read(DHT11_PIN);
  • This reads temperature & humidity from the DHT11 sensor.

6️⃣ Displaying Temperature & Humidity on Serial Monitor

Serial.print("Temperature: ");
Serial.print(DHT.temperature);
Serial.println("°C");

Serial.print("Humidity: ");
Serial.print(DHT.humidity);
Serial.println("%");
  • This prints temperature & humidity data to the Serial Monitor in a readable format!

7️⃣ Adding a Delay to Avoid Rapid Readings

delay(2000);
  • The DHT11 can only be read every 2 seconds, so we add a delay.

What You’ll See!

1️⃣ Open the Serial Monitor (Ctrl + Shift + M in Arduino IDE).
2️⃣ You’ll see real-time temperature & humidity readings update every 2 seconds!

Fun Activity!

In this activity, you will program your Arduino Nano to use the DHT11 temperature and humidity sensor along with five LEDs to create a temperature warning system.

The system will monitor the current temperature and use the LEDs to visually indicate how hot or cold it is.

  • If the temperature is low, fewer LEDs will turn on.

  • If the temperature is high, all LEDs will turn on, acting as a warning.

This is similar to real-life warning systems used in factories, greenhouses, and smart homes to monitor temperature changes.

#include <dht11.h>  // Include the DHT11 sensor library

#define DHT11_PIN 11  // DHT11 data pin connected to Arduino pin 11
#define ledPin1 4
#define ledPin2 5
#define ledPin3 6
#define ledPin4 7
#define ledPin5 8

dht11 DHT;  // Create a DHT object

void setup() {
  Serial.begin(9600);  // Start serial communication

  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
}

void loop() {
  int chk = DHT.read(DHT11_PIN);  // Read temperature and humidity from the DHT11 sensor
  int temperature = DHT.temperature;  // Store the temperature value

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println("°C");

  // LED Control Based on Temperature Ranges
  if (temperature < 15) {  // Very Cold
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin5, LOW);
  }
  else if (temperature >= 15 && temperature < 25) {  // Cool
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin5, LOW);
  }
  else if (temperature >= 25 && temperature < 30) {  // Warm
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin5, LOW);
  }
  else if (temperature >= 30 && temperature < 35) {  // Hot
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, HIGH);
    digitalWrite(ledPin5, LOW);
  }
  else {  // Very Hot
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, HIGH);
    digitalWrite(ledPin5, HIGH);
  }

  delay(2000);  // Wait 2 seconds before reading again
}

How It Works

1. Setting Up the Components in setup()

void setup() {
  Serial.begin(9600);  // Start serial communication

  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
}
  • The DHT11 sensor is set up to measure temperature and humidity.

  • The five LEDs are configured as outputs so they can be controlled by the Arduino.

2. Reading Temperature from the DHT11 Sensor

int chk = DHT.read(DHT11_PIN);
int temperature = DHT.temperature;
  • The DHT11 sensor is read using the DHT.read() function.

  • The temperature value is stored in the temperature variable.

3. Displaying Temperature in the Serial Monitor

Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println("°C");
  • This prints the current temperature to the Serial Monitor.

  • You can view this in Arduino IDE → Tools → Serial Monitor.

4. Controlling LEDs Based on Temperature

Temperature Range

LEDs Turned On

Below 15°C (Very Cold)

1 LED

15°C - 24°C (Cool)

2 LEDs

25°C - 29°C (Warm)

3 LEDs

30°C - 34°C (Hot)

4 LEDs

Above 35°C (Very Hot)

All 5 LEDs

if (temperature < 15) {  
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
}

If the temperature is very cold (<15°C) → Only one LED turns on.

else if (temperature >= 15 && temperature < 25) {  
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
}
  • If the temperature is between 15°C and 25°CTwo LEDs turn on.

else if (temperature >= 25 && temperature < 30) {  
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, LOW);
}
  • If the temperature is warm (25°C - 29°C)Three LEDs turn on.

else if (temperature >= 30 && temperature < 35) {  
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledPin5, LOW);
}
  • If the temperature is hot (30°C - 34°C)Four LEDs turn on.

else {  
  digitalWrite(ledPin1, HIGH);
  digitalWrite(ledPin2, HIGH);
  digitalWrite(ledPin3, HIGH);
  digitalWrite(ledPin4, HIGH);
  digitalWrite(ledPin5, HIGH);
}
  • If the temperature is very hot (above 35°C)All five LEDs turn on.

What You’ll See

  1. The serial monitor will display temperature readings every 2 seconds.

  2. The LEDs will turn on based on the temperature:

    • Low temperature → Few LEDs ON.

    • High temperature → All LEDs ON.

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+91 9910566229

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+91 9910566229

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+91 9910566229

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us