Hello friends, I hope you all are doing great. Today, I am going to share the Automatic Railway Gate Control with Raspberry Pi - Proteus Simulation, project 108 of our IoT Projects series. Many level crossings still depend on a gatekeeper who closes the gate by hand when a train is due, and accidents happen when the gate is closed late or not at all. An automatic railway gate sees the train coming and closes the crossing by itself. It is one of the most popular final year projects, and here is a complete one on a Raspberry Pi 5, with the circuit, the Python program and a Proteus simulation you can run before you build the model.

Two IR sensors watch the track, one on each side of the crossing, so the Pi knows when a train is coming and from which direction. A third sensor looks across the road under the barrier. When a train comes, the road's red lights flash and a bell rings, and after a few seconds an SG90 servo lowers the barrier. Only when the barrier is down does the train's signal turn green. If a car is standing on the crossing, the barrier waits and the train's signal stays red: the crossing is never closed on top of a vehicle. When the train has passed the sensor on the far side, the signal goes red again and the barrier rises. In this tutorial, we will go through the circuit, how the train is detected, the safety rules and the Python code. Then we run two trains and a stuck car 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 automatic railway gate control with Raspberry Pi in Proteus simulation:

Figure: The automatic railway gate in a Proteus simulation.
Figure: The project working in the Proteus simulation (an animation of the main steps).

What Does This Automatic Railway Gate Do?

  • Train detection: an IR sensor on each side of the crossing tells which way the train is coming.
  • Road warning: two red lights flash in turn and a bell rings before and while the barrier moves.
  • A servo barrier: an SG90 lowers the barrier slowly, the way a real one moves.
  • A train signal: green only while the barrier is down, red at all other times.
  • Never on a car: a sensor on the crossing holds the barrier up and keeps the signal red while something is there.
  • Fail-safe and maintenance: a train that never arrives keeps the crossing closed; CLOSE on the dashboard closes it for work.

In the simulation, each FC-51 has its own pop-up whose Scene page puts a train or a car in front of it, the SG90's pop-up shows the barrier's angle, and the LEDs light on the sheet.

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 automatic railway gate and their library posts
PartProteus deviceLibrary postConnection
Raspberry Pi 5RASPBERRY PI 5Raspberry Pi 5 Library for ProteusPI1, railway_gate.py, Ethernet 192.168.1.51:8000
FC-51 IR sensors (x 3)FC51ADVTEPInfrared Sensor Library for ProteusU1 WEST GPIO17, U2 CROSSING GPIO22, U3 EAST GPIO27
SG90 servoSG90SERVOTEPSG90 Servo Motor Library for ProteusSERVO1: the barrier, SIG GPIO18
5 mm LEDs (x 4)LED5MMREDTEP, LED5MMGREENTEP5mm LED Library for ProteusD1 / D2 road lights, D3 / D4 the train signal
220 ohm resistors (x 4)RESISTOR4BANDTEPResistor Library for ProteusR1 - R4: one with each LED
Active buzzerBUZZERACTIVETEPActive Buzzer Library for ProteusBZ1: the bell, GPIO25
Laptop WiFi MonitorLAPTOP WIFI MONITORLaptop WiFi MonitorPC1
Mobile WiFi MonitorMOBILE WIFI MONITORMobile WiFi MonitorMOB1

Proteus Simulation of the Railway Gate: the Circuit

Figure: The complete circuit of the automatic railway gate on an A3 Proteus sheet.
The connections of the automatic railway gate
SignalRaspberry Pi 5Goes to
IRW / IREGPIO17 / GPIO27 (pins 11 / 13)the WEST and EAST track sensors, LOW = a train
IRXGPIO22 (pin 15)the CROSSING sensor, LOW = something on the road
GATEGPIO18 (pin 12)the SG90 barrier's signal, 50 Hz pulses
ROADL / ROADRGPIO23 / GPIO24 (pins 16 / 18)the road's red lights
SIGR / SIGGGPIO5 / GPIO6 (pins 29 / 31)the train's red and green signal
BELLGPIO25 (pin 22)the active buzzer

The FC-51s run from the Pi's 3.3 V pin, so their outputs go straight to the GPIO pins. The servo takes its power from the 5 V pin and only its signal from GPIO18. Each LED has its own 220 ohm resistor.

How the Automatic Railway Gate Works

Detecting the Train and Its Direction

The two track sensors sit some way before the crossing, one on the west side and one on the east side. Whichever sees the train first tells the Pi where it is coming from, and the other one becomes the exit sensor. The train has passed when the exit sensor has seen it and is clear again. This way one program handles trains from both directions, and a long train that covers both sensors for a while is still counted only once. On a model railway the FC-51 sees the train from a few centimetres away; on a real line, track circuits or axle counters do the same job.

