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

TOUCH PLATE BASED DOOR BELL

Title:  Touch plate based door bell  Circuit:  Components: IC 555 Resistors: 1 M, 100k, 330 ohms Transistor: BC547  PN2222A Capacitor: 10n 1 Copper plate : as touch plate. A 6v battery An LED / Ic UM66 Description: This is the simple circuit for touch plate based security system. In this project what basically done is, circuit detects stray voltages produced by mains voltage and electrostatic built  up in the room.If sufficient static voltage is detected by the plate then chip will charge up. Transistor BC 547 or PN2222A is used basically to increase the sensitivity.In place of led just connect IC um 66(melody IC). Applications: In homes, of course. This can be specially used in places like hospitals, when patients need to call doctor by himself.

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.