ArduinoGate

The Arduino crossing gate controller is designed to use an Arduino Nano driving two 9g hobby servo motors connected to HO scale model railroad crossing gates.  In addition to driving the servos, the controller also drives an audio speaker with 8khz PCM audio recording of a bell.  

The controller is designed to interface with a CMRI railroad controller with a single connection.  The CMRI controller contains all of the logic for when the gates will go down based upon occupancy sensors on the tracks.  The logic can be as simple or as complex as your installation demands.

The controller is built on a 2 inch square piece of perf board using wire-wrap point to point wiring.   There are 3 RJ45 connectors to interface to the gates and the CMRI system.   For just a few of these, it didn't make sense to design a circuit board.

ArduinoGate Schematic: (click for full size image)
ArduinoGate Schematic


The RJ45 connector on the left supplies 12 volt power and ground and connection to the detector logic.   In the case of the Southern California Railway,  the crossing gate logic is implemented with a computer and CMRI detectors, but this could be any of the optical or electronic detectors available on the market.  The detector or CMRI system is tied to pin 6 of the left RJ45.  When the detector system drives this pin low, the gates go down.  The two ports on the right connect to the gate modules via a cat5 cable.  The 10k variable resistor is used to program the servo for gate up and down positions.  The momentary push button is used for programming the gate positions and for testing.  Unfortunately, I forgot to take a picture of the finished module before I installed it under the layout.  If you watch the videos below, you get a glimpse of how the unit is built.  

Component list:
  • Arduino Nano -- various suppliers (try ebay and search for arduino nano)
  • perf board -- Radio Shack
  • 1 10K Variable Resistor,  Digikey 3352W-103LF-ND or equivalent
  • 3 RJ45 connectors -- Digikey part# A31442-ND or equivalent
  • 1 momentory push button -- digikey 450-1650-ND or equivalent
  • 1 piezoelectric speaker -- Radio Shack 273-073 or equivalent
  • 1 220 ohm 1/4 watt resistor. 
  • 2 10uf electrolytic capacitors 25 volt or higher.  These aren't critical, any size 10-50uf should be fine.
tools required:
  • wire wrap tool.  Radio shack used to carry these, but they don't list it on their website.  Try amazon.com or ebay search for "wire wrap tool"
  • 30 gauge wire-wrap wire
  • 30 gauge wire stripper
  • wire cutter
  • long nose pliers
  • soldering pencil (25w) and solder.
  • optional solder-sucker.  Essential if you make mistakes.
Here is a short youtube video on using wire-wrap for electronic prototyping: http://www.youtube.com/watch?v=8SSOKppwpAE

The trickiest part of the assembly process is wrapping the wire on the tiny pins of the RJ45 connectors.  These are designed to be used with a through hole printed circuit board.   Since I didn't have a custom printed circuit board, I just cut a large hole in the perf board that surrounds all of the pins and used my wire-wrap tool to wrap the wire onto the small pins.  I found that the pins are too short to allow for a secure wrap, so I ended up soldering the connection to the rj45 pins just to be certain.

The gates are designed to be installed from the top of the layout with minimal connections underneath.  In this way, you can build and adjust the gates on the bench.  The connection from the gates to the controller is a standard cat5 cable with an RJ45 connector.  

Gate module schematic:

Here is what the gate module looks like.    A small piece of strip-board provides the connection to the cat5 cable
and holds the current limiting resistors for the LEDs.
gate module rback side
crossing gate module


The bell audio is not very loud by design.  We are working with scale sound as well as scale size.    There are 3 sets of these gates on the Southern California Railway, and when things are busy, all 3 gates are making noise.  I wouldn't want it any louder.

The bell audio started with an mp3 of a crossing gate bell.  I used the open source audio editor Audacity to isolate the last ding which included the long decay at the end of the recording.  This sample was saved as an 8 bit wav file with 8khz sample rate.  The wav file was then converted to a hexadecimal in the form of a "C" include file using "wav2c.cpp" (attached below).  The resulting file was named "pcm_sample.h" and included in the main program.

The program source is developed using the WINAVR gcc compiler and does not use the Arduino IDE.   The program could be ported to the Arduino IDE, or Atmel Studio.  I started with WINAVR long before Arduino was on the scene, so I stuck with what is familiar.

Here is the controller in action on John Signor's Southern California Railway.  This is an HO scale layout complete with a functioning CTC machine and signals.   The videos were shot before the logic was finalized in the CMRI controller.  As a result, the gates stay down longer than necessary after the train leaves the signal island.


Here are the gates in place on the layout:


Here is a brief video describing the procedure for programming the gate positions and speeds.  Once the settings have been programmed, they are saved in non volatile ram on the Arduiono.  This only needs to be done once.


The pin numbers on the schematic are numbered from 1 thru 30 as if the arduino is a big DIP package.  I don't use the arduino names because I'm not using the arduino IDE.  

How to compile and install the software

The winavr tools that I use are based off of a unix style development system. Unfortunately, there is a fairly steep learning curve.   There are a few tutorials on the internet, search for "winavr tutorial".   Here is one:  http://www.avrprojects.net/index.php?option=com_content&view=article&id=96:c-programming-with-winavr-step-by-step&catid=49:avr-c-programming-projects-&Itemid=69

Here is a very brief procedure.  I'm writing this off the top of my head, so I'm probably missing something.

2. download all of the files at the bottom of the this page.  Put them all in a working directory.
3. The winavr utilities use the file "Makefile" to determine how to compile and load the program onto the Arduino.  The only thing that needs to be changed is the line that tells how to connect to the Arduino.    You will need to identify which "COM" port has been assigned to the Arduino.  (windows device manager will tell you this.).  Change the following line in Makefile:

AVRDUDE_PORT = COM12

Change COM12 to whatever windows has assigned to your Arduino.  NB: always plug the arduino into the same USB port, otherwise windows will assign you a different COM port number.

The editor program used with Winavr is called "pn" (programmers notepad.)  This is a text editor, but is much more programmer friendly and also has the ability to compile and upload onto the arduino.  I use it to edit all ".c" files, and the "Makefile".

4. Open "Arduino-crossing-gate-controller.c" in "pn".  Select "tools->[winavr] Make all".  This will compile the program.

5. then select "Tools->[winavr] Program"  If everything goes according to plan, the compiled program will be loaded onto your arduino.  If everything worked properly, the led on the arduino should start blinking on and off once per second indicating that it is alive, and waiting for the signal to start the gates.


with this you can hook up the flashers and servo motor without having to wait for a printed circuit board.  The arduino nano plugs directly into the breadboard and you hook up LEDs and resistors and servo motors using jumper wires.

All of the timing critical pieces are handled by interrupts.   The "main" program is at the very bottom of the source.   All the mainline does is wait for a train to come along, then it sets a couple flags that tell the interrupt routines what to do.  When the train moves out of the block, the mainline clears the flags and the gates go up.   It should be fairly easy to add additional relay inputs rather than the single input that I'm using.

ċ
Arduino-Crossing-Gate-controller.c
(16k)
Martin Nile,
Feb 10, 2013, 8:30 PM
ċ
Makefile
(17k)
Martin Nile,
Jun 26, 2013, 5:18 PM
ċ
pcm_sample.h
(45k)
Martin Nile,
Feb 10, 2013, 8:30 PM
ċ
wav2c.cpp
(2k)
Martin Nile,
Feb 10, 2013, 8:38 PM
Comments