Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/engineermichael/rasb-pi-research-project-electronic-engineering-

Pending Revisions, NDA Final Version
https://github.com/engineermichael/rasb-pi-research-project-electronic-engineering-

python raspberry-pi traffic-simulation

Last synced: 29 days ago
JSON representation

Pending Revisions, NDA Final Version

Awesome Lists containing this project

README

        

# Rasb-PI-Research-Project-Electronic-Engineering-

Creating a Raspberry Pi-based ultrasound sensor system to measure the height of bridges or gaps (such as for a vehicle’s clearance) involves the following steps:

1. Components Needed:

• Raspberry Pi (any model, e.g., Raspberry Pi 4 or Zero W)

• HC-SR04 Ultrasonic Sensor (used to measure distance)

• Jumper wires

• Breadboard (optional for prototyping)

• Python (with libraries like RPi.GPIO or gpiozero)

2. Hardware Setup:

Connect the HC-SR04 Ultrasonic Sensor to the Raspberry Pi as follows:

HC-SR04 Pin Raspberry Pi Pin

VCC 5V

GND GND

Trig GPIO 23 (or any free GPIO pin)

Echo GPIO 24 (or any free GPIO pin)

3. Wiring the Ultrasonic Sensor:

1. VCC: Connect to 5V (Raspberry Pi).

2. GND: Connect to ground (Raspberry Pi).

3. Trig: Connect to a GPIO pin (e.g., GPIO 23).

4. Echo: Connect to a GPIO pin (e.g., GPIO 24).

4. Python Code for Distance Measurement

The HC-SR04 sensor works by sending a pulse and measuring the time taken for the signal to bounce back. Using the formula:

We can calculate the distance (in centimeters) based on the pulse duration.

Here is the Python code to interface with the sensor:

import RPi.GPIO as GPIO

import time

# Set up GPIO pins

TRIG = 23

ECHO = 24

# Set up the GPIO mode

GPIO.setmode(GPIO.BCM)

GPIO.setup(TRIG, GPIO.OUT)

GPIO.setup(ECHO, GPIO.IN)

def measure_distance():

# Send a pulse to the Trig pin to trigger the ultrasonic sensor

GPIO.output(TRIG, GPIO.LOW)

time.sleep(0.1) # Wait for sensor to settle

GPIO.output(TRIG, GPIO.HIGH)

time.sleep(0.00001) # Send a very short pulse

GPIO.output(TRIG, GPIO.LOW)

# Record the start time

while GPIO.input(ECHO) == GPIO.LOW:

pulse_start = time.time()

# Record the end time

while GPIO.input(ECHO) == GPIO.HIGH:

pulse_end = time.time()

# Calculate the time difference between sending and receiving the pulse

pulse_duration = pulse_end - pulse_start

# Speed of sound is approximately 34300 cm/s (at room temperature)

distance = pulse_duration * 34300 / 2 # Divide by 2 to get the one-way distance

return distance

def main():

try:

while True:

distance = measure_distance()

print(f"Distance: {distance:.2f} cm")

# Define a threshold (for example, if the bridge height is below 100 cm)

if distance < 100:

print("Warning: Low clearance detected!")

time.sleep(1) # Delay before the next measurement

except KeyboardInterrupt:

print("Program stopped by user")

GPIO.cleanup()

if __name__ == "__main__":

main()

5. Explanation of Code:

• GPIO Setup: We set up the Raspberry Pi GPIO pins for the TRIG and ECHO pins to communicate with the ultrasonic sensor.

• Distance Calculation:

• The TRIG pin is used to initiate the pulse. When it goes high for a very short time, it sends out an ultrasonic sound wave.

• The ECHO pin is used to measure how long it takes for the sound wave to return after hitting an object.

• By measuring the time duration between sending and receiving the pulse, we can calculate the distance using the speed of sound formula.

• Threshold Warning:

• If the measured distance is below a defined threshold (in this case, 100 cm), it prints a warning. You can adjust this threshold based on the bridge’s clearance you want to monitor.

6. Running the Code:

To run the code:

1. Save the script as ultrasonic_bridge.py.

2. Open a terminal on your Raspberry Pi.

3. Run the script:

python3 ultrasonic_bridge.py

The output will look like this:

Distance: 12.34 cm

Warning: Low clearance detected!

Distance: 95.67 cm

Warning: Low clearance detected!

Distance: 150.23 cm

7. Possible Enhancements:

1. Multiple Sensors: You can add more ultrasonic sensors (e.g., placing them at different points of the bridge) and integrate them into the system.

2. Data Logging: You can log the measurements to a CSV file or send them to a remote server for further analysis.

3. Alert System: Integrate a buzzer or send email/SMS alerts if the clearance is below a certain level.

4. Real-time Display: Add a real-time display (using an LCD or an OLED screen) to show the distance continuously.

8. Important Notes:

• Ultrasonic sensor limitations: The HC-SR04 has a limited range, typically between 2 cm to 400 cm, and its accuracy can degrade with longer distances or noisy environments.

• Power Supply: Ensure your Raspberry Pi is properly powered, especially if using sensors and additional peripherals.

GNU General Public License v3.0

This basic setup will allow you to measure the clearance height of bridges, which could be used for vehicle height monitoring or automated systems for detecting low clearance.