Now that we have understood the H-bridge circuit, we will write a program called Forward.cpp to move our robot forward. After that, we will write a program to move the robot backward, left, and right, and then stop. You can download the Forward.cpp program from Chapter03 of the GitHub repository.
The program for moving the robot forward is as follows:
#include <stdio.h>
#include <wiringPi.h>
int main(void)
{
wiringPiSetup();
pinMode(0,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
for(int i=0; i<1;i++)
{
digitalWrite(0,HIGH); //PIN O & 2 will move the Left Motor
digitalWrite(2,LOW);
digitalWrite(3,HIGH); //PIN 3 & 4 will move the Right Motor
digitalWrite(4,LOW);
delay(3000);
}
return 0;
}
Let's see how this program works:
- First, we set the wiringPi pins (numbers 0, 1, 2, and 3) as output pins.
- Next, with the following two lines, the left motor moves forward:
digitalWrite(0,HIGH);
digitalWrite(2,LOW);
- Then, the next two lines make the right motor move forwards:
digitalWrite(3,HIGH);
digitalWrite(4,LOW);
- After that, the delay command means the motors will move forwards for three seconds. As we are currently inside a for loop, the motor will keep rotating continuously.
- Once you have finished the code, compile the program to check if there are any errors.
- Next, connect the 9V battery to the battery snapper and upload the program. Before doing this, however, make sure that you lift up the wheels of the robot. This is because, when the robot starts moving, you might get one of the following three outputs:
- Both the motors move forwards. If you get this output, this means your robot will move forwards once you put it on the ground.
- One motor moves forwards and the other motor moves backward. If you get this output, interchange the wires of the motor that is moving backward on the motor driver. For example, if the right motor is moving backward, insert the M3-OUT wire in the M4-OUT socket and the M4-OUT wire in the M3-OUT socket, as shown in the following diagram:
-
- Both the motors move backward. In this case, your robot will move backward. If you get this output, interchange the wires of both the left and right motor on the motor driver. To do this for the left motor, connect the M1-OUT socket wire in the M2-OUT socket and the M2-OUT socket wire in the M1-OUT socket. For the right motor, connect the M3-OUT socket wire in the M4-OUT socket and the M4-OUT socket wire in the M3-OUT socket, as shown in the following diagram:
Alternatively you can also interchange the pins on the RPi to move the robot forward; connect pin 0 in place of pin 2 and pin 2 in place of pin 0 for the left motor. Similarly, connect pin 3 in place of pin 4 and pin 4 in place of pin 3 for the right motor.
- Click on the upload button and check the final output. Since this program is in a for loop, the motor will keep running continuously. After testing the output, disconnect the battery from the battery snapper so that the power to the motors through the motor driver is turned off to stop the motors from moving.