DCC loco decoder that works with ESP32?

Shdwdrgn Apr 28, 2018

  1. Shdwdrgn

    Shdwdrgn TrainBoard Member

    209
    174
    8
    I know I asked about loco decoders just recently, and I know some folks threw out some ideas for me to try... And I can not for the life of me find which thread that discussion was in. :( So I'm sorry for asking again but does anyone know of a DCC loco decoder that will compile for the ESP32? I've been working with Geoff Bunza's code for accessory decoders and tried to use his example loco decoder but it appears that NmraDcc.h won't compile for the ESP32 (although there IS some code for the ESP8266). Specifically it seems to be missing some functions for reading and storing data using the EEPROM library and I don't know how to fix it. So if anyone knows of a loco decoder that WILL compile on the ESP32, I'd love to get your recommendation.
     
    Scott Eric Catalano likes this.
  2. Trusty

    Trusty TrainBoard Member

    38
    31
    2
    Hello. ESP32 have no EEPROM embedded, so saving data directly in the controller itself is not possible. The ESP32 normally runs its code from an external flash, however, and this can also be used to store values like you would normally do in an EEPROM. Esp-idf actually has an easy-to-use API for this: https://esp-idf.readthedocs.io/en/lates ... flash.html . The other way is to add an external EEPROM chip. This is cheap, and can be done very easily.
     
    Scott Eric Catalano likes this.
  3. Atani

    Atani TrainBoard Member

    543
    499
    14
    there is also an NVS based wrapper available as an EEPROM library but it does not implement every method that NmraDcc.h may use so you may have to add a few dummy methods to make it work.
     
    Scott Eric Catalano likes this.
  4. Shdwdrgn

    Shdwdrgn TrainBoard Member

    209
    174
    8
    There seems to only be two methods missing, I just have to get familiar with how eeprom access works on the esp8266 and then convert that logic to the esp32. I found the point in the code that was calling those methods so I can add in my own code with "#ifdef ESP32" blocks. Guess I'll have to go that route and see what I end up with.
     
    Scott Eric Catalano likes this.
  5. Shdwdrgn

    Shdwdrgn TrainBoard Member

    209
    174
    8
    In case anyone is interested, I just saw this pop up on reddit...

    https://www.aliexpress.com/store/pr...ess-Bluetooth-Module/2090076_32889146892.html

    This is a TTGO micro-ESP32 board measuring in at about 13x19mm (however it requires an external antenna). That should be plenty small enough to fit inside a narrow-gauge HO loco, and maybe even fit in N gauge. I'm not sure what the pin spacing is, but it's smaller than the wroom32, so I'm guessing around 1mm? Overall this should solve any space issues, but I haven't had a chance to get back to working on the code.
     
  6. Simon Mitchell

    Simon Mitchell TrainBoard Member

    101
    105
    7
  7. Shdwdrgn

    Shdwdrgn TrainBoard Member

    209
    174
    8
    @Simon Mitchell -- It's not feeble if it works. :) I scanned through the first page of your thread, will finish reading the other pages through the day. The first thing I see is that you are using the ESP8266. Great chip, lots of variants available. In my case I am starting with Geoff's decoder code, which *does* work on the 8266 chips, and I figured once I had DCC from the tracks working then I would add in wifi access later. I plan on having power on the tracks, but would prefer to receive the DCC++ commands over wifi instead (who can argue against redundancy?). I am trying to use the ESP32 chips, however, because eventually I want to add cameras onboard my locos and will need the extra processing power and I/O pins to perform this feat. So I figure I might as well start out with the ESP32 instead of upgrading all the locos at a later date. I do have an 0-4-0 with an ESP8266 mounted in the tender, but unfortunately that chip has stopped running code so I think it got zapped.

    Right off the bat you received some good suggestions for deciphering the DCC++ commands. I definitely need to bookmark that for when I get to that point. My initial start on having a wifi train involved writing all of the code from scratch, so I'm familiar with opening TCP ports over wifi to send and receive info. It's actually pretty easy to work with as long as you keep in mind to only use non-blocking code.

    Like you, I want a lot of features available to my locos. Lights, servo-controlled couplers, and so on. I also plan to add an IR sensor to the front and rear of the loco and see if I can't set up collision avoidance. A tach input of some sort would be great for getting accurate speed control and syncing the chuff-chuff sounds. And the ESP32 has two DAC channels which I'm hoping to set up for direct sound output (a simple .wav file can easily be stretched and compressed to sync with the loco speed, I just need to add on an SD card to store the files).

    Big plans, I just have to get through the first phase of getting some basic DCC code to compile for the ESP32, then I can throw together some test hardware and start expanding the code. I have a 4x4 test loop set up with three spurs, which I plan to use for various stages of adding code. Eventually I hope to be able to give each loco a task, schedule, and priority, and have it performed while avoiding other locos. Should be fun!
     
    Scott Eric Catalano and Mariano like this.
  8. Shdwdrgn

    Shdwdrgn TrainBoard Member

    209
    174
    8
    I received the TTGO ESP32 boards in the mail today. Holy crap these things are tiny! I mean, I knew what the dimensions were, but seeing it in person... To give you an idea, I could bury this board in the floor of an HO scale hand car, and still have room around the edges. Can you imagine having a DCC-controlled hand car on your track??? I could even hide this in the cab of an HOn3 steam loco, but my power converter board is larger than this ESP so I think everything still needs to go in the tender. And yes, I'm pretty certain you could fit this board into an N-scale loco as well.

    The pin spacing on this puppy is a mere 1mm. My soldering iron is just way too big for this, so I'm going to have to find one of those little pencil irons before I can even try hooking it up. It also requires an external antenna if you plan to use wifi. I have some on order, but initial testing will be pulling DCC through the rails anyway. And it just so happens that I already have an assortment of surface-mount components including the 6N137 to filter the DCC signal. I'll just have to see exactly how small of a setup I can build with all this. :)
     
    NScaleKen and Mariano like this.
  9. Shdwdrgn

    Shdwdrgn TrainBoard Member

    209
    174
    8
    So... anyone care to look over my schematic? :) I'm just learning this software, haven't done schematics on the computer in years, so forgive me if I used any incorrect symbols. However the idea is to get all power from the rails, have an ESP32 with SD card (to store sound files later on) controlling the motor, up to 16 lights/smoke/etc, and have front and rear IR devices to detect potential collisions (and possibly read IR mile marker info). I think that should cover just about everything except a camera?

    ...Well apparently I'm not allowed to insert images. So for those who are curious, here's the link: http://sourpuss.net/projects/trains/DCCpp/DCC_ESP32_Loco_2.png
     
  10. Shdwdrgn

    Shdwdrgn TrainBoard Member

    209
    174
    8
    One of the common questions I've noticed in DCC++ threads is from people asking where they can buy complete solutions or at least a parts list of pre-made components that they can simply buy and hook together without any soldering. One area I've noticed where this is currently not possible is in the opto-isolator circuit used to read the DCC signal from the tracks. Yesterday I submitted my first board design to a company that creates PCBs, which is just the standard 6n137 opto-isolator with appropriate support components. I based this around a specific arduino board I started working with this Spring which is ideally set up to plug servo motors into it directly. The entire board is surrounded by black/red/yellow headers to connect power with each output on the arduino. Using that same three-pin concept I made a 6n137 that I could plug in between the track and the arduino. This new board I'm having created is built the same way, but with surface-mount components, so the entire board is almost the same width as the three-pin header and should make it really easy to add accessories to a DCC layout.

    The next board I am working on will be a power board. Basically it contains the 6n137, a bridge rectifier to get a DC voltage from the track, a 1 amp voltage regulator (either 5v or 3.3v), and an L293D to drive a loco motor. The idea here is that all the higher current lines will be contained here and the board should be small enough to fit inside the cab of an HO steamer, then you have very small wires carrying signals to an arduino mounted inside the tender. This board combined with a small arduino should be enough hardware for anyone to set up a DCC decoder in their loco (although you will still need to solder the wires between the cab and tender).

    And finally, I want to build a complete solution (which is essentially the schematic above) that has everything all together in a single board. My goal here is keep all the hardware designs open source like DCC++, but try to make assembled boards available for purchase for folks who are not able to solder. With the hardware easily accessible maybe more software folks can jump in to continue to improve the project.

    But yeah, one thing at a time... first I have to get some boards together to prove if I even know what I'm doing. ;)
     
    Mariano, NScaleKen and Atani like this.
  11. Shdwdrgn

    Shdwdrgn TrainBoard Member

    209
    174
    8
    The optoisolator boards came in today! Very nice professional finish, and I'm happy with what I can see of the board. Of course once I get my contacts out later tonight I'll be able to really see the close-up detail, but nothing stands out as being incorrect from my original layout. I soldered the 6n137 on by hand, my first surface-mount component! I was a bit surprised at how thick this chip is when I pulled it out of the packaging, I expected it to be rather thin like the other chip I have in this form factor. Ah well, it is what it is.

    Next step is to find the case for my multimeter and dig out the temperature probe so I can get the proper temps when I heat up the toaster oven. And unfortunately I left my camera at work, so I won't be able to share any pics until Tuesday, but chances are I will have the first board assembled and tested by then. Overall it is a very tiny board, only .320 x .370 inch. Basically the .320 is the same width as the 3-pin plug on a servo motor. It will have a 6" cable to plug in to the arduino, and then a 2-pin header to connect it to the track. Pretty simple unit, just got to get the last four components soldered on and then add the wires.
     
  12. Shdwdrgn

    Shdwdrgn TrainBoard Member

    209
    174
    8
    WOOT! It works!

    A bit of trouble figuring out how to use the toaster oven, but I finally got it to coast in the right temperature range to soak the board before setting the solder.smeared on some solder paste and let the board cook... and ugh...

    So the first problem is that apparently the rosin-core solder I use for doing parts by hand melts at the same temperature as the paste. I thought it would be a lot higher. Then one of the reistors decided to rotate 90 degrees, so I had to heat up the board enough to yank it off, then try to set it back in place. Finally, the other two resistors were placed back-to-back, a little too close together, and I got a nice solder bridge between them. I need to figure out another way to lay out the parts so I have more space between them, but for now I need to work with the boards I have.

    Good news is that I hooked up the board to my arduino, sent some DCC++ commands over telnet, and the servos responded to my commands. Sweet!
     
    Mariano likes this.
  13. Shdwdrgn

    Shdwdrgn TrainBoard Member

    209
    174
    8
    Got some pictures, lets see if I can post them...

    This is the back side of the new board with all the really tiny components. The two pins sticking out the front can plug in to a connector that leads to the tracks.
    [​IMG]

    The arduino board labeled "nano strong' has three rows of pins around each I/O line, making it ideal for running servo motors. The wire coming from my board gets power off the arduino and sends the DCC signal to GPIO2 to trigger an interrupt.
    [​IMG]

    The board design allows me to connect up to 17 servo motors but I set up the software to run 16 so it's easier to keep track of the count. The positions where there is no pin in the yellow header are I/O lines that do not play well with servos, so I marked them this way. However because of the heavy traces on the circuit board, I can plug 5v power lines directly into those headers to provide power for both the servos and the CPU. I believe three power plugs, one in each bank, would be sufficient to run all 16 servos simultaneously, but your power supply would need to provide a couple amps. In reality you would rarely be switching ALL the servos at once, so it should never be a problem.

    I'm waiting on delivery of some 1.5k resistors to limit the current coming in from the track. The original circuit called for a 1k (which I do have), but even on the breadboarded circuit with 1/4W resistors it still overheated. The 1.5K worked fine on the breadboard, but I also ordered some 2.2k in case I needed something bigger for the surface-mount version.
     
    Mariano and NScaleKen like this.
  14. WillemT

    WillemT TrainBoard Member

    32
    38
    3
    I have been following your experimenting. After seeing the pictures I can understand some of your problems soldering the board. I never caught on that it had components both sides.

    I have been doing something similar but with a different end aim. Attached are some pictures of my initial efforts.

    The first board is also an opto decoder using the same 137 unit (and yes they are somewhat large). It does include a power section as well. I have not attempted squeezing it into the smallest size possible since it is intended as an experimental module, The output header pins provide 5V and the DCC signal to a breadboard.

    The second unit is an ATtiny84 with only the essential components. That MCU chip is a 20 pin VQFN package and measures 3x3mm - talk about tricky soldering. The rest of the SMD components are 805 plus a 16MHz resonator. The board size is somewhat dictated by the header pins - again intended to be used on a breadboard.

    I have connected the Opto to the ATtiny, using the 5V power from the opto board, and tested with some sketches using the NMRA DCC library. All seems to work just fine. I can decode all the Throttle and Function instructions (the only ones I can issue from my DCC++ unit) and display the details on an attached I2C LCD display. So far so good.

    The third picture is my version of an ESP8266 development board. It uses a CP2104 USB to UART bridge. That chip is a 24 pin QFN and is 4x4mm - also rather tricky to solder. The board supports auto reset and upload and includes 2 level converters which I currently mainly use to drive a 5V I2C LCD display. They can be hooked up to talk to a 5V Arduino via serial.

    For soldering I use an temperature controlled commercial 15cm x 15cm hotplate (a relatively cheapy). I initially intended to make my own but it would have cost more in parts (if I could obtain them all locally - which I could not). It is a rather unsophisticated process in that i do not run a proper heating profile, I just plunk the board with components on the preheated hotplate (at 220 degrees C) and watch when the paste melts and flows, then remove it.

    I place tiny drops of solder paste on the pads using a solder dispenser (this one) and then push the components into the paste. They hold OK till they are soldered. The small QFN packages is an entirely different story. I also do all my soldering iron work after reflowing. And yes, the paste and rosin core solder melts at the same temperature.

    I will keep watching your progress.

    Opto Board.jpg ATtiny84.jpg ESP8266.jpg
     
    Last edited: Sep 9, 2018
    Mariano likes this.
  15. Shdwdrgn

    Shdwdrgn TrainBoard Member

    209
    174
    8
    Do you use a CNC to make your boards? I thought about getting a small one for that purpose, but never got around to it. There are some decent-looking kits on ebay for under $200.

    Another question -- I see you included the small cap across the track inputs on the 137. Have you ever checked to see if you need it? While scouring through various circuits I only found a few that included a 270pF cap but most seemed to not need it. I think when I do the next version of this board I'll include pads for it just in case, but for now I'm not using it. My biggest concern here is that on a heavily-used DCC circuit with a lot of trains running noise could start to become an issue, but I don't have anything to test that with right now.

    Eventually I plan to make a board using the pico ESP32. Unfortunately LilyGo is ignoring all requests for information on their github page, so I have no information about whether this board can be placed in the oven and soldered with the other components. Probably means I'll have to hand-solder it, and that board only has a 1mm spacing on the pins. Yeesh.

    My regular soldering iron was way too big (and probably too hot) for these pieces so I picked up a low-wattage iron with the smallest tip I could find on ebay. For $10 I got one that plugs into a USB port (use a hub, it will probably blow the ports on a standard computer!) which has a simple little metal knob that you put your finger on to heat it up. Takes about 5 seconds to get up to temp and cools quickly when you set it down, but I've found this little iron has enough power to easily solder header pins.

    I was using a toothpick to place blobs of solder paste on the pads. I think next time I'll order the solder mask though, it's supposed to make it easier to apply the paste. I definitely needed something smaller as there was too much paste applied to several spots. Or maybe I just need more practice...
     
  16. WillemT

    WillemT TrainBoard Member

    32
    38
    3
    PC board layout is done in Eagle (none of my boards are larger than what the free version allows). I print to a pdf file and then just print with a laser onto film. I use a homemade UV light box with old fashioned fluorescent tubes (the correct frequency ones for resist) along with pre-sensitised pc boards and good old ferric chloride (care must be used to not stain everything in sight). I can go down to about 8 mil track width but that is starting to push things.

    The biggest problem is that I cannot do through hole plating and have to solder wires into the vias. That also means for screw terminals and such I can only solder on the bottom and must allow during the layout for that. Header pins can be soldered both sides. It gets too expensive to have one or two boards made locally for experimenting (invariably with some faults as well). Biggest advantage is, once laid out, it is an hour or so to make a board (drilling requires a bit of care - 0.5mm holes etc). Any faults - just repeat.

    Geoff Bunza first mentioned that he had intermittent noise problems until someone suggested to use a 270pf cap. I just added it as insurance. It is easier to remove than add.

    I did find a problem when driving high current from the DCC++ to a resistive load in that I start missing decodes from the Opto/ATtiny combo (while the decoder still worked fine). It is all setup on a bread board, including the mobile decoder, so that could introduce some problems as well. I do not have a scope so cannot really debug. I will worry about that later.

    It is going to be tricky to solder 1mm tabs with an iron, though not impossible. The ESP8266-12F has 2mm spacing which is easy, the ESP32, which I have some of, is 1.27 and will be OK I think. My soldering iron is not that fine tipped (0.5 radius round tip, I think) but is adjustable temperature controlled and some soldering is getting tricky.

    I have read all sorts of ways to make solder masks but have not tried any yet. I think I will have problems making a mask for 0.5mm spaced tabs. Yes, I also use a pointed tool (ground from a thin steel rod) to place paste for small components - that along with a good magnifier. Great care is required to not place too much paste on tabs for the tiny QFN packages which could cause shorts underneath the chip (don't ask).

    What we don't do for fun?

    Willem.
     
    Last edited: Sep 9, 2018
  17. Shdwdrgn

    Shdwdrgn TrainBoard Member

    209
    174
    8
    I remember making lots of boards from the old Radio Shack kit... just don't have the patience for that part any more. I asked about a CNC because your style of board, where the copper is only cut out to define the traces, it exactly how the CNC setups works as well. I believe the one I was looking at is supposed to be good down to 10mil spacing, but that probably just depends on the size of cutting bit being used. Seems like it would be great for making a couple boards to test out a design.

    Using a metal probe to place the solder paste sounds like a good idea, and I have some dental picks that might do the trick. As for the solder mask -- that's something I can add to my order for a couple bucks when I have the boards made up. I can't imagine trying to make one by hand.

    You are correct on the spacing for the 8266 and standard esp32 boards, however this pico board I'm working with has all the same pins exposed but is physically only 1/2 the size of the regular esp32, so yeah they really do have a 1mm spacing and without any info from the manufacturer I won't trust putting it in the oven. The tip of my iron measure right at 1mm, but it also has a small solder blob on it. Would be easy enough to turn a finer tip on it though if I needed to.

    I'm actually lucky in this regard in that I'm very near-sighted. Take out my contacts and I can see all those tiny details on the board without a magnifier.
     
  18. WillemT

    WillemT TrainBoard Member

    32
    38
    3
    Makes sense. I do it that way mostly to etch as little copper as possible. It greatly extends the life of the Ferric Chloride. Makes design a little simpler as well since you have a mostly full copper ground plane eliminating the need to place all ground traces. Also I quite like the copper look.

    I would not put it in there either. Those modules were flow soldered with solder similar to what you use - same melting point. It may be OK to melt the solder again but it also could mess up things hugely. When I solder my QFN packages I reflow them on their own to make sure it goes as planned - meaning, no shorts. I then place the other, larger, components and reflow again. Sometimes I may do it 3 times, like the micro-usb socket next, and had no problems yet.

    I am looking at something different now. I got a few single channel full bridge motor drivers (relatively cheap ones). The specs says max 2Amps up to 40Volt. I assume 1 - 1.5 Amps should be possible. However, they are 4x5mm chips and cooling might be a problem. They have an exposed metal area on the bottom and the data sheet recommends at least double sided board with a matrix of "thermal vias" under and around it. I cannot do those so am trying something else. I got a piece of pre-sensitised aluminium backed PC board (bloody expensive). The copper is bonded to the aluminium backing, instead of fibre, with an electrically insulating but thermal conducting layer.

    Making the board was a bit tricky since ferric chloride attacks aluminium with a vengeance. The back is covered with a plastic protective material but the sides are exposed. I tried painting them with a resist pen but the sharp corners along the top and bottom of the edge does not cover well. Lots of activity there. I will have to try and cover them up with some tape or something next time.

    Attached is a picture of the little board I made - it actually came out quite well. Size is 20x30mm (excluding temporary outside copper edge). Now to populate it and then some testing.

    The plan is to used it to power smaller track sections, booster like, using the current sensing to track occupancy. I could also make an electronically reversible unit to power a reverse loop. Problem is the chip has internal over current sensing and shuts down with a short. I do not know if I can catch a short fast enough to prevent that. May have to determine reverse loop track polarity by some other means. This chip also does not have a fault trigger output - I did say they are cheap (I have some other chips on order that has, but only to be delivered sometimes next month - also more expensive). Lots to play with.

    How are you going?

    Willem.

    Motor Board.jpg
     
    Last edited: Sep 12, 2018
  19. Shdwdrgn

    Shdwdrgn TrainBoard Member

    209
    174
    8
    Haven't had any time here to work on the board again, but I did receive the 1.5K resistors yesterday. I've been trying to rebuild my raspberry pi setup, fighting with a second external wifi dongle. Unfortunately I've decided the chipset on this wifi is just way too much of a pain to work with and even the compiled driver for it seems to not really work (lots of trouble getting it to even associate with an SSID) so I ordered a new module last night that is supposed to be directly compatible. As you might imagine, that has taken up all my free time fighting a losing battle.
     
    Mariano likes this.

Share This Page