How to interface DC motor with Arduino IDE
About This Tutorial
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:
- 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.
- 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:
|LOW||LOW||Motor is free|
|LOW||HIGH||Motor rotates in one direction|
|HIGH||LOW||Motor rotates in another direction|
|HIGH||HIGH||Motor is stalled|
Below is a list of function that are to be used on evive for controlling direction and speed of motors.
|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 isFree();||+1 for free and 0 for not free|
|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.
In this example, we will be controlling a DC motor through channel M1. Below is the sequence of events:
- Motor rotates for 1 second in clockwise direction with 150 PWM.
- Motor stops for 1 second.
- Motor rotates for 1 second in counter-clockwise direction with 150 PWM.
- Motor free for 1 second.
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.
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 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.
How to use evive’s Joystick (Navigation Key)
This Arduino joystick tutorial discusses how to work with evive's 5-way navigation key, how to use Arduino controller, Arduino joystick library and how to read its state and display on the TFT screen with the help of an Arduino IDE sketch.
How to interface Servo Motor with Arduino IDE
This Arduino servo program tutorial discusses what are servo motors and how to interface them with Arduino using motor control in evive with the help of an Arduino sketch( Arduino servo code ). Servo motor Arduino code is also given in The content. This tutorial can also help in Building Arduino servo control projects.