PWM Bug or Programmer Error?

I am writing a script to control two servos from an Arduino Portenta H7 with the vision shield, and got started from the Cheat Sheet here.

I calibrated the servos using a servo tester and center is pretty close to 1500. Each has ~120 degrees of movement. I can control them predictably using Arduino code, but

x_signal_pin = Pin("PC6", Pin.OUT_PP, Pin.PULL_NONE)
y_signal_pin = Pin("PC7", Pin.OUT_PP, Pin.PULL_NONE)

timer = Timer(3, freq=50)

x_channel = timer.channel(1, Timer.PWM, pin=x_signal_pin, pulse_width=1500)
y_channel = timer.channel(2, Timer.PWM, pin=y_signal_pin, pulse_width=1500)
x_channel.pulse_width(10)
time.sleep(2)
x_channel.pulse_width(1500)

Doesn’t move the way I expect which is,

  1. Center
  2. Move to one side
  3. sleep
  4. Move to center

Am I assuming or doing something wrong?

Use the machine PWM class? class PWM – pulse width modulation — MicroPython 1.20 documentation

from machine import PWM

ImportError for the name PWM. Do I need to install this module?

Mmm, it should be built-in. Okay, let’s use the PYB module then.

x_channel.pulse_width(10)
time.sleep(2)
x_channel.pulse_width(1500)

Doesn’t seem right. A pulse width of 10 is usually ignored by a servo. Try:

x_channel.pulse_width(1500)
time.sleep(2)
x_channel.pulse_width(1000)
time.sleep(2)
x_channel.pulse_width(2000)
time.sleep(2)
x_channel.pulse_width(1500)

Is it possible that it’s unavailable because of the hardware?

You’re right about the pulse width range, these servos seem to operate in 500-2500usec.

They are moving but they don’t seem to be moving into the correct positions based on what I’ve seen with Arduino code and the servo tester. Almost like they are skewed 90 degrees off, at times.

Were you using pulse lengths with the Arduino or a library ? Typically you need to calibrate the ranges.

Looks like I was using the library that takes angles, so I’ll ignore the Arduino implementation for now.

Here is what 1500us (not 500) looks like when I attach the servos to a consistency tester (black arrow for reference):

And after running the micropython code:

// hit media upload limit, I’ll upload in a second post

code:

import pyb
from pyb import Pin, Timer
import sensor
import time

x_signal_pin = Pin("PC6", Pin.OUT_PP, Pin.PULL_NONE)
y_signal_pin = Pin("PC7", Pin.OUT_PP, Pin.PULL_NONE)

timer = Timer(3, freq=50)

x_channel = timer.channel(1, Timer.PWM, pin=x_signal_pin, pulse_width=1500)
y_channel = timer.channel(2, Timer.PWM, pin=y_signal_pin, pulse_width=1500)
time.sleep(2)

# test bottom servo movement
x_channel.pulse_width(1500)
time.sleep(2)
x_channel.pulse_width(1000)
time.sleep(2)
x_channel.pulse_width(2000)
time.sleep(2)
x_channel.pulse_width(1500)

Are you saying I can fix this with calibration?
Thanks for your time, btw.

// Second picture