# How to interface DC motor with Arduino IDE

This tutorial discusses what are DC motors, how to control them, and how to interface them with Arduino IDE using motor control units in evive with the help of an Arduino IDE sketch.
Tutorial Info

## Components Required

Image Component Quantity Available in Kit
evive 1
USB A-B Cable 1
Dual Shaft BO Motor 1

## Introduction

The motors that run on direct current are termed as DC motors. Current that constantly flows in one direction is called direct current(DC).

There are two characteristics of DC motor that can be controlled:

1. Direction: We can control the direction of rotation of the motor by reversing the direction of voltage applied at its terminal. For that a motor driver is needed. However evive has an in-built motor driver hence there is no need of extra circuit of motor driver.
2. Speed: We can control the voltage of the motor by varying the supply voltage to it.

evive has two inbuilt motor control units with which you can control motors having current limit of 1A each. It uses a SN754410NE Quad H-Bridge IC to control two DC Motors. In this example 12V BO shaft dc motors are used.

Each motor on evive is controlled by a set of two digital and one pwm pin. For a motor at M1, direction of rotation is controlled by states of digital pin 28 (MOTOR1_D1) and 29(MOTOR1_D2) whereas speed is controlled by pwm pin 44 (MOTOR1_EN) on evive. Similarly for a motor at M2 digital pin 30 (MOTOR2_D2) and 31 (MOTOR2_D1) are for direction and pwm pin 45 (MOTOR2_EN) is for controlling speed.

You can determine the state of the motor by looking at the states of pins mentioned for direction:

MOTORx_D1 MOTORx_D2State
LOWLOWMotor is free
LOWHIGHMotor rotates in one direction
HIGHLOWMotor rotates in another direction
HIGHHIGHMotor is stalled

Below is a list of function that are to be used on evive for controlling direction and speed of motors.

## Functions

FunctionComment
Motor();Constructor
Motor(int Dir1,int Dir2,int Pwm);Constructor with attachments of pins
void attachMotor(int Dir1,int Dir2,int Pwm);Attachments of pins
void moveMotor(int Pwm);Positive for CW and negative for CCW
void moveMotor(int Dir1,int Dir2,int Pwm);dir1 and dir2 can be 1 or 0,pwm can only be positive for CW
void stopMotor();By default stop motor will lock motor
void lockMotor();To lock the motor
void freeMotor();Free the motor
void setMeanSpeed(int Speed);Sets the mean speed with which motor moves when speed=100%
void setMotorSpeed(int Speed);Positive for CW and negative for CCW. Speed in percentage of mean speed
void setMotorSpeed(int Dir1,int Dir2,int Speed);dir1 and dir2 can be 1 or 0
void changePWM(int Pwm);Just to change the PWM in whatever direction the motor was moving
void changeSpeed(int Speed);Just to change the speed (In percentage) not the direction
int getDirection();+1 for CW and -1 for CCW and 0 for free or locked
int isLocked();+1 for locked and 0 for not locked
int getSpeed();Returns speed in % of mean speed
int getPWM();Returns positive for CW and negative for CCW.
void startSmoothly(int Speed);Positive for CW and negative for CCW.
void stopSmoothly();Stops motor smoothly.

There is a dedicated motor driver unit present on evive that uses direction and pwm pins mentioned above for controlling motors.

The maximum voltage that a motor gets on evive is the voltage value that is applied to motor driver unit of evive. Motor driver unit can be powered through VVR as well as VSS supply of evive. To check which power unit of evive powers the driver, check the location of jumper JP1 on evive. If it is on VVR side then driver circuit gets the voltage value set by VVR(Variable Voltage) knob of evive. If it is on VSS side, the voltage supply to driver circuit is same as supply given to evive. This means if we power evive by a 12V adapter then drivier circuit gets 12V given that JP1 jumper in on VSS side. And if JP1 is on VVR side then supply to driver circuit will be any value in range of 0V to 12V depending upon position of knob VVR on evive.

Always keep in mind the maximum supply ratings of your motor, according to that only you should be select power source .If you are using motors used in this example then DO NOT exceed VVR voltage over 12V because this may damage your motor.

## Example

In this example, we will be controlling a DC motor through channel M1. Below is the sequence of events:

1. Motor rotates for 1 second in clockwise direction with 150 PWM.
2. Motor stops for 1 second.
3. Motor rotates for 1 second in counter-clockwise direction with 150 PWM.
4. Motor free for 1 second.
5. Repeat.

Below is the Arduino IDE sketch:

In the above example we rotated motor in different directions at constant PWM now we will vary the PWM applied to motor with help of potentiometer to see the change in speed of motor.

OUTPUT

## SN754410NE:

If you look at the SN754410NE chip, you will notice a u-shaped notch at one end.  This helps you to identify pin 1 of the IC.

Pin -DescriptionPin-Description
1-Enable 1
2-1A
3-1Y
4-GND
5-GND
6-2Y
7-2A
8-Vcc2
16-Vcc(5V)
15-4A
14-4Y
13-GND
12-GND
11-3Y
10-3A
9-Enable 2
• Pin 4, 5, 12, 13 are connected to ground.
• In the image of motor driver numbers shown in blue box determines the digital pins on evive to which motor driver is connected.
• Pin 2,7,10 and 15 are input pins of motor driver, when logic 1 (5v) signal is applied to them then outputs 3,6,11 and 14 respectively also show an output voltage.
• The output voltage available at output pins is depended on the voltage applied to Enable 1 and Enable 2 pin.
• Enable 1 and Enable 2 pins are given a PWM signal from evive. Depending upon the value of PWM signal applied the voltage supplied at Vcc2 pin of the IC is mapped, and given as an output on output pins of driver IC.
• Enable 1 controls the voltage value that will appear on pin 3 and pin 6 of IC when signal is given to pin 2 and pin 7 respectively.
• Similarly Enable 2 controls the voltage value that will appear on pin 14 and pin 10 when signal is applied to pin 15 and pin 9 respectively.Hence these factors decide the supply voltage at motors connected to evive.

Consider one example if Enable 1 is given an analog input of 2.5V from PWM pin of evive and an input signal is given on input pins 2 and 7 of IC then the voltage that appears across the output pins 3 and 6 respectively will be half of the voltage applied at Vcc2 i.e Vcc2/2  on driver IC. Same will be case for Enable 2 and remaining input and output pins of the IC.

[tutorialRelatedProjects]