Cover by Michael Margolis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

Chapter 4. Building the Four-Wheeled Mobile Platform

This chapter provides advice on the construction of the 4WD (4 Wheel Drive) chassis shown in Figure 4-1. Construction is straightforward—you can follow the detailed steps or improvise if you want to customize your robot. The chapter also shows how you attach and connect sensors used in the projects covered in later chapters.

If you prefer to build a four wheeled robot of your own design, you should read the sections on attaching control electronics and sensors if you want to use the code for the projects in the chapters to come. Information in this chapter my also provide some ideas to help with the design of your own robot.

The 4WD robot chassis

Figure 4-1. The 4WD robot chassis

You will need a Phillips screwdriver, long-nose pliers, wire cutters, wire strippers, a soldering iron, and solder. If you don't have these on hand, you can find more information in Chapter 1, Introduction to Robot Building.

Hardware Required

See for a detailed parts list.

  • Tools listed in Tools

  • The assembled electronics (see Chapter 2, Building the Electronics

  • 4WD Mobile Platform (four wheeled robot kit made by DFRobot)

  • Four 0.1uF ceramic capacitors

  • Two lengths of 3 conductor ribbon cable, two 3 way 0.1" headers for edge sensors

  • Optional: charging circuit resistors and diode, see detailed parts list

Mechanical Assembly

Mechanical assembly of the 4WD chassis is straightforward and the only tools needed are a Phillips screwdriver and pliers. Following the steps in order will ensure that you use the correct hardware in each assembly. You will need a soldering iron, wire cutters, and wire strippers to wire up the motor and power leads.

Lay Out the Chassis Parts

Figure 4-2 shows all of the parts contained in the 4WD chassis package. Figure 4-3 shows the contents of the bag containing the mounting hardware. The three black brackets to the left of the figure are not needed for any of the projects in this book. Locate the two bolts with the flat heads and put them aside for mounting the battery case. The remaining short bolts in this pack are identical.

4WD chassis parts

Figure 4-2. 4WD chassis parts

4WD hardware pack contents

Figure 4-3. 4WD hardware pack contents

Motor Assembly

Use four long bolts to attach two motors to each of the side plates. The motor shaft goes through the large hole and there is a small locating stud on the motor that fits into the smaller hole. The lock washer (the one with a raised edge) goes between the nut and flat washer. Ensure the motor is flat against the plate and tighten the nuts firmly but take care not to use too much force or you will stress the plastic motor housing. Figure 4-4 and Figure 4-5 shows the assembly.

Motor assembly

Figure 4-4. Motor assembly

Assemble the Chassis Components

The battery pack is bolted to the bottom base plate with two countersunk (flat headed) Phillips bolts as shown in Figure 4-6 and Figure 4-7.

Battery holder assembly

Figure 4-6. Battery holder assembly

Battery holder assembly

Figure 4-7. Battery holder assembly

The DC power jack is bolted to the rear plate using the large (M8) lock washer and nut as shown in Figure 4-8. The switch is mounted using two nuts and a lock washer (the locating washer is not used). Put one nut on the switch, leaving about enough thread for the nut to be attached to the other side. Place the lock washer on the thread and push this through the opening in the rear plate and secure with the second M6 nut. Orient the switch so the toggle moves from side to side, as shown in the figure. Figure 4-9 and Figure 4-10 show two views of the assembly.

Switch and power jack assembly

Figure 4-8. Switch and power jack assembly

Solder the Power and Motor Connections

Connecting the Battery Pack and Power Switch

The battery can be wired as shown in Figure 4-14, but you cannot charge the battery in this configuration. The power switch will disconnect the battery when the robot is not in use. The DC jack is not used in this configuration (other than as a junction point for the black ground wires). The switch is off when the toggle is closer to the DC jack as shown (the toggle is a lever, when the exposed end is up as seen in the figure, the contact at the bottom is connected and the contact wired to the shield is open). Figure 4-15 shows the completed circuit.

Basic switch wiring (no trickle charger)

Figure 4-14. Basic switch wiring (no trickle charger)

Red wires soldered to switch

Figure 4-15. Red wires soldered to switch

Building the Optional Trickle Charger

You can build a simple trickle charger into the robot if you will be using rechargeable NiMH batteries. See Trickle Charging for information about using the charger.

The circuit is wired as shown in Figure 4-16 and Figure 4-17. The battery is connected to both the robot and charger when it is switched on, enabling the Arduino to monitor and display the battery voltage. The connection via the resistor to pin 13 is required to tell the Arduino that a charger is connected so it can monitor the voltage instead of driving the robot.

Wiring for trickle charging with Arduino voltage monitoring

Figure 4-16. Wiring for trickle charging with Arduino voltage monitoring

Wiring for trickle charging with Arduino voltage monitoring

Figure 4-17. Wiring for trickle charging with Arduino voltage monitoring

Assemble the Chassis

Attach the front and rear plates to the sides using eight of the M3x6 bolts (Figure 4-18). The sides are symmetrical so it doesn't matter which end goes to the front or back.

Chassis assembly

Figure 4-18. Chassis assembly

Attach the bottom plate using four M3x6 bolts (Figure 4-19).

Bottom plate assembly

Figure 4-19. Bottom plate assembly

Mounting Arduino and Connecting Wires to the Shield

The easiest way to mount the Arduino board is with a strip of Velcro. A 2.5" x 1.5" strip is supplied with the Rovera 4W (Arduino-Controlled 4 Wheel Robotics Platform) kit. To prevent the Arduino pins from accidentally shorting to the chassis, apply insulating tape to the underside of the Arduino board. Gaffer tape works well but you can use (non-conductive) duct tape or heavy duty electrical tape. Attach the 'hairy' side of the Velcro to the taped Arduino board; the hook side is fastened as shown in Figure 4-20. Figure 4-21 shows some other views of this.

Velcro pad in position on the top plate

Figure 4-20. Velcro pad in position on the top plate

The Velcro will hold the boards in position when the robot is moving about, but use one hand to steady the Arduino when you unplug the shield and take care not to use too much downward pressure that could push the Arduino pins through the tape when plugging in the shield.

Inset shows Velcro attached to the Arduino board.

Figure 4-21.  Inset shows Velcro attached to the Arduino board.

If you prefer a more rigid mount, you can use three 3/8" or 1/4 inch (5mm) spacers with three 1/2 inch 2-56 bolts and nuts. Figure 4-22 and Figure 4-23 show the location of the mounting hardware.

Figure 4-24 shows the motor wires and battery wires inserted through the cutouts in the top plate ready for the connections shown in Figure 4-25 .

Figure 4-26 shows how the motor and battery wires attach to the connectors on the motor shield.

Motor and battery connections

Figure 4-26. Motor and battery connections

Attach the sensor plate with two M3 bolts as shown in Figure 4-27; the top plate is attached using four M3 bolts as seen in Figure 4-28.

Sensor plate assembly

Figure 4-27. Sensor plate assembly

Top plate assembly

Figure 4-28. Top plate assembly

The upper deck is bolted to four 50mm standoffs that are attached as shown in Figure 4-29.

Attach the upper deck

Figure 4-29. Attach the upper deck

Figure 4-30 shows the fully-assembled chassis (a side view is visible in Figure 4-31). You can see the front and rear views in Figure 4-32 and Figure 4-33.

Mounting the IR sensors

This section covers mounting of the infrared (IR) reflectance sensors for use in edge detecting or line following. Infrared Reflectance Sensors explains how these sensors work and Chapter 9, Modifying the Robot to React to Edges and Lines describes how to use IR sensors. This section explains how to mount these sensors onto the 4WD platform and connect them to Arduino. The first projects in this book should have the sensors mounted as shown in the section on edge detection. When you are ready to implement the line following application in Chapter 9, refer back to the section on positioning the sensors for line following. The stripboard mount described in Making a Line Sensor Mount simplifies the attachment and wiring of the sensors for line detection and this can also be used for edge detection, but bear in mind that the robot will perform the edge detection task best with the sensors further apart. If the sensors are close together, the robot can have difficulty determining the best angle to turn when an edge is encountered.

Mounting the IR Sensors for Edge Detection

Edge detection requires two QTR-1A sensors mounted on the front of the robot. These should be spaced as widely apart as possible. The ideal location is with each sensor positioned in front of a wheel so an edge can be detected before a wheel would otherwise fall off a 'cliff'. However, if your priority is simplicity of construction rather than accuracy of edge detection, you can use the same mount described in the next section covering line detection. But bear in mind that the robot will perform the edge detection task best with the sensors further apart. If the sensors are close together, the robot can have difficulty determining the best angle to turn when an edge is encountered.

Mount each sensor using a 2-56 bolt and nut (M2 bolt and nut can also be used). The component side faces the ground and the header pins face upwards. The sensors can be angled as shown in Figure 4-34 and Figure 4-35.

Reflectance sensor location for edge detection

Figure 4-34. Reflectance sensor location for edge detection

Reflectance sensor location for edge detection

Figure 4-35. Reflectance sensor location for edge detection

Mounting the IR Sensors for Line Following

Three QTR-1a sensors are required for line following. Making a Line Sensor Mount describes how to build a stripboard mount for line sensing. However, you can also mount and attach each sensors as described in this section if you want to experiment with how varying the spacing of the sensors affects line following. Like the edge sensors, they can be attached using 2-56 or M2 hardware. The component side faces down and the header pins face upwards. They are mounted in the front, equally spaced with approximately 1/2 inch between the center and the left and right bolts (see Figure 4-36). Figure 4-37 and Figure 4-38 show the sensors attached to the chassis.

Reflectance sensor location for line following

Figure 4-36. Reflectance sensor location for line following

Next Steps

Chapter 5, Tutorial: Getting Started with Arduino explains how to set up and use the development environment that will be used to upload code to the robot. If you are already an Arduino expert, you can skip to Chapter 6, Testing the Robot's Basic Functions, but first, see Installing Third-Party Libraries for advice on the libraries used with the code for this book and the steps needed to configure the RobotMotor library for the 4WD robot.

If you have the libraries installed and want run a simple test to verify that the motors are working correctly, you can run the following sketch:

Example 4-1. Initial motor test for 4WD

* MotorTest4wd.ino
* Initial motor test for 4WD 
* robot rotates clockwise 
*  (Left motors driven forward, right backward)

* Michael Margolis 24 July 2012
const int LED_PIN = 13;
const int speed = 60; // percent of maximum speed

#include <AFMotor.h>  // adafruit motor shield library (modified my mm)
AF_DCMotor Motor_Left_Front(4, MOTOR34_1KHZ);   // Motor 4 
AF_DCMotor Motor_Right_Front(3, MOTOR34_1KHZ);  // Motor 3
AF_DCMotor Motor_Left_Rear(1, MOTOR12_1KHZ);    // Motor 1 
AF_DCMotor Motor_Right_Rear(2, MOTOR12_1KHZ);   // Motor 2

int pwm;

void setup()
  blinkNumber(8); // open port while flashing. Needed for Leonardo only  
  // scale percent into pwm range (0-255) 
  pwm= map(speed, 0,100, 0,255);  

// run over and over
void loop()
  Serial.println("rotate cw");;;;;

  delay(5000); // run for 5 seconds
  Serial.println("stopped");;  // stop the motors;;  // stop the motors;

  delay(5000); // stop for 5 seconds

// function to indicate numbers by flashing the built-in LED
void blinkNumber( byte number) {
   pinMode(LED_PIN, OUTPUT); // enable the LED pin for output
   while(number--) {
     digitalWrite(LED_PIN, HIGH); delay(100);
     digitalWrite(LED_PIN, LOW);  delay(400);

This sketch runs the motors in opposite directions to cause the robot to rotate clockwise for 5 seconds, then stops for 5 seconds. This will repeat until the power is switched off.


This test sketch does not use the RobotMotor library—if this test functions correctly but the test in Chapter 6, Testing the Robot's Basic Functions does not work, the most likely cause is the configuration of the motor library—make sure you copy the 4wd version of the library code as described in Installing Third-Party Libraries.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required