servo PWM frequency and strange behaviour

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
academo
Posts: 8
Joined: Thu Feb 10, 2022 8:21 pm

servo PWM frequency and strange behaviour

Post by academo » Thu Feb 10, 2022 8:33 pm

Hello everyone.

I've been trying to control some hobby servos using micropython and a esp32 wroom nodemcu board with strange results.

Everywhere I find documentation for hobby servos (I have a MG996R) I find a code like this:

Code: Select all

from machine import Pin, PWM
servo = PWM(Pin(27), freq=50)
servo.duty(55) #or other value
This code won't work in my esp32 (esp32-20220117-v1.18). But this exactly same code works in my esp8266 (esp8266-20210902-v1.17) using micropython.

What I also figured is that if I set the frequency somewhere between 800 and 1000 it works but for instance, in a continous rotation servo I can only rotate to one direction and never to the other.

The other non-continous servo is rated for 180 degrees range, the minimum (500us) and maximum pulse width (2500us) but I can barely manage to make it go 40 degrees using these high frequencies. Once again, it works perfectly in the esp8266.

I am fairly new to electronics and micropython and I also don't have an oscilloscope. I appreciate any help you can bring me on how to control the servos on the esp32 since I want to benefit from this board features over the old one.

Thanks!

EDIT: I failed to mention I tried this in two different ESP32 boards from different models and I had the same behaviour. I also tested 3 different servos.
Last edited by academo on Thu Feb 10, 2022 8:40 pm, edited 1 time in total.

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: servo PWM frequency and strange behaviour

Post by Roberthh » Thu Feb 10, 2022 8:36 pm

There was a PWM bug in v1.18, which prevented to set the frequency <613 Hz. That is fixed in the daily builds. Try the most recent daily build.
Edit: The fix was merged 10 days ago.

academo
Posts: 8
Joined: Thu Feb 10, 2022 8:21 pm

Re: servo PWM frequency and strange behaviour

Post by academo » Thu Feb 10, 2022 9:06 pm

That was it! thank you very much for such a fast answer. I can confirm esp32-20220210-unstable-v1.18-121-gd8a7bf83c.bin fixed my issue and now my servos are working as expected on the right frequency.

Thanks again

academo
Posts: 8
Joined: Thu Feb 10, 2022 8:21 pm

Re: servo PWM frequency and strange behaviour

Post by academo » Sun Feb 13, 2022 2:12 pm

@Roberthh the unstable firmware fixed the issue. now it happens that when I initialize the PWM the pin always initialized on a duty of 512 regardless of what I send in the constructor.

example:

```
servo = PWM(Pin(32), freq=50 duty=50)
```

this will actually set a duty of 512. no matter what I put on the duty value. My current workaround is to init it on a frequency I know the servo doesn't undertand (e.g. 9000), then lower the frequency and then set the duty.

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: servo PWM frequency and strange behaviour

Post by Roberthh » Sun Feb 13, 2022 4:28 pm

Yes, you're right. That behavior must be changed. As a workaround, you can use duty_u16 in the constructor. That would be:

Code: Select all

servo = PWM(Pin(32), freq=50, duty_16=1600)
Edit: Forget it. The problem seems more complicated. It works if you run the same command a second time, with duty= or duty_u16=.

academo
Posts: 8
Joined: Thu Feb 10, 2022 8:21 pm

Re: servo PWM frequency and strange behaviour

Post by academo » Tue Feb 15, 2022 12:22 pm

Yes. I sadly had to go back to Arduino. Because of this I can't initialize a servo on reboot without making the servo move (which opens and closes a gate).

That means when coming out of sleep it always opens and closes the gate and that happens every 10 minutes or so.

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: servo PWM frequency and strange behaviour

Post by Roberthh » Tue Feb 15, 2022 1:16 pm

I made already a not to the person who implemented the most recent version of PWM, but I can also raise an issue.

academo
Posts: 8
Joined: Thu Feb 10, 2022 8:21 pm

Re: servo PWM frequency and strange behaviour

Post by academo » Tue Feb 15, 2022 1:27 pm

I can also raise the issue in github if you want. Though keep in mind I can't probably provide more information on it than my anecdotal experience since i am not a professional in electronics.

I'd love to be able to use micropython for the project I am starting but I also can't dedicate all the time to try to find and fix a bug myself in the C-code, the best I can help is with testing and reporting.

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: servo PWM frequency and strange behaviour

Post by Roberthh » Tue Feb 15, 2022 1:37 pm

I've raised that issue already. A short look into the code yesterday did not reveal an obvious cause for that.

Post Reply