Model Rail Digital DCC Command Station Arduino and Free App

2,528

5

73

Introduction: Model Rail Digital DCC Command Station Arduino and Free App

Update 15 October 2019: Added sketches for IBT_2 and DRV8871 modules.
Update 8 October 2019: On some phones running Android android 6, 7 or 8 there is a bluetooth issue to be aware of. The phone will pair OK but may not connect through the App. When this happens try switching the phone Bluetooth setting off and on again then close the App before opening again - the BT connection should be OK. - see link for possible answers here.

Assembled modules available on eBay here

Update 14 September 2019:
Updated sketch to extend read/write of CV's to match Lenz, ESU, Hornby, NCE and TCS decoders.
Please use sketch 'locomotive_dcc_v1.ino'

Update 6 September 2019:
Updated sketch to extend read/write of CV's to match NCE and TCS decoders.
Please use sketch 'locomotive_dcc6_Sep_2019'

Update 3 September 2019:
Corrected error in sketch whereby CV write only worked properly on CV1 - 9 e.g. CV29 could not be written. Please use sketch 'locomotive_dcc3_Sep_2019'

Update 21 August 2019:
New App released includes CV read/write facility now available on Google Play Store
here.
Modular kit available on eBay
here.

Update 20 May 2019: please download improved App version 2 making DCC o/p active only when Bluetooth is connected. Updated sketch available here.

Update 18 May 2019: New App released with much more functionality - up to 28 functions with editable titles, visibility and momentary switching. Increase to 16 turnout/accessory buttons. On screen speed control bars added allowing 1 to 4 locos to be controlled at once.
New App on Google Play Store 'LocoMotive Control'

Update 17 Feb 2019: You may have to modify the HC-06 Bluetooth module to extend its range of reception.
I added a solid copper wire (from mains earth wire) 31.2 mm in length (1/4 of wave length for 2.4 GHz). See attached photo in Step 2.


Update 29 Jan 2019:
Addition of momentary option on F2 and F3 for horn sounds
Preset max speed for each loco

This is the most efficient DCC command station possible to build.
No other system can do so much with so few components.
An easy to assemble fully functioning DCC Command station with PCB available on eBay plus requiring only a few parts.
The App 'LocoMotive' is designed to operate on Android phones running version 4.1 and upwards.

No need to purchase any propriety systems, this is a stand alone unit operated by a free App called 'LocoMotive'
Works with NMRA compliant decoders. This system compiles the operating DCC packets within the App which are sent to the Arduino interface to combine with the DCC clock signals.
Purchase this PCB on eBay or here on eBay for modular kit.
Get free App LocoMotive on Google Play Store or full version 'LocoMotive DCC'
Features include:

  • Control of 1 to 20 locos
  • Ideal for small to medium size layouts
  • 2 Amps load drives up to 16 of OO/HO locomotives using H-bridge specified
  • Add a higher current compatible h-bridge to extend load capacity
  • Short circuit protected
  • Automatic over current cut-out, adjustable in Arduino code
  • Lights and direction
  • Speed control bars for 1 to 4 locos
  • Functions 1 to 28
  • Edit function names, visibility and momentary action
  • Turnout / points / accessories operates up to 16 pairs of outputs
  • Edit turnout names
  • Custom naming of your locos
  • Programming the CV1 loco address
  • Read / write CV's 1 to 255
  • Add your own accessory addresses
  • Choose a DC power source to suit scale used (Z/N/OO/HO/O/G) 12v to 20v

Further to previous work on DCC wireless systems, I have developed a Bluetooth Command Station linked to a receiver Arduino based circuit with a HC-06 BT module and a LMD18200 H-bridge Motor driver delivering 2 Amps.

The overall cost of parts is around £20 with parts purchased from eBay.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Arduino Sketch

Depending on the h-bridge you select, use the appropriate Arduino sketch:

For LMD18200T : 'locomotive_dcc_v1_1.ino'

For IBT_2: 'locomotive_IBT_2.ino'

For DRV8871 : 'locomotive_dcc_DRV8871.ino'
Note: The DRV8871 module has a 30k resistor on current limit, change this to 15K

The following portion is the loop and data receiver code. The Android App sends a complete packet to the receiver and is decoded here into an array of data. Depending on whether 3 or 4 bytes are received the code will generate the correct DCC packet format for the train track.

void loop() {
if(bluetooth.available() > 0); {inString = bluetooth.readStringUntil('\n');}
if (inString.substring(0,1)== "D") {
string();}
delay(20);}

