This repository contains different implementations of a Railroad Crossing Protection System (CPS) simulation, demonstrating various approaches to visualize and control railway crossing gates.
A Python-based simulation using Matplotlib that shows the status of:
- Train presence (blue line)
- Gate position (red line)
- Road accessibility (green line)
Features:
- Real-time plotting of system states
- Two operation modes:
- Automatic (Random) - Simulates random train arrivals
- Manual (Button) - User-triggered train arrivals
- Interactive control panel with countdown timer
- Visual representation of gate delays and train passing duration
A visual representation using Tkinter that provides:
- Animated train movement
- Interactive railway crossing gate
- Status indicators for:
- Gate position (Open/Closed)
- Warning lights
- Sirens
- Real-time distance monitoring
- Automatic gate control based on train proximity
Implementation for physical simulation using ESP32 microcontroller:
train_crossing.ino: ESP32 firmware for controlling:- Servo motors (gate mechanism)
- LED indicators
- Sensors
train_crossing.py: Python interface to communicate with the ESP32 hardware
python 3.x
tkinter
matplotlib
- ESP32 Development Board
- Python with required libraries
- Hardware components:
- ESP32 Dev Kit
- Ultrasonic Sensor HC-SR04
- Servo Motor (SG90)
- Jumper Wires
-
ESP32 Operation:
- Continuously reads distance from HC-SR04 sensor
- Logic:
- If distance < 20cm: Simulates "Train Present" → Closes gate (Servo to 90°)
- If distance ≥ 20cm: Simulates "Safe/No Train" → Opens gate (Servo to 0°)
- Sends JSON status updates to computer via USB (train presence and gate position)
-
Python GUI:
- Reads serial data from ESP32
- Displays real-time status:
- Train Present/Not Present
- Gate Closed/Open
- Visualizes data in graphical format
| Component | ESP32 Pin | Notes |
|---|---|---|
| Servo Signal | GPIO 13 | PWM pin for servo control |
| Servo VCC | VIN | 5V from USB |
| Servo GND | GND | Ground |
| HC-SR04 VCC | VIN | 5V from USB |
| HC-SR04 GND | GND | Ground |
| HC-SR04 Trigger | GPIO 5 | Ultrasonic signal out |
| HC-SR04 Echo | GPIO 18 | Ultrasonic signal in |
- Line Graph Simulation:
python CPS1.py- Graphical Simulation:
python CPS2.py- Hardware Simulation:
- Upload
train_crossing.inoto ESP32 - Run the Python interface:
python train_crossing.py- The system automatically detects approaching trains
- Gates close approximately 5 seconds before train arrival
- Warning systems (lights and sirens) activate when gates are closing
- Gates remain closed until the train has completely passed
- System includes safety delays for gate opening after train passage
This project is open source and available under the MIT License.