Here I present Arduino library to control DC motor. The library is having so many facilities and flexibilities that it can control any DC motor in required manner. The library is designed as per the industrial motion control requirements. The library is having 16 different functionalities such that it can control the motor in most versatile manner. Here is the list of facilities in library.
1. It controls direction of rotation of motor
2. It controls speed of motor from 0-100%
3. It can provide soft start to motor in specified time – means motor speed will increase from 0% to desire level in specified time
4. It can provide smooth stop to motor in specified time – means motor speed will decrease from current speed to 0% in specified time
5. It can apply jog control to motor in either direction – means motor will jerk in specified direction
6. It can apply DC BREAK (means immediate or emergency STOP) to motor
The brief descriptions of all library functions are given here. Some examples are given afterwards that explains how motor is controlled using this library. There are three videos given that shows the demonstration of these examples. At last, the circuit is suggested that uses H-Bridge circuit that is widely used to control DC motors.
To use this library in your arduino sketch just copy the DC_Motor folder into the root directory of arduino library folder like C:arduino-1.6.7libraries.
Description of library functions
1. DC_Motor(int pin1,int pin2): this function declares arduino pins that drives DC motor
2. DC_Motor(int pin1,int pin2,int speed_flag): this function declares analog output pins of arduino that drives DC motor
3. start_motor(int dir): this function starts rotating motor in specified direction. If direction is 1 – motor will start rotating forward and vice versa
4. forward(): this function will start rotating motor in forward direction
5. reverse(): this function will start rotating motor in reverse direction
6. dc_break(): this function will instantly stop rotating motor*
7. stop_motor(): this function will stop rotating motor
8. jogg_full_speed(int dir): this function will apply jerk to motor for 5 sec at full speed in required direction
*Note: – this function will only work if DC motor has internal arrangements for such DC break. Also, proper motor driver circuit has to be design with dynamic braking resistances (DBR) and complete care has to be taken so that motor or circuit should not get damagedAll above functions provide simple control to DC motor. They do not control speed of motor. The next 8 functions controls speed as well as direction of motor. But it is required to select analog output pins of arduino to use these functions. The DC motor has to be initialized with 2nd function along with speed flag set to 1.
9. set_speed(int speed): this function will set DC motor speed between 0 to 100%
10. forward_with_set_speed(): this function will start rotating DC motor forward at set speed
11. reverse_with_set_speed(): this function will start rotating DC motor reverse at set speed
12. run_motor(int dir, int speed): this function will rotate DC motor in either direction at set speed
13. jogg_set_speed(int dir, int speed): this function will apply jerk to motor in either direction at set speed
14. motor_speed_zero()*: the function will reduce motor speed to 0 – means stop the motor
15. soft_start(int dir,int speed,int time_in_sec): this function will increase the speed of motor from 0 to desire level in specified time in either direction. The time has to selected in seconds
16. smooth_stop(int time_in_sec): this function will reduce the motor speed from current running speed to 0 in specified time. The time has to selected in seconds
*Note: one cannot use stop_motor() function here because it gives digital output while this function gives minimum analog output to make DC motor speed zero. When controlling DC motor with speed, don’t just stop motor but make its speed zero.
EXAMPLES
1) Rotate DC motor forward and reverse at full speed (no speed control)
*/
2) Rotate DC motor forward and reverse at set speed (with speed control)
3) Apply jog to motor in both direction with and without speed control
4) Apply soft start and smooth stop to motor
The schematic of the suggested circuits can be found in the circuit diagram section. These circuits demonstrate how to rotate DC motor using a simple H-Bridge motor driver. The first circuit controls just direction of motor, while the second one controls the direction as well as the speed.
These circuits are for small, low rating DC motors. For high power DC motor (with high current and high voltage) one can use H-Bridge motor driver chips L293, L293D or L298. For even more high power DC motors upto 100 V and 5 – 10A DC motors, one can use Darlington transistors like TIP122, TIP127, TIP142 etc or power MOSFETs of IRF series. Here two more motor driver circuits are given to drive higher rating DC motors using Darlington transistors (TIP) and power MOSFET (IRF).
Fig. 1: Circuit Diagram of BJT based H-Bridge for DC Motor Speed Control
Fig. 2: Circuit Diagram of MOSFET based H-Bridge for DC Motor Speed Control
The given library and example programs along with above circuit are tested with 12V, 100 RPM DC gear motor. See here the snap of circuit arrangements.
Fig. 3: Prototype of Arduino based DC Motor Controller
Just go through the provided video for demonstration.
DC motor library for Arduino
This library is used to control DC motors. It can:
· control its speed from 0 to 100% and its direction
· rotate motor forward and reverse at set speed
· start or stop the motor as well as provides DC BREAK for instant STOP
One has to select arduino pins for the motor then start rotating motor using given library functions
*/
#ifndef DC_Motor_h
#define DC_Motor_h
#include “Arduino.h”
class DC_Motor
{
private:
int pin_1,pin_2,motor_speed,dir_flag;
public:
DC_Motor(int pin1,int pin2);
DC_Motor(int pin1,int pin2,int speed_flag);
void forward(void);
void forward_with_set_speed(void);
void reverse(void);
void reverse_with_set_speed(void);
void run_motor(int dir,int speed);
void set_speed(int speed);
void start_motor(int dir);
void jogg_full_speed(int dir);
void stop_motor(void);
void dc_break(void);
void motor_speed_zero(void);
void soft_start(int dir,int speed,int time_int_sec);
void smooth_stop(int time_int_sec);
void jogg_set_speed(int dir,int speed);
};
#endif
Project Source Code
###
/* DC motor library for Arduino this library is used to control DC motors it can control its speed from 0 to 100% and its direction it can rotate motor forward and reverse at set speed it can start or stop the motor as well as provides DC BREAK for instant STOP it can provide soft start to motor - means the speed of motor will gradually increase from minimum to the desire level also it can provide smooth stop to motor - means the speed of motor will gradually decrease from the current speed to minimum just one has to select arduino pins for motor, then then start rotating motor using given library functions */ #include "Arduino.h" #include "DC_Motor.h" DC_Motor::DC_Motor(int pin1,int pin2) // this is a constructor { pinMode(pin1,OUTPUT); // this will create an instance of DC_Motor pinMode(pin2,OUTPUT); // in the arduino sketch with motor driver pins digitalWrite(pin1,LOW); digitalWrite(pin2,LOW); pin_1 = pin1; pin_2 = pin2; } DC_Motor::DC_Motor(int pin1,int pin2,int speed_flag) // this is a another constructor { // this will create an instance of DC_Motor if(speed_flag==1) // in the arduino sketch with motor driver pins with analog output only { pin_1 = pin1; pin_2 = pin2; } } /////////////////////////// this function will start rotating motor in either direction ////////////////////////////// void DC_Motor::start_motor(int dir) { if(dir==1) //if direction is set to 1 it will rotate forward { digitalWrite(pin_1,HIGH); digitalWrite(pin_2,LOW); } else // else for 0 it will rotate reverse { digitalWrite(pin_1,LOW); digitalWrite(pin_2,HIGH); } } /////////////////////// this function will jerk the motor in either direction /////////////////////////////////////// void DC_Motor::jogg_full_speed(int dir) { if(dir==1) { digitalWrite(pin_1,HIGH); digitalWrite(pin_2,LOW); delay(5000); digitalWrite(pin_1,LOW); } else { digitalWrite(pin_1,LOW); digitalWrite(pin_2,HIGH); delay(5000); digitalWrite(pin_2,LOW); } } /////////////////// this function will start rotating motor forward /////////////////////////////////////////////////// void DC_Motor::forward() { digitalWrite(pin_1,HIGH); digitalWrite(pin_2,LOW); } /////////////////////////////// // this function will start rotating motor reverse //////////////////////////////////// void DC_Motor::reverse() { digitalWrite(pin_1,LOW); digitalWrite(pin_2,HIGH); } //////////////////////////////////// this function will apply DC break to motor /////////////////////////////////////// void DC_Motor::dc_break() { digitalWrite(pin_1,HIGH); digitalWrite(pin_2,HIGH); } ////////////////////////////////// // this function will stop the motor ////////////////////////////////////////////// void DC_Motor::stop_motor() { digitalWrite(pin_1,LOW); digitalWrite(pin_2,LOW); } //////////////////////////////////// // this function will set motor speed between 0 - 100% ///////////////////////// void DC_Motor::set_speed(int speed) { motor_speed = speed; motor_speed = map(motor_speed,0,100,0,255); } ///////////////////// this function will start rotating motor forward at set speed //////////////////////////////////// void DC_Motor::forward_with_set_speed() { analogWrite(pin_1,motor_speed); analogWrite(pin_2,0); } ///////////////////////// // this function will start rotating motor reverse at set speed //////////////////////////// void DC_Motor::reverse_with_set_speed() { analogWrite(pin_2,motor_speed); analogWrite(pin_1,0); } /////////////////////// // this function will run motor forward or reverse at set speed ////////////////////////////// void DC_Motor::run_motor(int dir, int speed) { speed = map(speed,0,100,0,255); if(dir==1) { analogWrite(pin_1,speed); analogWrite(pin_2,0); } else { analogWrite(pin_2,speed); analogWrite(pin_1,0); } } /////////////////////////// this function will apply jerk to motor in either direction at set speed ///////////////////// void DC_Motor::jogg_set_speed(int dir, int speed) { speed = map(speed,0,100,0,255); if(dir==1) { analogWrite(pin_1,speed); analogWrite(pin_2,0); delay(5000); analogWrite(pin_1,0); } else { analogWrite(pin_2,speed); analogWrite(pin_1,0); delay(5000); analogWrite(pin_2,0); } } //////////////////////// this function will reduce motor speed to zero ///////////////////////////////////// void DC_Motor::motor_speed_zero() { analogWrite(pin_1,0); analogWrite(pin_2,0); } //////////////// this function will provide soft start to motor at desire speed in specified time /////////////// void DC_Motor::soft_start(int dir,int speed,int time_in_sec) { int s,dly; speed = map(speed,0,100,0,255); motor_speed = speed; dly = time_in_sec*1000/motor_speed; if(dir==1) { dir_flag=1; analogWrite(pin_2,0); for(s=0;s<speed;s++) { analogWrite(pin_1,s); delay(dly); } } else { dir_flag=0; analogWrite(pin_1,0); for(s=0;s<speed;s++) { analogWrite(pin_2,s); delay(dly); } } } ///////////////////////// this function will provide smooth stop to motor in specified time void DC_Motor::smooth_stop(int time_in_sec) { int s,d,dly; dly = time_in_sec*1000/motor_speed; if(dir_flag==1) { analogWrite(pin_2,0); for(s=motor_speed;s>0;s--) { analogWrite(pin_1,s); delay(dly); } } else { analogWrite(pin_1,0); for(s=motor_speed;s>0;s--) { analogWrite(pin_2,s); delay(dly); } } }
###
Circuit Diagrams
Project Video
Filed Under: Electronic Projects
Filed Under: Electronic Projects
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.