Category Archives: ESP32

ESP32 PI – Raspberry PI Replacement

I have a few Raspberry PI Hat’s laying around and I wonder if I should make a ESP32 based replacement for Raspberry PI Zero W. The IO on ESP32 is far more capable than the one on the PI, but we have a few limitations and less pins to play with.This is an early draft with the 3D above and PIN mapping below.

Raspberry PI Pin ESP32 PI Description
1 3.3V
2 5V
3 8 – GPIO32 GPIO2 / I2C-SDA
4 5V
5 9 – GPIO33 GPIO3 / I2C-SCL
7 10 – GPIO25 GPIO4
8 11 – GPIO26 GPIO14 / UART-TXD
10 12 GPIO27 GPIO15 / UART-RXD
11 13 GPIO14 GPIO17
12 14 GPIO12 GPIO18
13 16- GPIO13 GPIO27
14 GND
16 GPIO10 GPIO23
17 3.3V
18 GPIO11 GPIO24
19 27 GPIO15 GPIO10 / MOSI
20 GND
21 28 GPIO2 GPIO9 / MISO
22 7 GPIO35 GPIO25
23 30 GPIO4 GPIO11 / CLK
24 31 GPIO16 GPIO8 / CE0
25 GND
26 33 GPIO17 GPIO7 / CE1
30 GND
32 GPIO19 GPIO12
33 GPIO21 GPIO13
34 GND
35 GPIO22 GPIO19
36 GPIO26 GPIO16
39 GND


SG90 9G Servo is no wimp

This litle Servo about 3 x 3cm is far from a wimp as far at current usage goes. A single Servo have no problem dragging a 10A PSU down – actually I think it’s the current regulation that is predictive, meaning it drops out because current raise is to fast – but, anyway – the only PSU I have that can manage this is a 80A battery.

I connecting 4 of these to my ESP32 Utility Driver and some of my dodgy wires burned up.

Looking at the scope I observe a small Voltage drop as the Signals pulse in. As I add servo’s it become worse. I think I need to alter my algorithm a little to spread the pulses out in time and avoid 12 servos that pulse at the exact same time. I also think I need to buy a 6V battery and ditch the 12V to 5V regulator as it most likely is this that dip a bit.

The ESP32 Utility Deriver does however work well. I am a bit puzled over my DPS5005 PSU’s. They seem to use a predictive aproach to regulating current.

ESP32 with CH340 Auto Reset Working

Been a long day with try & fail. I managed to get Auto Reset working on 1.0 by connecting RST to ChipPU amd DTR to GPIO0, but it only worked some times.

Walking back to my rev 1.1 I wanted to test manual loading and discovered it did not work either. Inspecting ESP32 I notice that I had missed a soldering – and voila… suddenly my progamming port work as it was supposed to.

1,5 days of fuzz for a bad soldering. A bit annoyed as I know I should have tested manual loading instantly to verify that I did not have a dodgy ESP32, but well.

ESP32 Utility driver still not working

I finally got new PCB’s for this utility driver and I still can’t get the programmer port to work automatically. All engineering is like that some times – try and fail – two steps forward, one step back. In this case it sadly involves waiting 6+ weeks for a new PCB once I find ut why it don’t work.

The logic with T1 & T2 should have driven the automation logic. I have double checked and this logic is used by many thought I also see a different solution used.

Well, next step is to use boot and see if I can program the ESP32 at all. After that I need to measure RTS & DTR to see if signals are as expected and basically see if I can tweak it to work.

ESP32 – GPIO 34-39

#4 on the picture above is Servo channels. I discovered that only 8 of there are working as expected. The 4 last ones have GPIO 34-39 that failed in testing – and reading small notes in the datasheet I realize that I have missed out a “input only” note on these pins.

And guess 3 times which pins I used for the single H-Bridge on the little one. I am going to need a special adapter for these so I can program them anyway.

I actually need 12 servos on the Utility Driver, so I will need to grab 4 channels from H-bridges or PWM. I probably need to re-think the Utility Driver(s) a bit.

ESP32 – PlatformIO

I have tried PlatformIO twice. Once for STM32 and now for ESP32. PlatformIO promote itself as open source, but it looks more like a commercial solution with version No 0.14. It can be added to Visual Studio Code. It imports existing Arduino Projects etc.

But, well – two attempts and it is still sorted under “crap that don’t work well” – waste of time and money!

ESP32 with Visual Studio Code & Arduino

