PWM on ESP8266

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
OutoftheBOTS_
Posts: 653
Joined: Mon Nov 20, 2017 10:18 am

PWM on ESP8266

Post by OutoftheBOTS_ » Wed Nov 29, 2017 6:26 am

I am using PWM for speed control of my motors through a H-Bridge. The H-bridge uses PNP transistors so it is backwards in that when it is pulled to high the h-bridge is off and when pulled to low it is active.

This is the issue that I am finding. Everyone once in a while my robot will suddenly accelerate (like the PWM has stopped and gone to full low for a short time) then it returns to normal. I asume it is caused by the fact that PWM on ESP8266 is software generated not hardware PWM and that every now and then the processor is too busy to maintain the PWM??

User avatar
pythoncoder
Posts: 3607
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: PWM on ESP8266

Post by pythoncoder » Wed Nov 29, 2017 6:48 am

The ESP8266 isn't the fastest or most responsive chip but I'm surprised it's that bad. While I haven't used PWM I have written code which needs to respond to events and haven't observed latency of anything like that magnitude. For example in my testing it responded to interrupts in 600μs maximum.

My approach would be to produce a bare minimum test case which drives one motor at a constant speed to see if the problem persists. If it does, there may be a specific issue with PWM which might need to be raised. If the problem does persist and you're using a 1KHz rate, it might be worth trying a lower rate to see if this has any effect.
Peter Hinch

OutoftheBOTS_
Posts: 653
Joined: Mon Nov 20, 2017 10:18 am

Re: PWM on ESP8266

Post by OutoftheBOTS_ » Wed Nov 29, 2017 7:45 am

I am currently using 40khz rate as this tends to be a fairly standard rate for PWM of motor control. The lower the rate the more efficient the H-bridge tends to be because of less switching delay in the transistors but if you go below 40khz then u get in the human hearing range and you will hear a hum from the motors because of the PWM turning them on/off at a rate that u can hear.

I must admit that I am asking a lot from the ESP8266. Because the little dual H-bridge that I am using requires PWM on all 4 IN pins for speed control it means that I am running 4 PWM channels at the same time I am reading 2 encoders and running PID speed control routines of course running a Webrepl for printing live data to my computer screen of what's happening.

I have ordered a tb6612fng dual h-bridge chip as this chip has seperate PWM pins from the IN pins so I will only need 2 PWM channels instead of 4. I have also ordered a LS7366R encoder counter but I am hoping to make it work without needing to use it as they r not the cheapest chip to buy here in Australia.

In the end the obvious solution is for me to move to the ESP32 for this project as it is both faster with 2 cores to spilt up the WiFi and my program and it also has hardware PWM. The down side to the ESP32 is webrepl isn't available so I suppose that I will have to sit down and become a good websockts programmer to be able to send the data live via a webserver.

User avatar
deshipu
Posts: 1348
Joined: Thu May 28, 2015 5:54 pm

Re: PWM on ESP8266

Post by deshipu » Wed Nov 29, 2017 6:41 pm

Another possibility is to oursource the PWM to a dedicated chip, such as a PCA9685. That also saves you pins.

OutoftheBOTS_
Posts: 653
Joined: Mon Nov 20, 2017 10:18 am

Re: PWM on ESP8266

Post by OutoftheBOTS_ » Wed Nov 29, 2017 9:16 pm

Yes I do have a PCA9685 that I use for my hexapod robot and my worm robot that uses a lot of servos.

One of my biggest factors always with all my robots is available space. I am trying hard to avoid using too many external chip like the encoder counter chip and PWM chip (using a external h-bridge can't be avoided). The PCA9685 is very small in size so doesn't use much space but the main downside to that will be coms speed, I normally am running my PID speed control update at 10hz(most people will use 20hz but I think this is over kill and the motors won't respond that fast). To update the PWM on the PCA9685 requires writing a 2 byte word iva I2C for each channel your updating.

ATM I am just using a brought dev board but I do plan to make my own PCB once I sort out how everything is going to work.

User avatar
deshipu
Posts: 1348
Joined: Thu May 28, 2015 5:54 pm

Re: PWM on ESP8266

Post by deshipu » Wed Nov 29, 2017 10:40 pm

2 bytes is 18 clock cycles, add the protocol overhead, and you get around 32 clock cycles for this. PCA9685 works with I2C clock up to 400kHz in fast mode. That means you can do updates at 12.5kHz. That's three orders of magnitude faster than what you are currently doing.

OutoftheBOTS_
Posts: 653
Joined: Mon Nov 20, 2017 10:18 am

Re: PWM on ESP8266

Post by OutoftheBOTS_ » Thu Nov 30, 2017 2:46 am

Well that's certainly fast enough updates even with multiply channels. The PCA9685 also is very small in size and cheap to buy too. It is definitely an option to think about. Thanks

User avatar
pythoncoder
Posts: 3607
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: PWM on ESP8266

Post by pythoncoder » Thu Nov 30, 2017 1:47 pm

OutoftheBOTS_ wrote:
Wed Nov 29, 2017 7:45 am
I am currently using 40khz rate as this tends to be a fairly standard rate for PWM of motor control...
That puzzles me as the manual http://docs.micropython.org/en/latest/e ... hlight=pwm states "PWM can be enabled on all pins except Pin(16). There is a single frequency for all channels, with range between 1 and 1000 (measured in Hz). "
Peter Hinch

OutoftheBOTS_
Posts: 653
Joined: Mon Nov 20, 2017 10:18 am

Re: PWM on ESP8266

Post by OutoftheBOTS_ » Thu Nov 30, 2017 9:04 pm

Yes I use the same freq on all 4 pins but I change different duty cycle on different pins.

[edit] 40hz not khz
here's my setup code
left_forward = PWM(Pin(4))
left_forward.freq(40)
left_forward.duty(1023)


My H-bridge works backwards in the fact u have to pull to low to active the motor. So when 1 wheel only suddenly accelerates for what must be a few hundred ms (considering that I can see the change) it means just 1 of the 4 pins r getting stuck on low and failing to go high in normal cycle. I can happen on all the pins at different times.

I should maybe put the output pin on the scope and see how clean the software PWM is on the ESP8266, the software PWM on the raspberry pi is very dirty and and vary on freq and duty a lot(bounces all over the shop on the scope)

OutoftheBOTS_
Posts: 653
Joined: Mon Nov 20, 2017 10:18 am

Re: PWM on ESP8266

Post by OutoftheBOTS_ » Fri Dec 01, 2017 12:59 am

I did just google human hearing range and it seems that humans hear from 20hz to 28khz so 40khz is what I should be running the PWM at for motors to stop them humming or squealing. I suppose this would be just 1 more advantage of the PCA9685 being able to do PWM at that freq

Post Reply