Model train lighting Part 2

Hello again and welcome to this second installment on this topic.

The last post here, dealt with the basic design and code for the MCU. Today is all about the circuit. Firstly the ability of the MSP430 to source current form a port is a little complex. It can sink a total of 48 ma over all pins in a port, but a maximum of 20 ma per pin in a port. Additionally this MCU is a push pull type where a voltage drop happens when you increase the current draw on a pin. Thankfully none of that matters here as the LED’s are quite stable and are current limited by a resistor. The point we can make here is that on the 2 port chips like the 2553, we have many open pins to run additional current sinks off. The code in the previous post can be expanded to incorporate all sorts of other cool stuff.

Lighting layout

Now on to the circuit. Its actually very simple. Battery positive attached to chip pin1, and ground to pin 20. S1 is attached to pin 6 (P1.4) and S2 is attached to pin 7 (P1.5). They are both in turn connected to ground. The LED feed is pin 4 (P1.2), and feeds a resistor (in my case I used a 100 ohm 1/4 watt, and 4 X 5mm white LED’s, who are in turn connected to ground.

MSP430 pinout

The MCU is capable of running on between 3.6v and 1.8v. To handle odd behavior you should be using a low dropout circuit to stop the source from dropping below 1.8v, however in this setup its not mission critical and you’ll just notice flicker in the cabin.

On the point of power, I would have loved to used a 3.7v Lion cell but those things are just too heavy. My next choice was to use a CR2032, which I ordered for this project, however I realized that in standby it would only provide me 18 days and 3.5 days with LED’s on. Not an option when those little suckers are not rechargeable. In its place Ill use 2xAAA rechargeable cells. HA!!! I found an even better source of power. Seems that those coin cells are varied, even more so than I had thought.


Everyone knows the CR2032, and more than likely the LR44, however there is a nice little hidden gem in the CR2477. Firstly, its 24mm in radius, and 7.7 mm high, but this little guy has 1000mah capacity. NOW WE’RE TALKING!! This is perfect. It will fit nicely in the space provided. Ok, so they are not rechargeable, but they certainly will do the job.

Another point of note, and to be honest I found this out during assembly of the test board, is don’t forget the 100k resistor between RST pin and Vcc. She just wont run right if ye dont.

Next item on the list are the tilt/vibration sensors. I settled on these little guys from Assemtech.








With a little fine tuning they should do the job nicely. The only issue is that I have to mount them on their sides, but a solid wire extension per pin should work well.

So, all thats left is to wire in the LED’s, and not forgetting the current limiting resistor, that’s about the last of it. See you on the next post.


Model train lighting.


This post is going to be the first in a multi-part post on adding custom lighting to some model train coaches.

First some background. I am currently setting up an H0 scale layout modeled on South Africa. One of the reasons for this is that I have a few of my old Lima Trans Karoo coaches from when I was young. As they are the junior version they are pretty sparse. No interiors, no glass, very basic indeed.

I bought some extra, including a baggage wagon that I had never personally owned, and wanted to make them a little better. I added some 3D interiors, which I will do a post on later, people, “glass”, and finally some lighting. Being that these units have plastic wheels, it would be impossible to run pickups from the rails for power, and I didnt want to go through the pain and cost of finding metal ones that would work.

I wanted a system that would light the moment the train starts, but would continue to burn for a short while after the train stops so that they would stay lit in stations etc. Clearly some sort of triggered timer circuit was in order, but I also wanted to be able to add customization after the fact if I could. The obvious choice was a cheap MCU, and as I have been playing with the Texas Instruments MSP430’s, it was an easy choice.

The next part was to figure out how to trigger once moving. After a couple of days on this, I settled on using a kind of vibration sensor as this would allow me to run a setup clear of track voltage. This could be a self contained system that would require battery changes every so often. For vibration sensors, I decided to use non mercury tilt switches because most of them use a little metal ball in a channel that shorts out 2 or more pins depending on their tilt. If I set the angles correctly, the slightest movement in one direction would set the ball (through inertia) onto or off their pins and so create a trigger effect.

Now I could start working on code for the MCU. It must be said here that, while I am aware that button or trigger events should be written as an interrupt and not polled in the MCU world, what I found was that when the system runs the LED’s, it draws more power in a short period of time than is saved by interrupting. It was simple to get the code to run the way I wanted in a poll situation and meant I could add as many triggers as I wanted without too much additional code. In “standby” mode, the MCU is drawing 0.47 ma anyway. On a set of 900 mah AAA cells, I would get 79 days in full standby. Interrupt code gave me no better than 0.37 ma and would result in full standby of 100 days. The figures make no difference when running with LED’s on as the draw is 2.7 ma regardless of interrupt usage.

With that out of the way, here is the code, currently running on an MSP430G2553.

Code was done in code composer studio. Test areas have been left in and commented. I have added comments inline for clarity, and will supply the code in a file attached at the end.

#include <msp430.h>

Setup the variables

//volatile unsigned int mvg; // For Test
volatile unsigned int cartrg;
volatile unsigned int offcnt;
volatile unsigned int offtrg;
volatile unsigned int stillon;

Rough estimation of time

//Static assignments
unsigned int seconds = 60;
unsigned int secsplit = 25713;

* main.c
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

// Set P1.2 for LED output
// Set P1.4 for vibration sensor1
// Set P1.5 for vibration sensor2

// Set P1.6 – LED1 for test mode – moving signal
// Set P1.0 – LED2 for test mode – not moving signal

//P1DIR |= (BIT0 | BIT6 | BIT2); // Set out at P1.0 1.6 1.2 – For Test

Using P1.2 as the LED pin. All LED’s that need to be light are powered from here. I currently use 4 LED’s and they do not overtax the amp rating of the pin.
P1DIR |= BIT2;
//P1OUT &= ~BIT6; // Switch LED at P1.6 off – For Test
P1OUT &= ~BIT2; // Switch LED at P1.2 off

Set the resistor networks at P1.4 and P1.5 on. Some of this code is useless as it is for interrupts, but it works as is so well leave it.

P1REN |= BIT4;
P1OUT |= BIT4;
P1IES |= BIT4;

P1REN |= BIT5;
P1OUT |= BIT5;
P1IES |= BIT5;

Set P1.3 P1.4 P1.5 as inputs for the vibration sensors. I kept P1.3 in as it allows us to use the dev boards button as a trigger as well.

P1IN |= (BIT3 | BIT4 | BIT5); // Set in direction at P1.3 1.4 1.5
//mvg = 0; // Set moving off – For Test
cartrg = 0;

for (;;)

// Main cycle

Here I poll the input for P1.4 and P1.5

if ((P1IN & BIT4) | (P1IN & BIT5))

If either (the | is or) triggers off (circuit open) – thats the reason we use pullup and a resistor network to hold the line high.

//mvg = 1; // For Test

This is a flag for the LED test later. In this case, cartrg is 1 which means turn on LED’s

cartrg = 1;

These reset the offtrg and offcnt so that the counter never gets above the turn off value. In other words, when the ports trigger, they reset the 1 minute counter, and the counter is always working to turn off the LED’s

offtrg = 0;
offcnt = 0;

else {

//mvg = 0; // For Test

Here the flag is 0 and so tells the LED test to begin the count down to turn off the LED’s
cartrg = 0;


// For Test
//if (mvg == 1) // Is movement detected
//P1OUT |= BIT6; // P1.6 – Test signal green on
//P1OUT &= ~BIT0; // P1.0 – Test signal red off
//cartrg = 1;
//else {
//P1OUT |= BIT0; // P1.0 – Test signal red on
//P1OUT &= ~BIT6; // P1.6 – Test signal green off
//cartrg = 0;

If the flag is set

if (cartrg == 1 )

and the LED’s are off

if (stillon == 0)

Switch them on

P1OUT |= BIT2; // Switch P1.2 – main lights on
stillon = 1;


else {

If they are on
if (stillon == 1)

increment counter

offcnt ++;
if (offcnt >= secsplit) // count the number of seconds

Count up the seconds

offcnt = 0;
if (offtrg >= seconds)

if the offtrg is larger than the amount of seconds, then turn off the lights

stillon = 0;
P1OUT &= ~BIT2; // Switch P1.2 – main lights off


As promised, here is the file: Lima-Light-Timer.