Line Follower
Line Follower
Line Follower
PROJECT
MASTER OF SCIENCE
in
at
FALL
2010
LINE FOLLOWING AND OBSTACLE AVOIDING CAR
A Project
by
Approved by:
____________________________
Date
ii
Student: Manan Shaileshkumar Shah
I certify that this student has met the requirements for format contained in the University
format manual, and that this project is suitable for shelving in the Library and credit is to
iii
Abstract
of
by
Robots are becoming important in various fields. Robots can help humans in places
where human access is limited, like deep-sea exploration, hazardous waste site, and
thermal/nuclear power plants. Path tracing and obstacle avoiding robots are intelligent
robots that can perform desired tasks in unstructured environments by finding and
avoiding them without any human guidance. The purpose of this project is to design
autonomously driven car, which can perform path tracing and obstacle avoiding
controlled by microcontroller. IR sensors track the path and ping ultrasonic sensor sense
the obstacle on the line path. If there is any obstacle, the car will automatically divert its
position to left or right. This project is very useful for industries where automated
supervision is required.
_______________________
Date
iv
ACKNOWLEDGMENTS
With all due respect, I would like to thank my project advisor, Dr. Jing Pang for
assigning me very interesting project of Line Following and Object avoiding Car. She has
always encouraged me to work hard. It would be impossible to finish this project without
her valuable time, help, timely guidance and moral support. Her timely advice helped me
I would also like to thank Dr. Preetham Kumar for spending his valuable time on
In the end, I would like to thank my family members especially my parents for
supporting me during the time of project, and providing constant support and warmth for
v
TABLE OF CONTENTS
Page
Acknowledgments..................................................................................................................... v
List of Tables ........................................................................................................................ viii
List of Figures .......................................................................................................................... ix
Chapter
1. INTRODUCTION .............................................................................................................. 1
1.1 Introduction............................................................................................................ 1
1.2 Organization of the Report..................................................................................... 2
2. BASIC DESIGN AND REQUIREMENTS ........................................................................ 3
2.1 Block Diagram ....................................................................................................... 3
2.2 Line Following & Obstacle Avoiding Car structure .............................................. 4
2.2.1 I.R. LED & Phototransistor ................................................................... 4
2.2.2 DC Motors ............................................................................................. 8
2.2.3 Differential Drive Mechanism ............................................................. 10
2.2.4 H-Bridge .............................................................................................. 11
3. ATMEL ATMEGA168 MICROCONTROLLER ............................................................ 16
3.1 Overview.............................................................................................................. 16
3.2 Description ........................................................................................................... 16
3.2.1 Pin Descriptions ................................................................................... 17
3.3 Block Diagram ..................................................................................................... 19
3.4 ATmega168 CCP Modules .................................................................................. 21
4. PING ULTRASONIC RANGE SENSOR ........................................................................ 24
4.1 What is PING Ultrasonic Range Sensor? ............................................................ 24
4.2 Working of Ping Ultrasonic Range Sensor ......................................................... 24
4.3 Practical Applications and Limitations ................................................................ 25
5. DESIGN OF OBSTACLE AVOIDING CAR .................................................................. 27
5.1 Implementation of Obstacle Avoiding Car ......................................................... 28
5.2 Flowchart of the Design ...................................................................................... 28
5.3 Results ................................................................................................................. 30
vi
6. CONCLUSION ................................................................................................................. 34
References…............................................................................................................................ 35
vii
LIST OF TABLES
Page
viii
LIST OF FIGURES
Page
4. Figure 2.4 Line Following with More than One Sensor Pair……………………. 7
ix
20. Figure 4.1 The Ping Sensor….……..………………………………………...…. 24
x
1
Chapter 1
INTRODUCTION
1.1 Introduction
Line following car is a system that traces black lines on white surfaces. There are
various sensing methods available to sense these lines. The choice of these schemes
depends upon sensing accuracy and required flexibility. In my project, I have used 6 Tx
LEDs and 6 IR sensors to accurately calculate the position of the robot on the tape.
Sensors keep sensing the line and microcontroller system will keeps maneuvering the
robot to stay on the course. In addition, microcontroller constantly keep correcting wrong
moves using feedback from previous states, making it an effective closed loop system.
The core of the car is ATMEGA168 microcontroller from ATMEL. To detect the
line, 6 IR LEDS (Tx) and 6 IR sensors (Rx) are used with distance between each sensor is
25mm. The Tx will emit the light, this light will be reflected by the black line and Rx will
There are many ways to move a car [1]. In addition, I used differential steering
method. It relies on two back-wheels, DC motor is connected to each of the two wheels
on the back and front wheels were kept free for easy turning. For forward drive, both
motors are given same voltage. In addition, for right turn, the voltage on the right wheel
needs to be reduced to decrease the rotation of the wheel. Exact opposite is applied for
left turn.
2
Chapter two of the report explains the fundamentals of line following and
Obstacle avoiding car structure. It explains in detail about DC Motors, Differential Drive
Chapter four explains about Ping))) ultrasonic sensor used for object detecting
and avoiding. Use of this sensor in this project and the limitations are also discussed.
Chapter five contains information about the implementation of the algorithm for
object avoiding car. It discusses about various steps of algorithm in detail. Suitable
images are also provided to show output at different stages in applied algorithm.
Chapter six presents the result of the project, challenges, limitations and future
Chapter 2
BASIC DESIGN AND REQUIREMENTS
This car it built using ATmega168, IR sensors, Motor Driver (LM293D) and
aluminum chassis [3]. At the bottom, it has line sensor array to detect the line and send
ATmega168 and motor driver L293D are used to control the motors.
1) Capture the black line with the help of mounted sensors on the car. The sensors used
2) Steer the car to track the line with differential steering method. This is achieved by
Structure of the line following car can be divided into several parts as follows.
• DC geared motors
• Motor Drivers
• ATmega168 microcontroller
Sensors are important components in the process of making line follower [3].
There are different types of sensors available to use for line following car. When
selecting sensors, three characteristics should be kept in mind: response time, sensitivity
As I.R. LEDs have good response time [3], they are used as sensors in this
project. Moreover, they are less sensitive to ambient light. A photodiode has a p-i-n
structure. When an infrared photon of sufficient energy strikes on the diode, it excites an
electron and creates free electron and hole. Due to absorption in depletion region of the
5
junction, holes move toward the anode and electrons move toward the cathode, which
generates photocurrent [4]. The LED and detector have very narrow emission and
detection angles, so their placement is very important. Best method to place them is to
place them in parallel and also close to the track surface. In this case, they are placed
apart by 5-7 mm. Figure 2.2 shows a pair of similar type of I.R LED and sensor.
To get accurate result for line tracing more than one Tx/Rx pairs are used. Use of
fewer numbers of pair will make the car wobble about the line and may not be fast
Figure 2.3 Line Following with Only One Sensor Pair [3]
Figure 2.3 shows most basic algorithm of line following, which includes only one
sensor. The sensor is placed on the right. When sensor detects no line, it will move the
car to the left, after this, when sensor detects the line, it will move the car to right. As a
result, the car with algorithm will wobble along the line and the detection speed will also
be slow. [3]
7
Figure 2.4 Line Following with more than One Sensor Pair [3]
The modification of the method used as in Figure 2.3 is to use more number of
sensors. If the sensors are used on the both side of the line as shown in Figure 2.4, then
8
line detection will be less wavering along the line. So if left sensor senses the line then it
will move the car on the left and if right sensor sense the line then it will move the car on
the right [3]. To get more precise line tracing result, Line Sensor Array is used. In this
project, 7 sensors are totally used. They are placed in the form shown in Figure 2.5. The
sensors are mounted on a seperated board along with the biasing amplifiers and a power
is provided with 2 pin connector. Output of each sensor is connected to the main board
via an 8-pin connector to the comparators on the main board. To prevent the interference
2.2.2 DC Motors
DC motors are widely used, inexpensive, small and poweful for their size. They
are most easy to control. One DC motor requires only two singals for its operation. They
are non-polarized, means you can reverse the voltage without any damage to motor. DC
motors have +ve and –ve leads. Connecting them to a DC voltage source moves motor in
one direction (clockwise) and by reversing the polarity, the DC motor will move in
9
rpm (rotation per minute). It has two rpms: no load and loaded. The rpm is reduces when
moving a load or decreases when load increases. Other specifications of DC motors are
voltage and current ratings. Table 2.1 shows the specifications of the motor used in the
project.
Characterisitcs Value
Operating Voltage 3V to 6 V
Operating Current 3A Max.
Speed 180 RPM
Torque 30 gm-cm
Speed of the motor can be changed by changing the applied voltage across motor.
DC motors don’t have enough torque to drive the car directly by connecting wheels with
it. As noticed in table 2.1, the torque provide can hardly move 30 gm of weight. This is
not acceptable if requred load capacity is in Kgs. To achieve more torque by gears, the
speed will be reduced and effectively increase the torque. One more advantage of using
gearbox motors instead of DC motors is it has gears and an axle, so speed does not
change towards uphill or downhill. It is noteable that the more speed is, the less precesion
will be. Figure 2.6 shows the DC geared motor used in this project.
10
This is the most common locomotion scheme used for building cars. It is a
method of controlling a car with only two polarized wheels. It is easy to build, easy to
control and permits the car to move in all directions. In this method two motors are
connected to each left and right wheels at car’s base. These two motors are responsible of
driving the car in desired direction. This system allows car to spin in its place [6].
The term “differential” means that car’s turning speed is determined by the speed
difference between both wheels. Figure 2.7 shows in a simplified way the principle of
operation of differential drive. If both motors rotate in the same direction at equal speeds
the car will move forward or backwards based on the rotation of the wheel. If the speed
of one motor is faster then the other, the car will turn in the direction of the slower motor.
If both motor rotates in opposite directions, the car will spin in its place [7].
This kind of two wheel differential drive system needs one or more caster wheels
(free wheels) to support the rest of the chassis while freely following the movement of
the robt engaged by the two main drive wheels. In this perticular project, only one caster
2.2.4 H-Bridge
side of the load and the H-bridge DC motors allow the car to run backwards or forwards.
the direction of the current through the motor. Figure 2.9 shows simplified H-bridge as
switches. The states of these four switches can be changed in order to change the voltage
across the motor, of the current flow and the rotation of motor [7] [8].
12
In Figure 2.9, all switches are open and the motor terminals are disconnected from
the circuit. This state allows the motor to spin freely. If we open switches S1 & S4 and
close S2 & S3 as in first part of Figure 2.10 there will be current flow across the circuit
and motor will run. But if S1, S4 are close and S2, S3 are open, the voltage across the
motor will switch around and that will cause the motor to rotate in the opposite direction.
Table 2.2 summarizes the basic operation of the H-bridge depending upon the voltage
S1 S2 S3 S4 Result
1 0 0 1 Motor moves right
0 1 1 0 Motor moves left
0 0 0 0 Motor free runs
0 1 0 1 Motor brakes
1 0 1 0 Motor brakes
To control the speed and direction of the DC motor from the microcontroller, this
simple H-bridge will be of no use. H-bridge which make use of transistors works best for
robotics projects. These transistors work as switch and they can control the current flow
The difference between the mechanical switch and the transistor switch is that
mechanical switch can be turned on or off mechanically but a transistors switch can be
turned on or off by applying small current at the base. For an NPN transistor, when a
small current of 20mA is applied to the base of the transistor, current will flow from
14
collector to emitter. In case of, for PNP transistor, the current will flow from emitter to
collector. For transistor to work as switch, the applied voltage at base needs to be higher
than collector voltage for NPN transistor and lower than collector voltage for PNP
transistor [9].
integrated, high voltage, high current, 4-channel driver” [10]. The L293D supports two
DC motors. Pin 8 is voltage for the motors and pin 16 is the +5 voltage for the chip. First
motor is connected between pin 3 and 6. The motor is turned on by sending a high signal
to both the enable (pin 1) pin and one of the two direction pins, i.e. pin 2 or pin 7. To stop
motor, the enable pin is high and both pin 2 and pin 7 are low [11].
The same goes for the other side of the chip. When using two motors, the best
practice is to connect pins 2 and 15 togather and pin 7 and 10. Figure 2.13 shows the
Motor drivers are the simplest modules that provide power amplification for low-
level control singals like PWM and direction supplied by the user.
16
Chapter 3
3.1 Overview
Its features includes: 23 general purpose I/O lines, 32 general purpose working
512 bytes of EEPROM and 1K bytes SRAM. In Idle mode CPU stops working while
allowing the SRAM, timers/counters, USART, SPI port and interrupt system to continue
functioning. It also has 6 channel 10-bit ADC, a programmable watchdog timer with
3.2 Description
“The on-chip ISP flash allows the program memory to be reprogrammed in-system
VCC
GND
PORT B (PB7:0)
Port B is an 8-bit bi-directional I/O Port with internal pull-up resistors. As Inputs,
Port B pins that are externally pulled low will source current if the pull-up resistors are
activated [12].
Depending on the clock selection fuse settings, PB6 can be used as input to the
inverting oscillator amplifier and input to the internal clock operating circuit [12].
18
Depending on the clock selection fuse settings, PB7 can be used as output from
PORT C (PC5:0)
Port C is a 7-bit bi-directional I/O port with internal pull-up resistors. As inputs,
Port C pins that are externally pulled low will source current if the pull-up resistors are
activated [12].
PC6/RESET
If RSTDISBL is not programmed, PC6 can be used as a Reset input. A low level
on this pin for longer than the minimum pulse length will generate a reset even without
the clock signal. Shorter pulses are not guaranteed to generate a Reset [12].
PORT D (PD7:0)
Port D is an 8-bit bi-directional I/O port with internal pull-up resistors. As inputs,
Port C pins that are externally pulled low will source current if the pull-up resistors are
activated. The Port D pins become tri-stated if the reset condition become active, even if
AVCC
AVCC is the supply pin for the A/D Convertor, PC[5:0]. It should be externally
connected to VCC, even if the ADC is not used. If the ADC is used it should be connected
AREF
XTAL1
It is an input to the inverting oscillator amplifier and the internal clock circuit [2].
XTAL2
Oscillator Characteristics
amplifier that can be configured for use as an on-chip oscillator. To use external
oscillator as clock source, XTAL2 should be left unconnected while XTAL1 is driven.
Figure 3.2 shows the block diagram of the ATMEL ATmega168 microcontroller.
The AVR core has 32 general-purpose registers. All these registers are directly connected
to the Arithmetic Logic Unit (ALU), allowing two independent registers to be accessed in
20
one single instruction executed in one clock cycle. The resulting architecture is code
efficient [12]
technology. “The on-chip ISP flash allows the program memory to be reprogrammed in-
master/slave duty cycle register. The CCP modules are identical in operation, with the
Most registers and bit references for this IC are written in general form. For
example, a lower case “n” replaces the Timer/Counter number, and a lower case “x”
replaces the output compare unit channel. When these registers or bits are defined in a
program, they are declared as TCNT2 for accessing Timer/Counter2 counter value and so
on. Figure 3.3 shows a block diagram for the 16-bit Timer/Counter [12].
3.4.1 Registers
For this project, TCCR1A register value is changing many times. Main function
used was to drive the motor in forward and reverse direction. For forward direction,
TCCR1A is set to 0x81. As a result the value of WGM[13:10] is “0101”. With this value,
the timer/counter will work in 8-bit Fast PWM and the value of COM1A[1:0] value is set
to “10” to clear OC1A port upon the compare match. For reverse driving TCCR1A is set
to 0x21 with same 8-bit Fast PWM mode. To stop the motor TCCR1A is set to 0x00.
For this project, I did not use any scaling of the clock. CS[10:12] is set to “001”.
23
These bits are used in conjunction with TCCR1A Control Register bits
Chapter 4
The Ping Sensor is a device to measure the distance between sensor and the
object. With a range of 3 centimeters to 3 meters, it is easy to use in most of the robotics
projects that involve distance measure or object avoiding. Its accuracy is half centimeter
that means it can easily detect the object’s distance. Figure 4.1 shows PING Ultrasonic
Sensor [13].
Figure 4.2 shows how the Ping sensor sends a brief chirp with its ultrasonic
speaker and makes it possible for the basic stamp to measure the time it takes the echo
from the object to reach the microphone of the sensor. Controller will send a signal to
Ping sensor to start the measurement. Then, the Ping sensor waits for the controller to
start the Pulsein command. At the same time, the sensor will chirp 40 kHz tone and sends
When ping sensor detects the echo with its ultrasonic microphone, it changes that
high signal back to low, and the controller will store the time signal high pulse
information. That time measurement tells how long it takes sound to travel to the object
and back. With this measurement, the speed of light can be considered as reference and
the distance of the object can be measured in the desired unit (centimeter, feet etc…)
[13].
such as measuring the distance of the object, then based upon distance measuring or
detecting obstacle, object avoidance can be implemented too. It can use to implement
When using Ping ultrasonic range sensor, position of the target object is also
important. If the object is placed as shown in Figure 4.3, then Ping microphone will not
be able to hear the reflected signal and the object detection will be a problem. Also even
if the distance between object and receiver is more than 3 meters, the object detection
26
will be a problem. Also, if Ping detector is placed very low near the surface, then it may
Moreover, the target object material also plays an important role when using Ping
ultrasonic sensor. Sound absorbing objects, objects with soft or irregular surface such as
stuffed animals and pillows may not reflect the sound with enough intensity to make
detection easy. Ping detector should not be used outside in wet environment.
Chapter 5
This chapter describes the behavior and architecture of the obstacle-avoiding car.
In this project, the car follows the black track using line following sensors and it also
look for the objects/obstacles coming on the path. The car will choose its own path
whereby if it meets obstacle on the path. Ping ultrasonic sensor helps measuring the
Figure 5.1 shows the circuit diagram for the obstacle-avoiding car, Ping ultrasonic
sensor will chirp the sound at 38 KHz, but it is inaudible to human ears. Once it sends the
sound signal out, it will send high signal to INT0 pin of the controller to set the interrupt
The microphone of the ping ultrasonic sensor waits for the reflecting signal from
the object. Once it receives the echo, it will send a signal to INT0 interrupt to set INT0 to
“0”. Next, the total time for which signal was high will give the distance between sensor
and the object. If that distance is less than 10 cm, then INT0 will send a signal to OC1A
and OC1B registers to change the speed of the motors and so that motor will turn
approximately 90° right. That means the right motor connected to OC1B will rotate at
lower speed then compared to left motor which allows differential drive with right turn.
Figure 5.1 shows the circuit diagram for the design. In that design Vout pin of the
ultrasonic sensor is connected to pin 4 (INT0) of the microcontroller via 330 ohm
resistor. I used 0.1 μF bypass capacitor on the output pin +5 V of the voltage regulator
L7805 to smooth out the supply voltage to microcontroller. PWM output from
microcontroller is given to pin 2 of the H-bridge. H-Bridge acts as a high power switch.
This H-Bridge switches on and off at PWM frequency and controls the speed of the
motors. There is a LED connected to Vout pin and microcontroller pin so that if there is a
signal then LED will glow and it will indicate the activity of the sensor.
Figure 5.2 shows flowchart of the design. When program executes the timer-
interrupt routine INT0 is initialized after reset. Ping ultrasonic sensor will transmit the
37Khz wave and it will send high signal to INT0 Interrupt Handler to start the counter.
29
Start
Timer-Interrupt Handler
Transmit Ultrasonic-wave at 37
Khz
No
Receive Echo
Yes
Yes Turn 90
Distance < 10
degrees
cm ?
RIght
No
Continue on Route
Now, ultrasonic microphone will scan for the echo from the object. Once echo is
received, INT0 interrupt pin will be set low. Now the total time difference between the
signal was high will be stored in the register. And the distance between the sensor and
30
object will be calculated in reference to the speed of the light. If the calculated distance is
less than 10 cm, then controller will set OC1A to low which is connected to right motor.
So now the car will take right turn on its axis. Car has successfully avoided the obstacle
and car will continue tracing the path and keep looking for the obstacle.
5.3 Results
Figure 5.3 shows the first part of the project. Sensor array is attached at the
bottom of the car to sense the black line. Breadboard on the top of the car contains the
Figure 5.4 shows the working model of the line following car. The glowing LEDs
shows that the IR sensors have detected the black tape on the floor and these sensors send
32
the data to microcontroller and microcontroller controls the motor speed of the left and
Figure 5.5 shows the second part of the project, obstacle-avoiding car. Ping
ultrasonic sensor was drawing more power, so instead of giving one 9V DC power supply
to whole car, each part were given different power supply. The code includes the
calculation of the object with the average value calibration because Ping ultrasonic sensor
is calculating distance very rapidly and there may be fluctuation is the result. The time it
took for the ultrasonic wave to travel back from the object will be in microsecond. To
calculate the distance in centimeter I took the median of the sample data taken from the
test. For every 40 cm, it took 2150 microsecond for the ultrasonic wave to comeback.
33
55.
34
Chapter 6
CONCLUSION
Line following and object avoiding car is successfully completed. Many efforts
were put into design, implementation and days of toil in front of computer writing and
debugging the code. During the project, I did a lot of research work on the architecture
microcontroller programming.
In the future, more complicated control of the car can be explored. More
advanced sensors may also be utilized to control the care more accurately.
35
REFERENCES
[2] David Cook, “Robot Building for Beginners”, 2nd ed., Apress, pp 247-250, January,
2010.
[4] Newport Inc., “Optical Meters and Detectors”, User Guide, pp 1-7, Sept 2009
[6] Ikalogic.com, “Robot Driving Mechanism”, Retrieved on November 18, 2010 From
the World Wide Web:
http://www.ikalogic.com/tut_mech_1.php
[8] Vibhor Gupta, "Working and Analysis of the H – Bridge Motor Driver Circuit
Designed for Wheeled Mobile Robots", IEEE, 2010.
[9] Ikalogic.com, “Robot Driving Mechanism”, Retrieved on November 18, 2010 From
the World Wide Web:
http://www.ikalogic.com/h_bridge_old.php
[11] Claudio Miklo, “DC Motor & H-Bridge Connections”, Retrieved on November 18,
2010 From the World Wide Web:
http://www.miklos.blog.br/2009/11/controlar-motores-dc-com-l293d.html