We connected the motor for a drill to pin 6. I can turn the motor on or off by setting the pin high or low, but now we’d like to use PWM to control it. When I use the following example code, nothing happens:
from pyb import Pin, Timer
p = Pin(‘P6’)
tim = Timer(2, freq=10)
ch = tim.channel(1, Timer.PWM, pin=p)
ch.pulse_width_percent(50)
# PWM Control Example
#
# This example shows how to do PWM with your OpenMV Cam.
import time
from pyb import Pin, Timer
tim = Timer(4, freq=1000) # Frequency in Hz
# Generate a 1KHz square wave on TIM4 with 50% and 75% duty cycles on channels 1 and 2, respectively.
ch1 = tim.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width_percent=50)
ch2 = tim.channel(2, Timer.PWM, pin=Pin("P8"), pulse_width_percent=75)
while (True):
time.sleep(1000)
Hi,
Trying to use PWM (for a buzzer), using setup below. Hoiwever, I get error attached ie Timer does not exist - I’ve tried different timers, different pins.
def buzzer_init():
# Set up the timer for PWM
tim = Timer(4, freq=1000) # Frequency in Hz
# Set up the PWM channel
ch = tim.channel(1, Timer.PWM, pin=Pin(‘P7’), pulse_width_percent=50) # 50% duty cycle
machine imports look like this:
from machine import LED, RTC, Pin, PWM, ADC, Timer
In the machine module, you need to use the PWM class. There’s only virtual Timers of -1 in the machine module which are used for callbacks to run a method.