Density Based Traffic Light with Raspberry Pi 5 - Proteus Simulation: four signal heads and eight FC-51 IR sensors on a Raspberry Pi 5

Density Based Traffic Light with Raspberry Pi 5 - Proteus Simulation

40
700
60
25
60
PCBWay

Hello friends, I hope you all are doing great. Today, I am going to share the Density Based Traffic Light with Raspberry Pi 5 - Proteus Simulation, project 104 of our IoT Projects series. An ordinary traffic light gives every road the same green in a fixed turn, even when one road is empty and another has a long queue. A density based traffic light counts what is actually waiting and gives the green where it is needed. It is one of the most popular final year projects, and here is a complete one with the circuit, the Python program and a Proteus simulation you can run before you build anything.

The junction has four roads, north, east, south and west, each with its own signal head of red, yellow and green LEDs. Two IR sensors on each road tell the Raspberry Pi 5 whether a car is waiting at the stop line and whether the queue reaches back to the second sensor. From these the Pi works out the traffic density of every road, picks the busiest road for the next green, makes the green longer when the queue is long and shorter when it is not, ends a green early when its road empties, and makes sure no road waits too long. An ambulance can take the green from a web dashboard on a laptop or a phone. In this tutorial, we will go through the circuit, how an IR sensor detects a car, how two sensors give a density, the decision rules and the Python code. Then we run a whole rush hour in the simulation. At the end, you can download the project with the program and every library file.

NOTICE: Our team has put a lot of effort into this project: the circuit, the code and every part in the simulation are our own work, and you get them all free. Your feedback is the fuel that keeps us going, so please tell us what you think in the comments below. And if our free projects help you, you can buy us a coffee. So, let's get started with the density based traffic light with Raspberry Pi 5 in Proteus simulation:

Density Based Traffic Light with Raspberry Pi 5 - Proteus Simulation: four signal heads and eight FC-51 IR sensors on a Raspberry Pi 5
Figure: The density based traffic light in a Proteus simulation.
Density based traffic light with Raspberry Pi 5 Proteus simulation running: the green moves to the busiest road
Figure: The project working in the Proteus simulation (an animation of the main steps).

What Does This Density Based Traffic Light Do?

  • Traffic density: two IR sensors a road give EMPTY, LOW (one car) or HIGH (a long queue).
  • The busiest road first: the next green goes to the waiting road with the highest density.
  • Smart green times: 4 s for a LOW road and 8 s for a HIGH one in the simulation; the green is held while nobody else waits.
  • Early end: a green ends when its road has emptied and another road waits.
  • No road waits for ever: a road that has waited 20 s gets the next green, even against a busier road.
  • Ambulance priority: NORTH, EAST, SOUTH or WEST on the dashboard gives that road the green at once.

In the simulation, every FC-51 sensor has its own pop-up: its Scene page moves a car in front of the sensor or away from it. The signal heads are plain 5 mm LEDs, so the sheet itself shows the junction working.

Components Used in the Proteus Simulation

All the parts come from our Proteus libraries, each with its own tutorial. The project zip at the end includes every LIB and DLL file.

The parts of the density based traffic light and their library posts
PartProteus deviceLibrary postConnection
Raspberry Pi 5RASPBERRY PI 5Raspberry Pi 5 Library for ProteusPI1, traffic_light.py, Ethernet 192.168.1.51:8000
FC-51 IR obstacle sensors (x 8)FC51ADVTEPInfrared Sensor Library for ProteusU1 - U8: two a road, OUT LOW = a car
5 mm LEDs (x 12)LED5MMREDTEP, LED5MMYELLOWTEP, LED5MMGREENTEP5mm LED Library for ProteusD1 - D12: the four signal heads
220 ohm resistors (x 12)RESISTOR4BANDTEPResistor Library for ProteusR1 - R12: one in series with each LED
Laptop WiFi MonitorLAPTOP WIFI MONITORLaptop WiFi MonitorPC1
Mobile WiFi MonitorMOBILE WIFI MONITORMobile WiFi MonitorMOB1

Proteus Simulation of the Traffic Light: the Circuit

