ESP32-based DCC loco controller

Shdwdrgn Dec 12, 2018

  1. Shdwdrgn

    Shdwdrgn TrainBoard Member

    251
    182
    13
    One of the items that doesn't seem to have an easy open-source solution is putting a full computer on a loco. You might be able to fit an arduino and motor shield into O-scale and larger, but things get really tight when you go smaller. I have experimented in the past with an ESP8266-12 and an ESP32 on a HO-scale steamer, but it's a pretty tight fit with a lot of point-to-point wiring.

    This Summer I discovered there are online companies that will make high quality circuit boards these days. That opens up a whole world of surface-mount components. The first board I had made was an inline connector using the 6n137 isolator to pull the DCC signal into an arduino I set up to run turnout servos, and I learned a lot of new things about working with surface mount components.

    Now I'm ready to tackle a full loco decoder. Sure, many people are perfectly happy with commercial offerings of DCC decoders, but some of us want to go even further with the current technology. One of my goals is to make boards small enough to fit into HOn3, which *might* even fit into some of the larger N-scale locos (time will tell on that aspect).

    To make the design more versatile I decided to split the heavy power components into a separate board. This would be a bridge rectifier to provide DC power from the AC off the track, a voltage regulator and capacitors to provide 3.3VDC, the opto-isolator circuit to obtain the DCC signal, and an L293D H-bridge to drive the loco's motor. If this board is placed near the power pickups and the motor, then I only need a few low-current wires to bring the signals from the ESP to this board. The target size for this board is 0.5 x 0.75 inch.

    The main board will hold a miniature ESP32 (only 19x13mm with a plug for an external antenna), a micro-SD socket to hold sound files and other data, and a 74595 serial output chip, plus all the plugs for other components. Outputs for lights, smoke, etc are usually low speed, and with three lines of input the 595 chip provides 8 individual outputs. Even better, you can stack these chips to add up to 992 outputs, and the code on the ESP just has to send the appropriate number of bits. The target size for this board is 2.0 x 0.75 inches.

    As far as features go, besides the outputs above I have designated I/O pins for a tach input, two IR LEds and detectors (I want to pick up location markers on the layout, and maybe even use them for collision-avoidance), two audio outputs (the ESP32 has DAC outputs that can be fed into amplifiers), detectors on the front and rear couplers (with the intent of sensing when you connect to a car), and a plug for flashing new code. In addition, the ESP32 supports wifi and bluetooth, and can be flashed over the air.

    So I think I have a pretty full-featured design, I'm just wondering if there are any other types of sensors that folks might use on their locos? At the moment I still have three I/O pins available, assuming I correctly assigned the other pins. Anyway I just thought I'd throw the idea out here and see what everyone thinks. I have the schematics drawn up, and want to start designing the boards and testing circuits after the holiday season.
     
    Simon Mitchell and Atani like this.
  2. Shdwdrgn

    Shdwdrgn TrainBoard Member

    251
    182
    13
    Simon Mitchell likes this.
  3. Simon Mitchell

    Simon Mitchell TrainBoard Member

    113
    105
    7
    I like the idea of a custom board. I’d be wanting some power choices, ie BAttery as well as track power, and the signal via WiFi rather than the dirty track..
     
  4. Shdwdrgn

    Shdwdrgn TrainBoard Member

    251
    182
    13
    I do plan to have my own locos able to select between DCC and wifi (I'm using the DCC++ESP32 base station). It would be nice if it could use both simultaneously, as I don't think there should be any conflict in the commands received through either method.

    I was actually thinking about battery power and keep-alive capacitors yesterday. Capacitors are easy to work with, but batteries present several challenges. Some people run 100% dead rail and have separate chargers. I've also seen instances where only some of the rail is dead, but there are several live areas which recharge the batteries. And then you have different types of batteries (NiMH and lithium-ion being popular right now) which require different techniques to recharge. I think the best bet there is to assume that if you want to recharge from the rail, you would have the charging circuit as part of the battery pack, and just stick with a simple hookup of 2-3 wires. And then there's supercapacitors, which seem like a great idea for live but dirty rail.

    I'm also trying to consider different placement methods within the locos. I work exclusively with steamers, but I've seen some models that have the motor in the tender. I'm going to be using small plugs to connect a lot of features and have been playing around with the idea of different types of harness cables to transfer connections between the tender and loco. I know I'm not going to be able to cover *all* circumstances with a single board design, but I'm hoping to make things fairly universal. Time will tell how that works out...

    There is one other consideration I have in mind... mobile decoders that aren't necessarily on the track. In particular I was thinking of cranes with several degrees of motion (either on a work car or in a construction site). For this you would need control of several motors. I doubt the 595 chip could provide a PWM signal, but there are a couple of options. First, the three unused I/O pins in my schematic all can provide PWM signals, then I could use the slower lines from the 595 to provide the direction signals. Second, I could provide a single universal PWM to all the motors, then use two more lines on the 595 for each motor to control the direction of each. The first method would limit me to a maximum of four motors, the second method would allow nearly unlimited motors but they would all run at the same speed.

    My hope is that if I can create some cheap hardware with an open design then the community can help build software to handle their needs for a variety of situations. As far as I can tell this is the only remaining area of DCC that can't easily be covered by off-the-shelf components (at least for the smaller scales). Maybe some day soon we'll be able to build full DCC systems with completely open hardware and software designs.
     
  5. Simon Mitchell

    Simon Mitchell TrainBoard Member

    113
    105
    7
    I like the way you think, especially away from the rails (power = command signal)

    I’m not that wedded to the DCC comms protocols. I was quite happy using JMRI.

    Another source of the physics engine is via open rails. They supposedly have good steam sound effects, which could be an interesting option for under baseboard sound.
     
  6. Shdwdrgn

    Shdwdrgn TrainBoard Member

    251
    182
    13
    I'm not familiar with 'under baseboard sound', but it sounds like something where you have a sound board permanently mounted in one location? I've seen such items for buildings such as sawmills or factories. For that sort of thing you might check out the BY8001-16P, which is basically an MP3 player with an SD card slot. You can wire it to an arduino to control the sounds, or just use it by itself to sequentially or randomly play the tracks on the card. This board would be ideal for loco sound as well, except I need a way to stretch and compress sound bytes to match the loco speed, which I don't believe this board can do.
     
  7. Simon Mitchell

    Simon Mitchell TrainBoard Member

    113
    105
    7
    Under baseboard sound for me would be a nice big 5.1 sound system so that I got the bass sounds of big O gauge locos. The little 20mm speakers just don’t do it for me. My view would be whole layout sound, not just specific items of interest. So rather than soundcards, likely something running in a RPi3. It has been done for JRMI, but not fully developed, especially for steam locos.
     
  8. Shdwdrgn

    Shdwdrgn TrainBoard Member

    251
    182
    13
    The quality of the sound just depends on the amplifier you are using. My initial experiments in getting sound out of my ESP32 were using a single 3904 transistor with a capacitor and a couple resistors. You could tell what the sound was, but just barely. Even the BY8001-16P I mentioned above has a much higher quality amplifier, and at $5 each you could easily put one under each industry rather than wiring up a single sound system for the entire layout.

    As for speaker size, yeah bass from a tiny speaker is tough, but they've done some really amazing things in recent years. I've even seen some 1" speakers that amazed me. They won't have the 'thump' of a good bass that you can feel in your chest, but they do still put out an impressive volume.
     
  9. Simon Mitchell

    Simon Mitchell TrainBoard Member

    113
    105
    7
    Other limitations of DCC sound decoders is that they are usually only playing loco sounds, not the coupled rolling stock.

    A Logitech sound system is relatively cheap...especially if it’s doing all locos and industries etc.
     
  10. Shdwdrgn

    Shdwdrgn TrainBoard Member

    251
    182
    13
    A nice discovery today... The TTGO esp32 module I was designing around is actually built from an ESP32-PICO-D4 SIP module which is only 7x7mm by itself and just needs a few external capacitors for power and antenna filtering. My original plan was to hand-solder the TTGO board onto my own circuit board, but this will allow me to solder everything at once in the oven. More importantly, it will also provide the flexibility to move the parts around and probably shrink the processor board even further. Time to do some more research...
     
  11. Simon Mitchell

    Simon Mitchell TrainBoard Member

    113
    105
    7
    Great news!

    I’ve had a bit of a look around to remind myself of where I was up to with my previous experiments. I came up with;

    Power in;
    1. DCC (17v AC?)
    2. DC (+/- 0 to 12v DC)
    3. Battery (+/- 3.3v to 12v DC)
    Power Out;
    1. 3.3V
    2. 5V (servo’s)
    3. 12V, 1A (motive power)
    Command in;
    1. DCC track (via opto-isolator)
    2. WiFi
    3. Serial via micro usb plug?
    4. OTA update
    Possible IO usage;
    1. Motor Control, PWM 12v 1A
    2. Front Coupler (Servo PWM + 5v)
    3. Rear Coupler (Servo PWM + 5V)
    4. Reverser (Servo PWM + 5v)
    5. Front lamps (LED PWM)
    6. Rear Lamps (LED PWM)
    7. Firebox glow/Flicker (LED PWM)
    8. Tachometer input
    9. Audio Out
    10. IR LED/detectors
    I would have thought that the big excitement with this project would be stepping away from the track based DCC, especially given the cheap decoders that are out there. Given the interest in non-track based items, such as cranes, I would have thought WiFi was the space that had the least options available.

    I like the idea of being able to handle a wide range of power sources, including recharge via the track from DCC/DC or a DC plug. I gather, but can’t confirm, that a number of LiPo’s effectively have charge controllers built onto the battery pack, but they would still need a bridge rectifier/voltage regulator/capacitor to ensure the fire stayed inside the battery pack! Given the size limits you are working on, a 1s would be ideal, with 2 step ups (5v and 12v). Most of the charge issues arise from balancing multi-cells, and so sticking with 1s May be the way to go, notwithstanding the loss of efficiency. Run time doesn’t seem to be the biggest issue, as not many layouts have locos running for an hour continuously.

    The SD card and the update plug will suck up real estate?

    1A for the traction motor is because O gauge..just me being selfish!

    There are issues with current draw for direct driven LED’s, so further H Bridge or transistors may be needed? (Out of my depth).

    Also found whilst scouting around https://www.protocab.com/welcome
     
  12. Shdwdrgn

    Shdwdrgn TrainBoard Member

    251
    182
    13
    Good idea, let's compare notes...

    To begin with, power. I was originally using a bridge rectifier capable of 1A, but since *everything* on the loco will need to draw through this I should probably reconsider a 2A model. An LM1117-3.3 can take up to a 20VDC input and put out 800mA. That's more than enough power to handle the logic chips and this has a pretty tiny footprint. I hadn't considered having servos onboard, but it would make a lot of sense to have one power board for HO and smaller scales, and a second power board for S and larger, since the larger scales would also have the space for bigger components. Both models would then use the same cable to connect to the logic board. The one thing I did forget about was providing a regulated 12V output. My standard gauge locos work just fine on the 16v track voltage, but my narrow gauge loco does require 12v. I think ideally here I would provide a jumper to determine if the motors are fed with 12v or track voltage so one board retains both options. The power board for the larger scales could also include a hefty 5v regulator, perhaps offering up to 1.5A? And this board would probably need a 3A bridge rectifier.

    Regarding batteries... This guy on reddit is what got me to looking at the pico-d4 chips today. He is also using LiPo batteries and his schematic here also includes the charging circuit and power monitor (for dual-source power). I haven't looked up any specs yet, but that would at least be a starting point for including a battery charger onboard.

    Regarding commands and programming... I would like to support both track and wifi DCC commands, and there should be no reason why the ESP32 couldn't handle both. For reprogramming, OTA is great for anyone running a raspberry pi to create a private network for their train (like I'm doing), but wired programming should also be supported. A 6-pin connector should allow automatic resets and setting the programming mode, so a typical FTDI serial board could be connected by anyone who doesn't know how to program.

    Overall realestate... The logic board will be crowded with connectors. I have a 6-pin connector for the power board, 6 pins for the serial programmer, a pair for 3-pins for speakers and a pair of 4-pins for the IR LED sensors. I'm looking at polarized 1mm-spaced plugs which can be found in through-hole or SMD. There will also be a pair of 4-pin 2mm plugs to stack the serial output chips, with solder pads for the devices they will run. I was also planning to design that part to accept a 3904 smd transistor which can carry up to 200mA, hopefully that would be enough for most devices that are simply turned on and off? The board will have the ESP chip and the 74595 serial output chip, plus just handful of resistors and caps, and an antenna socket for wifi. The micro-SD card socket would be on the back side of the board, and probably hand soldered. The parts themselves won't take up that much space, but I might be tight on clearance for all the I/O lines I need to route. The goal for the logic board would be about 3/4 x 1.5", and a simple double-sided board.

    I reserved two I/O pins for front and rear couplers, but it sounds like different scales might use them for different things. The two pins have ADC outputs, but also touch inputs. In my scale, I would love a way to actually sense when a car is added or disconnected from the train, especially for the switchers. It sounds like we should be able to provide one or the other feature from those pins, and people can decide how they want to use them.

    There is another possibility in the realm of output... The PCA9685 board uses an I2C input, provides sixteen PWM outputs, you can plugs servos directly into the board, and the chip supports a 6-bit I2C address space (so up to 64 of these can be added). The chip also can use an external power source, so even though the logic is 3.3V you can directly drive 5v servos. We could easily use the outputs for both simple 0/1 devices as well as PWM devices, so this chip may turn out to be a better solution that supports everything anyone could want. I also believe each line can also be used as a 12-bit ADC input (not certain, I need to check up on that). I'll look into this chip and see what kind of support circuitry it takes, because this could provide the answer to a completely universal board, handling all sorts of inputs and output, including multiple motors.

    [Update] Found a datasheet, the PCA9685 does not have inputs on any pins, it is output only. However the PWM outputs are 12-bit resolution. The chip also has a mode to stagger the PWM timing so you don't have a lot of devices pulling power at the same moment. Also of note, because of different packages the 9685 chip is nearly identical in size to the 74595, so no loss of realestate for the additional functionality.
     
    Last edited: Dec 27, 2018
  13. Simon Mitchell

    Simon Mitchell TrainBoard Member

    113
    105
    7
    Excellent, bearing in mind a lot of that went over my head!

    Would it be worth considering a long board, with all components fitted, but in sections so that the board could be split and joined by cables if users had a space problem? Thus an O gauge user might have one long skinny board, but an HO user might chop it up and stow some in the tender etc?
     
  14. Shdwdrgn

    Shdwdrgn TrainBoard Member

    251
    182
    13
    That was the reason behind having separate power and logic boards. If I can set up the connectors right, then the power board could be plugged directly into the logic board when space allows, but for smaller scales a cable could be used between the two. Using the PCA9685 chip, the output section would provide 16 channels with sockets to plug in more chips if needed. For the first-run boards I'll be happy if the setup fits inside my standard-gauge HO 2-6-0, but as I get more comfortable working with surface-mount I hope to compact that down enough to some day fit in N scale.

    So I've been thinking that for more universal compatibility I want to provide high-output 12v and 5v from the power board. That should give us everything needed for the various scales, and also provide a starting point for anyone who wants a similar setup with an arduino. I think I forgot to mention before, but the power board will also contain the motor driver chip (L293DD) which can drive 800mA by itself (I'll keep working towards that 1A goal). This board should contain all of the high-power components so only very fine wires need to be run between it and the logic board. And because track power is coming directly in to this board, it will also contain the 6n137 to filter the DCC signals from the track.

    Looking at the PCA9685 chip, it appears that it can only provide 5V outputs. I'm wondering how this will work for smoke units, or if there are any components on larger scales that would need a higher voltage? I think I can use a simple transistor circuit to change that to the 12V supply, not sure how it would work with PWM outputs though.
     
  15. Simon Mitchell

    Simon Mitchell TrainBoard Member

    113
    105
    7
    The standard motor I use in O gauge is the Mashima 1833 with a 40:1 reduction. Max current draw is rated at 500mA. If people want to fit some Amp sucking dinosaur, why are they looking at BPRC concepts. I reckon 800mA should be fine.

    I don’t know about smoke units...
     
  16. Shdwdrgn

    Shdwdrgn TrainBoard Member

    251
    182
    13
    Nice timing, I was just settling in on some parts to order. :) I'm starting with a bridge rectifier that can handle 2A (since literally everything draws power through this part). Following that I have a 12v regulator rated at 1A, and a 5v regulator rated at 800mA. I'll also be putting a 3.3v regulator on the logic board to supply the ESP32, I think the one I have is rated at 500mA? It's the same one used on the breakout boards so it should work.
     
  17. Simon Mitchell

    Simon Mitchell TrainBoard Member

    113
    105
    7
    Dumb Q, I assume that the battery charge/track power would come in upstream of the bridge rectifier, and the battery would come in after it, so that it only gets voltage of the correct polarity. Hmm. Time to go hunting some battery on board management specs.
     
  18. Jimbo20

    Jimbo20 TrainBoard Member

    274
    178
    11
    Just an observation - are you going to use linear regulators for 3.3 and 5v? They are very inefficient, especially if you are going to run them from a battery supply. Eg a 3.3v regulatpr running on 12v is only about 27% efficient, with 73% being wasted as heat. It may be worth considering the option of using buck convertor(s), which typically can be more than 80% efficient.
     
  19. Shdwdrgn

    Shdwdrgn TrainBoard Member

    251
    182
    13
    @Jimbo -- erg, it looks like the LM1117 line of regulators I'm using are in fact linear. I don't suppose you have any suggestions for other parts around the 1A range that would fit on a SOT223 or smaller footprint? I knew the 78xx series regulators would be crap for this, but for some reason I was thinking the 1117's were buck converters.

    @Simon -- honestly I've never worked for battery supplies before, so this will be a learning experience for me. To start off, I think you said your motors run on 12VDC, so would I be correct in assuming your batteries are also 12V? If that were the case, then only the 5v and 3.3v regulators would need to be connected, and it would probably make sense to just feed those regulators directly from the 12v source so they are in line regardless of where the power comes from. And yes, battery power would come into the circuit after the bridge rectifier, that part is only used to convert the AC from DCC track into a DC supply (with a capacitor to help smooth it out). I would like to have the option of recharging the battery directly from live rail, but initially it might be easier to assume the battery gets plugged into its own charger, off the track? Or maybe we'll just build different power boards for live or dead rail. Good news on that front, I previously used PCBway for building my boards but they had a minimum order of 50 boards... Today I saw that they now offer sales down to as few as 5 boards, which means I can easily get a few test boards knocked out without having to buy a big batch, and once we have a working design then I can put in an order for 50 or so.
     
  20. vasilis

    vasilis TrainBoard Member

    110
    39
    10
    interesting
     

Share This Page