Powering 1 Servo and 2 DC Motors

I would like to power 1 Servo and 2 DC Motors using the OpenMV. I connected a 3.7V battery.

Using the motor-shield-pwm.py example, I can power 2 DC motors. Using the servo-control.py example, I can power 1 servo motor on pin 9. According to the documentation, the motor shield does not use pin 9.

However, when I insert the following code to the top of motor-shield-pwm.py…

from pyb import Servo
s3 = Servo(3) # P9
s3.pulse_width(1000)

… the servo doesn’t move. It seems like the OpenMV is unable to configure both the Servo and the Motor Shield, despite the fact that they should be using completely separate pins. Please advise.

The OpenMV Cam H7 Plus does not have Servo 3. Only the H7 regular does. Which version are you using?

I am using the OpenMV H7 Plus. I also have the regular H7, but my application requires the higher resolution of the H7 Plus.

Okay, then there’s no servo 3 on the plus.

This is not something we can fix. We had to use the I/O pin for that for SDRAM. No timer PWM outputs were left for the servo.

Ok, thanks for the reply. I think I’ll just use Servo 3 on the regular H7. It should still be ok.

Hi there,

I have similiar configuration: one DC motor which I control using motor shield and one servo (sg90) controlling using regular servo 3 pin, but I own regular H7 cam.

Anyway here’s the code:

import time
from pyb import Servo
from tb6612 import Motor

#s1 = Servo(1) # P7
#s2 = Servo(2) # P8
s3 = Servo(3) # P9
m1 = Motor(1) # motor 1: A0 and A1

while(True):
    s3.angle(0)
    time.sleep_ms(2000)
    s3.angle(90)
    time.sleep_ms(2000)
    s3.angle(120)
    time.sleep_ms(2000)
    m1.set_speed(50) # Forward
    time.sleep_ms(3000)
    m1.set_speed(0) # Forward
    time.sleep_ms(3000)

When I comment code for DC motor then servo will work without problem. What could be the problem?

Can motor shield and 3-wire hobby servo work in the same time? I forgot to mention that I use 24V DC DSE38BE27-001 (https://www.majju.com/product/dse38be27-001-dc-servo-motor-24v/)motor. Servo, dc and OpenMV share same ground. Everthing is powered using 5V 2A adapter.

Hi, the motor driver shield uses a timer that would power the servo to do PWM for driving the motor.

So, these don’t work at the same time. You can however not use the library and directly control the motor yourself using the basic Timer calls: class Timer – control internal timers — MicroPython 1.15 documentation

Hi, thanks for your answer, but is there any example for controlling dc motor using Timers?

Yes, please see the board control examples → PWM.

Hi,

I tried with the following code, but sometime it work and sometimes not:

import time
from pyb import Pin, Timer
tim = Timer(4, freq=1000)
tim2 = Timer(4, freq=50)
ch1 = tim.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width_percent=100)
ch3 = tim2.channel(3, Timer.PWM, pin=Pin("P9"), pulse_width_percent=50)
while (True):
      ch3.pulse_width(640)
      time.sleep_ms(2000)
      ch3.pulse_width(1500)
      time.sleep_ms(2000)
      ch3.pulse_width(2420)
      time.sleep_ms(2000)

I tried with different timer id’s, but with no luck.

Hi, just create one Timer object. That’s what sets the PWM freq.

Then the channel objects set PWM output percentages.

I needed to power high-current dc motors, so it worked using h-bridge motor driver example code. Thanks again.