BeagleBone Cape If you have been following my articles on the train controller, you are probably are thinking that it looked a bit silly to use a Beaglebone and an Arduino side to side in order to control a model train.

At least, that’s what I thought, so I decided to correct this, by designing a Beaglebone train controller cape.

Main features

The main features are as follows:

  • Control of one set of tracks with speed regulation.
  • Multiplexed control of 32 relays – one “on” at a time, used to control turnouts.
  • Built-in monitoring of output status, including accessory connected/not connected
  • Four independent relay outputs, to control garage tracks.
  • Built-in switching power supply to power the Beaglebone from the same adapter as the train.
  • Onboard AVR that runs the same program as the Arduino shield version of the controller.
  • AVR can be programmed directly from the Beaglebone, of course.

The biggest differences and improvements over the previous design are:

  • Single cape design
  • Single power supply
  • Relay/accessory detection for diagnostics
  • Relay output

The following sections on this article will go a bit deeper into some of the more interesting features of this cape.

Built-in relay outputs

The train controller is designed to send control pulses to the relays in turnouts: it controls 32 solenoids using barely two chips – see my previous articles on the design of the controller for more information. In a real life setup, though, you might also want to get not only pulsed, but more permanent on/off outputs. Since the solenoid control design left four outputs unused on my initial design, I am now using those as 5V on/off outputs that can be switched using a new firmware command.

The main use case for these on my layout, is to turn power on or off in some track sections, using small 5V relays.

More powerful solenoid controllers

In my first design, I used the Freescale MC33880 relay controller which includes a built-in testing feature, that enables it to check whether relays connected to it are present or not. Those chips are automotive controllers, an environment where this type of feature is essential to detect problems. One shortcoming of the 33880 though, is that this testing feature cannot be turned off. And since I used two chips in a multiplexed configuration, this meant that I always had current flowing from the high side of the multiplexer through the relays, and as soon as several relays were connected, the detection mechanism did not work anymore.

For this reason, I moved to a similar reference, the MC33879, which supports turning accessory detection on and off: in my implementation, this means that I always turn off accessory detection on the high side to avoid leak currents, and only turn it on on the low side: accessory detection in the diagnostics page now works perfectly.

Other than this, the new design also includes the diodes that were missing on the initial version 🙂

Relay controller

Built-in Beaglebone power supply

Another shortcoming of the Arduino shield version, was the need to have a power supply for the BeagleBone, and one for the Arduino shield – the Arduino itself was powered by USB. Not very practical.

With this Beaglebone cape, I included a small switched mode power supply that draws power from the power supply of the controller cape: this way, the train controller (BeagleBone + Cape) is powered through a single wall adapter. The role of that supply is to convert the 8 to 12V of the input to 5V, to power the BeagleBone and its USB peripherals, if any.

Requirements for this power supply were to output 5V, obviously, with a maximum of 3A of output current, and input from 8V to 12V. Good efficiency was also very important, as I did not want to have to put a heat shield on the board. The good news is that TI does not only make the BeagleBone – understatement of the century -, they are also very good with power supplies. Using their online selectors and design tools, I chose the TPS54327 which fit all my requirements.

The resulting schematics for the power supply looks like this:

Switched mode 5V/3A power supply

Switched mode 5V/3A power supply

I used TI’s WebBench online design tool to come up with the electric design and spec out the components, and then followed a mix of common sense and design recommendations in the reference manual of that chip to design the PCB. Switched mode power supplies can get quite ugly if you mess up PCB design, and end up either unstable or very noisy, or both. The end result is shown below:

Switched Mode PS PCB

I won’t pretend I am a professional PCB designer, but after assembly and testing, I was quite pleased to see that the output was actually very clean: no overshoot at powerup, which is important, and not noisy either. In any case, the Beaglebone also does its own power conditioning since it mainly uses 3.3V and lower internally, but a clean primary power is important, especially since the role of the train controller is to switch DC motors and solenoids, a job that tends to generate tons of noise and spikes.

Built-in AVR

The last nice feature of this cape, is that it contains its own microcontroller that runs the train controller firmware. While it would have been possible to design a port of the firmware to Linux and dedicated drivers on the BeagleBone, it was just simpler and more time-efficient to include an ATMega there directly: for communications, the BeagleBone simply uses one of its many serial ports to talk to the AVR.

This of course means that we need a way to program this processor: for initial programming, I included an ISP header on the board. The trick is to simply program the Arduino bootloader to the AVR, not the whole firmware. Once this is done, you can simply use the serial port for uploading the Arduino sketch to the controller!

Using “ino”

Of course, you are probably interacting with the BeagleBone through a SSH session: fortunately, the awesome inotool is a complete command line Arduino toolchain that is perfect for compiling sketches, and then uploading them to the AVR. There is a trick, though: just before sending the “ino upload” command, you need to short the “Reset” and “GND” pins on the ISP header, and remove the short rapidly after you sent the command.

This way, you force the Arduino into bootloader mode and “ino upload” takes control the sketch starts. This simple manual step makes the hardware and software designs much simpler, and is easy to follow.

Using the Beaglebone pins

One of the most powerful features of the Beaglebone is also one of the most intimidating: the sheer number of pins available for input/output, and the fact any of those pins can be remapped to nearly half a dozen functions. Thankfully, the Linux kernel offers great support for PIN muxing through what is called “device tree overlay”. There are many very good articles on the subject that you can find on the Internet, below is the device tree that I have defined to enable the UART I need on this Beaglebone cape (UART2):

 * Copyright (C) 2013 Edouard Lafargue
 * UART2 Pin mux firmware for the Train Controller cape
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.

/ {
    compatible = "ti,beaglebone", "ti,beaglebone-black";

    /* identification */
    part-number = "uart2";

    fragment@0 {
        target = <&am33xx_pinmux>;
        __overlay__ {
            pinctrl_uart2: pinctrl_uart2_pins {
                pinctrl-single,pins = <					 				   
                        0x180 0x20  /* P9_26 = UART1_RXD, Input, MODE0 */
                        0x184 0x0  /* P9_24 =  UART1_TXD ,Output, MODE0 */

		target = <&uart2>;
		__overlay__ {
			status			= "okay";

    fragment@2 {
        target = <&ocp>;
        __overlay__ {
            test_helper: helper {
                compatible = "bone-pinmux-helper";
                pinctrl-names = "default";
                pinctrl-0 = <&pinctrl_uart2>;
                status = "okay";

Conclusion and next steps

This BeagleBone cape has now been running on my layout for a bit more than a month with no issue whatsoever. I have yet to fully solder the output relays for track isolation control, but what would be the fun of a fully finished project? Ideas for future development would be to create a DCC (digital control) version of the cape, to support detection of the position of the train on the tracks, to design an enclosure, add track layout design tools in the web interface, you name it…

As usual, you can find all the code and hardware schematics on my github page (elafargue), I would appreciate very much if you drop me a note if you plan to reuse those in any way, or if you have any question of comment!

2 comments on “Train Controller: Beaglebone Cape version

  • December 23, 2013 at 17:13

    Thanks for series on train controller. I’m interested in adapting to N-scale trolley or T-Trak.

    • December 26, 2013 at 19:15

      Joe, you can probably use this design directly for N Scale – how much power do your locomotives use? If you want a PCB, please contact me directly through the contact form, I have several spares!


Leave a Reply

Your email address will not be published. Required fields are marked *