Welcome to my model steam train blog. This blog will be showing how to set up the DCC++ base station on an Arduino being controlled by a Raspberry Pi running JMRI. This is an open source program kindly provided by Gregg E. Berman.
Equipment needed is an Arduino Uno or Mega and a motor shield. In my setup I am using the Arduino motor shield R3 which should be capable of running 2 amps on both channels.
First thing will be to setup the motor shield. This shield will plug onto the Arduino, so no wiring connections will be needed between the shield and Arduino. One end of the shield has a terminal bar with 6 terminal points. The first two will be the DC supply 15V for the layout control. The middle two will go to the running track and the last two will be the programming track.
Setting up the DCC++ power supplies, usually the supply to the shield will power the Arduino through the Vin pin to the Arduino. Seeing as the Arduino can only run on max 12V and we are going to be using 15V on the shield to run our layout we will need to cut the Vin solder pad on the underside of the shield to prevent the supply from the shield going into the Arduino. Once this has been done a separate supply will be required for the Arduino. At the moment for this article I will be using the USB computer connection to power the Arduino.
The only other connections required are some jumper wires on the shield. These will differ depending on the Arduino you are using.
For Mega Arduino we will require one jumper wire from pins 2 to pin 13
For Uno Arduino we will require two jumper wires one from pin 5 to 13 and the other from pins 10 to 12.
So that’s it for the connections all we need to do now is download the DCC++ program onto the Arduino and connect it up to JMRI following the steps below.
This next step sets up the Arduino IDE for the first time and can be skipped if you have been using Arduinos before
1. If you have not used the Arduino before you will need to download the Arduino IDE software
2. Once the IDE software has been installed plug in the Arduino with the USB cable to you PC. The Arduino drivers should automatically install.
3. Open the Arduino IDE software so we can test the Arduino board
4. Click on Tools and select board, then select the Arduino you have connected to the PC (Uno or Mega)
5. Click on Tools > Port and select the port your board is connected to.
6. Click on File > Example > Basics > Blink which should open the LED blinking sketch
7. Click on Upload button to upload this sketch to your Arduino. Once this has uploaded if all has gone well you should now see the on-board LED flashing.
Once we have our Arduino connected and working with the Arduino IDE we will now download and install the DCC++ program. Go to the following link.
Click on the code tab, then click on 'Clone or Download' button and select down load zip file to get the DCC++ program.
Open the zip file and copy the BaseStation-master file to your Arduino file within you documents folder. This is the whole file and must be kept together within the Arduino folder for this to work.
Now open the master file within the Arduino folder click on DCCpp_Uno folder then double click on DCCpp_Uno INO file.
This will again start a new Arduino IDE which will contain all the files needed to run the DCC++ base station. Click on the upload button, once uploaded you now have a DCC++ control station, easy right, lets now see if we can get some sensors working with JMRI
First, we will set up two sensors on the Arduino and write these to the EEPROM so they will still be stored when power is removed. These sensors can be used as a micro switch, reed switch or current occupied sensor input into the Arduino.
First open your Arduino IDE and click on the serial monitor (ensure you have the Arduino connected by USB to the PC). When the serial monitor has opened change the baud rate to 115200 speed (bottom right corner). Close the serial and restart it and you should now see the version number and date when it restarts indicating the serial communication is working OK.
To set up the two sensors we will be connecting them to pins 25 and 26 and we will be using the Arduino internal pullup resistor. To do this type the following into the serial monitor. <S 1 25 1> click enter then <S 2 26 1> click enter (capital S). Each time you should see <o> in the monitor indicating the sensors have been set correctly. This setup is explained within the sensor.cpp sketch, in brief the code we are entering means the following.
<S – capital S for sensor
Id – next number is an id you can set from 0 – 32767
Pin number – this is the pin you will be using on the Arduino for your sensor
Pullup resistor – set this to 1 to use internal Arduino resistor or 0 to use an external resistor
Once this has been done we need to save all this to the EEPROM so that it is not lost when the power to the Arduino is unplugged. To do this type <E> into the serial monitor and click enter. You should see <e 0 2 0> indicating this has now been store in the EEPROM. To check all sensors setup within the EEPROM type <S> which will show all sensors stored. If you make a mistake and wan to delete one sensor type the following <S ID> (ID being the sensor you want to delete). Finally, if you want to erase all the sensors within the EEPROM type <e> and click enter.
Now we have the sensor store in EEPROM we need to connect two wires from pins 25 to GND and 26 to GND. This will simulate a closed switch.
We will now setup JMRI to monitor the sensors and show any state change. Open JMRI PanelPro. For a first time of using the DCC++ control station we will need to setup the preferences. You may automatically be asked to set this up on start-up but if not click on Edit > preferences. Set the following up as shown below
Now restart PanelPro.
To setup the sensors click on Tools > Tables > Sensors. Then click the add button to add a new sensor. The control station will be DCC++, the address will be 1 and the name can be what you like, click on create to add the new sensor. Do this again for sensor two this time the address will be 2.
Now let’s turn on the power click on Tools > Power Control and click the On button.
The sensors will initially be set to Unknown, remove the GND cable to pin 25 and the JMRI sensor table will now show inactive for that sensor. Reinsert this and it should have gone to Active. Do the same with pin 26 to see that this does the same. We now have two input sensors working on the DCC++ control station being monitored by JMRI. This could easily be adapted as an occupied sensor.
We will now be setting up to control a DCC loco with JMRI and the Arduino DCC++ station.
Remove the sensor cables installed previously and remove the USB cable from the Arduino so it is now powered down. Place the Arduino shield on top of the Arduino and carefully insert the pins ensuring none bend out.
For connections attach a DC bench power supply to Vin and GND set to 15v and the center two to the track. Ensure you have the jumper cables on the shield depending on the Arduino you are using.
Reconnect the USB cable and power up the bench power. Now test the Arduino is communicating with JMRI by opening the power control panel. When clicking the ON button the 4 LEDs on the Arduino shield should come on.
Now with a loco on your track with DCC chip fitted, enter the address for this loco into the throttle click set and you should hopefully have full control of this loco by JMRI.
Finally, for this article we will be running all this on the Raspberry pi. You should already have JMRI installed on the Pi and a remote connection using VNC viewer. If not please see my previous setup. JMRI on Raspberry Pi
Turn off the bench power supply to the Arduino shield, unplug the Arduino USB from you PC and plug this into one of the USB outlets on the Pi. You can now turn the bench power supply back on. Open the VNC viewer on the windows PC and connect to your raspberry Pi. Now run PanelPro on the pi and go through the setup for a new controller as we did on the windows machine. The only difference here we will be using serial ttyACM0 in the drop-down menu. You will have to restart PanelPro for this to work.
Once the control station if connected to PanelPro try the power on control and see if the LED’s on the shield come on. If they do, then you should easily be able to setup some throttles and control your DCC locos using the Raspberry pi and DCC++ Arduino.
My next step will be setting up the WIFI throttle and look at an interface with pi.