Chasing Electronic Gremlins

I built the test loop for a couple of reasons. First I needed to revive my long unused track laying skills. Second, I needed a place to test and repair locos and rolling stock. Third, and perhaps most importantly, I needed to learn how to deploy Arduinos on a layout in a bullet-proof sort of way, before investing time and money building the main layout.

If you’ve been following along, you know that lighting is one of my big challenges in building the layout in the chosen location. The soffits above the bar are a thin skin of paneling, with little structure supporting it, hiding HVAC duct-work. Standard light fixtures are not possible here.

Addressable RGB LED Strip

Addressable RGB LED Strip

That constraint started a search for a lighting solution that was light enough to affix to the paneling with 3M Command Strips, but would produce enough light to effectively light the layout. When I found strip ALEDS, I knew I’d found a solution. The first two light bars I made are demonstrated here (a simple light show accompanied by a little Debussy):

The bars are easy to create.  Mine are sized at 26″ long; the right size to both light from above and install on the underside of the top level of the layout to light the lower level.

To make them I split 1/2″ PVC pipe on a table saw, then cut the pipe halves to length [TIP: PVC pipe from your home store is dirty stuff. I clean the cut halves with denatured alcohol before final assembly]. I attach 1/2″ reflective Mylar tape to each inside half of the pipe (creating a reflector), leaving a strip of bare PVC down the center of the pipe.  A bead of Liquid Nails for Projects down the middle holds a prepared (with JST 3 wire connectors at each end) 38 LED strip.

My standard RGB LED light bar.

My standard RGB LED light bar.

The demo above shows two bars chained together. You can keep lengthening the array by adding additional bars, at least until you reach the limits of your power supply.

Not So Fast

I have to admit that when I made and tested 4 bars together (152 LEDS), I was  disappointed with the amount of light I was getting. It was good, but just not quite enough.  Two more bars (228 LEDS) ought to do it I thought.

What I got when I expanded the array to 6 bars was a very obvious light intensity drop off (and resulting color change, since these are RGB LEDS) from the beginning to end of the array. Nowhere in the information I’ve assembled about ALEDS has there been any mention of this problem.  I got out the multi-meter and, sure enough, the supply voltage drops steadily as you progress along the strip; and the greater the total number of ALEDS chained together, the more pronounced the effect throughout the strip.

Well, I’m a model railroader and I know all about resistance and current drop off; this is our classic problem of current loss over long runs. The solution? A 16Ga supply bus that injects current every two bars.

Light Controller and Bus Bar

Light Controller and Bus Bar

Head end of the lighting bus bar.

Head end of the lighting bus bar.

A strip of plywood provides the mounting surface for the required capacitor near the first LED, the bus wire (I CA’ d it to the wood) and circuit board fragments with with PCB screw terminals.

The test loop under lights.

The test loop under lights.

Problem solved.

Booting a Loaded Arduino

The basic reliability of the Atmel platform used by Arduino boards is impressive. So when I started seeing boot problems, I was puzzled.  In all cases, the problem occurred on initial power up; rebooting the affected board by hitting the reset button solved the problem.

This was not a good development.

My first three loaded up Arduinos are installed on the Test loop, Lighting Control and the Control Panel.  All three have an Ethernet Shield and are attached to additional devices. All three evidenced cold boot problems in one form or another.

It had to be a power problem.  The additional load from the Ethernet Shield and other devices (although, in most cases I supply power to attached devices separately so they don’t draw from the Arduino’s limited current handling capacity) had to be the issue. One of the confusing things about UNOs is that you can power them from USB at 5 volts DC, or from a separate DC power supply at 7 – 12 volts.

Umm, how much power should I be supplying?

Enercell Power Supply

Enercell Power Supply

In the context of the control panel, where an UNO has an Ethernet shield with multiple digital and analog connections to the touchscreen, I found that I need to supply 12 volts.  At that level the rig is 100% reliable, something I easily established with the help of an adjustable power supply. The trade-off with the control panel, because everything is enclosed, is heat buildup, requiring a fan I didn’t originally plan for.

New deployment rule: the standard power supply for Arduino boards with built-in voltage regulators (primarily UNOs and MEGAs on this project) will be 12 volts DC. Connected devices will run at the standard logic 5 volt level. Smaller Arduino boards without a voltage regulator will get 5 or 3.3 volts DC as required.

Modified Computer Power Supply

Modified Computer Power Supply

I use converted computer power supplies with simultaneous outputs at 3.3, 5 and 12 volts DC, so my layout bus has all three feeds.

It may not seem like it, but that is progress.

Powering Up In Order

Unlike the control panel, the lighting controller did not settle down with a 12 volt supply. There, the Ethernet Shield would go into an initialization loop (attempting to start up and failing, over and over) on power up — but would work fine after a hard reset.