Density based traffic light Proteus simulation circuit: a Raspberry Pi 5, four signal heads and eight FC-51 IR sensors
Figure: The complete circuit on an A3 Proteus sheet: one column a road, the signal head above its two sensors.
The connections of the density based traffic light
RoadRed / yellow / greenCAR / QUEUE sensors
NORTHGPIO17 / 27 / 22 (pins 11 / 13 / 15)GPIO4 / 18 (pins 7 / 12)
EASTGPIO5 / 6 / 13 (pins 29 / 31 / 33)GPIO23 / 24 (pins 16 / 18)
SOUTHGPIO19 / 26 / 21 (pins 35 / 37 / 40)GPIO25 / 8 (pins 22 / 24)
WESTGPIO20 / 16 / 12 (pins 38 / 36 / 32)GPIO7 / 11 (pins 26 / 23)

Each LED runs from its GPIO pin through a 220 ohm resistor to ground: 3.3 V minus the LED's forward voltage across 220 ohm is a few milliamps, well inside what a Pi pin can give. The FC-51s take their supply from the Pi's 3.3 V pin, so their outputs swing between 0 and 3.3 V and go straight to the GPIO pins. Twelve outputs and eight inputs use twenty of the header's GPIO pins.

How the Density Based Traffic Light Works

How an IR Sensor Detects a Car

The FC-51 has an infrared LED and a photodiode side by side, both looking forward. When something stands in front, it reflects the infrared back into the photodiode; an LM393 comparator compares the photodiode's signal with a threshold set by the trimmer and pulls OUT LOW. With nothing in its range, OUT stays HIGH. The range is about 2 to 30 cm, set by the trimmer, so on a model junction a toy car in front of the sensor is a car at the stop line. On a real road, the same job is done by induction loops in the tarmac, radar or cameras, but the logic is the same: is there a vehicle here or not.

From Two Sensors to a Traffic Density

One sensor on a road can only say whether a car is waiting. Two sensors say more: the CAR sensor sits at the stop line, and the QUEUE sensor a few car lengths back. Nothing in front of either means the road is EMPTY. One of them blocked is LOW: a car or two waiting, or one coming. Both blocked means the queue reaches back past the second sensor: HIGH density. A real junction might use three or four sensors a road for finer steps, but two already give the three levels the decisions need.

Choosing the Next Green

When a green ends, the Pi looks at every other road with a car and picks the one with the highest density. If two roads have the same density, the one that has waited longer goes first. The wait of a road starts when its first car arrives at a red light, not at some fixed time, so a road that just got a car does not jump ahead of one whose cars have been standing for a while. A road with no car never gets a green, which is where most of the time saving over a fixed cycle comes from: no green is wasted on an empty road.

How Long a Green Lasts

The green's length is set when it starts: 4 s for a LOW road and 8 s for a HIGH road in the simulation, standing in for about 15 s and 30 s on a real junction. Three rules can change it. If no other road has a car when the time is up, the green is simply held, since changing the lights would only stop the traffic for nothing. If the road empties and stays empty for a second while another road waits, the green ends early, after a minimum of 2 s. And if another road has waited 20 s, it cuts the green short, again after the 2 s minimum. Every change goes through 2 s of yellow and 1 s of all red, so the junction is clear before the next road moves.

No Road Waits For Ever

Giving the green to the busiest road is fair to the traffic as a whole, but on its own it can be unfair to one driver: a quiet side road could wait as long as the main roads stay busy. Traffic engineers call this starvation. The maximum wait solves it: once a road has waited 20 s, it goes next, whatever the others have, and it does not wait for a running green to finish. The number is a compromise; a longer limit lets the busy roads flow better, a shorter one keeps the quiet roads happier.

Ambulance Priority

Emergency vehicles get priority on many real junctions, triggered by a transmitter in the vehicle or by a control room. Here it is the dashboard: NORTH, EAST, SOUTH or WEST ends the running green through yellow and all red, and gives that road the green, held until NORMAL. Then the density rules take over again.

The Python Code Explained

The program is traffic_light.py, written for the Raspberry Pi 5 with the lgpio library. One thread reads the sensors, one runs the lights, and the main thread serves the dashboard. The pins come in two tables, a road each:

LIGHT = {'NORTH': (17, 27, 22), 'EAST': (5, 6, 13), 'SOUTH': (19, 26, 21), 'WEST': (20, 16, 12)}   # red, yellow, green (BCM numbers)
SENSE = {'NORTH': (4, 18), 'EAST': (23, 24), 'SOUTH': (25, 8), 'WEST': (7, 11)}                   # CAR (stop line), QUEUE (further back)

The times, short for the simulation:

GREEN_LOW, GREEN_HIGH, MIN_GREEN, YELLOW, ALL_RED = 4.0, 8.0, 2.0, 2.0, 1.0                         # seconds (short for the simulation)
MAX_WAIT, EMPTY_GAP = 20.0, 1.0                                    # the longest wait for a road with a car; an empty road for this long ends its green

Every 50 ms the sensor thread reads both sensors of each road (LOW = a car) and turns them into the density, 0, 1 or 2:

            car = lgpio.gpio_read(chip, SENSE[r][0]) == 0
            d = 2 if car and queue else 1 if car or queue else 0

Choosing the next green: a road that has waited MAX_WAIT first, otherwise the highest density, a tie to the longest wait:

        late = [r for r in waiting if waited(r, now) >= MAX_WAIT]
        r = max(waiting, key=lambda x: (road[x]['density'], waited(x, now)))

The green's planned length from the road's density, and the early end for an empty road:

        plan = 0.0 if s['ambulance'] == r else GREEN_HIGH if d == 2 else GREEN_LOW
        if on >= MIN_GREEN and others and empty_since and now - empty_since >= EMPTY_GAP:

An ambulance on another road ends the green at once, and every head is set from one function, red unless it is the green or the yellow road:

        if amb and amb != r:
        for pin, ch in zip(LIGHT[r], 'RYG'):

The dashboard's links are Refresh and NORMAL, then NORTH, EAST, SOUTH and WEST for an ambulance; /data gives the phase, every road's light, density and wait, the counts of cars arriving at each stop line and the average wait as JSON.

How to Run the Proteus Simulation

  1. Download the project zip below and install the library files and the TEPPi folder (the steps are under the download button).
  2. Open Pi-5-Density-Traffic-Light.pdsprj from the Proteus Simulation folder. Keep traffic_light.py and the DLL files next to it.
  3. Press Play: READY, all red. The eight FC-51 pop-ups open.
  4. Put cars in front of the sensors: a pop-up's Scene page, distance 10 cm (30 cm means nothing in range). U1 / U2 are north's CAR and QUEUE, U3 / U4 east's, U5 / U6 south's, U7 / U8 west's.
  5. Open the Laptop or the Mobile WiFi Monitor (press Go) for the dashboard; try an ambulance and NORMAL.

Simulation Results: the Traffic Light in Proteus

We pressed Play and acted out a short rush hour with the FC-51 pop-ups: a single car on the north road, then a long queue on the east road and one car on the west road at the same moment, a long queue on the south road, cars driving away, and finally an ambulance from the phone. The Pi's terminal printed every decision with its reason, and the sheet shows the four signal heads changing. The times are the program's clock; the whole run took 73.0 simulated seconds.

Ready: All Red

The program printed READY - all red, waiting for traffic at 0.0 s and started its web server on 192.168.1.51, port 8000. All four red LEDs were lit, and every FC-51 saw nothing within its 20 cm range: the scene at 30 cm and OUT HIGH. The dashboard showed ALL RED, with every road EMPTY.

Density based traffic light Proteus simulation ready: all four signal heads red, no traffic on any road
Figure: Ready: all red.
FC-51 pop-up in the density based traffic light simulation: nothing within the 20 cm range, OUT HIGH
Figure: An FC-51 with nothing in front.
Mobile WiFi Monitor showing the traffic light dashboard in the simulation: ALL RED, every road EMPTY
Figure: The dashboard on the phone.

One Car: Green at Once

We moved a car to 10 cm in front of U1, the north CAR sensor. Its comparator pulled OUT LOW 2 ms later, and at 19.7 s the program printed NORTH: LOW and then NORTH GREEN - LOW, waited 0.0 s, 4 s planned. No other road had a car, so there was nothing to wait for; a fixed-cycle light could have kept this car at red until its turn came round. At 23.8 s the 4 s were up, but still no other road had a car, so the green stayed on: NORTH held green - no other road has a car.

Density based traffic light Proteus simulation: the north road green for its first car, the others red
Figure: North green for one car.
FC-51 pop-up in the density based traffic light simulation: a car at 10 cm, OUT LOW 2 ms after the comparator
Figure: The north CAR sensor: a car at 10 cm.
Raspberry Pi 5 terminal in the density based traffic light simulation: NORTH GREEN, then held green
Figure: The terminal: green at once, then held.

HIGH Beats LOW