The string() void then splits out the data from the text string received and places each instruction into its
appropriate position in the DCC packet.

For example:

A received text string of 'DD,3,63,12,48,' translates into a 4 byte Speed packet of 8 bits each :

DD = message ID
Byte: [1] [2] [3] [4]
Decimal: [3] [63] [12] [48]
Binary: [0000 0011] [0001 1111] [0000 1100] [0001 1000]
Action: [Loco 3] [speed steps] [ loco speed] [ XOR data check]

A received text string of 'DD,1,129,128,' translates into a 3 byte Function packet of 8 bits each :
Decimal: [1] [129] [128]
Binary: [0000 0001] [1000 0001] [1000 0000]
Action: [Loco 1] [ F1 on] [XOR data check]

For more information on the NMRA packet standard refer to:
https://www.nmra.org/sites/default/files/s-9.2.1_2...

The LMD requires the DCC signal on the DIR pin and the PWM pin held HIGH.

This latest version includes the Read Write of CV's facility. The sketch has additional code to create the required packet formats to enable reading and writing of CV values.

Step 2: Fritzing Circuit Diagram

Note: If you are using the IBT-2 module or the DRV8871 , another Arduino sketch is required - please use appropriate sketch.

Updated 2 Sept 2019: Fritzing circuit diagram updated with change in Tx Rx BT connections

The Arduino Pro Mini receives the Bluetooth data via the HC-06 module. The Arduino generates the NMRA standard clock cycles of data in packet format. The received data interrupts the sequence when new data is received and the DCC packet is updated to send instructions to the train tracks.

Note: The Arduino must be a Pro Mini ATmega 328 16MHz 5v version

List of parts required:

PCB available on eBay.

1 off Arduino Pro Mini Atmega328P 5V/16M eBay
1 off LMD18200T H-bridge eBay
1 off HC-06 Bluetooth module eBay
1 off 0.1 ohm 2W Metal Film Resistor 0.1R 2 Watt (11.5 mm x 4.5 mm) eBay
3 off Capacitor 0.1uf eBay
2 off 10kΩ Resistor eBay
1 off Capacitor 10uf 25v; eBay
1 off Capacitor 220uf 16v; eBay

1 off Phoenix Contact MKDS 1/ 2-3,5 2 Way Screw PCB Terminal Block 13.5A 200V 3.5mm eBay
1 off 4.7kΩ Resistor eBay
1 off L7805 CV Positive Voltage Regulator IC eBay
1 off 4 pin Female Header Edge Pins Strip 0.1" 2.54mm eBay
2 off 12 pin Female Header Edge Pins Strip 0.1" 2.54mm eBay
1 off 6 pin 2.54mm PCB Universal Screw Terminal Block eBay
1 off Zener Diode BZX79C3V6. 3.6V; 0.5 W eBay

Wire

