====== LamaPLC: HC-SR04 Ultrasonic Sensor Module ====== {{ :sensor:hc_sr04.png?250|HC SR04}} **Ultra-Sonic Ranger sensors** \\ This solution has two components: a speaker that sends out sound pulses and a microphone that detects their reflections. The distance is computed from the time delay between emission and detection, assuming the speed of sound. **HC-SR04** \\ The HC-SR04 ultrasonic sensor module is a widely used, affordable sensor popular among hobbyists for electronics projects with Arduino and Raspberry Pi, enabling non-contact distance measurement and obstacle detection. It uses sonar to measure distance by timing how long sound waves take to reflect back. ===== Key Specifications ===== * **Operating Voltage:** **5V DC** * **Operating Current:** 15mA * **Ranging Distance:** 2cm to 400cm (4m) * **Accuracy:** Up to 0.3cm, though some real-world tests suggest around 1cm * **Measuring Angle:** Less than 15 degrees in a cone * **Ultrasonic Frequency:** 40kHz * **Dimensions:** Approximately 45mm x 20mm x 15mm {{page>:tarhal}} ==== Arduino & HC-SR04 ==== * **VCC:** Power supply (5V DC) * **Trig (Trigger):** Input pin to trigger the ultrasonic burst * **Echo:** Output pin that produces a pulse proportional to the time taken for the echo to return * **GND:** Ground To operate the sensor, a microcontroller (like an Arduino) must provide a 10-microsecond (µS) high-level pulse to the Trig pin. The module then automatically sends out an 8-cycle burst of ultrasound at 40 kHz and raises the Echo pin to a high state. The duration of this high-level signal on the Echo pin is the time the sound waves took to travel to the object and back. The distance can be calculated using the formula: \\ Distance in cm = (Duration of Echo pulse in µS / 58) const int trigPin = 9; const int echoPin = 10; float duration, distance; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); Serial.begin(9600); } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration*.0343)/2; Serial.print("Distance: "); Serial.println(distance); delay(100); } {{tag>HC-SR04 Ultrasonic sensor arduino code}} This page has been accessed for: Today: {{counter|today}}, Until now: {{counter|total}}