Each locomotive has a decoder installed who interprets the orders given by the Command Station for it.
There are diferent PIC based home made decoders like TMWDCC or MERG, but I show how to install a Lenz decoder on a N-scale Minitrix locomotive.
See the small size of the decoder LE010XF from Lenz, compared with 1 euro cent coin, its only 11x9x4mm
This is the original aspect of Trix locomotive, one BR 216 in N-scale, with his circuit printed board.
After take out the circuit printed board and disconnect the motor contacts, we put the decoder in his position and connect the wires to the motor and to the wheels. I changed bulbs by 3mm yellow or white LED.
At last we put the LED in the bulbs place with his correspondig resistor and connect the wires to the decoder
Finally we run the locomotive and programm the CV consecuently