Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
4.3 Reversing Alarm
Introduction
Parking a car safely into a garage often requires careful navigation, especially in tight spaces. In this project, you’ll create a reversing alarm system using an ultrasonic sensor, and a buzzer. This system mimics the functionality of a real-world parking sensor, providing auditory feedback about the distance to obstacles.
What You’ll Need
Below are the components required for this project:
COMPONENT INTRODUCTION |
PURCHASE LINK |
|---|---|
- |
|
Raspberry Pi |
- |
Circuit Diagram
The system uses an ultrasonic sensor to measure the distance to obstacles. The buzzer emits warning tones of varying frequency depending on the distance.
Wiring Diagram
Follow this wiring diagram to set up your system:
Running the Example
All example code used in this tutorial is available in the ai-lab-kit directory.
Follow these steps to run the example:
cd ~/ai-lab-kit/python/
sudo python3 4.3_ReversingAlarm.py
This Python script integrates an ultrasonic distance sensor and a buzzer to create a real-time distance monitoring system. When executed:
Distance Measurement: The ultrasonic sensor measures the distance to the nearest object in front of it and converts the value to centimeters.
Buzzer Alerts: Based on the measured distance:
More than 50 cm: No buzzer sound.
Between 20 cm and 50 cm: The buzzer beeps twice with a short interval.
20 cm or less: The buzzer emits rapid beeps to indicate proximity.
Code
Here is the Python code for the project:
#!/usr/bin/env python3
import time
from fusion_hat.modules import Ultrasonic, Buzzer
from fusion_hat.pin import Pin
# Ultrasonic sensor: Trig -> GPIO 27, Echo -> GPIO 22
sensor = Ultrasonic(trig=Pin(27), echo=Pin(22))
# Buzzer connected to GPIO 17
buzzer = Buzzer(Pin(17))
def get_distance():
"""
Read distance from ultrasonic sensor and print it.
Returns distance in centimeters.
"""
dis = sensor.read()
print(f"Distance: {dis:.2f} cm")
return dis
def beep(times, on_time, off_time):
"""
Make the buzzer beep with given timing.
"""
for _ in range(times):
buzzer.on()
time.sleep(on_time)
buzzer.off()
time.sleep(off_time)
def loop():
"""
Continuously measure distance and control buzzer frequency.
"""
while True:
dis = get_distance()
if dis >= 50:
# Far distance: buzzer silent
time.sleep(0.5)
elif 20 < dis < 50:
# Medium distance: slow beeping
beep(times=2, on_time=0.05, off_time=0.2)
else:
# Close distance (<= 20 cm): fast beeping
beep(times=5, on_time=0.05, off_time=0.05)
time.sleep(0.3) # Measurement interval
try:
loop()
except KeyboardInterrupt:
buzzer.off()
print("\nProgram stopped, buzzer turned off.")
Understanding the Code
Distance Measurement: The ultrasonic sensor calculates the distance.
def get_distance(): """ Read distance from ultrasonic sensor and print it. Returns distance in centimeters. """ dis = sensor.read() print(f"Distance: {dis:.2f} cm") return dis
Auditory Alerts: The buzzer’s frequency changes based on the proximity of obstacles:
>50 cm: No sound.
20-50 cm: Beeps twice with medium intervals.
≤20 cm: Rapid beeping for urgent warning.
def loop(): while True: dis = get_distance() if dis >= 50: # Far distance: buzzer silent time.sleep(0.5) elif 20 < dis < 50: # Medium distance: slow beeping beep(times=2, on_time=0.05, off_time=0.2) else: # Close distance (<= 20 cm): fast beeping beep(times=5, on_time=0.05, off_time=0.05) time.sleep(0.3) # Measurement interval
Troubleshooting
Distance Not Measured:
Cause: Incorrect wiring or sensor malfunction.
Solution:
Ensure the ultrasonic sensor’s
echoandtriggerpins are connected to GPIO 22 and GPIO 27, respectively.Test the sensor independently to confirm functionality.
Buzzer Does Not Sound:
Cause: Buzzer not connected or faulty.
Solution:
Verify the buzzer is connected to GPIO 17 and ground.
Test the buzzer by turning it on manually:
buzzer.on() time.sleep(1) buzzer.off()
Extendable Ideas
Adjustable Alert Thresholds: Allow users to set custom distance thresholds for the buzzer alerts.
Data Logging: Log distance measurements to a file for later analysis:
with open("distance_log.txt", "a") as log_file: log_file.write(f"{time.time():.3f}, {dis:.2f} cm\n")
Visual Alerts: Use LEDs of different colors to indicate proximity levels (e.g., green for safe, yellow for caution, red for danger).
Conclusion
This project demonstrates a practical application of ultrasonic sensors, combining auditory feedback for an intuitive reversing alarm system. Such systems are valuable in vehicles and robotics, offering insights into proximity detection and IoT integrations. Extend its functionality to suit your innovative ideas!