A lightweight, cost-effective adaptive traffic light control system that uses computer vision and IoT technologies to optimize traffic flow in real-time.
This project presents an intelligent traffic light system that leverages computer vision and cloud computing to dynamically adjust signal timings based on real-time vehicle density. Unlike traditional fixed-time systems, our solution adapts instantly to traffic conditions, reducing congestion and improving urban mobility.
- Real-time Traffic Monitoring: Uses OpenCV for lightweight vehicle density estimation
- Cloud Integration: Firebase Realtime Database for seamless data synchronization
- IoT Control: ESP32 microcontroller for adaptive traffic light management
- Cost-Effective: Runs on low-cost hardware (Raspberry Pi + ESP32)
- High Performance: Achieves 15-20 FPS on Raspberry Pi, 100 FPS on laptop
- Robust Operation: Handles network failures with automatic fallback mode
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ IP Camera โโโโโถโ Processing Unit โโโโโถโ Firebase โ
โ (Video Input) โ โ (Raspberry Pi/ โ โ (Cloud DB) โ
โโโโโโโโโโโโโโโโโโโ โ Laptop) โ โโโโโโโโโโโโโโโโโโโ
โ - OpenCV Vision โ โ
โ - Density Calc โ โ
โโโโโโโโโโโโโโโโโโโโ โ
โผ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ Traffic Lights โโโโโโโ ESP32 Control โโโโโโโ Data Retrieval โ
โ (LED/Real) โ โ - Decision Logic โ โ โ
โโโโโโโโโโโโโโโโโโโ โ - State Machine โ โโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโ
Component | Model | Function |
---|---|---|
Processing Unit | Raspberry Pi 4B (4GB) | Computer vision processing |
Microcontroller | ESP32 | Traffic light control |
Camera | USB/IP Camera (720p-1080p) | Video capture |
LEDs | 5mm RGB LEDs | Traffic light simulation |
Mounting | Adjustable Camera Arm | Stable positioning |
- Processing: Raspberry Pi 4B + SD Card + Power Supply
- Control: ESP32 Development Board + Breadboard + Jumper Wires
- Sensors: USB Webcam or IP Camera
- Output: LEDs (Red, Yellow, Green) + 220ฮฉ Resistors
- Physical: Intersection Base + Traffic Poles + Model Vehicles
Total Cost per Intersection: < $100
- Image Preprocessing: Grayscale conversion, Gaussian blur
- Segmentation: Binary thresholding, morphological operations
- Density Estimation: ROI-based pixel counting
- Performance: 15-20 FPS on Raspberry Pi (vs. 2 FPS for YOLO)
- Real-time Database: Instant synchronization across devices
- Scalability: Support for multiple intersections
- Reliability: Automatic reconnection and error handling
- Decision Algorithm: Prioritizes lanes with highest traffic density
- State Machine: Safe transitions (Green โ Yellow โ Red)
- Failsafe Mode: Fixed-time cycle during network outages
- Video Capture: Camera monitors intersection from overhead position
- Image Processing: OpenCV pipeline estimates vehicle density per lane
- Cloud Sync: Density values uploaded to Firebase in real-time
- Decision Making: ESP32 identifies busiest lane and assigns green priority
- Signal Control: Traffic lights adapt based on actual traffic conditions
Raw Frame โ Grayscale โ Blur โ Threshold โ Morphology โ ROI Analysis โ Firebase โ ESP32 โ LEDs
Hardware | Without Firebase | With Firebase | YOLO Comparison |
---|---|---|---|
Laptop (i7, 16GB) | ~100 FPS | ~40 FPS | ~5 FPS |
Raspberry Pi 4B | ~20 FPS | ~15 FPS | ~2 FPS |
- End-to-end latency: 150-300ms (image capture โ signal change)
- Processing time: 10-30ms per frame
- Firebase sync: 50-150ms
- ESP32 response: <70ms
git clone https://github.com/saidalbardawil/Smart-Traffic-light.git
cd Smart-Traffic-light
pip install opencv-python
pip install firebase-admin
pip install numpy
pip install cvzone
- Create Firebase project at console.firebase.google.com
- Download service account key JSON
- Update Firebase credentials in Python script
- Set database rules for read/write access
- Install Arduino IDE and ESP32 board support
- Install required libraries:
- WiFi
- FirebaseESP32
- Update WiFi credentials and Firebase config
- Upload code to ESP32
ESP32 GPIO Connections:
Lane 0: Green(15), Yellow(2), Red(4)
Lane 1: Green(12), Yellow(14), Red(27)
Lane 2: Green(25), Yellow(32), Red(33)
Lane 3: Green(5), Yellow(18), Red(19)
-
Start Processing Unit:
python traffic_vision.py
-
Power ESP32: Upload firmware and connect to power
-
Position Camera: Mount overhead for clear intersection view
-
Monitor Operation: Check Firebase console for real-time data
For development/testing without real traffic:
- Use provided prototype models for simulation
- Generate traffic simulation video
- Feed video to processing pipeline
- โ Heavy Traffic: System correctly prioritizes congested lanes
- โ Balanced Traffic: Fair random selection when densities are equal
- โ Pedestrian Interference: Successfully ignores non-vehicle objects
- โ Network Failure: Automatic fallback to fixed-time operation
- โ Dynamic Changes: Real-time adaptation to changing conditions
- 10x faster than YOLO on Raspberry Pi
- Real-time responsiveness on low-cost hardware
- No GPU required unlike deep learning approaches
- Scalable architecture for city-wide deployment
- Lightweight Computer Vision: Pixel-density method instead of complex object detection
- Implicit Vehicle Weighting: Larger vehicles (buses/trucks) naturally get higher priority
- Cloud-IoT Integration: Real-time synchronization without expensive infrastructure
- Fail-Safe Operation: Automatic fallback ensures continuous operation
- Cost-Effective Deployment: Sub-$100 per intersection vs. $1000s for traditional systems
- Multi-Intersection Coordination: Network-wide traffic optimization
- Advanced Camera Features: Night vision, weather resistance
- V2I Communication: Integration with connected vehicles
- Predictive Analytics: ML-based traffic pattern analysis
- Emergency Vehicle Priority: Automatic preemption for ambulances/fire trucks
Smart-Traffic-light/
โโโ Computer_vision_code/ # OpenCV traffic density analysis
โโโ ESP32_controller_code/ # Microcontroller firmware
โโโ docs/ # Documentation and specifications
โโโ Prototype/ # 3D models and simulation files
โโโ .gitattributes # Git configuration
โโโ .gitignore # Git ignore rules
โโโ LICENSE # License file
โโโ README.md # Project documentation
- Python: OpenCV, NumPy, Firebase Admin SDK, cvzone
- Arduino: WiFi, FirebaseESP32, GPIO libraries
- Hardware: Raspberry Pi OS, Arduino IDE
Smart Traffic Light System - Mechatronics Engineering Project
Date: June 2025
๐น License
This project is shared for viewing and academic reference only. It is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0). You may read and cite this work, but you may not modify or use it commercially.
- โ Attribution: You must give appropriate credit and cite this work
- โ Academic Use: Free to reference in research and educational contexts
- โ No Commercial Use: Cannot be used for commercial purposes
- โ No Derivatives: Cannot modify, transform, or build upon this work
For commercial licensing or permission to modify, please contact the authors.
Note: This project is licensed under CC BY-NC-ND 4.0, which means modifications and derivatives are not permitted. However, you can:
- โญ Star the repository to show support
- ๐ Use it as a reference for your own implementations
- ๐ Cite it in academic work and research
- ๐ฌ Discuss ideas and improvements in Issues (for academic discussion)
- ๐ง Contact authors for collaboration opportunities
If you use this work in your research, please cite:
Smart Traffic Light System Using Computer Vision. (2025).
Mechatronics Engineering Project.
For questions, suggestions, or collaboration opportunities:
- Project Repository: GitHub
- Documentation: See
/docs
folder for detailed technical specifications
โญ Star this repository if you found it helpful!
๐ฅ This project demonstrates that intelligent traffic management can be achieved with low-cost hardware and lightweight algorithms, making it ideal for developing urban environments with budget constraints.