At 26.9 s we put cars in front of both east sensors, U3 and U4, and one car in front of the west CAR sensor, U7, at the same moment. The east road went HIGH and the west road LOW. The north green's time was already up and now other roads were waiting, so it ended at once: NORTH YELLOW - its time is up, then 2 s of yellow and 1 s of all red. Then the choice: east HIGH against west LOW, and east won with EAST GREEN - HIGH, waited 3.0 s, 8 s planned, the long green for the long queue. The dashboard showed the east green counting towards its 8 s, with the west road waiting.

Density based traffic light Proteus simulation: the east road green for its long queue, both east sensors blocked
Figure: East green: HIGH beats LOW.
FC-51 pop-up in the density based traffic light simulation: the east QUEUE sensor blocked at 10 cm
Figure: The east QUEUE sensor: the queue reaches back.
Laptop WiFi Monitor in the density based traffic light simulation: EAST GREEN of 8 s, west waiting
Figure: The dashboard: east green, 8 s planned.

The Busiest Road, Not the Next One

While east had its green, a long queue built up on the south road (U5 and U6) and the north car drove away. East's 8 s ran out at 38.0 s. After the yellow and the all red, at 41.0 s, south was HIGH and had waited 6.4 s, while west was LOW and had waited 14.1 s. A fixed cycle would simply have gone to the next road in turn; the density rule went to the busiest road: SOUTH GREEN - HIGH, waited 6.4 s, 8 s planned. The laptop's dashboard showed the west road waiting 16.7 s by then. The east cars then drove through, and the east road showed EMPTY at 44.1 s.

Density based traffic light Proteus simulation: the south road green before the west road, its queue HIGH
Figure: South green: the busiest road first.
Laptop WiFi Monitor in the density based traffic light simulation: SOUTH GREEN, west waiting 16.7 s
Figure: The dashboard: west still waiting.

No Road Waits For Ever

The west car had been waiting since 26.9 s. At 47.0 s it reached the 20 s limit, and the south green, only 6 of its 8 s old, was cut short: SOUTH YELLOW - WEST has waited 20 s. After the yellow and the all red came WEST GREEN - waited 23 s, 4 s planned. The south queue was still there and had to wait, but only through a short LOW green.

Density based traffic light Proteus simulation: the west road green after its 20 s wait, south waiting
Figure: West green after the longest wait.
Raspberry Pi 5 terminal in the density based traffic light simulation: SOUTH YELLOW - WEST has waited 20 s
Figure: The terminal: the green cut short for a late road.

An Empty Road Ends Its Green

The west car drove off at 52.2 s and the west road was EMPTY. One second later, with south waiting, the program did not waste the rest of the 4 s: WEST YELLOW - its road is empty at 53.2 s, and SOUTH GREEN - HIGH, waited 7.3 s, 8 s planned at 56.2 s. The south queue got its long green after all.

Density based traffic light Proteus simulation: the south road green again after the west road emptied
Figure: South green again.

Ambulance Priority

At 58.9 s we pressed WEST on the phone's dashboard: an ambulance on the west road. The page in that moment still showed the south green, but the program acted at once: SOUTH YELLOW - an ambulance on the WEST road, and after the yellow and the all red, WEST GREEN - an AMBULANCE, held at 61.9 s. The west road was empty, but the green stayed on: an ambulance needs a clear road whatever the sensors say. The laptop showed WEST GREEN - AMBULANCE with the south road waiting.

Mobile WiFi Monitor in the density based traffic light simulation: WEST pressed for an ambulance
Figure: An ambulance from the phone.
Density based traffic light Proteus simulation: the west road green for an ambulance, every other road red
Figure: West green for the ambulance.
Laptop WiFi Monitor in the density based traffic light simulation: WEST GREEN - AMBULANCE, south waiting
Figure: The dashboard: the ambulance green.

Back to Normal

NORMAL on the laptop at 65.2 s handed control back to the density rules. The west green had already been on for longer than a LOW green and south was waiting, so it ended: WEST YELLOW - its time is up, and at 68.9 s SOUTH GREEN - HIGH, waited 8.0 s, 8 s planned. The phone showed the counts at the end: 7 greens, 1 of them cut short for a late road, and an average wait of 8.0 s.

Laptop WiFi Monitor in the density based traffic light simulation: NORMAL pressed, density control again
Figure: NORMAL from the laptop.
Density based traffic light Proteus simulation: the south road green again after NORMAL
Figure: South green after NORMAL.
Mobile WiFi Monitor in the density based traffic light simulation: 7 greens, 1 cut short, average wait 8.0 s
Figure: The counts at the end.

