loading

Further to my previous Instructable on a wireless DCC control of a garden train, I have developed a method of wireless transmission of DCC packets from a Command Station to allow dead rail control of locomotives. The basic design is covered here on the circuits and Arduino coding required. The circuits fit into 'G' garden scale locos. However, I leave it to the reader to develop this system into a smaller size for mounting into HO / OO scale trains.

The system was originally developed using the uRF328 (Arduino Uno with wireless). The nRF24L01+ along with an Arduino Mini Pro provides a cheaper option, therefore the system has been adapted to use these modules.

Download the Arduino driver for nRF24L01 http://maniacbug.github.com/RF24

Step 1: DCC Packet Transmitter

Components:
Arduino Mini Pro

nRF24L01 wireless transceiver
SN754410 H-bridge
DC regulator 5v output
DC regulator 3v3 output
Rectifier diodes 1N4001 (2off)
Resistors 10K and 4K7
Capacitors 22uf, 3.3uf, 3.3uf

The circuit above, is the transmitter. A pair of resistors reduces the input voltage to acceptable levels for the Mini Pro. I used the 5volt version of the Mini Pro. The 3.3v version may be used which would eliminate the need for a 5 volt regulator. The Arduino code is based on great work done by Geoff Bunza in the Model Railroad Hobbyist Magazine forum.

The loco commands are extracted from the DCC signal. The packet data is therefore isolated from the signal and transmitted as digital instructions to the receiver via the nRF24.

// nrf24 - transmit DCC data 4 Oct 2016

#include "SPI.h"   
#include "RF24.h"  
#include "NmraDcc.h"

//Settings
int msg[1];
 RF24 radio(9,10);
 const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
 NmraDcc  Dcc ;
 DCC_MSG  Packet ;
 struct CVPair{
  uint16_t  CV;
  uint8_t   Value; };

void setup() {
    radio.begin();
    radio.openWritingPipe(pipes[0]);
    radio.openReadingPipe(1,pipes[1]);
    radio.stopListening();
  
  Serial.begin(115200); 

// Setup which External Interrupt, the Pin it's associated with, disable pullup.
  Dcc.pin(0, 2, 0);
// Call the main DCC Init function to enable the DCC Receiver
  Dcc.init( MAN_ID_DIY, 100,   FLAGS_DCC_ACCESSORY_DECODER , 0 ); 
  Serial.println("Ready");
}

void loop(){
Dcc.process();
}

extern void notifyDccMsg( DCC_MSG * Msg ) {
 if( ( Msg->Data[0] == 0 ) && ( Msg->Data[1] == 0 ) ) return;  //reset packlet
 if( ( Msg->Data[0] == 0b11111111 ) && ( Msg->Data[1] == 0 ) ) return;  //idle packet   
String s1 = String(Msg->Data[0], DEC) + ",";
String s2 = String(Msg->Data[1], DEC) + ",";
String s3 = String(Msg->Data[2], DEC) + ",";
String s4 = String(Msg->Data[3], DEC) + ",";
String theMessage = ("DD," + s1 + s2 + s3 + s4 + '\0');
  int messageSize = theMessage.length();
  for (int i = 0; i <= messageSize; i++) {
    int charToSend[1];
    charToSend[0] = theMessage.charAt(i);
    radio.write(charToSend,1);
  }
 }

Step 2: DCC Packet Receiver

Components:
Arduino Mini Pro
nRF24L01 wireless transceiver
SN754410 H-bridge
Step up module 5v to 3v3 (or 3v3 battery)
Step down module 5v to 12v (or 12v battery)
Capacitors 220uf, 220uf, 100nf

The receiver decodes the wireless signal and extracts the loco instructions. These instructions include all DCC command data for loco ID, speed and various functions. The instruction data is combined with the DCC command clock that runs on the receiver Arduino Mini Pro. The output from pin 6 and inverse output from pin 5 provides the input for the H-bridge.

