Skip to main content

Interfacing Raspberry Pi 3 with an Ultrasonic Sensor (HC-SR04) - English & Marathi

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

Popular posts from this blog

Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️

Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️ Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️ A load cell is a transducer that converts force (weight) into an electrical signal. The HX711 is a precision 24-bit analog-to-digital converter (ADC) designed for weigh scales. Today we’ll connect a load cell to Raspberry Pi 3 using the HX711 module. ЁЯзк ЁЯФз Components Required Component Quantity Raspberry Pi 3 1 Load Cell 1 HX711 Module 1 Jumper Wires 6 Breadboard (optional) 1 ЁЯФМ Pin Connections HX711 Pin Raspberry Pi Pin Pin Number VCC 5V Pin 2 GND Ground Pin 6 DT GPIO 5 Pin 29 SCK GPIO 6 Pin 31 Figure: Load Cell connected to Raspberry Pi 3 via HX711 ЁЯТ╗ Python Code from hx711 import HX711 import RPi.GPIO as GPIO import time hx = HX711(dout_pin=5, pd_sck_pin=6) hx.set_reading_format("MSB", "MSB") hx.set_reference_unit(1) hx.reset()...

Interfacing Sound Sensor with Raspberry Pi 3

ЁЯФ╣ Overview The KY-037 is a high-sensitivity sound detection sensor that can detect noise levels in the environment. It provides both analog and digital outputs. In this tutorial, we’ll interface the digital output of KY-037 with Raspberry Pi 3 Model B+ (without using an ADC like MCP3008) and detect sound events.

Interfacing Water Flow Sensor with Raspberry Pi 3 ЁЯЪ┐

Interfacing Water Flow Sensor with Raspberry Pi 3 ЁЯЪ┐ ЁЯОп Objective To measure the flow rate of water using a Water Flow Sensor (YF-S201) and Raspberry Pi 3. Useful in smart irrigation and water management systems. ЁЯз░ Components Required Component Quantity Raspberry Pi 3 1 YF-S201 Water Flow Sensor 1 10K Pull-down Resistor 1 Jumper Wires As required Breadboard 1 ⚡ Circuit Connections Sensor Pin Connect To Red (VCC) 5V (Raspberry Pi) Black (GND) GND (Raspberry Pi) Yellow (Pulse Out) GPIO18 (Pin 12) with pull-down resistor ЁЯза Python Code import RPi.GPIO as GPIO import time FLOW_SENSOR = 18 pulse_count = 0 def countPulse(channel): global pulse_count pulse_count += 1 GPIO.setmode(GPIO.BCM) GPIO.setup(FLOW_SENSOR, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.add_event_detect(FLOW_SENSOR, GPIO.FALLING, callback=countPulse) try: while True: pulse_count = 0 time.sleep(1) flow_rate = (pulse_count / 7.5) ...