Model Train Control System – Part 3 Programmer

Part 3 of the Train Controller will go to a separate USB card we need to connect for initial programming of the ESP32. On this we connect GPIO0, ChipPU and the default UART. This is needed to program and develop the FW on ESP32.

The USB chip is a CH340G. Most other designs will use a FTDI chip, but you will not see a FTDI chip in any of my designs. That Scottish company bricked my 3D printer a few years back and I simply dot trust them + CH340G is low cost, well supported and always work.

This schematics is borrowed from the utility driver. I think I can use it “as is”, but I will add a separate 3.3V regulator to feed the ESP32 power from the USB while programming. This means I need a 6 pin programmer port with +3.3V, RX, TX, GPIO0,ChipPU and GND. I will make a micro JST on the board and maybe an optional connector board that can be available under the train so we can connect without direct access to the control system. This is because I need some flexibility around hiding parts of the system in small locomotimes.


Model Train Control System – Part 2 H-Bridge

The second part of the Train control system is it’s motor driver. This is based on a classic L9110 H-Bridge chip. This allows us to drive a 500mA DC motor in both direction and we regulate speed with PWM duty. We need to return to speed adjustments later but we will be using the PWM signals on the ESP32 for this purpose.

This is where I went wrong on my previous version as I connected M1A and M1B to Inpu only capable ports. I discovered the error later as I tested the utility driver.

R1 & R2 is pull up’s that in this case is needed so the motor don’t accidentally start as we power up. C4 is to clip away DC motor noise.

Model Train Control System – Part 1 PSU

It’s time to get my train controller right. Starting with the PSU.

The power feed for a model train is the rail track that on some old systems can be AC, but mostly is 12V DC. To cover both these i use a classic rectifier MB6S. This will allow the 12V to be connected any way we please.

The next step is a bit trickier because I need 3.3V. This is easy using SPX3819 that comes in SO23-5 format and cover 16V with 500mA out. Just make sure you use a 3.3V Version.

The tricky part is that we will lose power as the train move over track gaps. Some old model trains had weights to give the train mechanical energy, but I am adding a 0.33F supercap on the 3.3V. These are small and aand using a small JST micro Connector we can hide this somewhere on the Train. The supercap will not drive the model train, but it will keep the ESP32 alive about 0,5 sec while the PSU have a glitch in Power. Without this the ESP32 will stop while it reboots and reconnect on Wifi.

Micro PLC – New format

These are just drawings of my new Micro PLC formats. I am considering 20x50mm, 30x50mm and 50x50mm formats. This discussion started as I received my previous PLC bus design and realized the actual size of it. The reality is that I need things much, much smaller and having a big backbone bus is very expensive.

As I accidentally made some 20×50 boards and by pure accident stacked them together I also make the observation that it would be easy to actually use that format as I can fit a stack almost everywhere, But, ok – 20×50 is a bit tight, so maybe 30×50, 40×50 or 50×50? Or maybe all of them as per need?

The way I want to stack them is with a bus cable and connectors. This way I can create a stack or simply mount them Distributed.

SW4STM32 – Short Review

System Workshop for STM32 or ac6wb or SW4STM32 is an Eclipse based, free IDE supported by ST. I started using this a while ago and the great thing about this is that CubeMX generate start-up code, project and everything for this. It makes it ideal for doing initial testing and to get off ground fast.

One word about CubeMX is that you can download different versions of their library and some simply do not contain all files as Project are generated. So the projects don’t compile. You just have to find a version that works for you. Except for that it is great.

As for SW4STM32 the best I can say is that it works – it is ok. A decent editor, debugger is decent and it’s a all-in-one that allow you to write code, compile, debug, download it etc. It is all you need and it’s free and supported from CubeMX and ST.

Sometimes as I download SW4STM32 lock up and I basically have to stop it and start again. It is annoying, but I can live with that. Using SW4STM32 is an option and as CubeMX integrate middleware including FreeRTOS you really get off ground with your projects fast with a standard CMSIS based project.

Another annoying issue is that messages as you can’t connect to SWD can be confusing. I can often have a it of connection issues before I get started, while CoIDE always was straight up. But, this is mostly me being more used to CoIDE than SW4STM32.

As an embedded C/C++ developer I usually have 1 comment – where is the C++ support? SW4STM32 have a button that change your project to C++. And this is GCC toolchain, so it will support C++, but CubeMX generate C so you need to convert that manually. I have not bothered digging into that because my usage have been electronics testing so far.

The example above is for MC3X60 running the motor Trapzoidal. What I did was to grab a FreeRTOS task and just run in a loop with a delay between the steps. Took me around 5 minutes to have the motor running.

The main limitation in SW4STM32 is STM32 – it only support STM32 out of the Box.

The project organization is a bit dictated from CubeMX. I live with that for now, but I would recommend that you organize your own projects and even support multiple IDE’s or build tools. Don’t let an IDE rule your life.

MC3X60V Working

I am only using 12V, but DRV8313 worked on 2nd attempt. First failed so I ripped it off, revoved a lane and placed a new that worked straight up. Just using trapsoidal for the moment, but it’s a start.

I need to code up a Sinusoidal algorithm and better Acceleration and Speed Control. I also want to see if I can get Hall sensors working.

MC3X60V DC/DC working again

This was a bit of work. Soldered 2 x 10uF in a bridge – simply removed the protection TVS for now. If you look next to the led you also see space for a coil where I use a 1206 component, so I can probably squeeze the 1206 caps in there. I don’t want to try 60V yet, I want to solder on and actually test the motor driver. I scoped 3.4V and it looks real nice. I will dig deeper into that later to see ripple.

New Micro PLC design

I have done a few PLC system drafts – one based on Raspberry PI, a second larger one and a 3rd based on distributed components. The pure PLC system was “ok”, but it was a bit larger with the bus than I expected. Looking at some of my 50x20mm designs I wanted to try a 50 x 50 mm design where we plug in a CAN/RS485 bus. The objective is to make a component based system that is very small.

The idea for this came as I stacked my CANUsb adapter and small motor controller (50x20mm) together for fun – I might even use that small format, but I think 50×50 is better. Worth a try.



This is the PSU from the MC3X60V – my small motor controller. It snapped at 35V. To save space I omitted the components in the yellow circle.

Reviewing this with a friend I believe that C10 is the cause. This circuit need a small cap close to TSP54060. I already know that this circuit misbehaved without the larger motor caps mounted (not shown), so it is something to this, but we will see as I get around to patching a PCB.

The resistors R11 and R12 should be ok according to the datasheet. This set the under-voltage cut off and can be left floating – or more correct that is my interpretation of the datasheet for TPS54060.

I will see if I can patch one of the PCB’s as proof of concept, but I also want to make and order a break out board for a few of the 60ich V designs. As you can see above – the PSU is in the top-left corner and I don’t have much space here either. Also – again I will need to allow 60V components on C10.

Next question – Is this actually a 60V design?

The answer is NO! The circuit say 60V, but if you actually operate a motor controller on marginal design terms you will break it. This is a design for 48V with a max limit of 60V allowing it some tolerance.

MC3P60V Cap Change

I was rather pleased with routing MC3P60V because it is a dense, functional design. But, as I reviewed the design I found this. The error here is that this is 60V and I used 0603 packages. 0603 usually hold 6ich V, so I will either need to upgrade to larger 60V or try something else.

 My first thought is to use 2 x 1206 components in series to achieve 60V, but I need to check around what I can get and actual size of components. I have 2 of these cap circuits related to DRV8301.

And looking at the current PCB you can see that I am a bit out of space. I have a heat-sink that cost 1ich USD and that is perfect size, so I seriously do not want to change size on this design.