In the above figure the pin that will control the motor driver speed is EN PIN, means enable pin.
- ENA: This pin will control speed of left two motors
- 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
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.
0 Comments
If you have any doubts, Please let us know.
Also you can visit my newly made freelancing account to make your project done at low price.
https://www.freelancer.com/get/jpmandal2?f=give