# PWM

**URL:** <https://forums.openmv.io/t/pwm/832>\
**Category:** OpenMV Boards\
**Created:** [July 19, 2018, 1:28am UTC](https://forums.openmv.io/t/pwm/832 "2018-07-19T01:28:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lab](https://avatars.discourse-cdn.com/v4/letter/l/ba8739/32.png) [@lab](https://forums.openmv.io/u/lab)\
**Post date:** [July 19, 2018, 1:28am UTC](https://forums.openmv.io/t/pwm/832/1 "2018-07-19T01:28:25Z")

</div>

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)

Could you please let me know what is the issue?

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [July 19, 2018, 1:29am UTC](https://forums.openmv.io/t/pwm/832/2 "2018-07-19T01:29:46Z")

</div>

Have you tried this example code?

```python
# 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)

```

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [July 19, 2018, 1:30am UTC](https://forums.openmv.io/t/pwm/832/3 "2018-07-19T01:30:05Z")

</div>

Note sure if PWM on PIN 6 works.

---

<div class="post-metadata">

**Author:** ![eamonm](https://avatars.discourse-cdn.com/v4/letter/e/eb9ed0/32.png) [@eamonm](https://forums.openmv.io/u/eamonm)\
**Post date:** [April 29, 2025, 1:57am UTC](https://forums.openmv.io/t/pwm/832/4 "2025-04-29T01:57:26Z")

</div>

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

This is all on openmv H7 board using an RT1062.

Any guidance appreciated.  
Thanks,Eamon.

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/2X/a/a31ab0413d6b209fc724945a568fef4dbbfcb8ed.png)

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [April 29, 2025, 5:07am UTC](https://forums.openmv.io/t/pwm/832/5 "2025-04-29T05:07:36Z")

</div>

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.