Having used Arduino IDE to develope the ESP32 Utility Driver code I must admit that while I in general like Arduino I also miss a proper code editor. Sticking to Arduino (for now) I decided top try out Visual Studio Code.

Visual Studio Code is basically a free and liteweight version of Visual Studio. In this case I just opened the folder and voila. This gives me the option to edit code here and use Arduino IDE to compile and upload.

I am fully aware that more advanced options exist, but I intend to stick to Arduino libs on this project because they are straight forward to use. I actually have the full toolchain & ESP-IDF etc installed. This would probably be my own preferred ways of working, but I need something that every hobbyist can work with – no fuzz – with as little fuzz as possible.

It’s other coder alternatives than Visual Studio Code. Friends of mine stick to Emacs etc. I learned years ago that you don’t mess with developers personal choises unless you want them frustrated and less productive. So whatever works for you is fine.

A note on Arduino 1.8.5 is that it will not detect changes made by a different editor, so changes will be reverted if you save from Arduino after having changed a file from another editor.

ESP32, Arduino & FreeRTOS

One of the things that I am not happy with is the FreeRTOS implementation on the Arduino ESP32 library. I try to reserve core 1 for bit-banging and I have a nice average 4uS accuracy. But, I also have 110uS long iterations ca once per second as I activate WiFi. Also, I fail to activate WiFi from anywhere but “loop()” (core 1). Any attempt on doing this from a thread causes FreeRTOS to “panic” and result in a core dump.

I suspect that it either is a FreeRTOS thread or interrupt that uses core 1 and not core 0. 110uS means 10% accuracy on a Servo Pulse. But, more important is that FreeRTOS get’s in my way. This is a classic conflict between hard real-time and threading OS designs.

Threading means that a OS uses a system interrupt to switch jobs. This interrupt can run 1000 or maybe 10,000 times a sec. At 10,000 times a sec I would expect 5-10% CPU load on ESP32 for this alone. The reason I do a round robin loop on core 1 is because I get higher accuracy. But, as Core 1 obviously have other things that slow it down I will need to look into that.

It is not critical for my 12 Servo channels because I also have 16 Led PWM channels available. Simply said I don’t need to bitbang the 12 Servo channels on the ESP32 Utility Driver and H-Bridge and PWM is less sensitive.

A Servo pulse is 50Hz, but last about 2500uS max, so 110uS is between 5-10% accuracy. 10% is sufficient to get me worried that the Servo  might have a minor move – in that case it would look like nervous twiches as the Servo try to hold positions – not sure. But, as I said – I do have the Led PWM’s that comes with a much higher accuracy.

ESP32 – CLI/Simple HMI

While I am working on more permanent HMI solutions I need a CLI that communicate through USB that can initialize the ESP32 based designs. A CLI is a command line interface that act like a brute force interpreter. In theory we could implement a full Unix alike shell, but I only want a few commands that is a must. But, I also recognize that this is a good way of testing board features before the HMI is finished – so here it is;

  •  *    set wifi <ssid> <password>
  •  *        // will set the ssid and password
  •  *        // automatically used at next startup
  •  *
  •  *    reboot
  •  *        // reboot the device
  •  *
  •  *    help
  •  *        // list available commands and syntax
  •  *
  •  *    stats on|off
  •  *        // switch statistics on/off.
  •  *
  •  *    hbridge <ch> <dir> <pwm-duty>
  •  *        // set hbridge to move
  •  *
  •  *    pwm <ch> <duty>
  •  *        // set pwm to duty
  •  *
  •  *    servo <ch> <pos>
  •  *        // set servo pos

For now I will use the Serial port through Arduino IDE. We can add more commands as needed. Later I will need to add this through Wifi/Telnet so I can test Model trains as well.

ESP32 Servo Programming Example

This little example demonstrate how simple it is to create a Servo using the build in LED PWM. ESP32 comes with 16 channels of PWM, so in setup we asscociate channel 0 with pin 14 and set pin 14 to OUTPUT. We also set channel 0 to 50Hz 16 bit PWM. The example will sweep 0-180 degrees using GPIO14 as signal.

#define SERVO_PIN 14

void setup()

void loop()
   for(int x=0;x<180;x++)

void SetServoPos(float pos)
    uint32_t duty = (((pos/180.0)
              *2000)/20000.0*65536.0) + 1634;
         // convert 0-180 degrees to 0-65536

        // set channel to pos

A servo signal is a 20ms pulse (50Hz) with the signal as a 500uS – 2500uS width to indicate servo angle.