View Course Path

Interfacing of Arduino with DC motor (single and multiple motors)

In this Arduino tutorial, we are going to learn how to control DC motors using an Arduino Uno. Additionally, we will have a complete review of the numerous methods to interface a DC motor/multiple DC motors with the Arduino Uno using L298N and L293D motor driver ICs. You can use the navigation table to scroll down to the code to interface DC motors with the Arduino. However, we would very much insist on you understanding the concept first. Let’s begin!

What are the components required for interfacing a DC motor with Arduino?

A man is as good as the tools he familiarizes himself with. So get yourself the following components, and we can begin assembling right away.

[geoip_detect2_hide_if country=”IN”] [/geoip_detect2_hide_if] [geoip_detect2_show_if country=”IN”] [/geoip_detect2_show_if]

Understanding the Arduino DC motor

Well, what is a DC motor? It is an electro-mechanical device that rotates when connected to a power source. If you open up any toy that has a component that turns, you’ll find a DC motor.

Alternatively, we can say that a DC motor is a rotatory electrical machine that converts electrical energy into mechanical energy. Whereas, a magnetic field usually produces the force that causes the “turning.”

There are a wide variety of DC motors available in the market. These motors are classified by their RPM (rotation per minute) value and their operating voltage/current ratings.

Let’s take a look at the different parts that make up a basic DC motor. Then we will indulge ourselves in its beautiful mechanism.

The relation between current flow and direction of rotation of a DC motor

When a current passes through the coil wound around a soft iron core, the side of the positive pole is acted upon by an upward force, while the other side is acted upon by a downward force. According to Fleming’s left-hand rule, the forces cause a turning effect on the coil, making it rotate. Therefore, to rotate the motor in a constant direction, “direct current” commutators make the current reverse in direction every half a cycle (in a two-pole motor), thus causing the motor to continue to rotate in the same direction.

The Pin description of an Arduino DC motor

Pin no Pin name Description
1 Terminal 1 Positive/Negative
2 Terminal 2 Positive/Negative

A normal DC motor has two terminals. Since these terminals must be connected through a coil. But they do not have any polarity. However, reversing the connection will only reverse the direction of the motor.

physical view of a DC motor with its terminals outlined Two terminals of the DC motor

Terminal 1-Positive & Terminal 2- Negative/Ground: Axis rotates in a clockwise direction.

Terminal 2-Positive & Terminal 1- Negative/Ground: Axis rotates in the anti-clockwise direction.

Dimensions of DC motor

Weight and dimensions of a DC motor
Weight and dimensions of a DC motor

Arduino Motor size:

  • Height with axis: 36.9 mm.
  • Height without axis: 27.5mm.
  • Body size: 27.5mm x 20mm x 15mm.
  • Shaft size: 8mm x 2mm diameter.
  • Axis diameter: 2mm.
  • Weight of DC motor: 17 grams.

Features of Arduino DC motor

  • Standard 130 type Dc motor
  • Operating voltage: 4.5V to 9V
  • Recommended Voltage: 6V
  • Current at No-load: 70mA (maximum)
  • No-load speed: 9000 rpm
  • Loaded current: 250mA (approximate)
  • Rated Load: 10g*cm

Understanding the H-bridge L298N IC

What is an H-bridge?

The H bridge controls the DC motor by allowing it to rotate in both directions. We know that the DC motor can turn in either direction, depending on the polarity of the current. However, if you wish to build a robot that goes backward and forward, you can’t always keep removing the battery. An H-bridge solves this problem.

Moreover, the L298N is a dual H-bridge motor driver. This means that it can control two DC motors at once. The module can drive DC motors that have voltages between 5 and 12V, with peak current up to 2A.

An H-bridge circuit
An H-bridge circuit. When S1 and S4 are closed, the motor will rotate in a clockwise direction. When S3 and S2 are closed, the motor will run in an anti-clockwise direction.

Circuit of H-bridge L298N IC

 

Working of H-bridge L298N motor driver

L298 truth table for motor control
L298N truth table for motor control

Pins of L298N IC

The module has two screw terminal blocks for the Arduino DC motor A and B, While another screw terminal block is for the Ground pin, the 12V VCC for motor, and a 5V pin, however, it can either be an input or output.

Pin diagram of L298 IC
Pin diagram of L298N IC

It has four digital input pins for Arduino motor control. In addition to digital pins, it also has two analog input pins for controlling the speed of the motor.

Ratings of Arduino motor controller – H-bridge L298N IC

  • Input voltage: up to 40 volts.
  • Output current: up to 3A
  • Power: 25W
  • Moreover, it has two built-in H-bridge, high voltage, large current, full-bridge driver, that can be used to drive DC motors, stepper motors, relay coils, and other inductive loads.
  • Using a standard logic level signal to control.
  • Furthermore, it can drive a two-phase stepper motor or four-phase stepper motor, and two-phase DC motors.
  • Above all, it can adopt a high-capacity filter capacitor yet a freewheeling diode that protects devices in the circuit from being damaged by the reverse current of an inductive load, enhancing reliability
  • Drive voltage: 5-35V
  • logic voltage: 5V

