Howto: 4 PWM channels for ESP-01

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Howto: 4 PWM channels for ESP-01

Post by HermannSW » Sat Nov 20, 2021 8:08 pm

On ESP-01 module, with MicroPython PWM is supported on pins 0 and 2 only.
Trying to use PWM with pin 1 results in this error message:

Code: Select all

ValueError: PWM not supported on pin 1


I played with Arduino IDE, and with that "analogWrite()" did work on all 4 ESP-01 pins 0-3.

I found reason for pins 1 and 3 not allowing for PWM for MicroPython -- they are not listed here:

Code: Select all

pi@raspberrypi400:~/pico/micropython/ports/esp8266 $ grep "2, 4, 5" *.c
esppwm.c:STATIC const uint8_t pin_num[PWM_CHANNEL] = {0, 2, 4, 5, 12, 13, 14, 15};
pi@raspberrypi400:~/pico/micropython/ports/esp8266 $ 

I am used to build my own MicroPython for Raspberry Pico.
Today I tried to build ESP8266 MicroPython and failed on 32bit Raspberrry Buster, 32bit Raspberry Bullseye as well as on 64bit Intel Ubuntu 20.04.

On Raspberry I tried to install esp-open-sdk, that failed complaining about bash version not >=3.1 although bash version is 5.0.3. Commenting that out building fails later in "_toolchain" target:
https://github.com/pfalcon/esp-open-sdk

On Ubuntu 20.04 python-serial is missing, and cannot be apt installed.


So I tried an adventure, and succeeded.
I searched for byte sequence 00 02 04 05 in hexdump of ESP8266 1M binary, and found it exactly once:

Code: Select all

$ od -Ax -tx1 esp8266-1m-20210902-v1.17.bin | grep "00 02 04 05"
08def0 00 00 00 00 00 00 00 00 00 02 04 05 0c 0d 0e 0f
$ 

Since pins 4 and 5 do not exist on ESP-01, I used hexedit to change them to 1 and 3:

Code: Select all

$ diff <(od -tx1 esp8266-1m-20210902-v1.17.bin) <(od -tx1 esp8266-1m-20210902-v1.17.bin.mod)
36074c36074
< 2157360 00 00 00 00 00 00 00 00 00 02 04 05 0c 0d 0e 0f
---
> 2157360 00 00 00 00 00 00 00 00 00 02 01 03 0c 0d 0e 0f
$ 

After flashing the ".mod" binary, I enabled webrepl by "import webrepl_setup".
Finally I removed ESP-01 from little USB programming board and connected only GND and 3V3.
(no idea why connecting 3V3 to EN was not needed)
<EDIT>this was not needed on newer 1M ESP-01 module with printed pin numbering, just tested on an older 1M ESP-01 module, and that works only when EN is high</EDIT>

Then I did "chromium-browser webrepl.html" in directory cloned:
https://github.com/webrepl/webrepl

Then I connected my Pi400 to "MicroPython-......" network and pressed on "Connect" button.
After connecting, PWM works on all 4 ESP-01 pins (I verified each pin with oscilloscope):
ESP01.PWM_all_pins.png
ESP01.PWM_all_pins.png
ESP01.PWM_all_pins.png (37.12 KiB) Viewed 10044 times
This modified MicroPython binary is only useful for ESP-01, but for that it enables control of two DC motors for my tiny MicroPython robots with DRV8833 motor driver:
viewtopic.php?f=5&t=11454

Until now I used ESP-01 for telnet wireless into Raspberry Pico microcontrollers (with Arduono WifiToSerial.ino example flashed):
Image

Now that I have 4 PWM channels on ESP-01, the next tiny robot will get only ESP-01 and DRV8833 motor controller (the robot is built from two solar car toys with the help of a Dremel and superglue). And it will still allow wireless robot control with MicroPython REPL:
Image
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: Howto: 4 PWM channels for ESP-01

Post by HermannSW » Wed Dec 15, 2021 8:26 pm

I had ESP8266 modules with and without the I01+I03 PWM patch. Too bad I was not able to query the module whether it was patched or not. So I used hexedit again to patch two more locations. For the next 78 years we will live in 21st century, starting with "20..". I modified that redundant information, when in webrepl session:

Code: Select all

Welcome to MicroPython!                                                                                                                               
Password:                                                                                                                                             
WebREPL connected                                                                                                                                     
>>> uos.uname()                                                                                                                                       
(sysname='esp8266', nodename='esp8266', release='2.0.0(5a875ba)', version='v1.17 on 3121-09-02', machine='ESP module (1M) with ESP8266')              
>>>                                                                                                                                                   

And when in minicom session, for soft reset:

Code: Select all

>>> 
MPY: soft reboot
WebREPL daemon started on ws://192.168.4.1:8266
Started webrepl in normal mode
MicroPython v1.17 on 3121-09-02; ESP module (1M) with ESP8266
Type "help()" for more information.                                             
>>> 


The version year now makes it obvious this is a module with PWM on pins 3+1 patch.

Code: Select all

$ od -Ax -tx1 esp8266-1m-20210902-v1.17.bin > 1
$ od -Ax -tx1 esp8266-1m-20210902-v1.17.bin.patch13 > 2
$ diff 1 2
36074c36074
< 08def0 00 00 00 00 00 00 00 00 00 02 04 05 0c 0d 0e 0f
---
> 08def0 00 00 00 00 00 00 00 00 00 02 01 03 0c 0d 0e 0f
36323c36323
< 08ee80 00 76 31 2e 31 37 20 6f 6e 20 32 30 32 31 2d 30
---
> 08ee80 00 76 31 2e 31 37 20 6f 6e 20 33 31 32 31 2d 30
36551c36551
< 08fcd0 6f 6e 20 32 30 32 31 2d 30 39 2d 30 32 3b 20 45
---
> 08fcd0 6f 6e 20 33 31 32 31 2d 30 39 2d 30 32 3b 20 45
$ 
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

pepsov
Posts: 1
Joined: Thu Apr 21, 2022 5:29 pm

Re: Howto: 4 PWM channels for ESP-01

Post by pepsov » Thu Apr 21, 2022 5:40 pm

Love your sense of humor about year 3121 :)

BTW, did you really test that the PWM really works on pins 1+3 - I mean hardware wise? Can you really control motors?
(and not just print out on the screen that they are capable of doing so)

Keep the good work!

Post Reply