Welcome to My HO Model Railroad Blog

November 12, 2016 Arduino UNO Random Lighting Project, Arduino Sketch

February 10, 2017 update:

I did a redesign of my random light controller sketch and expansion board from 12 to 14 outputs.  This is the 14 output sketch:


#define numleds  14                                                     
byte ledpins [ ] =  { 0,1,2,3,4,5,6,7,8,9,10,11,12,13 } ;
void setup( )  {                 
    for ( int  i=1; i <= numleds;  i++ )  {    
    pinMode ( ledpins [ i ], OUTPUT) ;
    digitalWrite ( ledpins [ i ] , HIGH) ;
void loop ( )  { 
                digitalWrite ( ledpins [ random ( 0, numleds+1 ) ], lightsw ( ) ) ;
                delay ( 4000 ) ; // 4000 = 4 seconds               
boolean lightsw ( )  {

 if  ( random (0,100) > 60 ) return LOW ;
   else return HIGH ;


Sketch uses 2,130 bytes (0%) of program storage space.  Maximum is 253,952 bytes.
Global variables use 27 bytes (0%) of dynamic memory, leaving 8,165 buyes for local variables.  Maximum is 8,192 bytes.


The only difference to the expansion board is wiring up the 7th output of each chip, all 14 outputs of the driver chips are used.

I decided to make a random lighting controller for my structures.  The Arduino UNO micro processor is perfect for my project.  The cost is very low due to the Arduino being "Open Architecture".   The Arduino can drive 14 output ports, each port can deliver 20ma at 5 volts.  That is the max current available and posts on the Internet say 'to be on the safe side keep the current below 15ma per port".

I rarely operate me LEDs over 10ma so I'm in the "Safe Zone".  I also have many structures with 12 volt incandescent bulbs for lighting.  The 12 volt incandescent bulbs can draw anywhere from 40 to 100ma at 12 volts, I operate my 12 volt bulbs in the vicinity of 9 volts for longer life but mainly for realism.  A small incandescent bulb operating near max voltage doesn't look very realistic, toy like to me.

To obtain a higher current output from the UNO I came up with a simple 500ma per channel driver chip.   It is rather a simple project again because of the Arduino open architecture.  The UNO will direct drive the TD62304AP seven channel driver chip.  The UNO 5 volt regulator will provide the power to the logic side of the driver chip and the driver outputs will switch the ground side of the lighting with an input from 1 to 50 volts at up to 500ma per channel.

Each channel of the driver chip requires .320ma from the UNO, as there is such little current needed from the UNO if you would like to monitor the outputs LEDs can also be used directly from the UNO outputs.  My design allows for monitoring.

To get started on this project here is what you need.

The Arduino UNO is available on eBay for under $5 including a USB programming/power cable.  

The UNO Expansion Boards come in several versions, most come with the Arduino connectors.  This particular board has paired links that make building this driver circuit easier.  This board has a part number of 64502SP13-10. 

I haven't been able to find it with that number, only with "paired links".   Cost of this board is between 2 for $5 and 4 for $8 from this eBay seller.


The TD62304AP Driver chips are also available on eBay under $2.  The 16 pin DIP sockets are available at any electronic store (Radio Shack).

I normally buy almost all of my electronic parts in bulk off eBay.

I buy the single row 40 pin .1"/.254mm breakable sockets off eBay.  The sockets can be used for either male or female connectors.  For a better male fit to the socket I buy the male header strips, the pins are a bit larger in diameter and fit tighter into the sockets.

I worked up a CAD drawing of the UNO expansion board.

I have posted a downloadable drawing in PDF format on my Google Drive.   

When wiring the expansion board the output terminals are not in order, Pin 0 is output #1 then it skips #1 & #2.  Pins #3 through Pin #13 are outputs #2 through #14.


This is my finished controller.

The 16 pin micro strip connector at the bottom is the UNO processor switched ground outputs 1 through 14 (Max 20ma each port, to protect heat build up I would recommend a Max of 15ma per port), Pin #15 is -5 volts or ground & Pin #16 is +5 volts.  This connector can drive LEDs directly.

The 16 pin strip above the chips is the high current switched ground (500MA).  Pins 1 through 14 are the high current lamp drivers, pin 16 goes to the red terminal block screw, any DC voltage from 1 to 50 volts.

You can do a copy and paste to your Arduino UNO and you're in business.

By changing the number in red you can change the delay portion of the sketch.  I like my on off duration long. 

All in all this project is one of my very best, I have three random light controllers in operation and working driving three triple story houses.

Update: For those that want to use 14 LEDs here is a simple wiring diagram.

The wiring diagram above can also be used as a test bench tool for checking the operation of the Arduino UNOs when used as a Random Lighting Controller without the high current driver expansion board.

No comments: