0

I'm trying to control a servo with a PWM signal with an ESP32 with micropython. I cannot seem to get the servo to move and therefore would like to check my PWM signal.

I created a testing script to generate a PWM signal on GPIO32 and measure this back on GPIO36. I connected a jumper wire between 32 and 36 and I'm using the following code:

"""Testing script"""
import machine
from machine import Pin, PWM
import utime

# PWM on pin 32
p_out = Pin(32, Pin.OUT)
pwm = PWM(p_out)
f = 500
pwm.freq(f)
dc = 512
pwm.duty(dc)

# Measure on pin 36
p_echo = Pin(36, Pin.IN)
while True:
    timeout_us = int(2 * 1 / f * 1e6)
    print(
        f"Trying to measure pulse length of {dc/1024*1 / f * 1e6} us with a timeout of {timeout_us} us"
    )
    print(f"Pulse length: {machine.time_pulse_us(p_echo,0,timeout_us)} us")
    utime.sleep_ms(100)

The only thing I get back is

Trying to measure pulse length of 1000.0 us with a timeout of 4000 us                                                                                 
Pulse length: -1 us    

I'm obviously missing something here. The documentation says:

machine.time_pulse_us(pin, pulse_level, timeout_us=1000000, /)

Time a pulse on the given pin, and return the duration of the pulse in microseconds. The pulse_level argument should be 0 to time a low pulse or 1 to time a high pulse.

If the current input value of the pin is different to pulse_level, the function first (*) waits until the pin input becomes equal to pulse_level, then (**) times the duration that the pin is equal to pulse_level. If the pin is already equal to pulse_level then timing starts straight away.

The function will return -2 if there was timeout waiting for condition marked (*) above, and -1 if there was timeout during the main measurement, marked (**) above. The timeout is the same for both cases and given by timeout_us (which is in microseconds).

Seems like the timeout expired and nothing happened. I don't really have something else to verify that the PWM output is actually doing something like a scope.

1 Answer 1

1

Figured it out, my firmware had a bug (v1.18), updating to esp32-ota-20220213-unstable-v1.18-128-g2ea21abae fixed the issue for me.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.