• Usha Saha

Interfacing DC motor with Arduino

Updated: Dec 7, 2020

A DC motor is an electric motor that converts direct electrical current energy into mechanical energy, mostly all DC motor works on the same mechanism for periodically changing the direction of current in the motor. Through this article one can learn how to interface a DC #l293d DC motor with #Arduino, along with the coding and circuit diagram.


Hardware List

The image shows the list of components needed. Let's have a quick look at each of them.


List of components
List of components

What is a Bread Board?

A breadboard is a rectangular board with small holes in it that are used for connecting elements. The connections are not fixed and can be varied. In a breadboard, all the holes in one horizontal line are in series connection with each other. Breadboards are made from plastic and come in all shapes, sizes, and even different colors, the most common sizes are "full-size," "half-size," and "mini" breadboards.


BreadBoard
BreadBoard

Buy a Breadboard:- https://amzn.to/3lsvU6x


What is an Arduino Board?

Arduino Board can address inputs such as light on a sensor, Twitter information, a finger on a button, and turn it to an output such as initiating a motor, turning on an LED, advertising something online. All of this can be done by transferring a set of instructions to the microcontroller on the board by the use of the Arduino programming language.


ARDUINO
ARDUINO

Wanna buy an Arduino here's the link:- https://amzn.to/3iCbCFW


l293d motor driver:-The Pin Diagram of an l293d DC motor is as shown. The VSS is where the power supply is given. IN4 and IN3 will be used for direction control. OUT4 and OUT3 will be connected to the DC motor. The 12 and 13 GND pin will be grounded. Enable (EN) is used to control the speed of the DC motor.


l293d DC motor pin diagram
l293d DC motor pin diagram

Buy DC motor:- https://amzn.to/36NPpCx


Jumper wires:- Jumper wires are used to establish a connection between different elements in the circuit and the Arduino board.



Jumper Wires
Jumper Wires


Buy jumper wires:- https://amzn.to/3jGvoBm


Circuit Diagram for dc motor

The circuit connections are as follows, The power terminal that is VSS is connected to the positive terminal of the breadboard which is further connected to the 5v of the Arduino board. The input 4 (IN4) is connected to the 4th pin of the Arduino chip, while the output 4 (OUT4) pin is connected to the DC motor. Both the ground pins are grounded on the negative terminal. Output 3 (OUT3) is again connected to the DC motor. Input 3 (IN3) is connected to pin 3 of the Arduino board. The enable (EN2) is connected to pin number 2 of the Adruino board.


There is an external power supply used because Arduino alone cannot handle the DC motor. The positive terminal of the power supply is connected to the positive of the breadboard which further is connected to the VS of the DC motor for extra power. The negative terminal of the power supply is connected to the ground of the breadboard

Circuit Connections
Circuit Connections

Code for interfacing dc motor with Arduino


The codes applied to the Arduino chip is as follows;

const int speedPin=2;

Pin2 is assigned the speed pin.

const int direction1=3; //Anticlockwise
const int direction2=4;  //Clockwise

Pin 3 and 4 are assigned the direction pins.

const int speed=220;

A constant speed of 220 is set. One can try different speeds for more clarity.

void setup()
  
{
  pinMode(speedPin, OUTPUT);
  pinMode(direction1, OUTPUT);
  pinMode(direction2, OUTPUT);

In the first void setup, under the pin mode pin 2,3, and 4 are made the output pins.

void loop()
{
//Anticlockwise
digitalWrite(direction1,HIGH);
digitalWrite(direction2, LOW);
analogWrite(speedPin,speed);

In the next void setup, the codes to rotate the fan in an #anticlockwise direction are written. Here the direction1 is made high and direction2 is made low because when one direction is higher than the other the fan rotates in the higher direction.

delay(2000);

After the completion of one cycle of anticlockwise rotation, a delay of 2sec is introduced.

//Clockwise
 digitalWrite(direction1,LOW);
  digitalWrite(direction2, HIGH);
  analogWrite(speedPin,speed);

Now the next set of codes is to rotate the fan in a #clockwise direction, here direction1 is assigned low and direction2 is assigned high. This time the fan rotates in a clockwise direction.

delay(2000);

Again a delay of 2sec is introduced. The cycle keeps on repeating.


Complete code

const int speedPin=2;
const int direction1=3; //Anticlockwise
const int direction2=4;  //Clockwise
const int speed=220;

void setup()
  
{
  pinMode(speedPin, OUTPUT);
  pinMode(direction1, OUTPUT);
  pinMode(direction2, OUTPUT);
  Serial.begin(9600);
}
void loop()
{
//Anticlockwise
digitalWrite(direction1,HIGH);
digitalWrite(direction2, LOW);
analogWrite(speedPin,speed);
delay(2000);
//Clockwise
 digitalWrite(direction1,LOW);
  digitalWrite(direction2, HIGH);
  analogWrite(speedPin,speed);
  delay(2000);
  } 

Download the codes from here.

Interfacing DC motor(Text file)1
.txt
TXT • 551B

DC motor working

The working of DC Motor is quite simple, As the code is applied to the Arduino chip the fan starts to rotate in an anticlockwise direction first then after a pause of 2 sec it will rotate in a clockwise direction. This process will continue. The below image shows the picture of the circuit when the code is not applied.

Without code insertion image
Without code insertion image

The view of the circuit when the code is applied.

Image with codes applied
Image with codes applied

Video explanation with the practical implementation, for a more beneficial perception.


Video by:- Kaushik Ram Selvaraj


The interfacing gives an overview of how to interface a DC motor with Arduino and use it to rotate a fan. Similarly, numerous other applications can be designed for more knowledge purposes. Small DC motors find applications in tools, gadgets, and appliances. Larger DC motors are currently employed in the propulsion of electric vehicles, elevators, hoists, etc.



See Also

INTERFACING SERVO MOTOR WITH ARDUINO

Interfacing Sound Sensor with Arduino

Interfacing Relay Module with Arduino

126 views