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.

ESP32 Programming with Arduino IDE

ExpressIF implemented FreeRTOS with their Arduino IDE library, so by using this we get a lot for free. By default we run Wifi etc on core 0, while the classic Arduino loop() run’s on core 1. In my case I want to use core 1 for bit banging and that is straight forward to program in loop(). By using an ever loop I bit-bang with < 50yS accuracy which is very good.

The more tricky part is the communication tasks that we have to add to core 0. Firstly we need to deal with the Task Watchdog. Since core 0 is used for system tasks it also monitor it’s tasks and trigger the watchdog if any of them run forever, so we need to (1) create a loop, and (2) call delay within that loop.

The console print below show a loop w/counters in loop being monitored by a task in core 0.

Ca 526191 iteration a second and stats show that we max have 10uS delta with an average of 1.9uS between iterations. This is our bit-bang accuracy. These 10uS will probably be 50uS as we add content, but that is still 50-100Khz accuracy or 0.05 – 0.01ms if you like.

My alternative would be to attempt a 10,000 timer per sec timer interrupt that would give 0.1 ms accuracy and use ca 10% CPU load. I have not tried this on ESP32, but this technique have its advantage that you can use 80-90% of Core2 for something else.

Programming a Servo Controller

It’s time to code up the ESP32 Utility Driver. I am not that found of Arduino IDE, but it works and I like the wire library concept and how it simplify things. A Servo port is a signal that send a 50Hz pulse. The technique I suggest is a bit-banger where we loop as fast as we can checking pulse length. We set up the 12 signals in a table, start them at the same time and then iterate as fast as we can closing them at the proper time to get a correct pulse length.


The alternative is to use an interrupt. This could work, but we would get a resolution of 1ms (1000/sec) or 0.1ms(10000/sec) max. Any interrupt faster than that would use to much CPU time. Another technique would be to bit-bang signal 1, then signal 2 etc. this is what the classic Arduino library does. The technique I suggest gives a much higher resolution as we iterate and check much faster that we can use interrupts.

This would have worked well with a single core, but as we have a dual core we can allocate this task to one core and I expect something like 10uS accuracy or 100Khz sampling if we use this as  data sampler.

We have 21 IO ports to maintain in an iteration:

  • 12 Servo or IO ports
  • 2 (4) H-Bridge signals
  • 7 PWM signals

ESP32 is perfect for this job as we can use 1 core for this purpose while the second core handle the Wifi and easyIPC that I will return to later.

ESP32 Utility Driver Update

This ESP32 based Utility driver is just awesome in what it can do. I have to make a Rev 1.1, but for most parts it worked “as is”

  1. 7 x PWM Signals 0,5A each.
  2. CH340G UART to USB.
  3. ESP32 WROOM.
  4. 12 x Servo or IO ports.
  5. 12V PSU Input.
  6. USB.
  7. 2 x H-Bride for DC Motors.

I had 2 errors on the board. (1) I did not cross Rx/Tx correctly and (2) I overlooked some logic needed for the serial bootloader. The later forces me to use the boot jumper all the time, but I will fix that on Rev 1.1. The schematics below illustrate the bootloader fix:

This is copied from the reference diagram at expressif and indicate how DTR and RTS on the serial interface is used to automatically toogle ChipPU and Boot as we download new firmware. This should avoid the need to set the boot jumper and restart to trigger the serial bootloader protocol. I will see if I can test this on a vero board before ordering 1.1 rev of the PCB’s.

ESP32 Utility Driver

Finally received the PCB for the Train Control System and the Utility Driver. I decided to solder the Utility driver because that has the CG340G USB connection. Soldered the PSU & CG340 was straight up. I am actually impressed by CH340G as it always work and connect to Windows. Soldering ESP32 was straight forward, but first attempt to get it working failed.

A quick look at schematics and swapping RX/TX did however do the job. A trick is to use Boot first time you load from Arduino IDE and it also help having a working reference like a breakout board.

Finally the Wifi scan works from my utility driver. I will solder up the rest of the circuit and start programming servo’s tomorrow. My first usage of this will be on my 12 legged robot.

These two last pictures show the Train Control System. We can play with that later. Actually I need to get my act together and buy a test track. As said my ambitions are limited to a track around the x-mas tree – ok maybe 2,3,4 or 5 tracks 🙂

PSU Ripple

The LMR14206 ripple was a bit much for my taste. It worked, I had 12V out, 3V3 out and the MCU ticked. But, it’s not a design I can live with. For now I continue on a different board and avoid mounting the DC/DC because I also realized that I could not really bypass it they way I had set up the jumpers. I decided for a 78M05 in TO252 format that gives 0,5A. But, for now I will use 12V directly.

This will work for now. I will return to DC/DC later, but looking at other peoples postings and the lack of LMR14206 popularity I get the picture.

The supercap in the schematics works well, but I probably need to add a bit of circuitry like I did on on PLC Com module. I use a 0.33F due to the size.

LMR14206 PSU Ripple

LMR14206 with 12V output to a LM1117 3.3V regulator. This is the 3.3V – 350mV ripple – not good. I need to work on that a bit. This is from my Motor Controller with MCU ticking.

I probably need to add another coil on the 3.3V, but that add a lot of space, so I am starting to think that a 7812/317 regulator is a better shoice. Will see how much is needed to fix this.

Regardless a bit impressed over my DS1054 Rigol Oscilloscope. I have an expensive Techtronics at work and to be honest I would gladly replace it with my Rigol costing only 10% of the price for a Techtronics.

12V/0.6A DC/DC Regulator

This is my LMR14206 DC/DC regulator with R1=15K and R2=1K which gives 12.2V out. The white wire is the PCB error I wrote about earlier.

This is basically only the reference diagram – just be aware that the datasheet and demo schemtics mess up R1/R2 a little. Myself I use E values, so by setting R2=1K I can adjust voltage out by changing R2.

You need ca 2V drop, so the input for 12V out is 14-42V. I will get back with updated schematics and values for 3V3 matching E24 series later. I also want to have a look at the ripple that need some improvements.

3V3 DC/DC PCB Failure

I finally got around to assemble my 3V3 DC/DC converter and it did not work. Somehow I have switched off all checks in the EDA and managed to send it out like this – notice pin 6 is not connected … 

Luckily this is the first time I stepped into a bug in the PCB. The project check usually pick everything up – if it is switched on…