# Controlling brushed motors with PWM

**URL:** <https://forums.openmv.io/t/controlling-brushed-motors-with-pwm/341>\
**Category:** OpenMV Boards\
**Created:** [July 18, 2017, 11:59am UTC](https://forums.openmv.io/t/controlling-brushed-motors-with-pwm/341 "2017-07-18T11:59:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sergeyzwezdin](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@sergeyzwezdin](https://forums.openmv.io/u/sergeyzwezdin)\
**Post date:** [July 18, 2017, 11:59am UTC](https://forums.openmv.io/t/controlling-brushed-motors-with-pwm/341/1 "2017-07-18T11:59:59Z")

</div>

Hello!

I’m trying to control brushed motor with OpenMV’s PWM and getting wierd result.

Wiring scheme is following:  
TB6612FNG dirvier used as a motor driver. There are two pins to define mode and another one for PWM. Mode pins connected to P8 and P9 OpenMV’s pin. P7 pin connected to driver’s PWM pin.

Test program is:

```python
from pyb import Pin, Timer

m1_1 = Pin('P8', Pin.OUT_PP)
m1_2 = Pin('P9', Pin.OUT_PP)
m1_1.high()
m1_2.low()

m1_tm = Timer(4, freq=488.28)
m1_pwm = m1_tm.channel(1, m1_tm.PWM, pin=Pin.board.P7, pulse_width_percent=50)

```

With this code I getting this result: [- YouTube](https://youtu.be/w19WEoutPcw)

Why it moves intermittently? Same setup with arduino doing slow smooth moving (at exactly same hardware).  
Any ideas?

---

<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 18, 2017, 4:27pm UTC](https://forums.openmv.io/t/controlling-brushed-motors-with-pwm/341/2 "2017-07-18T16:27:05Z")

</div>

Ibrahim, can you help out on this?

---

<div class="post-metadata">

**Author:** ![iabdalkader](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/iabdalkader/32/1169_2.png) [@iabdalkader](https://forums.openmv.io/u/iabdalkader)\
**Post date:** [July 18, 2017, 5:10pm UTC](https://forums.openmv.io/t/controlling-brushed-motors-with-pwm/341/3 "2017-07-18T17:10:31Z")

</div>

Hi, yes I’ll debug this and get back to you.

---

<div class="post-metadata">

**Author:** ![sergeyzwezdin](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@sergeyzwezdin](https://forums.openmv.io/u/sergeyzwezdin)\
**Post date:** [July 19, 2017, 6:33am UTC](https://forums.openmv.io/t/controlling-brushed-motors-with-pwm/341/4 "2017-07-19T06:33:42Z")

</div>

Hi!  
Any luck with it yet?

---

<div class="post-metadata">

**Author:** ![melofon](https://avatars.discourse-cdn.com/v4/letter/m/b38774/32.png) [@melofon](https://forums.openmv.io/u/melofon)\
**Post date:** [July 19, 2017, 7:46am UTC](https://forums.openmv.io/t/controlling-brushed-motors-with-pwm/341/5 "2017-07-19T07:46:01Z")

</div>

I would suggest using machine module in micropython - [7. Pulse Width Modulation — MicroPython 1.19.1 documentation](https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/pwm.html). Haven’t had such experience yet, hope this thing works well with openMV. The plan is to test it maybe tomorrow (my robot is not ready yet).

---

<div class="post-metadata">

**Author:** ![iabdalkader](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/iabdalkader/32/1169_2.png) [@iabdalkader](https://forums.openmv.io/u/iabdalkader)\
**Post date:** [July 19, 2017, 1:52pm UTC](https://forums.openmv.io/t/controlling-brushed-motors-with-pwm/341/6 "2017-07-19T13:52:15Z")

</div>

Hi,

I tested your code with no changes (only added a loop after the code) it generates ~488MHz on pin P7 and P8 is high, and P9 is low:  
 ![wave.png](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/1X/24bdabd9d540559204bb69b80e274951b3d1ee08.png)  
I don’t have a motor to test, but it seems the cam is doing exactly what you want.

---

<div class="post-metadata">

**Author:** ![sergeyzwezdin](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@sergeyzwezdin](https://forums.openmv.io/u/sergeyzwezdin)\
**Post date:** [July 19, 2017, 3:34pm UTC](https://forums.openmv.io/t/controlling-brushed-motors-with-pwm/341/7 "2017-07-19T15:34:09Z")

</div>

> [@](#):
>
> I don’t have a motor to test, but it seems the cam is doing exactly what you want.

Yep, OpenMV works properly here. It seems problem with motor driver. I’ll investigate it a bit more.  
Thank you for your help and sorry for bothering.

---

<div class="post-metadata">

**Author:** ![sergeyzwezdin](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@sergeyzwezdin](https://forums.openmv.io/u/sergeyzwezdin)\
**Post date:** [July 19, 2017, 3:34pm UTC](https://forums.openmv.io/t/controlling-brushed-motors-with-pwm/341/8 "2017-07-19T15:34:56Z")

</div>

> [@](#):
>
> I would suggest using machine module in micropython - [7. Pulse Width Modulation — MicroPython latest documentation](https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/pwm.html). Haven’t had such experience yet, hope this thing works well with openMV. The plan is to test it maybe tomorrow (my robot is not ready yet).

“machine.PWM” is not available on OpenMV, so no way to use it directly.

---

<div class="post-metadata">

**Author:** ![deshipu](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/deshipu/32/8_2.png) [@deshipu](https://forums.openmv.io/u/deshipu)\
**Post date:** [July 27, 2017, 9:11pm UTC](https://forums.openmv.io/t/controlling-brushed-motors-with-pwm/341/9 "2017-07-27T21:11:53Z")

</div>

One possible difference is that Arduino UNO is 5V, whicle OpenMV is 3.3V — perhaps your motor driver expects 5V signal?

---

<div class="post-metadata">

**Author:** ![sergeyzwezdin](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@sergeyzwezdin](https://forums.openmv.io/u/sergeyzwezdin)\
**Post date:** [August 11, 2017, 5:55am UTC](https://forums.openmv.io/t/controlling-brushed-motors-with-pwm/341/10 "2017-08-11T05:55:24Z")

</div>

> [@](#):
>
> One possible difference is that Arduino UNO is 5V, whicle OpenMV is 3.3V — perhaps your motor driver expects 5V signal?

In my case the root casus was broken driver.
