Introduction
The HC-SR04 ultrasonic sensor is used to measure distance by sending and receiving ultrasonic waves. In this tutorial, we will interface the HC-SR04 sensor with Raspberry Pi 3 to measure the distance of an object.
HC-SR04 เค เคฒ्เค्เคฐाเคธोเคจिเค เคธेเคจ्เคธเคฐ เค เคฒ्เค्เคฐाเคธोเคจिเค เคคเคฐंเค เคชाเค เคตूเคจ เคเคฃि เคช्เคฐाเคช्เคค เคเคฐूเคจ เค ंเคคเคฐ เคฎोเคเคฃ्เคฏाเคธाเค ी เคตाเคชเคฐเคฒा เคाเคคो. เคฏा เค्เคฏूเคोเคฐिเคฏเคฒเคฎเคง्เคฏे, เคเคชเคฃ HC-SR04 เคธेเคจ्เคธเคฐเคฒा Raspberry Pi 3 เคถी เคोเคกूเคจ เคตเคธ्เคคूเคे เค ंเคคเคฐ เคเคธे เคฎोเคाเคฏเคे เคคे เคถिเคू.
Components Required | เคเคตเคถ्เคฏเค เคเคเค:
1. Raspberry Pi 3
2. HC-SR04 Ultrasonic Sensor
3. Jumper Wires
4. Breadboard
Circuit Diagram | เคธเคฐ्เคिเค เคोเคกเคฃी:

Connections:
- HC-SR04 VCC → 5V (Pin 2)
- HC-SR04 GND → GND (Pin 6)
- HC-SR04 Trig → GPIO23 (Pin 16)
- HC-SR04 Echo → GPIO24 (Pin 18)
Python Code | เคชाเคฏเคฅเคจ เคोเคก:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
TRIG = 23
ECHO = 24
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
def measure_distance():
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO) == 0:
pulse_start = time.time()
while GPIO.input(ECHO) == 1:
pulse_end = time.time()
duration = pulse_end - pulse_start
distance = (duration * 34300) / 2 # Convert to cm
return round(distance, 2)
try:
while True:
dist = measure_distance()
print(f"Distance: {dist} cm")
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
}
Explaination | เคธเคฎเคाเคตเคฃी:
1. The Trig pin sends a short ultrasonic pulse.
2. The Echo pin receives the reflected wave.
3. The time taken for the wave to return is used to calculate the distance.
Trig เคชिเคจ เค เคฒ्เค्เคฐाเคธोเคจिเค เคชเคฒ्เคธ เคชाเค เคตเคคो, เคเคฃि Echo เคชिเคจ เคคो เคชเคฐเคค เคฏेเคฃाเคฐा เคธिเค्เคจเคฒ เคช्เคฐाเคช्เคค เคเคฐเคคो. เคตेเคณेเค्เคฏा เคเคงाเคฐे เค ंเคคเคฐ เคฎोเคเคฒे เคाเคคे.
Running the Code | เคोเคก เคाเคฒเคตिเคฃे:
1. Save the script as `ultrasonic.py`
2. Run the script:
python ultrasonic.py
Output | เคเคเคเคชुเค:
Distance: 10.5 cm
Distance: 10.4 cm
Distance: 10.6 cm
Conclusion | เคจिเคท्เคเคฐ्เคท:
Now, you have successfully interfaced the **HC-SR04 ultrasonic distance sensor** with Raspberry Pi 3!
เคเคคा เคคुเคฎ्เคนी HC-SR04 เค เคฒ्เค्เคฐाเคธोเคจिเค เคธेเคจ्เคธเคฐเคฒा Raspberry Pi 3 เคธोเคฌเคค เคฏเคถเคธ्เคตीเคชเคฃे เคोเคกเคฒे เคเคนे!
Stay tuned for the next sensor tutorial! ๐ | เคชुเคขीเคฒ เคธेเคจ्เคธเคฐ เค्เคฏूเคोเคฐिเคฏเคฒเคธाเค ी เคคเคฏाเคฐ เคฐเคนा! ๐
Comments