Using PWM for motor on H7 Portenta

I want to control a regular 5V DC motor using H7 Portenta, but haven’t figured how PWM works on OpenMV. I checked Examples > Board Control > pwm_control.py and read how only PWM7/PH15 pin is available for the task on Portenta, which happens to be one of the high-density pins. What’s the name of this pin to use in

timer = pyb.Timer(4, freq=1000)
ena = timer.channel(1, pyb.Pin.OUT_PP, pin=**PWM PIN NAME HERE**)

When I use pyb.Pin(“PH15”), I get ValueError: Pin(PH15) doesn't have an alt for Timer(4)

Regards,
PC

It’s Timer8 on the Portenta:

Could you try to use one of the nonnegative channels? You generally want to use the non-N channels.

Thanks for that! I should have known about Timer8.

Could you please help me figure out how PWM works to power a motor on micropython? I have connected D0 to the motor driver and am running the following code that I got from what you suggested someone here:

import time
from pyb import Pin, Timer

tim = Timer(8, freq=1000) # Frequency in Hz
ch1 = tim.channel(1, Timer.PWM, pin=Pin("D0"), pulse_width_percent=75)

while (True):
    time.sleep(1000)

Am I missing something fundamental in understand how this works compared to Arduino/Raspberry Pi’s PWM?

PC

It’s Timer8 channel 3.

import time
from pyb import Pin, Timer

tim = Timer(8, freq=1000) # Frequency in Hz
ch1 = tim.channel(3, Timer.PWM, pin=Pin("PH15"), pulse_width_percent=75)

while (True):
    time.sleep(1000)

Sorry but I still don’t get how I can connect PWM7/PH15 to the motor in this case with a wire?

I saw another tutorial on getting this running on Youtube, based on which I have tried the following (still doesn’t work):

import pyb

# Define PWM pin
tim = pyb.Timer(8, freq=1000)
ch1 = tim.channel(3, pyb.Timer.PWM, pin=pyb.Pin("PH15"))

# Declare pin connected to motor driver
motor= pyb.Pin("D5", pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE)

# Test in a loop
while True:
    print("ON")
    motor.value(1)
    ch1.pulse_width_percent(100)
    pyb.delay(1000)
    
    print("OFF")
    motor.value(0)
    pyb.delay(1000)

PC

Can you post a picture of your setup? The Portenta would normally drive PWM into the speed input on the motor driver and then also apply a direction pin to control forward and backwards. Note that the devices need to share a ground and the motor driver needs to be powered. The Portenta would not power the motor driver.

Here’s my setup:

To clarify further:

  1. Portenta Pin D5 to Driver Pin IN4
  2. Portenta Gnd to common ground
  3. Driver powered by the powerbank
  4. Driver MotorB to DC motor
  5. Motor driver: MX1508 DC Motor Driver with PWM Control Pinout, Datasheet, Features, Working

I have confirmed that all wires and components are working fine.

EDIT insufficient voltage was causing the problem. Changed the power source, tested on a multimeter, worked like a charm on this program:

import time, pyb
 
tim = pyb.Timer(8, freq=1000) # Frequency in Hz
ch1 = tim.channel(3, pyb.Timer.PWM, pin=pyb.Pin("PH15"))
carousel = pyb.Pin("D5", pyb.Pin.OUT_PP, pyb.Pin.PULL_DOWN)

while True:
    print("carousel on")
    carousel.value(1)
    pyb.delay(1000)
    
    print("carousel off")
    carousel.value(0)
    pyb.delay(1000)

Thanks for your support, @kwagyeman!

PC

1 Like