Motors

My Blog

Latest blog
Previously we learned how to control DC motors using arduino. We used a motor driver IC named L293D for the purpose. Although it does get the job done, it isn't the best way to do it. The IC can get hot rather quickly and there is no filtering of motor noise which could cause problems in RF communications and data transfer. 

To avoid these problems we can use a motor drive board/module. Motor drive boards have a driver IC as the heart with a heat sink and some other electronic components to reduce the motor noise. Let's see what a motor driver is, how it works and how to use it for our projects.

What is a Motor Driver Board?

A module with a motor driver IC which helps us control speed and direction of DC motors is called a motor driver board. There are a variety of motor driver boards in the market. Most popular being L298N and L293D based motor drivers. 

These boards are compatible with all arduino, esp and similar microcontrollers. In this article we will perticulerly look into 2 very popular motor drivers, The L298N Driver and L293D based motor controller shield.

So let's see these boards in more detail:

L298N Motor Driver:

This board is very popular in the makers community for its smaller size and power. The IC has a heat sink already which makes the board highly efficient. 


L298N motor driver board
L298N is dual H-bridge IC, which means we can control 2 motors uisng this IC.
Here is a list of specification of the driver:



  • Dual H-Bridge Drive Chip: L298N
  • Logical Voltage: 5V
  • Drive Voltage: 5V-35V
  • Logical Current: 0-36mA
  • Drive current: 2A (MAX single bridge)
  • Max Power: 25W
  • Dimensions: 43 x 43 x 26mm
  • Weight: 26g
Using this motor driver we can control 12v-30v motor with max current of 2A. Using driver is pretty similar to using bare IC. Check out this tutorial for more information about that.

L293D Motor Shield:

Shields are something you use with Arduino. These boards stack on top of arduino and we can use them with libraries. It reduces the mess created by wires and saves a lot of space.
Here we will see the popular Arduino Motor Shield V1. 

arduino motor shield motor driver
This motor shield has two L293D ICs which control two DC motors each, hence we can control four DC motors or two stepper motors using this shield. Along with that we can also control two servo motors with this shield.

  • Double H Bridge Drive Chip: L293D
  • Logical Voltage: 5V
  • Drive Voltage: 5V-36V
  • Logical Current: 0-36mA
  • Drive current: 1.2A (MAX single bridge)
  • Max Power: 25W
  • Dimensions: 69 x 53 x 14.3mm
This motor shield just stacks on top of arduino and using a library called AFMotor.h we can easily control motors and servos using it. 

How To Use Motor Shield?

Motor shield can be stacked upon our Arduino uno. Before coding, we need to install the library.
To install library first open the IDE and goto >> Tools >> Manage Libraries
In the search box type "Adafruit Motor Shield" and install the first library.


After the library is installed, goto >> Files >> Examples >> Adafruit Motor Shield Library >> Motor Test. 

Next connect the Arduino to PC and upload the code. Now connect a DC motor to the M4 terminal and see how the motor works. You will notice that the motor slowly spin in one direction gradually gaining speed and then it does same in other direction. 

That's all for this article, now you know how to control a motor using a motor shield. You can use this for testing different codes in the example and learn more. In future we will use this to create an arduino based robot. Till then keep experimenting.

If you like my tutorials and want to stay updated for more, make sure you follow my blog. If you have any questions, feel free to comment down bellow.
Are you a beginner and want to get into robotics? then you must have many questions in mind like "How to make the robot move?" or "which tools to use to make the robot move?". If you are anything like me then you probably have disassembled tons of toys to figure that out. Most common thing you find in these toys are DC motors. 

In this tutorial we will see, how to control a DC Motor, it's speed and direction using arduino and L293D motor driver IC.

So without wasting any more time, lets get right into it.


Requirements:

For this project we will need,
So every thing in the list might look familiar except "L293D". Let me explain about this IC in a simple and short term. L293D is a motor drive IC which can be used to control 2 DC motors or 1 Stepper motor. It has 2 H-bridges which helps control the speed and direction of  motors. 

l293d ic

We cannot connect a motor directly to arduino as it will fry the board instantly. So we use a motor driver like L293D or L298N. You can find motor driver modules and shields online which help control multiple motors but for this tutorial we will stick with minimum for learning the basics. 

In the above image you can see the pin outs of the IC. Each side controls one motor. For this tutorial we will control one motor using arduino.

 Pin No
 Function
 Name
1
Enable pin for Motor 1
Enable 1,2
2
Input 1 for Motor 1
Input 1
3
Output 1 for Motor 1
Output 1
4
Ground 
Ground
5
Ground 
Ground
6
Output 2 for Motor 1
Output 2
7
Input 2 for Motor 1
Input 2
8
 Power the Motors
 Vcc 2
