Introduction: Line Following Car With Wireless Control Option

This is a small Arduino controlled line following car. Based of the sensor spacing it can be modified to follow any width of line within reason (1/2" - 5" Generally). There also is an option to wirelessly control the car.

This Project was completed for the Mechatronics Class at Rowan University College of Engineering.

Step 1: Parts List

  • Ardrino Uno
  • 2x Continuous Servos
  • Transfer Case/Caster Bearing
  • 2x Servo Wheels
  • 9V
  • 9V Case
  • Small Breadboard
  • Wire
  • 3x IR Sensor (TCRT-5000 Model Used)
  • 1/4" Acrylic / Particle Board / Other Similar Material For Chassis
  • Perfboard
  • Switch (Optional)
  • Xbee (Optional)
  • Xbee Shield (Optional)

Step 2: Chassis

Using laser cutter, hand tools, or other method cut out bottom, top, and two sides of chassis. Leave the front and back open. The dimensions used for example product are 3" wide, 5" long, 2" high. Cut out mounts for the servos on each side. Cut out mounting holes for caster bearing on bottom. Also cut out mounting holes for battery clip. Cut out holes in the top to allow wires to pass through. Assembly chassis, including mount the servos and caster bearing. Then attach the servo wheels to the servo motors. Using mounting holes or velcro attach the bread board and Arduino to the top of the chassis.

Step 3: Sensors - Test Circuit

You will need: Three TCRT5000 IR Sensors, three
10kΩ resistors and three 100Ω resistors, an Arduino Uno, a breadboard, and wires.

Build the test circuit using the diagram, taken from blog.huntgang.com, with three sensors in parallel. The first sensor should output to pin A0 on the Arduino, the second should output to pin A1, and the third should output to pin A2. Connect the Arduino to the computer, and upload this test code.

void setup() {

Serial.begin(9600);

}

void loop() {

// read the input on analog pin 0:

int sensorValue0 = analogRead(A0);

int sensorValue1 = analogRead(A1);

int sensorValue2 = analogRead(A2);

// print out the value: 1 for black 0 for white

Serial.print("Sensor1_");

if (sensorValue0 <300)

Serial.print(0);

else Serial.print(1);

Serial.print("\t");

//sensor2, plugged into A1

Serial.print("Sensor2_");

if (sensorValue1 <300)

Serial.print(0);

else Serial.print(1);

delay(1);

Serial.print("\t"); // delay in between reads for stability

//sensor 3, plugged into A2

Serial.print("Sensor3_");

if (sensorValue2 <300)

Serial.println(0);

else Serial.println(1);

delay(1); // delay in between reads for stability

}

When the Arduino runs the code, all three sensors will output values of either 1 or 0 to the Serial Monitor. The value of 1 stands for dark or black, while 0 is light or white. Test the sensors by passing them over a piece of paper with thick black and white lines. Depending upon the lighting conditions in the room, the sensors may not be precise. If they don’t immediately pick up the difference between black and white, increase or decrease the “SensorValue” in the code.

Step 4: Sensor Bar

Build a T support about 5 inches wide and about 3 inches tall. We used perfboard so that we could easily attach the circuit parts. The sensors are mounted on the T, with two sensors positioned 4.75 inches apart and 1 inch forward of the third, center sensor. Solder the circuit together as seen in the Figure. Red wires go to power, black wires go to ground. For coding purposes, the front left sensor is wired to pin A0, the front right is A1, and the center is A0. When mounting to the car, make sure that the sensors are positioned no more than half an inch off the ground. Otherwise they will pick up too much ambient light and will produce errors. The distance the sensors are apart can be altered for different line width.

Step 5: Wiring

Connect the 9V battery to the V(in) and ground pin on the Arduino. (Optionally, a switch may be added between the battery and the arduino).

Provide a power and ground rail on the breadboard. Connect the sensors' power and servos' power wire to the power rail. Do the same for ground.

Connect the 3 sensor signal wires to 3 Analog In ports on the arduino. The example car used A0, A1, and A2.

Connect the 2 servo signal wires to 2 PWM ports on the arduino. The example car used pins 9 & 10.

Step 6: Coding

Code is Below

Code Explained:

The continuous servos take a data input like normal servos, but instead interpreting that data as an angle they interpret that data as a speed. For most continuous servos the angle (90) is stop, (0) is full speed reverse, and (180) is full speed forward. The example car used continuous servos which used (95) for stop, (0) for full speed revers, and (190) for full speed forward. You may have to take this in account and edit your code based off your servo choice. In the example code the forward direction is (75) and (115). This is because the servos are mounted mirrored to each other. So forward for one is reverse for the other.

The sensors are read in the code. They will give a value between 0-1023. When the returned value is low the sensor sees a light color (white). When the return value is high the sensor sees a dark color (black). The code uses "k" which is a tuning constant. "k" is the cut off the determining whether the color is black. This can be adjusted based off of environment lighting and surface reflectivity. The example car uses a cut off of 500.

There are 3 sensors on the car. Right, Left, and Center. The general concept of this algorithm is that the car is attempting to always keep the Right and Left sensors on each side of the line and the center center on the line itself.

  • If the Right and Left see white and the Center sees black the car goes forward.
  • If the Right and Center see black and the Left sees white the car notices a right turn and turns right.
  • If the Left and Center see black and the Right sees white the car notices a left turn and turns left.

  • If the Center sees right at all, it realizes as a fail safe that it is off the track and backs up for .8 seconds to return to the track.

The serial print outs are only for testing and tuning the "k" value if needed. They serve no function to the actual performance of the car itself.

#include
Servo servoLeft; // Define left servo Servo servoRight; // Define right servo int k = 500; // White/Black Calibration cut off

void setup() { servoLeft.attach(10); // Set left servo to digital pin 10 servoRight.attach(9); // Set right servo to digital pin 9 Serial.begin(9600); }

void loop() { // read the input on analog pin 0: int sensorleft = analogRead(A0); int sensorright = analogRead(A1); int sensorcenter = analogRead(A2); // straight

if (sensorleft < k && sensorright < k && sensorcenter > k) {

servoLeft.write(75); // fwd servoRight.write(115); }

// left

if (sensorleft > k && sensorright < k && sensorcenter > k) {

servoLeft.write(55); // left servoRight.write(90); } // right

if (sensorleft < k && sensorright > k && sensorcenter > k) {

servoLeft.write(100); // righ servoRight.write(135); }

// safety off track

if ( sensorcenter < k) {

servoLeft.write(100); // re servoRight.write(90); delay(800); }

Serial.print(sensorleft); Serial.print(" "); Serial.print(sensorright); Serial.print(" "); Serial.print(sensorcenter); Serial.print(" ");

}



Step 7: Wireless Control (Optional)

The car should now work following a line. If you would like to make your car RC controllable as well like the example car you can attach an Xbee shield with Xbee to the Arduino. Simply add the wireless to the code.

https://www.instructables.com/id/Wireless-CarXbee-Series-2-Section/

Step 8: Enjoy Your Line Following Car

Enjoy your car.

Note: You may notice it maybe wiggles around the corners but always follows them correctly.