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

IOT : GARDEN WATER SPRINKLER SYSTEM

IoT projects have gained immense popularity due to their ability to leverage the power of interconnected devices and data to create innovative solutions across various domains. IoT projects involve integrating sensors, actuators, and communication technologies to enable the seamless exchange of data between physical devices and the internet. These projects range from simple DIY experiments to complex industrial applications, each offering unique opportunities to transform our lives and improve efficiency. Whether it's creating a smart home automation system, monitoring environmental conditions, optimizing energy usage, or developing intelligent transportation systems, applications of IoT open up a world of possibilities for innovation and connectivity. By harnessing the potential of IoT, we can build intelligent and interconnected systems that revolutionize industries, enhance daily life, and pave the way for a more efficient and sustainable future. In this project we will be using...

POWER BI - THE ARCHITECTURE

Power BI Architecture: A Comprehensive Guide to Building Effective Data Analytics Solutions     Understanding Power BI Architecture: Power BI's architecture comprises four main components, each playing a pivotal role in the data analytics process: Data Sources:   Power BI connects to a wide range of data sources, including databases, cloud services, Excel files, web services, and more. This versatility enables organizations to consolidate their data from various systems into a single dashboard. Data Transformation:   Once the data is sourced, it undergoes transformation and cleaning processes to make it suitable for analysis. Power BI's Power Query Editor provides a user-friendly interface to manipulate, filter, and shape data according to specific requirements. Data Model:   The heart of Power BI's architecture lies in its data model, whic...

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.