You're reading ...
Software

DCC Function Decoder for Arduino and ATtiny

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.

ATtinyNow 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.

 

..

About RudyB

Hobbyist

Discussion

20 thoughts on “DCC Function Decoder for Arduino and ATtiny

  1. 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?

    Like

    Posted by Le Grin | August 14, 2022, 21:29
  2. 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?

    Like

    Posted by Gareth McNaugton | September 18, 2020, 08:11
  3. 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?

    Like

    Posted by Brendon Gerber | September 14, 2020, 18:51
    • I don’t understand your question … what are you trying to accomplish?

      Like

      Posted by RudyB | September 15, 2020, 10:08
      • 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.

        Like

        Posted by Brendon Gerber | September 15, 2020, 17:39
      • 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.

        Like

        Posted by RudyB | September 16, 2020, 09:13
  4. 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

    Like

    Posted by Josef Zinkernagel | September 6, 2020, 20:35
  5. 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.

    Like

    Posted by Hans van Scheijen | December 26, 2016, 21:32
    • 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?

      Like

      Posted by RudyB | December 27, 2016, 11:08
      • 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

        Like

        Posted by Hans | December 27, 2016, 19:26
      • 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.

        Like

        Posted by RudyB | December 27, 2016, 21:45
      • 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

        Like

        Posted by Hans | January 27, 2017, 20:00
      • 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

        Like

        Posted by RudyB | January 30, 2017, 09:37
      • 🙂 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

        Like

        Posted by Hans | January 30, 2017, 09:57
    • 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

      Like

      Posted by Harjo | April 30, 2017, 09:50
      • 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

        Like

        Posted by Hans | May 1, 2017, 06:45
    • 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

      Like

      Posted by bouyeyes | May 23, 2020, 19:49
      • 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.

        Like

        Posted by RudyB | May 26, 2020, 16:25
  6. 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

    Like

    Posted by Claude Vandenbogaerde | October 27, 2015, 20:22

Leave a comment

Archive of all posts