# Motor

OpenMV has IO port to drive all kinds of electrical motor.

## Stepping motor

### What is stepping motor

Stepping motor is open-loop control eletrical motor that can turn electric
impulse signal into angular displacement or linear displacement,which is the
main power element in the modern digital procedure control system,whose
application is extremely extensive.

Like 3D printer and mechanical arms.

### Like 3D printer and mechanical arms

Many chips and modules can be used to control stepping motor.We use a simple
module EasyDriver.

To be simple,control direction through dir,give an impulse to step,the motor
will move.

![](/assets/06-07-001.jpg)

### Wiring

![](/assets/06-07-002.jpg)

## Install module

[Module code is in this
link](http://book.openmv.cc/module/write-module-motor.html)

Save the code of this class as stepper.py file and put it in the root directory
of OpenMV.

## Code

```python
# 步进电机控制 - By: 小智智- 周日 4月 2 2017

from pyb import Pin

from stepper import Stepper

import time

step_pin = Pin('P0')
dir_pin = Pin('P1')

my_stepper = Stepper(step_pin,dir_pin)

print("relative angle move test begin:", my_stepper.read_pos())
my_stepper.rel_angle(180)#相对角度控制
print("relative angle move test end:", my_stepper.read_pos())

time.sleep_ms(1000)

print("steps angle move test end:", my_stepper.read_pos())
my_stepper.steps(-400)#步进脉冲控制
print("steps angle move test end:", my_stepper.read_pos())

time.sleep_ms(1000)

print("absolute angle move test begin:", my_stepper.read_pos())
my_stepper.abs_angle(0)#绝对角度控制
print("absolute angle move test end:", my_stepper.read_pos())

time.sleep_ms(1000)
```

## DC Motor

#### Tutorial 26 - Motor Extension Version Controlling DC Motor: [https://singtown.com/video](https://singtown.com/video)

### What is DC Motor

The rotation speed of DC motor is in direct proportion to voltage.

### Driving module

Numerous driving module can drive DC motor,here we use TB6612FNG，double H shape
bridge.

![](/assets/06-07-003.jpg)

### Wiring

![](/assets/06-07-004.jpg)

### Code

```python
待添加
```

## Servo

OpenMV2 has two pin for servo，P7,P8

OpenMV3 has three pin for servo，P7,P8,P9

### Wiring

![](/assets/06-07-005.jpg)

### Check power

Servo is a high current device and must use an external power supply (voltage
stabilizer, lithium battery...). USB cannot power the servo!

### Code

```python
# Servo Control Example
#
# This example shows how to use your OpenMV Cam to control servos.

import time
from pyb import Servo

s1 = Servo(1) # P7
s2 = Servo(2) # P8
#s3 = Servo(3) # P9 Only for OpenMV3 M7
while(True):
    for i in range(-90,90):
        s1.angle(i)
        s2.angle(i)
        time.sleep_ms(10)
    for i in range(90,-90):
        s1.angle(i)
        s2.angle(i)
        time.sleep_ms(10)
```

## PCA9685 connects multiple servos

Product link:
[https://singtown.com/product/49277](https://singtown.com/product/49277)

#### Video tutorial 25 - pca9685 controlling multiple servos: [https://singtown.com/learn/50057/](https://singtown.com/learn/50057/)



The PCA9685 module uses only 2 pins (I2C protocol) to control 16 PWM channels,
that is, 16 servos or 16 LED lights. You can even connect 62 modules, still
using only 2 pins, and can control 992 PWMs!

Wiring

![](/assets/06-07-006-01.jpg)

Code

![](/assets/06-07-007.jpg)

Click Run

