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
Code: Select all
PWM(5, freq=50, duty=0)
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?