Qns: I am doing a project ( Line follower-robot) , What do I need to change in order to make the two motors turn in same direction at the same time?
Test code with Arduino
As a simple test, you can make the motors turn forward and backward alternatively with this code:
void setup() {
// initialize the digital pins as outputs.
// Pin 13 has a LED connected on most Arduino boards:
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);//Motor connected on pin 7 en 8, these are PWM capable
pinMode(8, OUTPUT);
}
void loop() {
digitalWrite(7, HIGH); //start motor at full speed
digitalWrite(8, LOW);
digitalWrite(13, HIGH); //turn on LED
delay(5000);//wait
digitalWrite(7, LOW); //stop motor
digitalWrite(8, LOW);
delay(1000); //wait a second to be sure motor is stopped
digitalWrite(7, LOW); //start motor in reverse direction, full speed
digitalWrite(8, HIGH);
digitalWrite(13, LOW); //turn off led
delay(5000);//wacht
digitalWrite(7, LOW); //stop motor
digitalWrite(8, LOW);
delay(1000); //wait a second to be sure motor is stopped
}
Speed control
Just like with the LED in the first session, the analogWrite() command can be used to set a speed for the
motor:
void setup() {
// initialize the digital pins as outputs.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
pinMode(5, OUTPUT);//Motor aangesloten op pin 7 en 8, deze zijn PWM capable
pinMode(6, OUTPUT);
digitalWrite(6, LOW);
}
void loop() {
analogWrite(5, 128);
digitalWrite(13, LOW);//Show speed on LED
delay(4000); //wait a bit
analogWrite(5, 255);
digitalWrite(13, HIGH);//Show speed on LED
delay(4000);
}