Power `supply: For OO/HO layouts use a 14v DC power supply (e.g. laptop charger unit from eBay). For example this delivers up to 2.5 Amps. Check the size of panel mounted socket required to fit the DC jack plug on the power supply chosen for this project. Do NOT use a DC train controller as these do not provide a true DC voltage.

For OO/HO layouts use a 14v DC power supply (e.g. laptop charger unit from eBay).
14V 4 Amp version with a 2.5 x 5.5 mm plug, search for eBay item # 401748337196
15V 2 Amp version with a 2.1 x 5.5 mm plug, search for eBay item # 401871382681

To extend the range of the HC-06 Bt module, solder a wire onto the end of the antenna on the module. I used a solid copper wire (from mains earth wire) 31.2 mm in length (1/4 of wave length for 2.4 GHz). See photo above.
This wire should be covered in sleeving to avoid accidental shorting to other components.
Please note - the HC-06 connections are Rx (on PCB) to Rx (on module) and Tx to Tx

For the use of a LMD18200 h-bridge module (as displayed above this is in module form with heatsink etc), I have added current sense components to provide protection from short circuit or overload issues on the train track, The updated circuit diagram and Arduino code now includes the current sense function. The code limits the max current to 2 Amps when reached, the LMD18200 h-bridge is shut down by applying a HIGH signal from D13 on the Arduino to the 'Brake' input on the h-bridge. The use of a 0.1 ohm current sense resistor gives an analog input range of 0 to 200 mv on Arduino sensor pin A0 for up to 2 Amps load. For approx 5mv per analog step input (1023 total steps or divisions) gives 0 to 40 divisions or 2 Amps / 40 = 50mA per division. The measured current precision is 50 mA however we are protecting the h-bridge when 2 Amps is reached. The LMD18200 also has in-built over temperature protection as a back up.

if(C >2000){ // 2 amps

Serial.println("Over Current");
digitalWrite(13, HIGH); }

The value of the current drawn is sent to the Android App via Bluetooth every second.

void send_c(){
t = millis() - lastmillis;
if (t >= interval){
bluetooth.write(sensorValue);
bluetooth.write('c');
lastmillis = millis();
} }

Step 3: Android App

Download the free version of the App from Google Play Store - 'LocoMotive_free'

Download the full version (£5.99) of the App from Google Play Store - 'LocoMotive DCC'
Instructions attached for full version with enhanced features including read/write of CVs, full function control 1-28, 4 off speed control sliders, buttons with fine control of speed steps up and down.

Be the First to Share

    Recommendations

    • Magnets Challenge

      Magnets Challenge
    • Raspberry Pi Contest 2020

      Raspberry Pi Contest 2020
    • Wearables Contest

      Wearables Contest

    73 Discussions

    0
    cvetkov.alexander
    cvetkov.alexander

    Question 3 months ago

    Can the LMD18200T be replaced with the L298?

    0
    BillC35
    BillC35

    Answer 3 months ago

    Hi, the L298 does not switch on/off as fast as LMD18200T and output DCC waveform is not as clean - however it may work OK. This would need a change to the Arduino code which I have attached for you to test. The connections would be:

    DCC out 'Motor A'
    Remove jumper for 12v/5v reg
    Connection from connector block on PCB to LM298
    V+. to power pin Vs
    0v to Gnd
    Dir to Input 1
    Pwm to Input 2
    Br to enable A
    Solder wire from regulator 5v output on PCB to 5v input on module

    Please find attached amended copy of Arduino sketch.
    Please note- this is for your experiment and I do not guarantee it will work ok
    Let me know how you get on.
    Best regards,
    Bill



    0
    BillC35
    BillC35

    Reply 3 months ago

    Hi Alex, the L298 is not suitable for this DCC application - it does not have MOSFET outputs (has bipolar transistors with high sat voltage which reduces the DCC output level). I did find an alternative h-bridge that works quite well with amended sketch. The DRV8871 is on eBay at 4.60 GBP
    https://www.ebay.co.uk/itm/272665415480

    The on board resistor of 30k limits current spikes of 2 amps - this is too low to cope with noise levels on a layout where spikes of > 2 amp can occur. I changed this value to 15k (min allowed by TI) with a current limit of 6 amps. The DCC system will stop overloads >2amp while the DRV8871 will shut down on short circuit (recovers when short is cleared). The L298 sketch will not work with the DRV8871. Let me know if you want a copy of the new sketch.

    0
    cvetkov.alexander
    cvetkov.alexander

    Reply 2 months ago

    Hi, I finally got the LMD18200T, but it doesn't work again. Bluetooth connects, but only e-stop works and nothing else. I also bought the new version of the program, but again nothing. Everything under the scheme is checked. What have I not done correctly?

    0
    BillC35
    BillC35

    Reply 2 months ago

    Hi, cannot see the problem from your video. However please check:
    On the Arduino serial monitor, do you see a flow of data DD,n,n
    Are you sure that D5 from Arduino goes to Dir and D6 goes to Pwm ?
    Is there 14v on the V+ on LMD ?

    0
    cvetkov.alexander
    cvetkov.alexander

    Reply 2 months ago

    I am 100% sure that D5 from Arduino goes to Dir and D6 goes to Pwm. The program shows mA on the rails when there is a locomotive. I try with two locomotives - one with a piko decoder and one with an esu decoder, but both do not respond to commands. The piko decoder lights up and I can't turn them off unless I hit the e-stop (arduino diode light up), but nothing else works. The serial monitor show flow data.

    0
    BillC35
    BillC35

    Reply 2 months ago

    Hi Alex, something to try while you wait for new Arduino.
    If your board is running at 8Mhz, change the following lines from:
    #define TIMER_SHORT 0x8D // 58usec pulse length
    #define TIMER_LONG 0x1B // 116usec pulse length
    to:
    #define TIMER_SHORT 0x70 // 58usec pulse length for 8Mhz clock
    #define TIMER_LONG 0x0D // 116usec pulse length for 8Mhz clock

    Not certain if these are correct values, but worth a try.

    0
    cvetkov.alexander
    cvetkov.alexander

    Reply 2 months ago

    I tried, but it doesn't work.

    0
    BillC35
    BillC35

    Reply 2 months ago

    Hi Alex, I had another go at calculating the required numbers for pulse length:
    While you wait for Arduino, please try if you can:
    Change from:
    #define TIMER_SHORT 0x8D // 58usec pulse length
    #define TIMER_LONG 0x1B // 116usec pulse length
    To:
    #define TIMER_SHORT 0xC6 // 58usec pulse length for 8Mhz clock
    #define TIMER_LONG 0x8D // 116usec pulse length for 8Mhz clock

    It would be interesting to see if this works on 8Mhz devices (which I do not have).

    0
    cvetkov.alexander
    cvetkov.alexander

    Reply 2 months ago

    Yes! It already works with these values. Locomotives command fine, but I don't know how to set up the accessory decoder. For example I want T9 to command address 49. How is it done?

    0
    BillC35
    BillC35

    Reply 2 months ago

    Hi Alex, that is good to know for future reference.
    The address you want can be set, albeit a little complicated:
    Go to 'Address' on the App
    Under 'Accessory Address' got to 'Buttons T9-12'
    Enter '13'
    Now buttons T9 - 12 will respond to Acc addresses 49 to 52
    You will see a message at top of screen saying file not found (this will go when you close and open App again)

    The calculation is :
    Acc Address X 4
    Say the number you enter into box under 'Enter 1-63' is A
    (in above example A =13)
    For each set of 4 buttons gives actual addresses of :
    T1,T5,T9,T13
    Actual address = A*4 -3
    T2,T3,T10,T14
    Actual address = A*4 -2
    T3,T7,T11,T15
    Actual address = A*4 -1,
    T4,T8,T12,T16
    Actual address = A*4

    0
    BillC35
    BillC35

    Reply 2 months ago

    Hi Alex, I need to try out the Arduino sketch you have on your servo points, however it looks like a loco address 40 that is used. As my system only covers 1-20, you could try changing the Arduino sketch to address 20 and see if function keys operate the servos.

    0
    BillC35
    BillC35

    Reply 2 months ago

    Hi Alex, I loaded the sketch onto an Arduino Nano (on a points controller pcb I have) and a single servo on D11.
    You need to enter 11, 12, 13, 14, into the Buttons T1-T4, Buttons T5-T8,
    Buttons T9-T12, Buttons T13-T16, Accessory addresses
    This produces Acc addresses of 40 - 56
    The sketch covers 17 servos while my App is limited to 16 therefore whatever servo is on F0 will not work.

    0
    cvetkov.alexander
    cvetkov.alexander

    Reply 2 months ago

    Yes, that's what I thought and tried, but it doesn't work. I will not use F0, this is not a problem. I'll try again and write.

    0
    BillC35
    BillC35

    Reply 2 months ago

    Hi Alex, I have released an update to App that has improved data handling for accessory decoders. Please try with your servo board and hopefully works now. The update has just been uploaded and will be available on Google Play store in a few hours. Look for version 9 (v9)

    0
    cvetkov.alexander
    cvetkov.alexander

    Reply 2 months ago

    Hi, I upgraded to v9, but unfortunately it still doesn't work.

    0
    BillC35
    BillC35

    Reply 2 months ago

    Hi Alex, I will have a v10 available soon as a another attempt to improve the data packet going to your servo decoder. I will add a function to select the number of attempts at sending accessory decoder data (1 t0 5)
    I have made the changes but need to test it out on my layout etc. before release. I will inform you when available.

    0
    cvetkov.alexander
    cvetkov.alexander

    Reply 2 months ago

    Hi, after a few tries i put version 1.2.1 NmraDcc library and the decoder start working. Does not work with version 2.0.0. But there is another very strange problem - when a lighted passenger car passes through the turnouts (the lighting is made by me), the servos start rotating from start to finish without command. When passing a locomotive there is no problem, when I try with the original station there is also no problem. Probably the power of the lights disturbs the dcc signal.

    Screenshot_20191105-003213_Samsung Internet.jpg
    0
    BillC35
    BillC35

    Reply 2 months ago

    Hi Alex, good progress.
    I am aware of the issue with NmraDcc v2 as it applies to my points controller also. I did not mention this as I thought your servo controller worked with the Piko station therefore ruled that out.
    On this new issue, it sound like the Pro Mini on the servo board is being reset.
    This can happen due to spikes. When the car with lights passes the turnouts do the lights flicker ?
    Can you try keeping the wiring from DCC o/p to servo controller as short as possible ? Maybe try twisting the pair of wires ?