Two different PWM frequencies at the same time

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
laukejas
Posts: 29
Joined: Thu May 02, 2019 5:17 pm

Two different PWM frequencies at the same time

Post by laukejas » Mon Mar 16, 2020 9:49 pm

Hi,

I am making a robot project with Nodemcu V2, running MicroPython. I have two 6V DC motors, controlled through TB6612FNG. Motor speeds are controlled via PWM frequency and duty cycle. Frequency of 512 Hz works well for the whole duty cycle range, allowing good control of motor speed. However, I also have a SG90 servo motor for turning the ultrasonic sensor. It requires 50 Hz PWM frequency, and is also controlled via duty cycle.

Through experimentation, I noticed that whenever I set PWM frequency for one pin, it also changes for another pin. For example, if I type to console:

Code: Select all

pwm1 = machine.PWM(machine.Pin(0))
pwm1.freq(512)

pwm2 = machine.PWM(machine.Pin(13))
pwm2.freq(50)

pwm1
It prints

Code: Select all

PWM(5, freq=50, duty=0)
As you can see, changing frequency on one pin changes it for the other one as well. I suspect that there is some global PWM frequency variable in the ESP core, rather than setting individual frequencies for each PWM pin.

This is a problem because:
1. My DC motors do not run well on 50 Hz. On full duty cycle it is okay, but when I go lower, they start vibrating and shaking violently. With 512 Hz, they run smooth.
2. SG90 doesn't run well on 512 Hz. It loses part of it's range - instead of 180°, it can only turn as far as 130°.
3. While I don't need both the DC motors and the servo to turn simultaneously, these frequencies still interfere. For example, if I set the PWM frequency to 50 Hz, turn the servo to the desired orientation, and then set PWM frequency to 512 to run my DC motors, the servo violently slams back to 0° position. Clearly, changing the PWM frequency completely messes up it's mind.

I don't see any obvious workaround, and I'm finding little help in the documentation. I am unsure if this is a MicroPython, NodeMCU or ESP8266 issue, and what can be done to fix it. Can anyone help, please?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Two different PWM frequencies at the same time

Post by jimmo » Tue Mar 17, 2020 1:37 am

laukejas wrote:
Mon Mar 16, 2020 9:49 pm
I don't see any obvious workaround, and I'm finding little help in the documentation. I am unsure if this is a MicroPython, NodeMCU or ESP8266 issue, and what can be done to fix it. Can anyone help, please?
This is unfortunately a hardware limitation with the ESP8266.

laukejas
Posts: 29
Joined: Thu May 02, 2019 5:17 pm

Re: Two different PWM frequencies at the same time

Post by laukejas » Tue Mar 17, 2020 1:52 am

Then why do all tutorials I can find on PWM (regardless of the language or programming environment) show examples in which PWM frequency is set for pins individually, often doing it multiple times (though frequency is always the same)? Isn't that kind of misleading? Shouldn't there be some global setting?

Okay, so is there any way to work around this limitation? Or is it at all impossible to run DC motors and servo at the same time if they require different PWM frequencies?

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Two different PWM frequencies at the same time

Post by kevinkk525 » Tue Mar 17, 2020 7:00 am

Solution: Get an ESP32 or pyboard. The esp8266 is just a weak hardware ;)
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply