Hardware components | ||||||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
![]() |
| × | 3 |
Smart Reverse Parking Sensor SystemAn intelligent safety solution designed to assist drivers when backing up or parallel parking. This project utilizes ultrasonic sensors to accurately detect obstacles behind the vehicle and provides audible alerts to prevent collisions.
یک سیستم هوشمند و کاربردی برای افزایش ایمنی خودرو در هنگام پارک یا حرکت به عقب است. این پروژه با استفاده از سنسورهای اولتراسونیک فاصلهٔ خودرو از موانع را تشخیص داده و با هشدارهای صوتی به راننده کمک میکند تا از برخورد جلوگیری کند.
1 / 2
#include <TM1637Display.h>
TM1637Display display(2, 3);
void setup() {
Serial.begin(9600);
while(!Serial); //
pinMode(9, OUTPUT); // Trigger
pinMode(10, INPUT); // Echo
pinMode(11, OUTPUT); // Buzzer
pinMode(4,OUTPUT); //RED
pinMode(8,OUTPUT); //yellow
pinMode(7,OUTPUT); //green
display.setBrightness(7);
Serial.println(" "); //
}
void loop() {
//
digitalWrite(9, LOW);
delayMicroseconds(2);
digitalWrite(9, HIGH);
delayMicroseconds(10);
digitalWrite(9, LOW);
digitalWrite(4, LOW); // RED
digitalWrite(8, LOW); // YELLOW
digitalWrite(7, LOW); // GREEN
//
long duration = pulseIn(10, HIGH);
//
int distance = duration * 0.034 / 2;
//
display.showNumberDec(distance);
//
Serial.print("DISTANCE:");
Serial.println(distance);
//
if (distance < 10) {
digitalWrite(11, HIGH);
digitalWrite(4,HIGH);//RED
}
else if (distance <= 15) {
digitalWrite(11, HIGH);
digitalWrite(8,HIGH);//YELLOW
delay(250);
digitalWrite(11, LOW);
digitalWrite(8,LOW);
delay(250);
}
else if (distance <= 25) {
digitalWrite(11, HIGH);
digitalWrite(8,HIGH);//YELLOW
delay(500);
digitalWrite(8,LOW);
digitalWrite(11, LOW);
delay(500);
}
else {
digitalWrite(11, LOW);
digitalWrite(7,HIGH);
}
delay(100);
}
import tkinter as tk
from tkinter import ttk
import serial
import serial.tools.list_ports
from threading import Thread, Event
from queue import Queue
class UltrasonicApp:
def __init__(self, root):
self.root = root
self.root.title(" ")
self.serial_port = None
self.is_running = False
self.data_queue = Queue()
self.stop_event = Event()
self.create_widgets()
self.find_arduino()
self.root.after(100, self.process_serial_data)
def create_widgets(self):
# Main Frame
main_frame = ttk.Frame(self.root, padding="10")
main_frame.pack(fill=tk.BOTH, expand=True)
# Distance Display
self.distance_label = ttk.Label(main_frame, text=": --- ",
font=('Tahoma', 24))
self.distance_label.pack(pady=20)
# Buzzer Status
self.buzzer_label = ttk.Label(main_frame, text=" : ---",
font=('Tahoma', 14))
self.buzzer_label.pack(pady=10)
# Connection Status
self.connection_label = ttk.Label(main_frame, text=" ...",
font=('Tahoma', 10))
self.connection_label.pack(pady=5)
# Control Button
self.control_button = ttk.Button(main_frame, text=" ",
command=self.toggle_connection)
self.control_button.pack(pady=10)
def find_arduino(self):
ports = serial.tools.list_ports.comports()
for port in ports:
print(f" : {port.device} - {port.description}")
if any(x in port.description for x in ['Arduino', 'CH340', 'USB Serial']):
try:
self.serial_port = serial.Serial(
port.device,
baudrate=9600,
timeout=1,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE
)
print(f" {port.device}")
self.is_running = True
self.stop_event.clear()
self.connection_label.config(
text=f" : {port.device}",
foreground="green"
)
Thread(target=self.read_serial_thread, daemon=True).start()
return
except Exception as e:
print(f" : {e}")
self.connection_label.config(
text=f" : {str(e)}",
foreground="red"
)
self.connection_label.config(
text=" ! .",
foreground="red"
)
def read_serial_thread(self):
buffer = ""
while self.is_running and not self.stop_event.is_set():
try:
# Read all available data
data = self.serial_port.read(self.serial_port.in_waiting or 1)
buffer += data.decode('utf-8', errors='ignore')
# Split lines
while '\n' in buffer:
line, buffer = buffer.split('\n', 1)
line = line.strip()
if line:
print(f" : {line}")
self.data_queue.put(line)
except serial.SerialException as e:
print(f" : {e}")
self.is_running = False
self.connection_label.config(
text=" !",
foreground="red"
)
except Exception as e:
print(f" : {e}")
self.is_running = False
def process_serial_data(self):
try:
while not self.data_queue.empty():
line = self.data_queue.get()
if line.startswith("DISTANCE:"):
try:
distance = int(line.split(":")[1].strip())
self.update_display(distance)
except (IndexError, ValueError) as e:
print(f" : {e}")
elif line == "Error:NoPulse":
self.distance_label.config(
text=": ",
foreground="red"
)
except Exception as e:
print(f" : {e}")
finally:
if self.is_running:
self.root.after(100, self.process_serial_data)
def update_display(self, distance):
self.distance_label.config(text=f": {distance} ")
if distance < 10:
color = "red"
buzzer_status = " "
elif 10 <= distance <= 15:
color = "brown"
buzzer_status = " "
elif 15 < distance <= 25:
color = "orange"
buzzer_status = " "
else:
color = "green"
buzzer_status = " "
self.distance_label.config(foreground=color)
self.buzzer_label.config(text=f" : {buzzer_status}")
def toggle_connection(self):
if self.is_running:
self.is_running = False
self.stop_event.set()
if self.serial_port and self.serial_port.is_open:
self.serial_port.close()
self.control_button.config(text=" ")
self.connection_label.config(
text=" ",
foreground="red"
)
self.distance_label.config(
text=": --- ",
foreground="black"
)
self.buzzer_label.config(text=" : ---")
else:
self.find_arduino()
self.control_button.config(text=" ")
def __del__(self):
if self.serial_port and self.serial_port.is_open:
self.serial_port.close()
if __name__ == "__main__":
root = tk.Tk()
app = UltrasonicApp(root)
try:
root.mainloop()
except KeyboardInterrupt:
pass
finally:
if app.serial_port and app.serial_port.is_open:
app.serial_port.close()
Comments