Warning, Lowering and the Train Signal

As soon as a train is detected, the road gets its warning: the two red lights flash in turn, twice a second, and the bell rings. After 3 seconds the barrier starts to come down, 3 degrees every 50 ms, so it takes 1.5 s, slow enough for a car to stop. Only when the barrier has reached the bottom does the train's signal change from red to green. The order matters: a train must never get a green signal while the road is still open.

Never Lower the Barrier onto a Car

A barrier that comes down on a car is dangerous, and a car stuck between the barriers is worse. The third sensor looks across the road under the barrier. If it sees something when the barrier should start to move, the program waits: the state is HELD, the lights keep flashing and the bell keeps ringing, the barrier stays up and the train's signal stays red, so a driver would stop the train before the crossing. As soon as the road is clear, the barrier comes down and the signal turns green.

Opening Again, and the Fail-Safe

When the train has passed the exit sensor, the program waits 2 more seconds, turns the train's signal back to red, and raises the barrier. The lights stop when it is up. One more rule protects against a train that stops before the crossing or a sensor that fails: if the exit sensor has not seen the train after 60 seconds, the crossing stays closed. In railway safety, a failure must always leave the system in its safe state, and for a level crossing that is closed.

Maintenance

CLOSE on the dashboard closes the crossing for maintenance, with the usual warning first, and keeps it closed until AUTO. Then the barrier rises and the automatic control takes over again.

The Python Code Explained

The program is railway_gate.py, written for the Raspberry Pi 5 with the lgpio library. One thread flashes the lights, one runs the crossing, and the main thread serves the dashboard. The pins and the times:

IR_WEST, IR_EAST, IR_CROSS = 17, 27, 22                            # BCM numbers
UP, DOWN, STEP = 90, 0, 3                                          # degrees; the barrier moves STEP every 50 ms
WARN_S, CLEAR_S, OVERDUE_S = 3.0, 2.0, 60.0                        # the warning before it lowers; the wait after the train; the fail-safe

An FC-51 pulls its output low when something is in front, and the SG90 takes a 544 to 2400 microsecond pulse for 0 to 180 degrees:

    return lgpio.gpio_read(chip, g) == 0                            # (the FC-51's OUT is LOW with something in front)
    lgpio.tx_servo(chip, GATE, int(544 + (2400 - 544) * angle / 180), 50)   # SG90: 544 - 2400 us for 0 - 180 degrees

The barrier moves a few degrees at a time, and never comes down while something is on the crossing:

            a = max(target, a - STEP) if a > target else min(target, a + STEP)
    while seen(IR_CROSS):                                          # never lower the barrier onto a vehicle

The direction from the first sensor, and the lights flashing in turn:

        side, exit_ = ('WEST', IR_EAST) if entry == IR_WEST else ('EAST', IR_WEST)
            phase = int(time.monotonic() * 2) % 2

The train has passed when the exit sensor, having seen it, is clear again; a train that never arrives keeps the crossing closed:

            elif reached:
                event('TRAIN OVERDUE - the crossing stays closed (fail-safe)')

The dashboard's links are Refresh, CLOSE and AUTO; /data gives the state, the barrier's angle, the three sensors and the counts 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-Railway-Gate.pdsprj from the Proteus Simulation folder. Keep railway_gate.py and the DLL files next to it.
  3. Press Play: READY, the barrier up and the train's signal red.
  4. Send a train: the WEST TRACK sensor's (U1) Scene page, distance 10 cm, then 30 cm; then the EAST TRACK sensor (U3) 10 cm and 30 cm.
  5. A car on the crossing: U2 at 10 cm. The dashboard on the Laptop or the Mobile WiFi Monitor (press Go) has CLOSE and AUTO.

Simulation Results: Two Trains and a Stuck Car

We pressed Play and sent two trains through the crossing, the first from the west on a clear road, the second from the east while a car was stuck on the crossing, and finally closed the crossing for maintenance from the phone. The FC-51 pop-ups put the trains and the car in front of the sensors, the SG90's pop-up shows the barrier, and the LEDs light on the sheet. The times are the program's clock; the whole run took 64.2 simulated seconds.

Ready

The program printed READY - the barrier up, the train signal RED, watching both sides. The SG90 held the barrier at 90 degrees, a pulse of 1472 microseconds, and the train's red LED was lit.

Figure: Ready: the barrier up.
Figure: The barrier up.
Figure: The dashboard on the phone.

A Train from the West

