Controlling a servo

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
jlo
Posts: 4
Joined: Sun Feb 13, 2022 1:40 pm

Controlling a servo

Post by jlo » Sun Feb 13, 2022 1:48 pm

Hi, I'm trying to control a servo with an ESP developer board. To do so I have an external 5v supply to power the servo and I'm trying to use pin 23 with PWM to control the servo.

The servo does not move however, this is my code:

Code: Select all

from machine import Pin, PWM
import utime

pin_no = 23
servo = PWM(Pin(23, Pin.OUT), freq=50, duty=77)
PAUSE = 0.05

dc_min = 50
dc_max = 100

while True:
    # Sweep duty-cycle forward
    for duty in range(dc_min, dc_max):
        servo.duty(duty)
        dc_percent = float(duty) / 1024.0 * 1e2
        duty_time_ns = float(servo.duty_ns())
        print(f"Duty cycle: {dc_percent:.2f} [%], on-time: {duty_time_ns} ns")
        utime.sleep_ms(int(PAUSE * 1e3))
    # Sweep duty-cycle back
    for duty in range(dc_max, dc_min, -1):
        servo.duty(duty)
        dc_percent = float(duty) / 1024.0 * 1e2
        duty_time_ns = float(servo.duty_ns())
        print(f"Duty cycle: {dc_percent:.2f} [%], on-time: {duty_time_ns} ns")
        utime.sleep_ms(int(PAUSE * 1e3))
The response:

Code: Select all

Duty cycle: 4.88 [%], on-time: 0.0 ns                                                                                                                 
Duty cycle: 4.98 [%], on-time: 0.0 ns                                                                                                                 
Duty cycle: 5.08 [%], on-time: 0.0 ns                                                                                                                 
Duty cycle: 5.18 [%], on-time: 0.0 ns                                                                                                                 
Duty cycle: 5.27 [%], on-time: 0.0 ns                                                                                                                 
Duty cycle: 5.37 [%], on-time: 0.0 ns                                                                                                                 
Duty cycle: 5.47 [%], on-time: 0.0 ns                                                                                                                 
Duty cycle: 5.57 [%], on-time: 0.0 ns                                                                                                                 
Duty cycle: 5.66 [%], on-time: 0.0 ns                                                                                                                 
Duty cycle: 5.76 [%], on-time: 0.0 ns   
The on-time is suspicious I think, is this pwm output doing anything?

I'm using a 9g Micro Servo which needs a pulse width between 1000 and 2000 us.

I've tried to connect to the servo directly and using a level converter to convert from 3.3V to 5V. I've tried to power the servo from the 5V from the ESP. I've tied all the grouns together. What can I still look for?

Thanks!

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

Re: Controlling a servo

Post by Roberthh » Sun Feb 13, 2022 2:01 pm

If you use the v1.18 of the firmware, switch to ta recent nightly build. There was a PWM bug in version 1.18, which was been fixed about 12 days ago. The fix is only available in the nightly builds, until v1.19 is published.

jlo
Posts: 4
Joined: Sun Feb 13, 2022 1:40 pm

Re: Controlling a servo

Post by jlo » Sun Feb 13, 2022 2:45 pm

Hi Robert, thanks for your reply! I just saw the other topic with someone who had the same problem. The pwm now behaves as expected, the servo unfortunately does not move yet, I think my level converter is not working properly, I'm using the TXS0108E

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: Controlling a servo

Post by OlivierLenoir » Mon Feb 14, 2022 6:10 am

You dont' need TXS0108E, it works without it.

Proposed schematic is here, just change it to use wanted Pin.
If you want to use an external 5V power supply, you will need to connecte ESP32 GND with external power supply GND.

Post Reply