9
Enable pin for Motor 2
Enable 3,4
10
Input 1 for Motor 1
Input 3
11
Output 1 for Motor 1
Output 3
12
Ground 
Ground
13
Ground 
Ground
14
Output 2 for Motor 1
Output 4
15
Input2 for Motor 1
Input 4
16
Power the IC
Vcc 1


Connections:


motordriver

Here we have connected the input pins of IC to arduino:
  • IN1 to Pin 8
  • IN2 to Pin 9
  • Enable1 to Pin 5 (PWM)
Pin 4,5,12,13 of IC are connected to GND of Arduino and battery. If you don't do this, the motor will not work.



Connect Pin 8 and 16 of the IC to +ve terminal of 9v battery. This will power our motor and the IC itself. 



That's all we need to do. now we can write code to control the motor.

Coding:

#define ENA1 5 //Sets pin 5 of arduino as Enable1
#define IN1 8 // Sets pin 8 of arduino as Input 1
#define IN2 9 // Sets pin 9 of arduino as Input 2

 void setup()
{
//Set all pins as output
   pinMode(ENA1, OUTPUT);
   pinMode(IN1, OUTPUT);
   pinMode(IN2, OUTPUT);
}

 void loop()
{
   analogWrite(ENA1, 255); //Sets speed of motor using PWM
   digitalWrite(IN1, HIGH); //Input 1 is turned on
   digitalWrite(IN2, LOW); //Input 2 is turned off
   delay(3*1000); //Runs for 3 seconds

   analogWrite(ENA1, 255);
   digitalWrite(IN1, LOW); //Both inputs are turned off
   digitalWrite(IN2, LOW);
   delay(2*1000); // Waits for 2 seconds

   analogWrite(ENA1, 255);
   digitalWrite(IN1, LOW); // Input 1 is off
   digitalWrite(IN2, HIGH); // Input 2 is on
   delay(3*1000); //Runs for 3 seconds

   analogWrite(ENA1, 255);
   digitalWrite(IN1, LOW);
   digitalWrite(IN2, LOW);
   delay(2*1000);
}

Let's brake down the code to make it easy to understand.

First we define pins that we will be using.

#define ENA1 5
#define IN1 8
#define IN2 9
 

Here the ENA1 pin is connected to pin 5 of arduino as it is a PWM enabled pin. This will help us control the speed of our motor.
Next there are pins 8 and 9 connected to pin IN1 and IN2 respectively. These pins will make the motor start, stop and change direction depending on which pin is HIGH/LOW. 

void setup()
{
   pinMode(ENA1, OUTPUT);
   pinMode(IN1, OUTPUT);
   pinMode(IN2, OUTPUT);
}


In the setup we set the purpose of the declared pins, in this case all pins are set as output. 

Now comes the loop function. The code in this part runs in a loop.

  analogWrite(ENA1, 255);
 digitalWrite(IN1, HIGH);
 digitalWrite(IN2, LOW);
 delay(3*1000); 


Here notice the function analogWrite(ENA1, 255) we are using the PWM feature of pin 5. The PWM value can be anything between 0-255, where 0 will turn off the motor and 255 will spin the motor at full speed. Varying the value between 1-254 will give different speeds.
Next two functions are digitalWrite, these these sets a pin either HIGH or LOW. As in the above example, the IN1 is HIGH and IN2 is LOW, this will make the motor turn in one direction. Right or Left depending on the way you connected the terminals.

Swapping the HIGH and LOW will change the direction of rotation, and making both LOW/HIGH Will make the motor stop.


Now that you have an idea of the code, it's time to upload the code. So connect your arduino to PC and upload the code.

Testing:

Now that the code is uploaded, power the motors and IC with 9v as shown in the circuit in previous step. You will see that the motor spins in one direction for 3 seconds then stops for 2 seconds and spins in other direction.



Here you can see the code in action. Now you can edit the code and change the values in analogWrite. Make it anything between 0-255 and see the results. Similarly you can control one more motor with L293D.

That's all for this tutorial. Now you know how to control DC motors using Arduino. There are other better methods like using Motor drive modules and shields but that's a topic for another tutorial. Till then keep experimenting. 

Feel free to ask any doubts or leave any suggestions. If you liked the tutorial, follow for more. It's totally free!

Contact Us

Contact With Us

Followers

Translate

Get in touch with us.

  • +91 7276861817
  • projecthub.in@protonmail.com