Tutorial: How to make an Arduino / ESP32 DCC decoder for model trains and accessories?

Tutorial: How to make an Arduino / ESP32 DCC decoder for model trains and accessories?
Photo by Ainur Khakimov / Unsplash
  • Level of Complexity: EASY
  • Required time: 10 - 15 min

Why should I try to use a programmable DCC decoder?

  • Flexibility - you can implement complex control logic that it's not possible to do with proprietary decoders
  • Zero vendor lock-in - you're not locked with any brand
  • Much cheaper than proprietary decoders
  • Repairable - if even a decoder is broken you can easily replace a component inside it
  • In most cases, after you try a programmable decoder (made by you or purchased) you never return to proprietary decoders because of the previous 4 points

Components of a simple DCC decoder

For your own DCC decoder based on Arduino, ESP-IDF, and other MCU SDKs you need just 2 components

  • A board with MCU that has GPIO outputs. I would recommend ESP32 C3 or ESP32 S3 development boards with built-in Bluetooth and Wi-Fi, PWM, and analog-to-digital converters (for trains it can be Beetle ESP32 - a small ESP32 development board with a battery management chip, for layout accessories - ESP32 S3 because it has more than 30 connections for signals or lights with up to 40mA / output).
  • A DCC converter that can handle alternative current from rails, transform it to direct current, down voltage to power an MCU board, and convert voltage changes on rails to DCC messages. You can use our DCC converter or build it yourself with an optocoupler and rectifier bridge by using our schematic below as a reference (please, note the schematic below is only for non-commercial usage):
  • (Optional) A motor driver if you want to control a train
  • (Optional) An LED driver if the current and voltage of your MCU board aren't enough to control LEDs of signals (our signals can be controlled directly by ESP32 S3 by internal PWM, for example, so you don't need an LED driver for Loco.Engineering signals)
  • (Optional) Digital to Analog converter and amplifier to play sound
  • (Optional) Green connectors and/or a breadboard to avoid much soldering

In this tutorial, we'll create a simple DCC decoder based on the ESP32 C3 development board, Loco.Engineering DCC converter and the SimpleDCC framework (Arduino version). Also, you can use a ready-to-use programmable Loco.Engineering decoder that already has ESP32 and a DCC converter. The decoder will control one Polish Railway signal but you can use any other signal/signals. If you don't know how DCC works, check this guide. Don't worry if you have never programmed anything - what you need to do today is just type in the address of your decoder and output numbers where you connected the LEDs of a signal. Nothing more.

Connections

If you use Loco.Engineering decoder you should connect it to rails with DCC and a signal . If you use your board and a DCC converter you should:

  • connect an MCU board with a DCC converter
  • connect DCC converter to rails
  • connect a signal to an MCU board

Example of connections in case you use an ESP32 C3 development board and Loco.Engineering DCC converter

Arduino DCC decoder diagram

Code

It's time to upload some code on your DCC decoder. All steps below will work only for ESP32-based boards including Loco.Engineering decoders (also tested with ESP32-C3 and ESP32-S3 development boards, if you have another board you should find documentation on how to use it with Arduino).

How to upload firmware on Arduino DCC decoder:

If the decoder is connected to rails (don't forget that you need a DCC converter that can transform the DCC signal from rails into digital messages, convert alternative current to direct and drop down the voltage; DON'T connect your MCU board to rails directly), you'll DCC packets in Arduino serial monitor.

The idea of SimpleDCC is to make DCC as simple as possible. That's why you shouldn't spend hours or even days checking the decoder's manuals anymore. To get your decoder working you need to finish 2 steps:

  • Add an accessory or train decoder to your command station - use any decoder address, any port, and any function number.
  • Add the decoder settings you used in the previous step on a command station to your decoder code

How to update decoder settings in Arduino code (we plan to add the possibility to change decoder settings in a web browser in February):

  • Open simpledcc-arduino.ino in Arduino Studio
  • Find the line with the text "//Decoder Settings" and follow the hints in the code
  • Upload updated code to a decoder
  • Check that a decoder is connected to an accessory and rails, your command station is on
  • Test that everything works

What features do we plan to add to Simple DCC?

This is the first version of Simple DCC decoders for Arduino that will have more features in the next few months. For example, we plan to add in January 2024:

  • Update the decoder's logic in web browsers
  • Send simple messages between decoders

Feel free to contact us at hey[at]loco.engineering if you have any ideas about what should we add to SimpleDCC Arduino. If you need ready-to-use / programmable decoders with sound, DCC decoder, LED, and motor drivers - check Loco.Engineering decoders.