Line Followoing Robot

OBJECTIVE:

My objective was to build a line following robot using arduino software, a motor driver and a line sensor. I also wrote a c program that will be suitable to carry out the line following function.

MATERIALS:

• ¼ thick hdpe (high density polyethylene ) sheet $10
• Arduino uno R3 $35
• 1 small breadboard $4
• 2 Motor brackets $2.98
• 2 dc motors $11.90
• 1 SN754410 motor driver IC $2.95
• 4-AA Battery Holder $1.75
• QTR-8A Reflectance sensor array (6 used) $14.95
• 2 wheels $7.00
• 1 caster $3
• 1 9v battery connector $1
• Velcro $4
• Wires $4
• 4-AA Battery $2
• 19v battery $5

DISCUSSION:

A high-density polyethylene (HDPE) was used as the body of the robot because of its stronger intermolecular forces and tensile strength. Hdpe is a polyethylene thermoplastic made from petroleum. Its mass density can range from 0.93 to 0.97 g/cm3.

I also used a microcontroller; a microcontroller is a small computer on a single integrated circuit containing a processor core, memory, and programmable input and output peripherals. I used the Arduino micro controller. Arduino is a open source single board microcontroller designed to make the process of using electronics in projects more accessible to the public. I used the Arduino uno, which comes with the ATmega328 chip.

I made use of a dc motors; dc motor is an electric motor that runs on direct currentelectricity. The H-bridge is what I used to drive both my motors. I used the SN754410 motor driver IC. An H-bridge is an electronic circuit that enables a voltage to be applied across a load in both directions. It is used mostly in robotics because it allows dc motors to run forward and backwards.
The H Bridge is a great device because it allows the use of pwm signal to control the speed of the robot. PWM involves using digital control to create a square wave for, this is a signal that is switched between on and off. This on-off pattern can simulate voltages in between 5v and 0v by means of changing the portion of the time spends off versus the time it spends on. Using the pwm in arduino, all you need to do is write a value from 0 to
255 on a pwm pin, and arduino library will cause the pin to output a pwm signal whose on time is in proportion to the value written. When writing an output voltage the 0 – 255 value has no meaning. What we really need is the voltage. Arduino runs at Vcc of 5V. In that case, a value of 255 will also equal 5v. This is a simple formula needed
PinValue (0-255) = 255 * (AnalogVolts / 5);
I also used the QTR-8A reflectance sensor Array. The QTR-8A reflectance sensor array requires analog inputs to take readings. This sensor module has 8 IR LED/phototransistor pairs mounted on a 0.375″ pitch, making it a great detector for a line-following robot. Pairs of LEDs are arranged in series to halve current consumption, and a MOSFET allows the LEDs to be turned off for additional sensing or power-savings options. Each sensor provides a separate analog voltage output. The outputs are all independent, but the LEDs are arranged in pairs to halve current consumption. The LEDs
are controlled by a MOSFET with a gate normally pulled high, allowing the LEDs to be turned off by setting the MOSFET gate to a low voltage.

ELECTRICAL ASPECT:

This step required the understanding of how to connect the H-bridge chip to the motor,
the breadboard then the arduino. I placed the 16 pin H-bridge into the breadboard, then I
connected the pwm pins (2,7,15 & 10) to the Arduino pins (5,6,10 & 11). I then
connected H bridge pins (1,16 & 9) to power on the breadboard. I connected pin (4,5,13
& 12) to ground. Pins (14 and 11) were then connected to the left motor. Pins (3 & 6)
were connected to the right motor. Then I connected pin 8 to a separate 6 volts battery
pack.
The electrical connection of the sensor was very straightforward and easy, since I was
using 6 sensors from the original 8 sensors. I had 9 connectors from the sensor I
connected sensor output 1 to 6 to Arduino analog pin A0 to A5. Then I connected the
sensor ground to ground on the breadboard and sensor power to power on the breadboard.
I did not connect the led pin to anything i left it out.

The last thing I did was to connect 9v battery pack to the arduino Vcc and ground or I
could connect it directly to the arduino power jack.
PROGRAMMING ASPECT:

The programming aspect was the most difficult part of this project. The 1st thing I did was to define the pwm pins then in the setup I called the pwm pins as OUTPUT. Then I started my loops called sensor and driver. In the function called driver I defined my lowvalue as 60, which is white, and my highvalue as 300, which is black. I then stored the analog read of A0 – A5 in s0 to s5. The next thing I did was to start the if statement for each possible conditions.

My sensor function was not really for driving the robot but was for me to see what the sensor is currently reading by using the serial print. I know I can comment this section out and use it only for testing the sensor but I prefer seeing it running. There is a possibility that it might be making the program run slow.

CODE:

 

#define INA_2 10 //This is the left forward control of the robot, I defined each pwm 
pin that is connected to the arduino 
#define INB_2 11 //This is the left backward control of the motor, e.g set this high and 
INB_1 high and others low you will do a reverse 
#define INA_1 5 //This is the right forward control of the motor 
#define INB_1 6 //This is the right backward control of the motor 

void setup() 
{ // This is always the 1st void function in an arduino program 

Serial.begin(9600); 

pinMode(INA_2, OUTPUT); //each pwm pin was placed as an output for the 

program 
pinMode(INB_2, OUTPUT); 
pinMode(INA_1, OUTPUT); 
pinMode(INB_1, OUTPUT); 

} 