On the test loop, the problem was even subtler: upon cold power up everything appeared normal and the sketch would start to execute…. then freeze at the point where it is supposed to send a broadcast message across the network.

It had to be something about power again that manifests only on a cold start, but what? Faulty Ethernet shields?

Here the clue was a little warning from Adafruit about neopixels (ALED strips): always make sure the power supply to the strip is on before the data connection from the Arduino goes live, or the strip could be damaged.

I always figured that if the Arduino and the ALEDS (and other peripheral sensors and actuators) were powered from the same source and came on simultaneously, Adafruit’s warning would be satisfied.  Since I’ve never had damage to the strip, and I’ve been working with the same strip for some months now, I think technically I was right.  However, it seems that from the Arduino side, simultaneous start up is not necessarily so blessed, especially when attached to an Ethernet shield.

I conducted a simple experiment on the lighting controller:  I unplugged the power from the board/shield combo then powered up the ALEDS before plugging the power into the board.

BAM. Worked perfectly every single time. No confused Ethernet shield; perfect response to commands; no hitting the reset button.

Of course, manually plugging a fleet of embedded Arduino boards was not going to do at all.

Automating Power On Delay

Consulting the Internet Machine, I found a simple power-on delay circuit.  In my first attempt I built it as shown, except for substituting a variable resistor for R2 (on original schematic) to allow some adjustment of the delay.  For the relay, I chose a low power signal type — adequate for the power draw of the Arduino/Shield, but possibly not sufficiently durable for this application. Only long term experience will tell.

Anyway, as built it provides about a 50 – 100 millisecond delay in powering on the board. That turns out to be enough.  With the delay circuit attached, the lighting controller powers up perfectly every time.

I modified the circuit slightly for my second build, including both input and output indication LED’s [ green for input power on, and blue for output power on. ] and increased the size of the capacitor to 100µf. The bigger capacitor gives a little more time range to the delay (as adjusted by R4; if you go out of range either direction on R4, the circuit will not work.) from about 1/10th to 1/2 second. The I/O LEDs really help see the timing of the delay.

Here’s schematic of the board as I’m building it now:

Power-On Delay Circuit Schematic

Power-On Delay Circuit Schematic

Built on one quadrant from an SB4 Snappable Breadboard (these are my go-to, two sided solderable breadboards), the top looks like this:


Power-on Delay Circuit Top

Power-on Delay Circuit Top

And, the bottom:

Power-on Delay Circuit Bottom

Power-on Delay Circuit Bottom

Installed on the test loop and in operation:

Uno, Ethernet Shield and Power-on- Delay board.

Uno, Ethernet Shield and Power-on- Delay board, mounted under the Test Loop.

From here on, power on delays circuits are another standard component for reliable operation, though I think I’m going to double the size of the resistor on the blue led to tamp down its brightness more!.


A Programmable Layout Controller

Programming an Arduino to run turnouts, lights or animation on the layout is only part of the challenge. The other part is how do you control the board and tell it what you want it to do?

Servo Control with LED Feedback

Servo Control with LED Feedback

From an Arduino point of view, any sensor attached to a pin can trigger action in a sketch. As shown in Turnout Control with Arduino & Servos, mechanical buttons and switches can be attached to pins to tell the board what to do. In the example circuit, a single button triggers servo action. If you want to include feedback indicators as in this example circuit — these could be layout signals or panel indicators — you can hard-wire everything together to the same Arduino board.

Until you run out of pins

Pin management is critical as you ask the Arduino to do more and more. Every new sensor or triggering device consumes pins (as does every new actuator or output device). While learning what I could do with an Arduino on the layout, I realized that I needed get beyond the hardwired controls used in experiments and demos to a generic, software-based control system. To do that I was going to have to network everything together.

Networking Arduinos

Uno with Ethernet Shield

Uno with Ethernet Shield

In Roundhouse Rebuild Part 2 I mentioned, without explanation, that I was using Ethernet, and went on to discuss the evolving Simple Network Command System. I decided to go with wired Ethernet because of the easy availability of inexpensive Ethernet shields based on the WIZnet W5100 Ethernet Controller chip (under 10 dollars per shield), and an easy to use Arduino library included in the IDE. It is as close to plug & play as networking gets on an Arduino. The only additional equipment required are one or more inexpensive 10/100 switches (for example: TP-LINK TL-SF1005D 5-port 10/100Mbps Desktop Switch; don’t get gigabit switches to work with these shields, you’re just asking for trouble) to  interconnect the devices. I use a per-device assigned address system which helps keep the equipment roster simple (no router or DHCP required).

