Getting Started with DHT Sensors and Arduino
The DHT sensor family (DHT11, DHT22, etc.) are popular choices for measuring temperature and humidity in Arduino projects. These sensors are low-cost, easy to use, and provide reliable readings for a variety of applications. This tutorial will guide you through connecting a DHT sensor to your Arduino and reading the temperature and humidity data.
How Does It Work?
Ultrasonic sensors emit sound waves at a frequency too high for humans to hear. These sound waves travel through the air at the speed of sound (approximately 343 m/s). When an object is in front of the sensor, the sound waves reflect back, and the receiver detects them. By measuring the time between sending and receiving the sound waves, we can calculate the distance between the sensor and the object
Specifications
- Measuring Range: The HC-SR04 can measure distances from approximately 2cm to 400cm (0.8 inches to 157 inches).
- Accuracy: The accuracy is around 0.3cm (0.1 inches).
- Operating Voltage: Typically operates at 5V.
- Components: The module consists of an ultrasonic transmitter and an ultrasonic receiver.
Arduino Uno
Breadboard
SR-04 ultrasonic sensor
Jumper wires
#define trigPin 10 #define echoPin 13 float duration, distance; void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { // Write a pulse to the HC-SR04 Trigger Pin digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Measure the response from the HC-SR04 Echo Pin duration = pulseIn(echoPin, HIGH); // Determine distance from duration // Use 343 metres per second as speed of sound distance = (duration / 2) * 0.0343; // Send results to Serial Monitor Serial.print("Distance = "); if (distance >= 400 || distance <= 2) { Serial.println("Out of range"); } else { Serial.print(distance); Serial.println(" cm"); delay(500); } delay(500); }
Visualizing the Results:
- Open the Arduino IDE serial monitor (Tools > Serial Monitor).
- With each loop iteration (every second in this example), you’ll see the measured distance in centimeters displayed on the serial monitor.
Extra Notes:
- The speed of sound is assumed to be 340 m/s in the code. This value can be adjusted for more precise measurements depending on environmental conditions.
- The SR-04 has a blind spot of a few centimeters close to the sensor. Be mindful of this limitation when designing your project.
- Explore various applications for the SR-04, such as object detection, robot obstacle avoidance, and automatic door openers.