The Pi's terminal kept every decision with its time and its reason. The LED models reported each lit LED at 1.90 V and 14.0 mA, with 3.09 V across its 220 ohm resistor, because the simulation's GPIO outputs drive Proteus's standard 5 V logic level. On a real Raspberry Pi 5 the pins give 3.3 V, so the same LED draws about (3.3 - 1.9) / 220 = 6.4 mA: still a bright indicator, and well inside what a pin can give.

Raspberry Pi 5 terminal in the density based traffic light simulation: every decision with its reason
Figure: The terminal: the whole run.

Download the Project Files

Click the button below to download Density-Based-Traffic-Light-Raspberry-Pi-5-Proteus-Simulation-v1.0.zip (about 13.8 MB, without the source code of the models):

Density Based Traffic Light with Raspberry Pi 5 - Proteus Simulation V1.0
  • Proteus Simulation: Pi-5-Density-Traffic-Light.pdsprj, traffic_light.py and copies of the models.
  • Python Code: traffic_light.py, the program.
  • Proteus Library Files: the LIB files of every part.
  • Proteus Model Files: the DLL models and the TEPPi folder (the Pi's Python runtime).
  • README.txt: the installation steps, the properties, the test results and the links to every library post.

No Proteus yet? Read How to Download and Install Proteus 7 and 8. New to libraries? Follow How to Install a New Library in Proteus.

How to Install the Project Files

  1. Close Proteus and extract the whole zip file, for example to your Desktop.
  2. Copy the files from Proteus Library Files into the LIBRARY folder of Proteus.
  3. Copy everything in Proteus Model Files, the DLL files and the TEPPi folder, into the MODELS folder.
  4. Start Proteus and open the project from the Proteus Simulation folder.

Changing the Code

traffic_light.py is a plain Python file: open it in any editor, change the times or the rules, save it next to the project and press Play again. The same file runs on a real Raspberry Pi 5 with the lgpio library.

Ideas to Extend the Project

  • Count the cars that pass each stop line during its green and log the junction's throughput.
  • Add a third sensor a road for a VERY HIGH level and a 12 s green.
  • Give the main road a longer green and the side roads a shorter one: weights per road.
  • Add a pedestrian button a road that inserts a walk phase.
  • Link two junctions so a platoon of cars gets green after green: a green wave.
  • Show the wait of every road as a chart on the dashboard.

From Simulation to Real Hardware

  • Sensors: FC-51s are fine for a model junction on a table; mount them low, facing across the lane, out of direct sunlight.
  • LEDs: one 5 mm LED a lamp is fine from a GPIO pin; real signal heads need transistors or relays and their own supply.
  • Times: use real green times (15 - 60 s), at least 3 s of yellow and 1 - 2 s of all red.
  • Safety: a real traffic controller has a hardware conflict monitor that forces flashing red if two roads ever show green; never rely on software alone.

Troubleshooting

  • A road never gets the green: its sensor reads HIGH (clear); check its OUT wire.
  • The lights change with no cars: a sensor sees something within its range; move it to 30 cm.
  • A green ends at once: another road has waited 20 s, or its own road shows EMPTY.
  • No dashboard: the program serves on port 8000 once the Ethernet is up; press Go again after a few seconds.

Limitations of the Simulation

  • The FC-51, the LEDs and the resistors are our libraries' models, built from their datasheets and the parts' behaviour, not every detail of real parts.
  • The cars are the FC-51 pop-ups' scenes: a car stays in front of a sensor until the scene is moved, so the traffic does not drive away by itself when its light is green.
  • The times are shortened so a whole rush hour fits into a minute of simulation.
  • Raspberry Pi is a trademark of Raspberry Pi Ltd; this project is not made or endorsed by them or by Labcenter.

So, that was all about the Density Based Traffic Light with Raspberry Pi 5 - Proteus Simulation. I hope it helps you understand how IR sensors measure the traffic on a road, how a controller turns that into green times, and how rules like a maximum wait and an ambulance override make a junction both efficient and fair, all tested in a simulation before you build it. More popular projects with the ESP32 and the Raspberry Pi 5 are coming. If you have any questions, ask in the comments and I will help you out. Till the next tutorial, take care and have fun!


Comments

0

Join the conversation