Why not just use the Digital Command Control system for the Arduino net? The short answer is that while it is clearly doable, for the purposes of this project I am going to keep the Arduino net separate because:

  1. Everything I do here has to work for both DC and DCC layouts. I own both DCC and unconverted DC locomotives; the layout has to work the same in either mode.
  2. Compared to conventional networking, DCC is a relatively difficult way to conduct bidirectional communications between Arduino boards.
  3. Keeping them separate does not preclude enabling communication between the two systems down the road.

If you want to pursue DCC communication and Arduino, the Model Railroading with Arduino site is a great place to start. The biggest impediment for most modelers will be the lack of commercial interface hardware to connect an Arduino to either track power or the command bus (although the circuits are easy enough to build); the closest commercial solution would be to use a USB interface, like the Digitrax PR3XTRA USB Programmer, RR-CirKits LocoBuffer-USB or the SPROG II USB, to tap into the DCC command system just as you would with JMRI.

My ultimate goal is to build the layout’s electronic and mechanical foundation around a network of Arduino boards. For communication among Arduino boards, Ethernet makes the most sense right now because it is the most “frictionless” route to achieve my goals (a wireless form would be even better, but would be a little more difficult to implement, so I’m holding that option for the future); communication between the Arduino net and the DCC system is a topic for the future—and the possibilities go way beyond treating Arduinos as decoders.

Building The Controller


Adafruit 3.5″ TFT screen displaying a bitmap.

The concept for a prototype controller was simple enough: start with an Uno, add an Ethernet shield and add a small touchscreen for display and user input. Put it all in a box with an Ethernet jack, a USB jack and power connector. Software generates screen displays, interprets touches and communicates with devices it is controlling.

For the screen I chose the Adafruit 3.5″ TFT Touchscreen, seen here attached to an Uno via a breadboard (NB: The wiring shown is the minimum required to run the screen; the touch overlay and the SD Card reader require additional connections). It is capable of full 16bit color with a resolution of 320 x 480 pixels. The Adafruit library provides basic graphic primitive functions, basic text functions and bitmap functions allowing image display. It has a resistive touch overlay. Adafruit has an excellent tutorial on using this screen with their library.

Back of 3.8" TFT Screen

Back of 3.5″ TFT Touchscreen

The screen comes with a choice of interfaces: you can use the SPI bus interface in order to use the fewest pins on your Arduino, or you can devote more pins to use the faster 8 bit interface. You select the interface and solder the header pins on the appropriate side.  A  solder jumper on the back determines which interface is active; the decision is reversible. An SD Card reader is included for convenient storage of bitmap files.

On an Uno, the Ethernet shield dictates that the TFT screen has to be run via SPI; there aren’t enough pins otherwise. The application does not require the SD Card Reader so I don’t connect it to the UNO.

I fabricated a wiring harness for attaching the screen to the Uno\Ethernet combo, then mounted everything in a Radio Shack project box as shown below.

Wiring Harness

Wiring Harness

The connectors on the wiring harness are male or female PCB Headers; I solder the wires to the PCB side of the fittings, then cover each connection with heat shrink tubing. White wires connect to digital pins 7 through 13 (except 10, which is reserved for the Ethernet shield) and are for the TFT interface. Green wires are for the touch overlay and connect to Analog pins A2 – A5. Red supplies 5v, and black ground, to the TFT screen. The Ethernet extension cable and the USB extension cable both came from Adafruit.

Inside the Controller

Inside the Controller

Controller with Screen Wiring Attached

Controller with Screen Wiring Attached








Here it is in operation:

The Programmable Controller

The Programmable Controller







You may have guessed the fan ( on the left side ) was an afterthought. The cheap Ethernet shields I use are heat sensitive; they will crash when put in a confined space with poor air circulation.  Out in the open no problem; in a box, its a problem. Found that out the hard way. So I added a little fan to pull the air through the box (if you look closely, you’ll see there are holes around the bottom); works fine if noisily. Obviously, I will plan for air circulation when I build the main layout control panel. Such is prototyping!

What it Does

The controller sketch displays menus with buttons that, when touched, will cause the controller to either go to a different menu or send a command packet to the target device. Command packets are strings, formatted thus: function / option / data. For more about my protocol and the network polling process, see the Simple Network Command System section near the end of Roundhouse Rebuild Part 2.

The Main Menu provides access to sub-menus that I’ve created to support parts of the project.

Controller Main Menu

Controller Main Menu

All menus are built with buttons. A structure type called button_t holds button data:

typedef struct {
  int x;
  int y;
  int txtX;
  int txt;
} button_t;

X and y are the coordinates of the upper left corner of the button; the width and height are the same for all buttons in this version of the system. txtX is the x coordinate for the button text; the y coordinate is calculated and there is no text centering function. Finally, txt is an offset into a button_labels array pointing to the button text.

For the main menu, the button set definition looks like this:

