Category Archives: ESP32

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.

CAN/RS485-Wifi Adapter

I have mentioned that I need a dual CAN to Wifi Adapter as well as a dual RS485 to Wifi adapter, so I decided to make one that can serve both needs. This uses ESP32 to get Wifi, USB and Bluetooth and a STM32F405RG to get dual CAN as well as dual 10Mbps RS485 – all galvanic isolated.

The CAN Adapter is for professional usage, while the RS485 is partly as an adapter to my PLC system as well as a Wifi adapter to my DPS5005 & DPS5020 based PSU’s.

Layout on this was a bit tricky due to the size of the 9-pin D-Sub connectors and I am not done, but the current draft look very promising. Hopefully I will have this in rev 1.0 around October. Size is 100 x 70mm.

I actually started end partly developed a different adapter last time I needed CAN. I never finished that design, but much of the design is tested, verified and reused here. The only missing part is wired Ethernet.

ESP32 Oscilloscope & Logic Analyzer

Most of my Oscilloscope & Logic Analyzer needs are on frequencies below 1Mhz, and ESP32 have 16 12-bit ADC channels, so it would not take much to create a 16 channel low frequency Oscilloscope/Logic analyzer. This is why I bought a ESP32-WROVER dev kit. Wrover have 4Mb SRAM as well as all the features of ESP32.

Max sampling rate on ESP32 is 2Msps. The details here are not well documented, but if we assume a minimum of 10 samples per Hz we should be able to sample frequencies up to 200Khz. Even more interesting is that ESP32 contains support of programmable Gain through it’s logic pad’s. Again I must state that my knowledge in this area is limited and so it doc on the subject, so I need to experiment and see what I can achieve.

The good thing is that all I need is a breadboard and a kit costing 10.- USD to test this out. No guarantees, but it will regardless be interesting playing with the ESP32 ADC channels.

ESP32/STM32/Raspberry PI – all in one dev kit

Starting at left is a ESP32 Breakout board, then one of my own STM32F405RG breakout boards, a 5″ HDMI, Wireless keyboard and Raspberry PI 3 with my 5 port Hat – and your right it’s a different Hat with screw terminals, so I do have 2 x 5 port Hat’s working.

 What I will do here is to wire up SPI between ESP32 & STM32 for testing, and I can add CAN breakout boards etc getting the boards to communicate. The only drawback here is that I can only use one SWD at the time, but I will get around that.

Not so mini CAN Adapter

This is the same drawing as the previous post, but I added the TF Card option. Basically this is a small mini-computer Complete With display, keyboard (in the display), Wifi, Storage etc. Just for the record it actually got more “disk” and RAM than my first IBM Compatible PC.

I have never done a TF-card Interface, but I have some breakout boards laying around. As mention you can actually buy this on the NET known as “CAN32” for ca 39.-USD. It comes as a specialized breakout Board for ESP32. But, the good things is that I can easily assemble this on a vero Board to get started – and since someone else have made firmware for this we can use their project and modify it as a starting point.

What I want to achieve here is a CAN Tesrter, Analyzer & Adapter – all in one. I need CAN connectivity and I need to see what happens on several CAN Networks simultaneously. As mentioned earlier I will use a STM32 to deal with CAN and make this galvanically isolated later, but this is something I can wire up and start with while waiting for PCB’s.

Assuming the TF Board Works out I should also add this to the larger Board. I also think I want to Mount the display on ESP32 and if I have Space add the 16 analogue IO lines from STM32 as a separate port.

This design start to make a bit sence because we now use ESP32 for Wifi, HMI, Storage, while we leave CAN and IO to STM32.

The last part of this project is boxing it. In fact, we could use Bluetooth to connect to our phones/tablets as HMI rather than adding a UART based HMI as well. With Wifi I can obviously connect my laptop as HMI, but the reason I explore other options is because my laptop usually is occupied otherwise and I need the extra screen. Fiddling around swapping screens and turning Smart Phones orTablets on/off is not always the most convenient choice, so having a physical display on the adapter is a priority IMO.

It’s only one flaw here – I am making all this for CAN while I myself am a heavy user of RS485/RS-X. The STM32 I use here have 5(6) UART’s, and if I commit on using STM32F405RG I have 2 x 10Mbps RS485 capable ports that will be very handy for sniffing on my PLC system later. The only thing is that I can’t use galvanic isolation at that speed, but I can live with that.  Woops – wrong! Checking the datasheet of ADM2582E it supports data rate to 16Mbps!

Puh – I better stop writing before I add more into this – my only concern here is what speed I can get between ESP32 and STM32 on UART or SPI. I am hoping on 40ich Mbps on SPI, but that is yet a unknown that I need to investigate. I think ESP32 is capable of this, but it’s more if I am capable of using it – lets see.


It exist a project that simply add a CAN Tranceiver to a special ESP32 dev Board – called CAN32. This uses the CAN on ESP32 and provide a Wifi based CAN Interface. I have a few CAN breakout Boards and Nextion displays so I want to assemble one of these. The main drawbacks With this is (1) I am unsure about the quality of CAN on ESP32, and (2) it don’t uses a galvanic isolation.

Galvanic isolation complicate things a bit. You don’t really need it for Wifi usage, but you seriously should have it if you connect the USB to your PC With industrial equipment on the other side.

CAN Adapter with Wifi

I need a CAN adapter for a special job and decided to build my own. I use ESP32 to get Wifi, Bluetooth and USB Connectivity, but I decided on adding a STM32 to get dual CAN Connectivity. ESP32 do have an undocumented CAN port as well, but I assume ExpressIF have their reasons for hiding it. The display is because I also want to use this as an Analyzer sniffing. And as a adapter of this type will be connecting to industrial equipment I also want the galvanic issolation.

This will cost me around 20.- USD in components + display and boxing, but to buy something like this I would probably have to pay 500.- to 1000.- USD if I could find one.

I have listed both UART and SPI for communication between ESP32 and STM32 because I am unsure about both on ESP32. I know they exist, but have no experience with them so I can as well just Connect both . I also added a battery option on the PSU.