Iot 5 6
Iot 5 6
Aim:
To interface DHT11/DHT22 sensor with ARDUINO and write a program to measure the
temperature and humidity of the surroundings.
Theory:
1. Introduction
The DHT11 and DHT22 sensors are popular humidity and temperature sensors that provide
calibrated digital output. These sensors are commonly used in weather stations, environmental
monitoring, and home automation projects. The DHT11 is more affordable but less accurate
and has a slower response time compared to the DHT22, which provides more precise data.
In this practical, we will interface a DHT11/DHT22 sensor with an Arduino microcontroller to
measure the temperature and humidity of the surroundings.
2. Working Principle
The DHT11 and DHT22 sensors consist of two parts:
• A humidity sensor that senses relative humidity by measuring the resistance of a
material that varies with moisture.
• A thermistor that senses temperature by varying its resistance with changes in
temperature.
The sensor measures these quantities and converts them into a digital signal, which can be read
by microcontrollers like Arduino.
3. Differences Between DHT11 and DHT22
4. Hardware Requirements
• Arduino (Uno, Mega, or any compatible board)
• DHT11 or DHT22 sensor
• 10kΩ pull-up resistor (for data pin)
• Jumper wires
• Breadboard
• Power supply (from Arduino)
5. Pin Configuration
The DHT11 and DHT22 sensors typically have three or four pins:
1. VCC: Power supply (3.3V or 5V).
2. GND: Ground.
3. DATA: Serial data output.
4. NC (Optional): Not connected (only in DHT22).
The DATA pin requires a pull-up resistor (typically 10kΩ) to ensure stable communication.
6. Circuit Diagram
1. VCC of the sensor connects to the 5V pin of the Arduino.
2. GND of the sensor connects to the GND of the Arduino.
3. DATA pin connects to any digital input pin of the Arduino (e.g., Pin 2), with a 10kΩ
pull-up resistor between VCC and DATA.
7. Software Requirements
• Arduino IDE installed on your PC.
• DHT sensor library (DHT.h) by Adafruit to facilitate reading data from the sensor.
Arduino Code:
const int analogIn = A0;
int humiditysensorOutput = 0;
int RawValue= 0;
double Voltage = 0;
double tempC = 0;
double tempF = 0;
void setup(){
Serial.begin(9600);
pinMode(A1, INPUT);
}
void loop(){
RawValue = analogRead(analogIn);
Voltage = (RawValue / 1023.0) * 5000;
tempC = (Voltage-500) * 0.1;
Serial.print("\t Temperature in C = ");
Serial.print(tempC,1);
humiditysensorOutput = analogRead(A1);
Serial.print("\t Humidity: ");
Serial.print(map(humiditysensorOutput, 0, 1023, 10, 70));
Serial.println("%");
delay(5000);
}
Viva Voce:
What is the difference between DHT11 and DHT22 sensors?
• DHT11 is less accurate, has a narrower measurement range, and a slower sampling rate
compared to DHT22, which is more accurate, has a wider range, and faster response.
Aim:
To interface Ultrasonic sensor (HC-SR04) with ARDUINO and write a program to measure
and print distance readings.
Theory:
1. Introduction
The HC-SR04 ultrasonic sensor is widely used for measuring distance by emitting ultrasonic
waves and detecting the time taken for the echo to return. It is commonly used in robotics,
obstacle detection, and distance measurement projects. The sensor provides accurate readings
for distances ranging from 2 cm to 400 cm.
2. Working Principle
The HC-SR04 sensor works on the principle of echo-location:
• The sensor has two components: a transmitter (which emits ultrasonic waves) and a
receiver (which listens for the reflected waves or echo).
• The sensor sends out an ultrasonic pulse at a frequency of 40 kHz through the trigger
pin.
• When the pulse hits an object, it reflects back, and the sensor’s echo pin detects the
returning wave.
• The time interval between sending the pulse and receiving the echo is used to calculate
the distance of the object.
3. Ultrasonic Sensor Specifications
• Operating voltage: 5V DC
• Measuring range: 2 cm to 400 cm
• Accuracy: ±3 mm
• Operating frequency: 40 kHz
4. Pin Configuration
The HC-SR04 has four pins:
1. VCC: Power supply (5V).
2. GND: Ground.
3. TRIG: Trigger pin to send the ultrasonic pulse.
4. ECHO: Echo pin to receive the reflected signal.
5. Hardware Requirements
• Arduino board (e.g., Uno, Mega, or any compatible model)
• HC-SR04 ultrasonic sensor
• Jumper wires
• Breadboard
• Power supply (from Arduino)
6. Circuit Diagram
• VCC of the sensor connects to the 5V pin of the Arduino.
• GND of the sensor connects to the GND of the Arduino.
• TRIG pin connects to any digital output pin of the Arduino (e.g., Pin 9).
• ECHO pin connects to any digital input pin of the Arduino (e.g., Pin 10).
Arduino Code:
const int trigPin = 9;
const int echoPin = 10;
// defining variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
Viva Voce:
What is the working principle of the HC-SR04 sensor?
• The HC-SR04 sensor emits ultrasonic waves and measures the time it takes for the echo
to return after hitting an object, calculating the distance based on this time.