const button_t buttons_main[SIZE_MAIN_SET] = {
  {90, 80, 115, 0 },
  {250, 80, 254, 1},
  {175, 140, 185, 16}};

Determining if a button has been touched is fairly straight forward. The coordinates of a touch p are compared to each button, as b, in the current set to see if it is on or within the button boundaries.

p.x >= b.x && p.x <= (b.x + BUTTON_WIDTH) && p.y >= b.y && p.y <= (b.y + BUTTON_HEIGHT)

Whacking My Head on the Memory Ceiling

The graphics libraries contain a lot of code. With the newest Arduino IDE, the controller sketch compiles to 27,030 bytes, about 83% of available program space; it was about 29k bytes with the previous IDE.

That is still tight enough that I cannot include SD Card access and a function to draw a bitmap from a file without going 15% over the absolute memory limit for an UNO. In the future I’ll use an Arduino MEGA 2560 Board instead of an UNO for control panel applications because of its vastly superior memory resources (and it has a lot more pins to work with). The remaining 17% with the current sketch gives me plenty of room for now.

The trickier bit of memory management is “dynamic memory,” which (on an UNO) is 2,048 bytes of shared memory space used for local variables. Local variables are created when functions are called and destroyed when they are exited. Global variables–variables declared outside of any function that are always in scope and available wherever you are in your sketch–are also stored in the same space. Global variables reduce the amount of dynamic memory available for local variables and, if not managed, can strangle your sketch.

Fortunately, the majority of global variables turn out to be constants — unchanging values or text used by the application. This kind of data can be stored in the program space instead of dynamic memory; the limitations are that

  • you can’t change the value stored in program space while the sketch is running, and
  • you have to copy a value from program space to dynamic memory in order to use it.

The PROGMEM keyword is used to tell the compiler to store something in program space instead of dynamic memory. To park menu titles and button text in program space, I did this:

const char mstr_0[] PROGMEM = "Main Menu";
const char mstr_1[] PROGMEM = "Lighting Menu";
const char mstr_2[] PROGMEM = "Roundhouse Menu";
const char mstr_3[] PROGMEM = "Test Loop Menu";

const char* const menus[] PROGMEM = {mstr_0, mstr_1, mstr_2, mstr_3};

const char str_0[] PROGMEM = "Lights";
const char str_1[] PROGMEM = "Roundhouse";
const char str_2[] PROGMEM = "<-Back";
const char str_3[] PROGMEM = "  Night";
const char str_4[] PROGMEM = "   Day";
const char str_5[] PROGMEM = " Mid-Day";
const char str_6[] PROGMEM = " Sunrise";
const char str_7[] PROGMEM = " Sunset";
const char str_8[] PROGMEM = "   Low";
const char str_9[] PROGMEM = "  High";
const char str_10[] PROGMEM = " Stall 1";
const char str_11[] PROGMEM = " Stall 2";
const char str_12[] PROGMEM = " Stall 3";
const char str_13[] PROGMEM = " Stall 4";
const char str_14[] PROGMEM = " Stall 5";
const char str_15[] PROGMEM = "Afternoon";
const char str_16[] PROGMEM = "Test Loop";
const char str_17[] PROGMEM = "Main";
const char str_18[] PROGMEM = "Siding";
const char str_19[] PROGMEM = "Occupancy";

const char* const button_labels[] PROGMEM = {str_0, str_1, str_2, str_3,
 str_4, str_5, str_6, str_7, str_8, str_9, str_10, str_11, str_12,
 str_13, str_14, str_15, str_16, str_17, str_18, str_19};

Copying the title of the main menu into a local variable text looks like this:

strcpy_P(text, (char*)pgm_read_word((&menus[0])));

For getting the button labels:

 strcpy_P(text, (char*)pgm_read_word((&button_labels[b.txt])));

An alternate way to store static data in program memory is to use the F macro, as in this declaration of a local variable that initializes with a static value that is stored in and retrieved from program memory:

String readyStr = F("Ready");

At this point I find it useful to make it a habit to use these tools in all sketches to tame dynamic memory space. Currently the controller sketch uses only 771 bytes or 37% of dynamic memory for global variables, leaving plenty of space for locals.


The Lighting and Roundhouse menus look like this:


Lighting Menu


Roundhouse Menu

Roundhouse Menu

These are the controls I used off screen to control lighting when making the Roundhouse demo video. Overhead lighting was supplied by 4 led light bars (152 RGB ALEDS total) controlled by a networked UNO.

I’ve been busy at the test loop trying out various ideas.  Turnout control, signals and block occupancy detection (I have a method that works for both DC and DCC layouts), all play a part in the next step toward the layout. I’ll leave you to ponder the test loop menu until next time.

Test Loop Controls

Test Loop Controls