We put the train in front of the WEST TRACK sensor. At 6.9 s the program printed TRAIN from the WEST and WARNING: the two road lights flashed in turn and the bell rang (29.2 mA at 4.97 V on the buzzer's pop-up). After the 3 s warning, at 9.9 s, the barrier came down, and at 11.4 s it was down: CLOSED - the train signal GREEN, the servo at 0.4 degrees with a 544 microsecond pulse. Only now did the train's green LED light.

Figure: The warning.
Figure: The bell.
Figure: The barrier down, the signal green.
Figure: The barrier down.
Figure: The west sensor sees the train.
Figure: The dashboard: closed.

The train ran on and reached the EAST TRACK sensor at 18.1 s: the train at the EAST sensor. When it had cleared that sensor too, the program waited its 2 seconds and at 22.8 s printed OPENING - the train has passed (1 so far), turning the train's signal red before the barrier moved. At 24.3 s: OPEN - the road is clear, and the lights and the bell stopped.

Figure: The barrier up again.
Figure: The terminal: the first train.

A Car Stuck on the Crossing

Now the dangerous case. We put a car on the crossing, in front of the CROSSING sensor, and then a train in front of the EAST TRACK sensor. At 29.7 s the warning started as usual, but when the 3 seconds were up the program did not lower the barrier: HELD - something on the crossing: the barrier stays up, the train signal at RED at 32.7 s. The servo stayed at 89.6 degrees, the red signal stayed lit, and the lights and the bell kept warning. The dashboard showed HELD with the crossing sensor BLOCKED.

Figure: HELD for a car on the crossing.
Figure: The crossing sensor sees the car.
Figure: The barrier held up.
Figure: The dashboard: held.

The car drove off. At 40.0 s the barrier came down, at 41.5 s it was CLOSED with the signal green. The train passed the WEST sensor at 44.2 s, and at 50.3 s the crossing was open again: 2 trains, 1 hold for a blocked crossing.

Figure: The car gone: the barrier down.

Closed for Maintenance

Finally CLOSE on the phone at 51.4 s: the usual warning, and at 55.9 s the crossing was closed for maintenance. AUTO at 59.0 s opened it again at 60.6 s.

Figure: CLOSE from the phone.
Figure: Closed for maintenance.
Figure: AUTO from the phone.

One number deserves a note: the bell drew 29.2 mA. In the simulation the GPIO outputs drive Proteus's standard 5 V logic level; a real Pi pin gives 3.3 V, so the buzzer would draw less, but still more than a Pi pin should supply. On real hardware, drive the buzzer through a small transistor. The terminal kept every step with its time.

Figure: The terminal: every step.

Download the Project Files

Click the button below to download Automatic-Railway-Gate-Control-Raspberry-Pi-Proteus-Simulation-v1.0.zip (about 14.4 MB, without the source code of the models):

Automatic Railway Gate Control with Raspberry Pi - Proteus Simulation V1.0
  • Proteus Simulation: Pi-5-Railway-Gate.pdsprj, railway_gate.py and copies of the models.
  • Python Code: railway_gate.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

railway_gate.py is a plain Python file: open it in any editor, change the warning time, the barrier's speed or the fail-safe time, 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

  • A second barrier on the other side of the road, closing a moment after the first (exit barrier last).
  • Measure the train's speed between two sensors and start the warning earlier for fast trains.
  • Count the trains and log the time each one closed the road.
  • Send an alert to the station when a car blocks the crossing.
  • A pedestrian gate and a beeper for blind pedestrians.
  • A backup battery so the crossing closes even after a power cut.

From Simulation to Real Hardware

  • A model railway: FC-51s beside the track, a servo with a lollipop-stick barrier and 5 mm LEDs make a convincing model.
  • The bell: a buzzer draws about 30 mA, more than a Pi pin should give; drive it through a transistor.
  • The servo: give it its own 5 V supply, the grounds joined, so its current does not disturb the Pi.
  • Real crossings: real level crossings use track circuits or axle counters, fail-safe relay logic and certified controllers; a hobby board is for models and teaching only.

Troubleshooting

  • The barrier never comes down: the crossing sensor sees something, so the program holds it up on purpose.
  • The barrier stays down after the train: the exit sensor must see the train and then be clear again.
  • The servo jitters: check its pulse range and give it a separate 5 V supply.
  • 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 SG90, the LEDs and the buzzer are our libraries' models, built from their datasheets and the parts' behaviour, not every detail of real parts.
  • The trains and the car are the FC-51 pop-ups' scenes: an object stays in front of a sensor until the scene is moved.
  • The simulation's GPIO outputs drive Proteus's 5 V logic level, so the LEDs and the buzzer draw more current than from a real Pi's 3.3 V pins.
  • 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 Automatic Railway Gate Control with Raspberry Pi - Proteus Simulation. I hope it helps you understand how a crossing detects a train and its direction, why the order of the lights, the barrier and the signal matters, and how simple rules like never lowering onto a car and failing safe make a crossing trustworthy, all tested in a simulation before you build the model. 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!