Dimensions of H-bridge L298N IC module:

PCB size: 4.2 x 4.2 cm

The circuit diagram to interface single DC motor with Arduino Uno using L298N for Arduino DC motor direction control

1 Dc motor interfacing using l298N

2 dc mtotr interfacing using l298N

Code for interfacing a single DC motor with an Arduino Uno using L298N

Though, the Arduino motor program uses the basic built-in functions such as digitalWrite() and analogWrite().

const int rpm = 2 ;

const int in1 = 3 ;

const int in2 = 4 ;

void setup()

{

pinMode(rpm,OUTPUT) ;

pinMode(in1,OUTPUT) ;

pinMode(in2,OUTPUT) ;

}




void loop()

{

digitalWrite(in1,HIGH) ;

digitalWrite(in2,LOW) ;

analogWrite(rpm,255) ;




/*setting PWM of the motor to 255

we can change the speed of rotation

by changing PWM input but we are only

using Arduino so we are using highest

value to driver the motor  */




//Clockwise for 3 secs

delay(3000) ;




//For brake

digitalWrite(in1,HIGH) ;

digitalWrite(in2,HIGH) ;

delay(1000) ;




//For Anti Clock-wise motion - IN1 = LOW , IN2 = HIGH

digitalWrite(in1,LOW) ;

digitalWrite(in2,HIGH) ;

delay(3000) ;




//For brake

digitalWrite(in1,HIGH) ;

digitalWrite(in2,HIGH) ;

delay(1000) ;

}


Understanding the H-bridge L293D motor driver

L293D motor driver circuit with all parts outlined L293D motor driver

Circuit of Arduino motor controller – H-bridge L293D IC

Circuit and internal diagram of L293D IC interfacing with dual DC motors Circuit and internal diagram of L293D IC interfacing with dual DC motors

Working of H-bridge L293D IC

Input 1 = HIGH(5v) Output 1 = HIGH Motor 1 rotates in Clock wise Direction
Input 2 = LOW(0v) Output 2 = LOW
Input 3 = HIGH(5v) Output 1 = HIGH Motor 2 rotates in Clock wise Direction
Input 4 = LOW(0v) Output 2 = LOW
Input 1 = LOW(0v) Output 1 = LOW Motor 1 rotates in Anti-Clock wise Direction
Input 2 = HIGH(5v) Output 2 = HIGH
Input 3 = LOW(0v) Output 1 = LOW Motor 2 rotates in Anti -Clock wise Direction
Input 4 = HIGH(5v) Output 2 = HIGH
Input 1 = HIGH(5v) Output 1 = HIGH Motor 1 stays still
Input 2 = HIGH(5v) Output 2 = HIGH
Input 3 = HIGH(5v) Output 1 = LOW Motor 2 stays still
Input 4 = HIGH(5v) Output 2 = HIGH

The IC LM293D consists of 4-input pins where pin2 and 7 on the left side of the IC and Pin 10 and 15 on the right side of the IC. Whereas, Left input pins on the IC will control the rotation of a motor. Hence, the motor is connected across the side and right input for the motor on the right-hand side.

Rotation control

When Pin-2= logic 1 & pin-7 = logic 0, then it rotates in clockwise direction.
Pin-2=logic 0 & Pin7=logic 1, then it rotates in anti clock direction
Pin-2= logic 0 & Pin7=logic 0, then it is idle (high impedance state)
Pin-2= logic 1 & Pin7=logic 1, then it is idle

Likewise, the motor can also operate across input pin-15 and pin-10 for the motor on the anti-clockwise direction.

Pins of H-bridge L293D IC

in Number Pin Name Description
1 Enable 1,2 This pin enables the input pin Input 1(2) and Input 2(7)
2 Input 1 Directly controls the Output 1 pin. Controlled by digital circuits
3 Output 1 Connected to one end of  Motor 1
4 Ground Ground pins are connected to ground of circuit (0V)
5 Ground Ground pins are connected to ground of circuit (0V)
6 Output 2 Connected to another end of  Motor 1
7 Input 2 Directly controls the Output 2 pin. Controlled by digital circuits
8 Vcc2 (Vs) Connected to Voltage pin for running motors (4.5V to 36V)
9 Enable 3,4 This pin enables the input pin Input 3(10) and Input 4(15)
10 Input 3 Directly controls the Output 3 pin. Controlled by digital circuits
11 Output 3 Connected to one end of Motor 2
12 Ground Ground pins are connected to ground of circuit (0V)
13 Ground Ground pins are connected to ground of circuit (0V)
14 Output 4 Connected to another end of Motor 2
15 Input 4 Directly controls the Output 4 pin. Controlled by digital circuits
16 Vcc2 (Vss) Connected to +5V to enable IC function