Update 4 Oct 2016:

To decrease processing time, the Arduino code has been modified and now only accepts one loco address in line 36 (int locoAdr = 3).
The use of preamble + 3 bytes or preamble + 4 bytes (depending on DCC Command station in use) must be entered into line 53 { { locoAdr, 0, 0, 0, 0, 0, 0}, 3} // enter 3 for 3 byte and 4 for 4 byte
I have not been able to complete this automatically in the Arduino code as yet.
Also, I have removed the 'printf' functions and other radio functions not required and the speed is a lot better.

The nRF24L01 internal supply is very unstable and a 220uf capacitor must be connected across the +/- supply pins.

I have used one pair of the SN754410 H-bridge, the load capacity may be increased by connecting the other pair in parallel.

By using a suitable set of LI-Poly batteries to provide 3v3 and 12v supplies, the step up and step down modules may be eliminated.

<p>As as complete newby to Garden Railroads I think this is great.</p><p>Any chance someone could put down the RS product codes, as I would like to order the parts from one location. </p><p>When I search on the RS site I get about a dozen items for the same code, eg the 5 volt regulator.</p><p>Keen to have a try with my limited electronics knowledge.</p>
<p>Buy the Arduino pro mini on eBay</p><p><a href="http://www.ebay.co.uk/itm/Arduino-Pro-Mini-Atmega328P-5V-16MHz-Development-board-with-header-pins-/132081337876?hash=item1ec0a94614:g:gFIAAOSwnHZYkJNh" rel="nofollow">http://www.ebay.co.uk/itm/Arduino-Pro-Mini-Atmega3...</a></p><p>You will need a CH340 driver also</p><p><a href="http://www.ebay.co.uk/itm/CH340-CH340G-USB-TTL-Serial-Adapter-Arduino-Pro-Mini-like-FTDI-CP2102-PL2303-UK-/251742353968?hash=item3a9d037a30" rel="nofollow">http://www.ebay.co.uk/itm/CH340-CH340G-USB-TTL-Ser...</a></p><p>RS stock numbers for other parts:</p><p>nrf24l01 885-5748</p><p>5 v reg. 298-8508</p><p>3.3v reg 686-9552</p><p>sn754410 h-bridge. 527-704</p><p>0.1uf cap. 108-2700</p><p>220uf cap. 526-1480</p><p>10k ohm resistor. 150-928</p><p>4.7k ohm resistor. 151-000</p><p>1n4001 diode. 628-8931</p><p>3.3uf cap. 475-9289</p><p>22uf cap 449-1012</p>
<p>Thanks so so much Bill</p><p>Just cant find the 3.3uf cap. 475-9289 on the RS site?</p><p>Any alternatives to this?</p>
<p>Gary,</p><p>You should find this cap here</p><div><a href="https://uk.rs-online.com/mobile/p/aluminium-capacitors/4759289/" rel="nofollow">https://uk.rs-online.com/mobile/p/aluminium-capacitors/4759289/</a></div><div>If not any 3.3 uf 25 volt (or greater) capacitor will do.</div><div>Please note, the sn754410 h-bridge I use is suitable for train motors less than 2 Amps. If you have larger motors &gt; 2Amps, you should use a more powerful H-Bridge.</div><div>Also, I found the 12v supply to be too low and now use 14.4v batteries (12 x AA re-chargeable NiMh (1.2v each)). These are mounted under the floor of the home made diesel (DMU) or can be placed within a truck or carriage of the train with connecting cables. </div>
<p>Thanks Bill</p><p>Plan on using this with some Bachmann G scale locos powered by 14volt batteries as you now do.</p><p>Not sure on the Amp draw but I don't think they should draw over 2A as I wont be running long trains and don't have any gradients.</p><p>Is there another H bridge that I should use just to be on the safe side?</p><p>Thanks again for all your help, really kind of you</p>
<p>Hi Gary</p><p>Another h-bridge that supplies up to 3 Amps is this one on eBay (from Hong Kong). I have used this on my DCC Command Station project.</p><p><a href="http://www.ebay.co.uk/itm/3A-75W-PWM-DC-Motor-Driver-Module-LMD18200T-Speed-Regulator-Controller-Board-New-/201693961034?hash=item2ef5e59b4a:g:stoAAOSwh2xYBaDC" rel="nofollow">http://www.ebay.co.uk/itm/3A-75W-PWM-DC-Motor-Driv...</a></p><p>Happy to help.</p>
<p>hello Bill</p><p>Looks like it's just you and me getting excited about DCC over wireless, once again thanks for making it happen for me.</p><p>I think there is a fear of the small LiPo battery's etc, my &quot;on track &quot; charging circuit means that the battery becomes a &quot;fit and forget&quot; part of the equation when running with other locos on a DCC powered track and with over 2hours of running time with a converted Hornby OO gauge steam loco with no track power at all.</p><p>Yes it's day to day job is pulling the track cleaning unit, that's until we can get the other 50+ locos converted.</p><p>Still on the bench with Geoffs loco decoder, pin 6 to 2 showed no sign of life? </p><p>I hope you still think it was a good idea ;)</p><p>Thank you again</p><p>MartinK</p>
<p>I have upgraded 2 of my garden trains with the wireless DCC system. So yes I do think it was a great idea from you Martin. The old LGB loco needed a more powerful battery. I am currently using a small lead-acid 12v battery from an alarm system. It sits in a modified truck along with the Arduino circuit. This is much easier in these large scale trains of course. I agree, the Li-Po batteries are a bit scary for some. I use the Li-Ion type in my diesel engine which has space under the floor to house them. </p>
<p>hello Bill</p><p>I will have to read this twice! But I think you have &quot;nailed it&quot; really can not wait to upload the code and give it a try.</p><p>Great idea to make it a DCC to DCC end to end wireless solution with the H-Bridge at the receiver end, if I want to use Geoffs &quot;mobile decoder&quot; can I just connect pin 06 PWM from the Arduino to pin 02 on the mobile decoder to give it a try? In the long term the plan would be to incorporate your code with Geoffs to give a single Arduino /nRF24 (basic wireless DCC Decoder) not sure if the clock/timings and I/O are compatible :(</p><p>Buts that's in the future, this gives me a great stepping stone to my Battery/wifi loco project and I can keep my very expensive sound decoders working :) without having to clean the wheels and track every few weeks.</p><p>Best Regards</p><p>Martin</p>
<p>It should work:- pin 6 Arduino to 2 on mobile decoder. To get the speed packets, your code in the mobile decoder should be similar to mine as Geoffs filters out all non-function packets.</p><p>I have found variations on the output of different DCC Command stations on both packet contents and voltage levels. e.g. Bachmann EZ operates from a 16 volt supply (dangerously high for some decoders!), whereas my own runs from 12volts. This will make a difference to the resistor chain (10k / 4k7) on the input to the Arduino.</p><p>Good Luck and thanks again for the ideas.</p><p>Bill</p>
<p>hello Bill</p><p>Finally got a couple of hours spare, put it all together with only one problem so far! </p><p>The Reciver code throws the error &quot;void value not ignored as it ought to be&quot; on line 182 .... done = radio.read(inmsg, 1);</p><p>I have // it out to get it to upload, the serial monitor shows the rNF24 info but no received commands (the Transmitter does show commands in serial monitor) ?</p><p>Hope it's just a typo ?</p><p>Regards</p><p>Martin</p>
<p>Hi Martin, </p><p>I do not get this error. Have you got all the library files included for the nRF24.h and nRF24L01.h? There area few entries in the Arduino forum that cover this error code. Have you the latest Arduino IDE 1.6.12 ?</p><p>The radio will not work if this line is commented out (//)</p><p>By the way, what Command Station are you using ? Does it supply a 3 byte address or 4 byte ? My code is for 4 byte. I will develop code for 3 byte (or both soon). The nRF24 radio is quite slow and adds a noticeable delay to transmitted commands which might be a problem. Need to look into this further.</p><p>Regards</p><p>Bill</p>
<p>hello Bill</p><p>Thank you for the help with this as I assume the problem is with my setup!</p><p>Arduino is 1.6.12</p><p>RF24 library is TMRh20 Ver 1.1.7</p><p>My guess is this library is the problem,whilst up to date it is not the one you used? If I remove the done= from line 182 it builds and receives the DCC command but only once i.e the loop is stopped after the first run. Probably due to the Bool done test?</p><p>I am using the SPROG 2 as a bench test unit in service mode. (Have not tried it in Opps mode yet)</p><p>I also have ESU Ecos and Roco command stations.(no tested it yet with these)</p><p>Re the 3/4 Byte my understanding was that the Basic Packet Format (NMRA) is 4 Bytes, preamble (1byte) and 3 command Bytes.</p><p>The Extended Packet Format is upto 6 Bytes.</p><p>I see Geoff uses Alex Sheppards library NmraDcc.h to handle the Packet formats.</p><p>Re the speed of the RF24. Is it possible that the serial.println is slowwing thing up? I have seen others comment them out once they have their projects debugged.</p><p>Regards</p><p>Martin</p>
<p>Hi Martin</p><div><p>You should use the Maniacbug library driver for the nRF24</p><p>Arduino driver for nRF24L01<a href="http://maniacbug.github.com/RF24" rel="nofollow">RF24: Driver for nRF24L01(+) 2.4GHz Wireless Transceiver</a></p><p></p><br><br><br>RF24: Driver for nRF24L01(+) 2.4GHz Wireless Transceiver<br><p> I shall add this detail to the Instructable</p><blockquote>To decrease processing time, the Arduino now only accepts one loco address in line 36 (int locoAdr = 3). The use of preamble + 3 bytes or preamble + 4 bytes (depending on DCC Command station in use) must be entered into line 53 { { locoAdr, 0, 0, 0, 0, 0, 0}, 3} // enter 3 for 3 byte and 4 for 4 byte</blockquote><blockquote>I have not been able to complete this automatically in the Arduino code as yet.<br><br>Also, I have removed the printf functions and other radio functions not required and the speed is a lot better. </blockquote><p>Regards </p><p>Bill</p></div>
<p>hello Bill</p><p>You have been very busy! </p><p>It has been quite the learning curve on this one! Thank you for the library link, I looked at that one but it was quite old and there were a few more recent folks to it (newer is not always better it seems).</p><p>So my SPROG2 is 4 Bytes, not a problem setting that one, probably low on the &quot;features list&quot; to fix in software. As users give feedback you could create a list of command stations.</p><p>I have added a fixed wifi address of 108 (above most domestic wifi etc) also reduced the speed to 250kbps to help with data reliability and set the power to Low (as it's on a bench!) they can go back up once it's in a loco.</p><p>With the fixed decoder address 3 hard coded my set up is quite reliable forward/reverse and stop are all ok (just the odd time when at full speed it fails to respond to a slower speed? Serial monitor reset the Arduino so I am not sure why it stopped). The forward/reverse LEDs were working but have now stopped, not sure why yet? </p><p>I am still on &quot;training wheels&quot; when it comes to software so please forgive me if I say something inappropriate ;) but I am enjoying learning.</p><p>Good to hear the speed has improved, I have also seen the use of millis() to improve code speed? I read about it and had to have a lie down afterwards! :)</p><p>Thanks again</p><p>Martin</p>

About This Instructable

1,229views

11favorites

License:

More by BillC35:Arduino - adjustable & smart battery charger  Model Railway DCC Arduino wireless commands on a dead rail system Garden Train - Arduino / Wireless DCC 
Add instructable to: