In previous posts on Arduino DCC Decoders I have been using the word ‘function decoder’, which has led to some confusion. The decoders posted so far are what I should have called accessory decoders.
An accessory decoder listens to a DCC accessory address and switches an output or drives a servo. One Arduino DCC decoder can be configured to listen to max 16 DCC addresses.
A function decoder listens to one address (a locomotive address, or as the NMRA calls it, a multi-function decoder) and it switches locomotive functions, which are called F1, F2, etc. on command stations or handheld controllers.
Now that this is cleared up … here it is … an Arduino and ATtiny DCC Function Decoder.
And, if you like it small enough to fit into an HO wagon (with a diode bridge and a capacitor as a power supply), this code also works with the 22×18 mm ATtiny boards that can be had for under $1,50 here. For more info on how to get the ATtiny working, see this post.
Of course the DCC signal needs to be fed to the Arduino / ATtiny. For that I use this opto coupler circuit. If it needs to be smaller to fit inside a wagon, it is also possible to use a Zener diode and a resistor.
Configuration is really easy. In line 1 of the code state the loc address you want it to listen to. In the lines that follow, define the output pins of F0, F1, F2 … up to F12. Not all need to be defined, just the ones that you want to use.
On the Arduino outputs 3-19 are available (14-19 = A0-A5). On the Attiny P0, P1, P3, P4, P5.
On both the Arduino and the ATtiny, Pin 2 is the DCC input.
int decoderAddress = 1830; // This is the decoder address, change into the number you want.
#define F0_pin 13 // Define the output pin for every Function number in use. 13 has on board LED.
#define F1_pin 0 // Available pin numbers: 3-19
#define F2_pin 0
…
#define F12_pin 0
Download link to the Arduino and ATtiny DCC Function Decoder.
Unzip and place the content of the libraries folder in your Arduino Sketches/libraries folder. Place the other folders in your Arduino Sketches folder. The zip includes the function decoder as well as the accessory decoder software. Alternatively you can download the overall zip file from the Software page, it includes all software published so far.
..
Hello Rudy thanks for your website. I found a method different from yours that doesn t not require octocoupler (please refer to https://nagf2rpi.blogspot.com/2021/02/an-attiny85-as-dcc-function-decoder.html). This function decoder is using the library nmradcc. Whar do you think about?
LikeLike
Hi Rudy
I have recently acquired a couple of Digispark Attiny85 boards that I would like use with my train layout using your designs. I have successfully loaded the Accessory Decoder software on to a board and it works 100% but I cannot load the Function Decoder Software at all.
Using the Arduino IDE software I cannot even Verify the sketch. When I try to verify the following fault/s is displayed:- C:\Users\Main\Documents\Arduino\RB_DCC_Decoder_Function_ATtiny/RB_DCC_Decoder_Function_ATtiny.ino:91: undefined reference to `DCC’
Apart from the DCC address (50) I have not changed any code.
What am I doing wrong?
LikeLike
Hello
The Functiondecoder is a very interesting sketch.
At the moment i test your sketch for a Functiondecoder, but how i make 2 pins at the same function button for forward and backward?
LikeLike
I don’t understand your question … what are you trying to accomplish?
LikeLike
Hello
I would like to use this sketch for a control car as a decoder, it is possible for the decoder to recognize the direction of travel forwards and backwards.
Or have I misread?
the whole system is controlled with a train controller.
LikeLike
The code recognizes DCC ‘function key’ commands for a loc encoder. I’m not sure if direction of travel will be recognized with the current code.
LikeLike
Hi Rudy
I could load the Sketch for the Function decoder without any Problems on an Attiny 85, but it doesn’t work with DCC++.
The internal clock is set on 1 MHZ. Is this correct?
Best regards
Josef
LikeLike
Hoi Ruud. Met veel intresse de DCC functie decoder gedownload. Ik krijg alle f1 tot fx werkend, maar F0 wil maar niet lukken. Ik werk/test met een Roco MM. Is daar de F0 anders dan de standaard? Dit omdat alles lijkt te werken, alleen de F0 aan uit niet. Daardoor weet ik ook niet of de richtingsafhankelijkheid werkt, maar das secundair. Weet jij wat hier de oorzaak kan zijn? In ieder geval bedankt voor de software, ik ben er blij mee! Groetjes, Hans.
LikeLike
Hans, ik heb geeb idee. Zodra ik tijd en zin heb zal ik er eens naar kijken of te achterhalen is waarom F0 niet meer uit te schakelen is met de MM. Met de ECoS nooit gezien. Kan ook qua logica niet verzinnen waarom het zou gebeuren … het blijft toch een bit binnen een data byte dat aan of uit staat. Kan je een keer de Arduino DCC sniffer aan je baan hangen om te kijken welk commando de MM F0 uitstuurt?
LikeLike
Hoi Ruud, wonderlijk, ik zie bij het drukken van F0 geen verandering….
Ik heb een aantal keren de stop knop bedient, afwisselend met F0 aan of niet.
Aan het einde nog een keer de snelheid van de lok gestuurd, een zijde op.
De log van de sniffer:
Loc 1830 Stop Data 11000111 100110 1100000
Loc 1830 L F4-F1 0 Data 11000111 100110 10000000
–
Idle
Loc 1830 L F4-F1 0 Data 11000111 100110 10000000
Loc 1830 Stop Data 11000111 100110 1110000
–
Idle
Loc 1830 Stop Data 11000111 100110 1100000
Loc 1830 L F4-F1 0 Data 11000111 100110 10000000
Loc 1830 Stop Data 11000111 100110 1110000
–
Idle
Loc 1830 Stop Data 11000111 100110 1110000
Loc 1830 L F4-F1 0 Data 11000111 100110 10000000
Loc 1830 Stop Data 11000111 100110 1100000
–
Idle
Loc 1830 Rev 1 Data 11000111 100110 1000010
Loc 1830 L F4-F1 0 Data 11000111 100110 10000000
Loc 1830 Rev 5 Data 11000111 100110 1000100
Loc 1830 Rev 9 Data 11000111 100110 1000110
Loc 1830 Rev 13 Data 11000111 100110 1001000
Loc 1830 Rev 17 Data 11000111 100110 1001010
Loc 1830 Rev 21 Data 11000111 100110 1001100
–
Idle
Loc 1830 Rev 21 Data 11000111 100110 1001100
Loc 1830 L F4-F1 0 Data 11000111 100110 10000000
Loc 1830 Rev 23 Data 11000111 100110 1001101
Loc 1830 Rev 25 Data 11000111 100110 1001110
LikeLike
Tsja, dan lijkt het er op dat de F0 niet doorkomt, als de sniffer hem niet ziet, dan zou je zeggen day ie niet op de rails terecht komt. Maar als je 1830 loc hem wel ziet … dan is dat een interessant mysterie.
LikeLike
Hi Ruud, ondertussen weer aan het spelen geweest en ondertussen komt de F0 door. Geen idee wat het geweest is, maar de sniffer ziet hem en de Arduino reageert ook. Alleen nu het snelheidscommando nog, die lijkt niet in dccPacket[2] te zitten maar wel in dccPacket[3]: 1>>128>>255, waar 0=128.
Ik krijg voor instrByte1 nergens een 2 of 3, alleen 1, 4 en 5. Waar in dccPacket[2] zou de richting te zien moeten zijn? Ik kan dan wat gerichter kijken wat er zou moeten gebeuren. In ieder geval blijft het mij bezig houden zoals je merkt en ben blij met je voorbeelden!
In ieder geval bedankt, groetjes, Hans
LikeLike
Hans … het zou best kunnen dat ik in de code geen moeite gedaan heb om 128 snelheidsstappen te ‘decoderen’, ik kan het me niet herinneren, is te lang geleden. Om je aannames te verifieren zou je in de NMRA documenten kunnen kijken, daar heb ik alle decodeer informatie zelf ook uit gehaald. Hoofdsruk S.9.2.
http://www.nmra.org/index-nmra-standards-and-recommended-practices
LikeLike
🙂 Rudy, bedankt voor de feedback, ik begin het te snappen. Ik heb nu dccPacket[3] gebruikt om mede de snelheid te detecteren. Sketch werkt nu prima! Groetjes, Hans
LikeLike
Beste Hans, je schrijft in onderstaande bericht dat het je gelukt is om de juiste pakketten te lezen. Zou je de code mogelijk kunnen delen of misschien in contact met me kunnen komen om het te bespreken.
Ik hoor graag.
mvg
Harjo
LikeLike
Harjo, ik heb het voor 14 en 128 snelheids stappen gecodeerd, 28 snelheids stappen wil op 1 of ander manier niet lukken. Ik gebruikt zelf alleen 128 stappen, dus ik heb het even gelaten.
Ik weet niet hoe ik het hier kan delen helaas, inkopieren lukt niet.
Groetjes, Hans
LikeLike
Hello,
I use an attiny84, and i don’t found how modify your sketch attiny accessories to work with attiny84. When I upload, the pin output no change.
Can you help me?
Thank you
LikeLike
I have a DCC decoder code available specific for the Attiny. Not sure if it is included in the zip file on the software page, you can check. If not, let Me know and I’ll send it.
LikeLike
Hello
I’m searching for a function decoder where the ‘direction’ is available. Then it is possible to have a changeover of white/red lights on a push/pull train. I’ve looked on all arduino DCC sites and never found this option; If this is possible, it would by highly appriciated.
Kind regards
Claude
LikeLike
Hi Claude. This can be built. I’ll see what I can do and then get back to you. Don’t know yet at what time frame.
LikeLike