The module has two screw terminal blocks for motor A and B, while another screw terminal block for the Ground pin, the 12V VCC for motor, and a 5V pin. However, it can either be an input or output.

Whereas, it has four digital input pins for controlling the rotation motor as well as two analog input pins for controlling the speed of the motor.

Ratings of L293D IC

  • It can be used to run Two DC motors with the same IC.
  • Furthermore, Speed and Direction control is possible
  • Motor voltage Vcc2 (Vs): 4.5V to 36V
  • Moreover, the Maximum Peak motor current: 1.2A
  • Maximum Continuous Motor Current: 600mA
  • Supply Voltage to Vcc1(VSS): 4.5V to 7V
  • Transition time: 300ns (at 5Vand 24V)
  • Above all, Automatic Thermal shutdown is available
  • Available in 16-pin DIP, TSSOP, SOIC packages

The circuit diagram to interface multiple DC motors with Arduino Uno using L293D

Circuit diagram to connect Arduino with multiple DC motors using L293 motor driver shield Circuit diagram to connect Arduino with multiple DC motors using L293 motor driver shield

Code for controlling multiple DC motors with an Arduino Uno using L293D the the

Arduino motor code is quite simple because it doesn’t need any library files.

// connect motor controller pins to Arduino digital pins

// motor one

int enA = 10;

int in1 = 9;

int in2 = 8;

// motor two

int enB = 5;

int in3 = 7;

int in4 = 6;

void setup()

{

// set all the motor control pins to outputs

pinMode(enA, OUTPUT);

pinMode(enB, OUTPUT);

pinMode(in1, OUTPUT);

pinMode(in2, OUTPUT);

pinMode(in3, OUTPUT);

pinMode(in4, OUTPUT);

}

void demoOne()

{

// this function will run the motors in both directions at a fixed speed

// turn on motor A

digitalWrite(in1, HIGH);

digitalWrite(in2, LOW);

// set speed to 200 out of possible range 0~255

analogWrite(enA, 200);

// turn on motor B

digitalWrite(in3, HIGH);

digitalWrite(in4, LOW);

// set speed to 200 out of possible range 0~255

analogWrite(enB, 200);

delay(2000);

// now change motor directions

digitalWrite(in1, LOW);

digitalWrite(in2, HIGH);

digitalWrite(in3, LOW);

digitalWrite(in4, HIGH);

delay(2000);

// now turn off motors

digitalWrite(in1, LOW);

digitalWrite(in2, LOW);

digitalWrite(in3, LOW);

digitalWrite(in4, LOW);

}

void demoTwo()

{

// this function will run the motors across the range of possible speeds

// note that maximum speed is determined by the motor itself and the operating voltage

// the PWM values sent by analogWrite() are fractions of the maximum speed possible

// by your hardware

// turn on motors

digitalWrite(in1, LOW);

digitalWrite(in2, HIGH);

digitalWrite(in3, LOW);

digitalWrite(in4, HIGH);

// accelerate from zero to maximum speed

for (int i = 0; i < 256; i++)

{

analogWrite(enA, i);

analogWrite(enB, i);

delay(20);

}

// decelerate from maximum speed to zero

for (int i = 255; i >= 0; --i)

{

analogWrite(enA, i);

analogWrite(enB, i);

delay(20);

}

// now turn off motors

digitalWrite(in1, LOW);

digitalWrite(in2, LOW);

digitalWrite(in3, LOW);

digitalWrite(in4, LOW);

}

void loop()

{

demoOne();

delay(1000);

demoTwo();

delay(1000);

}

Common errors and troubleshooting that occurs while interfacing Arduino with a motor controller

Interfacing motor with Arduino can cause a few common errors. Hence, the module has an onboard 5V regulator, which is either enabled or disabled using a jumper. If the motor supply voltage is up to 12V, we can enable the 5V regulator, and the 5V pin can be used as an output, for example, for powering our Arduino Uno board.

However, you should disconnect the jumper if the motor that you are using has a voltage >12V. Because those voltages will cause damage to the onboard 5V regulator. In this case, the 5V pin will be used as input as we need to connect it to a 5V power supply for the IC to work properly. Only motors with input voltage range 5-12V and current 2A are controlled by Arduino using a motor controller because the voltage more than 12V cause damage to the IC in the motor controller. Moreover, this will also affect the Arduino board seriously.

The IC has a voltage drop of 2V. Therefore, we can’t get the full speed of the motor.

Don’t share the GND pin with both external battery and Arduino. Otherwise, it will power up the Arduino, and the battery will dry quicker.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.