void loop() { //This is a loop shows the sensor and the driver funtion 

sensor(); 

driver(); 

} 

void driver() //This is the driver function, it reads the sensors and drives the motor 
using pwm signal 

{ 

int lowVal = 60; // i did set my sensor low value to be 60 note this is not the 
lowest value the sensor can go 

int highVal = 300; // i did set my sensor high value to be 300 note this is not the 
highest value the sensor can go 

int s0 = analogRead(A0); //This defines the sensor value s0, it puts the reading of 
analogread(A0) into s0 

int s1 = analogRead(A1); 

int s2 = analogRead(A2); 

int s3 = analogRead(A3); 

int s4 = analogRead(A4); 

int s5 = analogRead(A5); 

//////////////////////////////////////////////// 

if ((s2 >= highVal) && (s3 >= highVal)) //This is the 1st if statement of the program, 
it says if s2 and s3 see's a black line robot should move straight at highest speed 

{ 
analogWrite(INA_2, 255); 
analogWrite(INB_2, 0); 
analogWrite(INA_1, 255); 
analogWrite(INB_1, 0); 

} 

else if ((s3 >= highVal) && (s4 >= highVal)) 

{ 
analogWrite(INA_2, 200); 
analogWrite(INB_2, 0); 
analogWrite(INA_1,240); 
analogWrite(INB_1, 0); 

} 

else if ((s4 >= highVal) && (s5 >= highVal)) 

{ 
analogWrite(INA_2, 0); 
analogWrite(INB_2, 0); 
analogWrite(INA_1, 200); 
analogWrite(INB_1, 0); 

} 

else if((s1 >= highVal) && (s2 >= highVal)) 

{ 
analogWrite(INA_2, 200); 
analogWrite(INB_2, 0); 
analogWrite(INA_1, 255); 
analogWrite(INB_1, 0); 

} 

else if ((s0 >= highVal) && (s1 >= highVal)) 

{ 
analogWrite(INA_2, 200); 
analogWrite(INB_2, 0); 
analogWrite(INA_1, 0); 
analogWrite(INB_1, 0); 

} 

else if(s0 >= highVal) 

{ 
analogWrite(INA_2, 200); 
analogWrite(INB_2, 0); 
analogWrite(INA_1,0); 
analogWrite(INB_1, 0); 

} 

else if(s1 >= highVal) 

{ 
analogWrite(INA_2, 200); 
analogWrite(INB_2, 0); 
analogWrite(INA_1,0); 
analogWrite(INB_1, 0); 

} 

else if(s2 >= highVal) 

{ 
analogWrite(INA_2, 255); 
analogWrite(INB_2, 0); 
analogWrite(INA_1,255); 
analogWrite(INB_1, 0); 

} 

else if(s3 >= highVal) 

{ 
analogWrite(INA_2, 255); 
analogWrite(INB_2, 0); 
analogWrite(INA_1,255); 
analogWrite(INB_1, 0); 

} 

else if(s4 >= highVal) 

{ 
analogWrite(INA_2, 0); 
analogWrite(INB_2, 0); 
analogWrite(INA_1, 200); 
analogWrite(INB_1, 0); 

} 

else if(s5 >= highVal) 

{ 
analogWrite(INA_2, 0); 
analogWrite(INB_2, 0); 
analogWrite(INA_1,200); 
analogWrite(INB_1, 0); 

} 

else if((s0 <= lowVal) && (s1 <= lowVal) && (s2 <= lowVal) && (s3 <= lowVal) 
&& (s4 <= lowVal) && (s5 <= lowVal)) //This says if its all white light the robot should 
make a 180 degree left turn 

{ 
analogWrite(INA_2, 0); 
analogWrite(INB_2, 200); 
analogWrite(INA_1,200); 
analogWrite(INB_1, 0); 

} 

} 

void sensor(){ //This function basically prints the sensor reading so you can see 
what sensors are working. 
int s0 = analogRead(A0); 
int s1 = analogRead(A1); 
int s2 = analogRead(A2); 
int s3 = analogRead(A3); 
int s4 = analogRead(A4); 
int s5 = analogRead(A5); 

Serial.print("s0 "); //This prints out the reading for each sensor 
Serial.print(s0); 
Serial.print(" s1 "); 
Serial.print(s1); 
Serial.print(" s2 "); 
Serial.print(s2); 
Serial.print(" s3 "); 
Serial.print(s3); 
Serial.print(" s4 "); 
Serial.print(s4); 

Serial.print(" s5 "); 

Serial.print(s5); 

Serial.print(' '); 

Serial.println(); 
}

Conclusion:
I ran into lots of problems in this project. The wheels I bought is very slippery and it slides in some surfaces. We fixed this problem by placing some extra weight on the robot. We also connected the pwm pins of the driver to the wrong digital pins of the Arduino, which aren’t pwm pins.I later figured that problem out. There was the problem of connecting the sensors to the Arduino, I didn’t know where to connect the sensor power to. The biggest problem of all was the programming. I was able to program the
Arduino to control the motors but without the pwm signal. We also could not make the sensor work using the analog read. We was later able to control the speed of the motor using the analog write and read the sensor using the analog read. I ran into lots of problems in building this robot, but it was worth it because i gain some valuable experience.

Leave a Reply