Most of the simple analog locomotive use DC voltage with normal or reverse polarity, normal polarity go locomotive forward and reverse voltage go locomotive backward.
I used for test on breadboard a 5mm bicolour led (red/white) LTW30EEDYJA but for 3mm can be used OSRWPA3132A.
I designed a simple schematic with additional 3 pieces: 2 diodes (1N4007) and a resistor (680Ω..1kΩ):
For normal polarity for power supply of the analog locomotive white led is on
As a noobie with the model train system and because I haven't too much free space for test with big railways, I search for an automatic model train system, where it used IR module for define frake momment (stop position is not instant, as in rea case, if push brake pedal and car stop is after few meters).
with PWM control for speed, so I adapt the sketch (my sketch) and made a first test with push a carriage push by me, to see how system work, as in video Automatic Model Train (test 2) Finally, I added L298 H-bridge module as in schematic
PS: Improved sketch is controlled_model_train_1b.ino where it used for start PWM at 105 (aprox. 40%) not 0, remain maximum at 255 from 255 (100%). This mode to start the loco is useful for old DC motor.
In next image you can see how automatic system for control the train ("loco") works:
If train go forward, speed is constant, at maximum, if IR module 2 is actuated, speed decrease slow until train stop. After a small pause, train go backward, with speed increase from zero to maximum, and than IR module 1 is actuated, speed decrease slow to until train stop. Again, after a small pause, train go forward and cycle is repeated, as in next image: