Project tutorial
DC Motor Controlling Library

DC Motor Controlling Library © GPL3+

The DC motor controlling library is provided to control all types of DC motors especially for industrial motion control.

  • 2,634 views
  • 9 comments
  • 23 respects

Components and supplies

Apps and online services

About this project

Here I present Arduino library to control DC motor. The library is having so many facilities and flexibility that it can control any DC motor in required manner. The library is design as per the industrialmotion 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.7\libraries

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 betaken so that motor or circuit should not get damaged.

All 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, inttime_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 (nospeed control)

/*this program will rotate DC motor forward for 5 sec

then stop for 2 sec and again rotate motor reverse for

5 sec, stop for 2 sec continuously

created by A M Bhatt (+91-9998476150), Gujarat, INDIA

on 12/10/2016

*/

#include<DC_Motor.h>

DC_Motormotor(8, 12);

voidsetup()

{

}

void loop()

{

motor.forward();

delay(5000);

motor.stop_motor();

delay(2000);

motor.reverse();

delay(5000);

motor.stop_motor();

delay(2000);

}

2) Rotate DC motor forward and reverse at set speed (withspeed control)

/*this program will rotate DC motor forward for 5 sec

at 40%speed then stop for 2 sec and again rotate motor reverse for

5 sec at 80% speed continuously

created by A M Bhatt (+91-9998476150), Gujarat, INDIA

on 12/10/2016

*/

#include<DC_Motor.h>

DC_Motor motor2(9, 10, 1);

voidsetup()

{

}

void loop()

{

motor2.set_speed(40);

motor2.forward_with_set_speed();

delay(5000);

motor2.motor_speed_zero();

delay(2000);

motor2.set_speed(80);

motor2.reverse_with_set_speed();

delay(5000);

motor2.motor_speed_zero();

delay(2000);

}

3) Apply jog to motor in both direction with and withoutspeed control

/*this program applies jog to two different DC motors

* it applies jog to 1st motor in full speed in both direction

* and then provides jog to 2nd motor with 80%speed in reverse

* direction and 30% speed in forward direction

* created by Ashutosh M Bhatt(+91-9998476150), Gujarat (INDIA)

* on 22/10/2016

*/

#include<DC_Motor.h>

DC_Motor motor(8, 12);

DC_Motor motor2(9, 10, 1);

voidsetup()

{

}

voidloop()

{

motor.jogg_full_speed(1);

motor.jogg_full_speed(0);

motor2.jogg_set_speed(0, 80);

motor2.jogg_set_speed(1, 30);

}

4) Apply soft start and smooth stop to motor

/*this program will give soft start to motor and increase its speed

from 0 to 90% in 10 sec in forward direction

then it gives smooth stop from 90% to 0 in 5 sec

again soft start from 0 to 50% in 6 sec in reverse direction

and finally smooth stop from 50% to 0 in 3 sec

created by A M Bhatt (+91-9998476150), Gujarat, INDIA

on 12/10/2016

*/

#include<DC_Motor.h>

DC_Motor motor2(9, 10, 1);

voidsetup()

{

}

voidloop()

{

motor2.soft_start(1, 90, 10);

delay(3000);

motor2.smooth_stop(5);

motor2.soft_start(0, 50, 6);

delay(3000);

motor2.smooth_stop(3);

}

Code

DC Motor controlling LibraryC Header File
This library controls speed and direction of all types of DC motors
No preview (download only).

Schematics

DC Motor controller using Arduino with H-Bridge driver
Arduino UNO and H bridge driver used to control DC Motor
Circuit2 nmse6shntk

Comments

Similar projects you might like

Unipolar Stepper Motor Controlling Library

Project tutorial by ambhatt

  • 85 views
  • 0 comments
  • 1 respect

Tricks for Controlling DC Motors

Project tutorial by tolgadurudogan

  • 20,940 views
  • 6 comments
  • 74 respects

Control the Speed of Brushless DC Motor Using Bluetooth

Project tutorial by mohitmehlawat

  • 3,487 views
  • 0 comments
  • 13 respects

Temperature Sensor To control Servo Motor

by Jasleen

  • 6,407 views
  • 2 comments
  • 10 respects

Using Serial Monitor to Control Servo Motor

Project tutorial by Kub_Luk

  • 4,977 views
  • 10 comments
  • 32 respects

How to Use a L293D Chip with Arduino and a Motor

Project tutorial by Austin Detzel

  • 8,452 views
  • 2 comments
  • 13 respects
Add projectSign up / Login