Full Width CSS

Control Motor Driver Speed Using Arduino

An Arduino cannot supply sufficient power to motor to operate, so we use Motor driver.It is a device that supplies sufficient power or voltage to the motor and helps us to connect them with Arduino and make our mechanical robot. We can use motor driver in two ways.

  • One is operate motor driver at high speed
  • And the other is operate motor driver at controlled speed
We have already discussed about point one in brief and cleared all the concepts of programming in the link given below:

So, in this tutorial we are going to see hoe to control the speed of motor driver. Also from this tutorial you can make your own four wheel robot that will operate according to the command described in the code.

COMPONENTS REQUIRED:
  1. Arduino UNO  X 1
  2. L298N Motor Driver  X 1
  3. (10-12) Volt motor X 4
  4. Male to female jumper wire X 6
  5. Male to male jumper wire X 2
CIRCUIT CONNECTION:
(Note: At first upload the code to the Arduino board then only do the circuit connection in order to avoid short circuit in the Arduino board from the previously uploaded code.)


fig: Connection between motor driver and Arduino

fig: Pin configuration of Motor driver

In the above figure the pin that will control the motor driver speed is EN PIN, means enable pin.

There are two EN pins in motor driver as shown in figure:
  1. ENA: This pin will control speed of left two motors
  2. ENB: This will control speed of right two motors
Note: Motor 1 and Motor 2 will have same speed because they are connected in parallel. And similarly Motor 3 and Motor 4 will have same speed since they are also connected in parallel.

The motor speed is divided rom 0 to 250 means 0 is the lowest speed and 250 is the highest speed.
So, while doing coding our speed range will be 0-250. Suppose if we put speed as 125 then the motor speed will be half and accordingly  we will number the speed to our desire.

In the below code,
analogWrite(ENA,250);  // means high or top speed
analogWrite(ENB,100);  //  means speed at value 100


CODING PART:

In the given below code the enable pins are connected to analog pins of Arduino in order to control speed of motor. You cannot control speed of motor if you connect enable pins to digital pins in Arduino.
The code is given below. Copy and paste the code to Arduino IDE and upload it to your Arduino UNO board.

int motor1=8;
int motor2=7;
int motor3=6;
int motor4=5;
int ENA=9;
int ENB=3;
void setup()
{
pinMode(motor1,OUTPUT);
pinMode(motor2,OUTPUT);
pinMode(motor3,OUTPUT);
pinMode(motor4,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
digitalWrite(motor1,LOW);
digitalWrite(motor2,LOW);
digitalWrite(motor3,LOW);
digitalWrite(motor4,LOW);
serial.begin(9600)
}
void forward()
 {
 analogWrite(ENA,250);
 analogWrite(ENB,250);
 digitalWrite(motor1,HIGH);
 digitalWrite(motor2,LOW);
 digitalWrite(motor3,HIGH);
 digitalWrite(motor4,LOW);
 }
void backward()
 {
 analogWrite(ENA,100);
 analogWrite(ENB,100);
 digitalWrite(motor1,LOW);
 digitalWrite(motor2,HIGH);
 digitalWrite(motor3,LOW);
 digitalWrite(motor4,HIGH);
 }
void left()
 {
 analogWrite(ENA,150);
 analogWrite(ENB,150);
 digitalWrite(motor1,LOW);
 digitalWrite(motor2,HIGH);
 digitalWrite(motor3,HIGH);
 digitalWrite(motor4,LOW);
 }
void right()
 {
 analogWrite(ENA,150);
 analogWrite(ENB,150);
 digitalWrite(motor1,HIGH);
 digitalWrite(motor2,LOW);
 digitalWrite(motor3,LOW);
 digitalWrite(motor4,HIGH);
 }
void stop()
 {
 digitalWrite(motor1,LOW);
 digitalWrite(motor2,LOW);
 digitalWrite(motor3,LOW);
 digitalWrite(motor4,LOW);
}
void loop()
{
  forward();
  delay(2000);
  backward();
  delay(5000);
  left();
  delay(3000);
  right();
  delay(4000);
  stop();
  delay(7000);
}

In this way you can make your own speed controlled four wheel robot and by changing the program according to your wish you can do your projects.

Post a Comment

0 Comments