In this lesson we will expand our knowledge from Two Wheel Drive Robot lesson to build a robot with four wheels and four motors , that`s why we call it Four Wheel Drive or 4WD. For Steering we will still use Differential Steering.
Estimated time: 25 minutes
You will learn about:
Digital Write
Motor Function
Direction control
Speed Control
4WD
Differential Steering
Components Needed
Main Board | USB Cable |
Arduino | Body |
4WD Front Support | 4WD Back Support |
4 pieces Motors | 4 pieces Wheels |
Hood | 2 pieces Spacers |
29 pieces/ M3 Flat Washer | 4 pieces M3 nylon washers |
10 pieces M3 x 8 Screws | Screw Driver |
Pliers | |
16 pieces/ M3 Hex Nut | 18 pieces/ M3 Lock Washer |
Step 1:
Install Mainboard on the body [|] using screw driver and pliers.
Step 2:
Install the Caster Wheel on the Main Board using M4 Screws Like here.
Step 3:
Install the motors on the 4WD front Support and 4WD back support [|] using 12 M3 x 30 screws, 24 flat washers , 12 lock washers and 12 hex nuts.
Step 4:
Connect [|] the Arduino to the Main board.
Step 5:
Plug in the USB cable[|] to the Micro USB port of Arduino and then plug it to the USB port of the computer.
Step 6:
Upload [|] the following code to the Arduino.
int M1A=3;
int M1B=4;
int M2A=2;
int M2B=5;
int M3A=6;
int M3B=12;
int M4A=9;
int M4B=8;
void setup() {
pinMode(M1A, OUTPUT);
pinMode(M1B, OUTPUT);
pinMode(M2A, OUTPUT);
pinMode(M2B, OUTPUT);
pinMode(M3A, OUTPUT);
pinMode(M3B, OUTPUT);
pinMode(M4A, OUTPUT);
pinMode(M4B, OUTPUT);
}
void loop() {
// Forward
//M1 Forward
digitalWrite(M1B,LOW);
digitalWrite(M1A,HIGH);
//M2 Forward
digitalWrite(M2B,LOW);
digitalWrite(M2A,HIGH);
//M3 Forward
digitalWrite(M3B,LOW);
digitalWrite(M3A,HIGH);
//M4 Forward
digitalWrite(M4B,LOW);
digitalWrite(M4A,HIGH);
delay(2000);
// Backward
//M1 Backward
digitalWrite(M1B,HIGH);
digitalWrite(M1A,LOW);
//M2 Backward
digitalWrite(M2B,HIGH);
digitalWrite(M2A,LOW);
//M3 Backward
digitalWrite(M3B,HIGH);
digitalWrite(M3A,LOW);
//M4 Backward
digitalWrite(M4B,HIGH);
digitalWrite(M4A,LOW);
delay(2000);
//Right
//M1 Backward
digitalWrite(M1B,HIGH);
digitalWrite(M1A,LOW);
//M3 Backward
digitalWrite(M3B,HIGH);
digitalWrite(M3A,LOW);
//M2 Forward
digitalWrite(M2B,LOW);
digitalWrite(M2A,HIGH);
//M4 Forward
digitalWrite(M4B,LOW);
digitalWrite(M4A,HIGH);
delay(2000);
//Left
//M1 Forward
digitalWrite(M1B,LOW);
digitalWrite(M1A,HIGH);
//M3 Forward
digitalWrite(M3B,LOW);
digitalWrite(M3A,HIGH);
//M2 Backward
digitalWrite(M2B,HIGH);
digitalWrite(M2A,LOW);
//M4 Backward
digitalWrite(M4B,HIGH);
digitalWrite(M4A,LOW);
delay(2000);
//Stop
//M1 Stop
digitalWrite(M1B,LOW);
digitalWrite(M1A,LOW);
//M2 stop
digitalWrite(M2B,LOW);
digitalWrite(M2A,LOW);
//M3 Stop
digitalWrite(M3B,LOW);
digitalWrite(M3A,LOW);
//M4 stop
digitalWrite(M4B,LOW);
digitalWrite(M4A,LOW);
delay(2000);
}
Step 7:
Remove the USB cable from the USB port of Arduino.
Step 8:
Install the 4WD Front Support and 4WD Back Support with motors on the body using M3 x 10 screws and lock washers and flat washers and hex nuts.
Step 9:
Connect the motor wires to M1, M2, M3 and M4 Screw terminal using the screw driver. The red wire goes to The Terminals closer to Cellpack, the black wires goes to the terminals away from Cellpack.
Step 10:
Install the spacers by removing the two screws at the hood part of the robot holding the Mainboard and placing the spacers on top and fastening the screws from beneath having the Nylon washers touching the Mainboard and flat washer and spring washers under it .
Step 11:
Install the Hood on the body with M3 x 10 screws.
Step 12:
Install the Cellpack in its place [|] and connect the DC power jack Connector to female DC connector on the Mainboard.
Step 13:
Put the 2C , 3C , 4C, 5C and 12C Jumper Caps [|] in the connecting state so the two corresponding pins connect to each other.
Step 14:
Turn the Cellpack on [|] and check the movement of the robot, we have four movements, robot forward, robot backwards,robot turning right, robot turning left.
Congratulations! You have your own Robot now.
Code Explanation
- In lines 1 to 8, [|]
- In lines 12 to 22 ,[|] .
- In lines 14 to 21,[|] .
- In lines 26 to 38,[|] .
- In lines 28 and 29,[|] .
- In lines 31 and 32,[|] .
- In lines 34 and 35,[|] .
- In lines 37 and 38,[|] .
- In line 39,[|] .
- In lines 41 to 53,[|] .
- In line 54,[|] .
- In lines 56 to 68, [|]
- In line 69,[|] .
- In lines 71 to 83, [|]
- In line 83,[|] .
- In lines 86 to 98,[|] .
- In line 99,[|] .
Further Experimentation
- Try using analog write instead of digital write for 3 , 5 , 6 and 9 pins and try giving the robot different speeds.
- Try using other turning strategies like one motor pause, the other one going forward or other one going backwards.
- Try uploading the 2WD code, to make the 4 on 2 WD configuration, then connect M1 and M3 motor wires to M1 screw terminal and M2 and M4 motor s to M2 screw terminal , in a double motor configuration and see how the robot will perform..
Practical Usage For our Projects
- We will use these codes for all kinds of robots on wheels and will combine that with other configurations to build remote controlled robots and transformer robots, other steering robots,… also we will use the 4 on 2 WD configuration when we need 4 more pins free for other functions of the robot like servo motors or Music player.