More topics in Arduino Course for Beginners

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 Arduino Uno. Additionally, we will have a complete review of the methods to interface a DC motor/ multiple DC motors with Arduino using L298N and L293D motor drivers. We will also give you the schematics to use standalone L298N and L293D ICs to connect with the Arduino Uno. Finally, 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.

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.

A DC motor is a rotatory electrical machine that converts electrical energy into mechanical energy. Whereas, the force that causes the “turning” is usually produced by a magnetic field.

There are a wide variety of DC motors available in the market. Moreover, we classified this assortment of DC motors 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 DC motor. Then we will indulge ourselves in its beautiful mechanism.

What are the components of the DC motor?

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 make the motor rotate 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.

Pin description of Arduino DC motor

Pin noPin nameDescription
1Terminal 1Positive/Negative
2Terminal 2Positive/Negative

Whereas, A normal DC motor would have two terminals. Since these terminals must be connected together 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. Whereas, An H-bridge solves this problem.

Moreover, the L298N is a dual H-bridge motor driver. It is used for Arduino DC motor direction control. Above all, It can control the speed and rotation direction of two DC motors at the time. Hence, 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

Circuit of H-bridge L298N IC

Working of H-bridge L298N motor driver

S1S2S3S4Operation1001Motor moves right0110Motor moves left0000Motor free runs0101Motor brakes1010Motor brakes1100Short power supply0011Short power supply1111Short power supply

[/su_table]

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

After all, 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 is able to 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 = HIGHMotor 1 rotates in Clock wise Direction
Input 2 = LOW(0v)Output 2 = LOW
Input 3 = HIGH(5v)Output 1 = HIGHMotor 2 rotates in Clock wise Direction
Input 4 = LOW(0v)Output 2 = LOW
Input 1 = LOW(0v)Output 1 = LOWMotor 1 rotates in Anti-Clock wise Direction
Input 2 = HIGH(5v)Output 2 = HIGH
Input 3 = LOW(0v)Output 1 = LOWMotor 2 rotates in Anti -Clock wise Direction
Input 4 = HIGH(5v)Output 2 = HIGH
Input 1 = HIGH(5v)Output 1 = HIGHMotor 1 stays still
Input 2 = HIGH(5v)Output 2 = HIGH
Input 3 = HIGH(5v)Output 1 = LOWMotor 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 NumberPin NameDescription
1Enable 1,2This pin enables the input pin Input 1(2) and Input 2(7)
2Input 1Directly controls the Output 1 pin. Controlled by digital circuits
3Output 1Connected to one end of  Motor 1
4GroundGround pins are connected to ground of circuit (0V)
5GroundGround pins are connected to ground of circuit (0V)
6Output 2Connected to another end of  Motor 1
7Input 2Directly controls the Output 2 pin. Controlled by digital circuits
8Vcc2 (Vs)Connected to Voltage pin for running motors (4.5V to 36V)
9Enable 3,4This pin enables the input pin Input 3(10) and Input 4(15)
10Input 3Directly controls the Output 3 pin. Controlled by digital circuits
11Output 3Connected to one end of Motor 2
12GroundGround pins are connected to ground of circuit (0V)
13GroundGround pins are connected to ground of circuit (0V)
14Output 4Connected to another end of Motor 2
15Input 4Directly controls the Output 4 pin. Controlled by digital circuits
16Vcc2 (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. The Arduino motor program is here,

// 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 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, in the meantime, it 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.


About The Writer

CLOSE

Leave a Reply

Your email address will not be published. Required fields are marked *