You are on page 1of 26

LineFollowing

Robot

By,
PriyankPatil
DepartmentofInformationTechnology
K.J.SomaiyaCollegeofEngineering

Mumbai,India

LineFollower
l
o
w
C
e
r
o
?
n 2.2. W
h
te
y
b
n
u
ts
i
l
d
a
1.
l
i
n
e
f
o
l
l
o
w
2.
e
r
?
2.3.
B
a
c
k
g
r
o
u
n
d
2.1.
2.4.
P
r
e
r
e
q
u
i
s
i
t
e
s
2.5.
T
h
e

3.2.

t
u
r
a
l
O
v
e
r
v
i
e
w

T
h
e
A
l
g
o
r
i
t
h
m

3.

4. I
3.1.

4.1.

4.2.

m
p
l
e
m
e
n
t
a
t
i
o
n
S
e
n
s
o
r
C
i
r
c
u
i
t
M

e
I
m
p
r
o
v
e
m
e
n
t
s

6. R

4.3.
6.1.

5. 6.2.

e
f
e
r
e
n
c
e
s
a
n
d
R
e
s
o
u
r
c
e
s
B
o
o
k
s
a
n
d
L
i
n
k
s
T
o
o
l
s
o

6.4.

6.3.

c
s
h
o
p
s
P
a
r
t
s
a
n
d
P
r
i
c
e
s

P
a
g
e
2
of
1
7

LineFollower

Summary
ThepurposeofthisdocumentistohelpyoubuildaLineFollowingRobot.
Startingwithanoverviewofthesystemthedocumentwouldcoverimplementation
detailslikecircuitsandalgorithms,followedbysomesuggestionsonimprovingthe
design.
TheReferenceandResourcespagehasalistofrelevantbooks,websites,electronic
shopsandcommonlyusedparts&theirprices.

Page3of
17

LineFollower

Introduction
Whatisalinefollower?
Linefollowerisamachinethatcanfollowapath.Thepathcanbevisiblelikeablack
lineonawhitesurface(orviceversa)oritcanbeinvisiblelikeamagneticfield.
Whybuildalinefollower?
Sensingalineandmaneuveringtherobottostayoncourse,whileconstantlycorrecting
wrong moves using feedback mechanism forms a simple yet effective closed loop
system.Asaprogrammeryougetanopportunitytoteachtherobothowtofollowthe
linethusgivingitahumanlikepropertyofrespondingtostimuli.
Practicalapplicationsofalinefollower:Automatedcarsrunningonroadswith
embeddedmagnets;guidancesystemforindustrialrobotsmovingonshopflooretc.
Prerequisites:
Knowledgeofbasicdigitalandanalogelectronics.
(AcourseonDigitalDesignandElectronicDevices&Circuitswouldbehelpful)
CProgramming
Sheerinterest,aninnovativebrainandperseverance!
Background:

I started with building a parallel port based robot which could be controlled
manuallybyakeyboard.Ontherobotsidewasanarrangementofrelaysconnectedto
parallelportpinsviaoptocouplers.
Thenextversionwasatruecomputercontrolledlinefollower.Ithadsensors
connectedtothestatuspinsoftheparallelport.Aprogramrunningonthecomputer
polledthestatusregisteroftheparallelporthundredsoftimeseverysecondandsent
controlsignalsaccordinglythroughthedatapins.
Thedrawbacksofusingapersonalcomputerweresoonclear
Itsdifficulttocontrolspeedofmotors
Ascablelengthincreasessignalstrengthdecreasesandlatencyincreases.
Alongmulticorecableforparalleldatatransferisexpensive.
TherobotisnotportableifyouuseadesktopPC.
Theobviousnextstepwastobuildanonboardcontrolcircuit;theoptionsa
hardwired logic circuit or a uC. Since I had no knowledge of uC at that time, I
implementedahardwiredlogiccircuitusingmultiplexers.Itbasicallymappedinputfrom
foursensorstofouroutputsforthemotordriveraccordingtoatruthtable.Thoughit
workedfine,itcouldshownointelligencelikecomingbackonlineafterlosingit,or
doingsomethingspecialwhensaythelineended.Togetaroundthisproblemandadd
somecoolfeatures,usingamicrocontrollerwasthebestoption.

Page4of
17

LineFollower

TheAVRmicrocontroller:
Atmel's AVR microcontrollers have a RISC core running single cycle
instructions and a welldefined I/O structure that limits the need for external
components.Internaloscillators,timers,UART,SPI,pullupresistors,pulsewidth
modulation,ADC,analogcomparatorandwatchdogtimersaresomeofthefeatures
youwillfindinAVRdevices.

AVRinstructionsaretunedtodecreasethesizeoftheprogramwhetherthecode
iswritteninCorAssembly.WithonchipinsystemprogrammableFlashand
EEPROM,theAVRisaperfectchoiceinordertooptimizecostandgetproduct
tothemarketquickly.
http://www.atmel.com/products/avr/
ApartformthisalmostallAVRssupportInSystemProgramming(ISP)i.e.you
canreprogramitwithoutremovingitfromthecircuit.Thiscomesveryhandy
whenprototypingadesignorupgradingabuiltupsystem.Alsotheprogrammer
usedforISPiseasiertobuildcomparedtotheparallelprogrammerrequiredfor
manyolduCs.MostAVRchipsalsosupportBootLoaderswhichtaketheidea
2

ofInSystemProgrammingtoanewlevel.FeatureslikeI Cbusinterfacemake
addingexternaldevicesacakewalk.WhilemostpopularuCsrequireatleastafew
external components like crystal, caps and pullup resistors, with AVR the
numbercanbeaslowaszero!
Cost:AVR=PIC>8051(by8051Imeanthe8051family)
Availability:AVR=PIC<8051
Speed:AVR>PIC>8051
BuiltinPeripherals:ThisoneisdifficulttoanswersincealluCfamiliesoffer
comparablefeaturesintheirdifferentchips.Forajustcomparison,Iwouldrather
saythatforagivenpriceAVR=PIC>8051.
ToolsandResources:8051hasbeenaroundfrommanyyearsnow,consequently
therearemoretoolsavailableforworkingwithit.Beingapartofmany
engineeringcourses,thereisahugecommunitiyofpeoplethatcanhelpyouout
with8051;samewithbooksandonlineresources.Inspiteofbeingnewthe
AVRhasaneattoolchain(SeeReferencesandResources).Availabilityof
onlineresourcesandbooksisfastincreasing.
Here,8051>AVR=PIC
Page5of
17

LineFollower

Overview

BlockDiagram
TherobotusesIRsensorstosensetheline,anarrayof8IRLEDs(Tx)andsensors(Rx),facing
thegroundhasbeenusedinthissetup.Theoutputofthesensorsisananalogsignalwhich
dependsontheamountoflightreflectedback,thisanalogsignalisgiventothecomparatorto
produce0sand1swhicharethenfedtotheuC.
L4
Left

L3 L2

L1 R1 R2
Center
SensorArray

R3

R4
Right

Startingfromthecenter,thesensorsontheleftarenamedL1,L2,L3,L4andthoseontheright
arenamedR1,R2,R3,R4.
Letusassumethatwhenasensorisonthelineitreads0andwhenitisoffthelineitreads1
TheuCdecidesthenextmoveaccordingtothealgorithmgivenbelowwhichtriestoposition
therobotsuchthatL1andR1bothread0andtherestread1.
L4
1
Left

L3 L2
1
1

DesiredStateL1=R1=0,andRest=1

L1 R1
0
0
Center

R2 R3 R4
1
1
1
Right

Page6of
17

LineFollower
Algorithm:

1.

L=leftmostsensorwhichreads0;R=rightmostsensorwhichreads
0.IfnosensoronLeft(orRight)is0thenL(orR)equals0;
Ex:
L4 L3 L2 L1 R1 R2
1
0
0
1
1
1
Left
Center
HereL=3R=0
L4
1

L3 L2
1
0

Left

2.

Ifallsensorsread1gotostep3,
else,
IfL>RMoveLeft
IfL<RMoveRight
IfL=RMoveForward
Gotostep4

3.

MoveClockwiseiflinewaslastseenonRight
MoveCounterClockwiseiflinewaslastseenonLeft
Repeatstep3tilllineisfound.

4.

Gotostep1.

L1 R1 R2
0
0
0
Center
HereL=2R=4

Page7of
17

R3
1

R4
1
Right

R3
0

R4
0
Right

LineFollower

Implementation
SensorCircuit:

Theresistanceofthesensordecreases
whenIRlightfallsonit.Agoodsensor
will have near zero resistance in
presence of light and a very large
resistanceinabsenceoflight.
We have used this property of the
sensortoformapotentialdivider.
Thepotentialatpoint2is
Rsensor/(Rsensor+R1).
Again, a good sensor circuit should
givemaximumchangeinpotentialat
point2fornolightandbrightlight
conditions. This is especially
importantifyouplantouseanADC
inplaceofthecomparator
Togetagoodvoltageswing,thevalueofR1mustbecarefullychosen.If Rsensor=awhenno
lightfallsonitandRsensor=bwhenlightfallsonit.Thedifferenceinthetwopotentialsis:

Vcc*{a/(a+R1)b/(b+R1)}

Relativevoltageswing=ActualVoltageSwing/Vcc
1=
Vcc*{a/(a+R1)b/(b+R1)}/Vcc
1=
a/(a+R1)b/(b+R1)

Page8of
17

LineFollower

ThesensorIusedhada=930Kandb=36K.Ifweplotacurveofthevoltageswingovera
rangeofvaluesofR1wecanseethatthemaximumswingisobtainedatR1=150K(usecalculus
foranaccuratevalue).
Thereisacatchthough,withsuchhighresistance,thecurrentisverysmallandhencesusceptible
tobedistortedbynoise.Thesolutionistostrikeabalancebetweensensitivityandnoise
immunity.IchosevalueofR1as60K.Yourchoicewoulddependontheaandbvaluesof
yoursensor.
Ifyoufoundthispartconfusing,usea10Kresistorstraightaway,aslongasyouareusinga
comparatoritwontmattermuch.
MotorInterfaceandControlCircuit:

The8sensorsareconnectedtoPORTA.
YouneednotconnectanythingtoAVCCandAREF,itisrequiredonlyifADCisused.
TheL298MotorDriverhas4inputstocontrolthemotionofthemotorsandtwoenableinputs
whichareusedforswitchingthemotorsonandoff.TocontrolthespeedofthemotorsaPWM
waveformwithvariabledutycycleisappliedtotheenablepins.Rapidlyswitchingthevoltage
betweenVsandGNDgivesaneffectivevoltagebetweenVsandGNDwhosevaluedependson
thedutycycleofPWM.100%dutycyclecorrespondstovoltageequaltoVs,50%corresponds
to0.5Vsandsoon.The1N4004diodesareusedtopreventbackEMFofthemotorsfrom
disturbingtheremainingcircuit.ManycircuitsuseL293Dformotorcontrol,IchoseL298asit
hascurrentcapacityof2Aperchannel@45Vcomparedto0.6A@36VofaL293D.L293Ds
packageisnotsuitableforattachingagoodheatsink,practicallyyoucantuseitabove16V

Page9of17

LineFollower
withoutfryingit.L298ontheotherhandworkshappilyat16Vwithoutaheatsink,thoughit
isalwaysbettertouseone.

InternalSchematicofL298

TruthTableforcontrollingthedirectionofmotionofaDCmotor

Page10of17

LineFollower

SourceCode
/*****************************************************
Project : Line Follower
Version :
Date
: 2/19/2006
Author : Priyank
Company : Home
Comments:
Chip type
: ATmega16
Program type
: Application
Clock frequency
: 7.372800 MHz
Memory model
: Small
External SRAM size : 0
Data Stack size
: 256
*****************************************************/
//#define debug 1
#include <mega16.h>
#include <delay.h>
#ifdef debug
#include <stdio.h>
#endif
#define FWD 0xAA
#define REV 0x55
#define R 0x22 #define
L 0x88 #define CW 0x99
#define CCW 0x66
#define STOP 0x00
#define B 0xFF #define
RSPEED OCR1AL #define
LSPEED OCR1BL #define
SPEED0 255 #define
SPEED1 0 #define
SPEED2 0 #define
SPEED3 0 #define MAX 3

#define HMAX 1
void move (unsigned char dir,unsigned char delay,unsigned char power);
unsigned char i,rdev,ldev,ip,delay,dir,power,dirl,history[MAX],hcount=0,rotpow;

#ifdef debug
unsigned char rep=0,prev=0;
#endif
void main(void)
{

1/ Input/Output Ports initialization


2/ Port A initialization
3/ Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
Page11of
17

LineFollower
1/ State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;

2/ Port B initialization
3/ Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
4/ State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

5/ Port C initialization
6/ Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
7/ State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0xFF;

8/ Port D initialization
9/ Func7=In Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In
10/ State7=T State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x30;

11/ Timer/Counter 0 initialization


12/ Clock source: System Clock
13/ Clock value: Timer 0 Stopped
14/ Mode: Normal top=FFh
15/ OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

1/
2/
3/
4/
5/
6/
7/
8/

Timer/Counter 1 initialization
Clock source: System Clock
Clock value: 921.600 kHz
Mode: Fast PWM top=00FFh
OC1A output: Non-Inv.
OC1B output: Non-Inv.
Noise Canceler: Off

Input Capture on Falling Edge


TCCR1A=0xA1;
TCCR1B=0x0A;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0xFF;
OCR1BH=0x00;
OCR1BL=0xFF;

9/ Timer/Counter 2 initialization
10/ Clock source: System Clock
11/ Clock value: Timer 2 Stopped
12/ Mode: Normal top=FFh

13/ OC2 output: Disconnected


ASSR=0x00;
TCCR2=0x00;

Page12of
17

LineFollower
TCNT2=0x00;
OCR2=0x00;

1/
2/
3/
4/

External Interrupt(s) initialization


INT0: Off
INT1: Off
INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
#ifdef debug
1/ USART initialization
2/ Communication Parameters: 8 Data, 1 Stop, No Parity
1/ USART Receiver: On
2/ USART Transmitter: On
3/ USART Mode: Asynchronous
4/ USART Baud rate: 57600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x07;
#endif

5/ Timer(s)/Counter(s) Interrupt(s) initialization


TIMSK=0x00;

6/ Analog Comparator initialization


7/ Analog Comparator: Off
8/ Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
while (1){
#ifdef debug
if(rep<255) rep+
+; if(prev!
=PINA) {
prev=PINA;
printf("%u\r",rep);
for(i=0;i<8;i++) printf("%u\t",
(prev>>i)&0x01); rep=0;

}
#endif
if(PINA!=255){
rotpow=255;

ldev=rdev=0;
if(PINA.3==0)
rdev=1;
if(PINA.2==0)
rdev=2;
if(PINA.1==0)
rdev=3;

Page13of

17

LineFollower
if(PINA.0==0)
rdev=4;
if(PINA.4==0)
ldev=1;
if(PINA.5==0)
ldev=2;
if(PINA.6==0)
ldev=3;
if(PINA.7==0)
ldev=4;
if(rdev>ldev)
move(R,0,195+12*rdev);
if(rdev<ldev)
move(L,0,195+12*ldev);
if(rdev==ldev)
move(FWD,0,200);

}
else

{
for(i=0,dirl=0;i<MAX;i++) {
if(history[i]==L)
{dirl++;}
}
if(rotpow<160) {rotpow=160;}
if(rotpow<255) {rotpow++;}
if(dirl>HMAX)
{move(CW,0,rotpow);}
else
{move(CCW,0,rotpow);}

};
}

void move (unsigned char dir,unsigned char delay,unsigned char power) {


PORTC=dir;
if(dir==L || dir==R) {
hcount=(hcount+1)%MAX;
history[hcount]=dir;

}
LSPEED=RSPEED=255;//power;
//delay_ms(delay);

PossibleImprovements:
Useofdifferentialsteeringwithgradualchangeinwheelspeeds.
UseofHysteresisinsensorcircuitusingLM339
UseofADCsothattheexactpositionofthelinecanbeinterpolated
UseofWheelChairorthreewheeldrivetoreducetraction.
Generalimprovementslikeusingalowdropoutvoltageregulator,lighterchassisetc
Page14of
17

LineFollower

ReferencesandResources
Books:
ProgrammingandCustomizingtheAVRMicrocontrollerDhananjayV.
GadreParallelPortCompleteJanAxelson
Links:
AtmelCorp.
MakersoftheAVRmicrocontroller
http://www.atmel.com
AVRbeginners.net
http://www.avrbeginners.net/
AVRassemblertutorial
TutorialforlearningassemblylanguagefortheAVRSingleChip
ProcessorsAT90SxxxxfromATMELwithpracticalexamples.
http://www.avrasmtutorial.net/
OneofthebestsitesAVRsites
http://www.avrfreaks.net
WinAVR
AnopensourceCcompilerforAVR
http://sourceforge.net/projects/winavr
PonyProg
Awidelyusedprogrammer.Supportfornewerchipsisaddedperiodically.Can
alsoprogramPICsandEEPROMS
http://www.lancos.com/prog.html
BasicElectronics
http://www.kpsec.freeuk.com/
WilliamsonLabs
Niceanimatedtutorials,articlesandprojectideas.
http://www.williamsonlabs.com/home.htm
SmallRobotSensors
http://www.andrew.cmu.edu/user/rjg/websensors/robot_sensors2.html
RoboticsIndia
AnIndiansitedevotedtorobotics.Mustsee
http://www.roboticsindia.com/
SeattleRoboticsSociety
http://www.seattlerobotics.org/

Page15of
17

LineFollower
LineFollowerROBOT
AwardwinnerfromVingPeawCompetition2543,therobotbuiltwith2051,L293D,
andfourIRsensors.Simplecircuitandplatform,quicktrackingand
EasytounderstandprogramusingClanguage.
http://www.kmitl.ac.th/~kswichit/LFrobot/LFrobot.htm
Tools:AVRStudio
Forwritingcodeinassemblyandsimulationofcode.CurrentversionshasAVRGCC
plugintowritecodeinC.
Compilers:IAR,ImageCraft,CodeVisionAVR,WinAVR
Programmers:PonyProg,AVRDude,AVRISPandmanymore.
EvaluationBoards:STK200,STK500fromKandaSystems

Shops:
Motors:
1. MechtexMulund,Mumbai
2. ServoElectronicsLamingtonroad,MumbaiPhone:56346573
3. BombayElectronicsLamingtonRoad,Mumbai
Electronics:
1. VishaElectronicsLamingtonroad,Mumbai
[Programmerfor8051,PICandAVRavailable]
Phone:23862650/23862622
2. GalaElectronicsLamingtonroad,Mumbai
3. ChipComponentsLamingtonroad,Mumbai
Telephone:56390468/56587005
PartsandPrices:
Part
VisibleLightLeds
WhiteorBicoloured
IRLED
IRSensor
Capacitor(smallvalues)
Capacitor(largevalues/electrolytic)
Resistors(1/4W)
VariableResistor(Preset)
VariableResistor(Pot)
Microcontrollers
AT89C2051(8051Core)
AT89C51(8051Core)
AT89S52(8051Core)
PIC16F84A(PICCore)

ApproximatePriceinIndianRupees
1.00
5.00
3.00
7.00
0.25to2.00
2.00to20.00Ormore
0.25
2.50
8.00
40to450
40
60
150
120
Page16of
17

LineFollower
ATmega8(AVRCore)
ATmega16(AVRCore)
ATmega32(AVRCore)
ATmega128(AVRCore)
Transistors
LowpowerEg:BC547
PowerTransistorEg:TIP31C
Connectors
Optocoupler(MCT2E)
CommonTools
SolderingIron
Soldermetal
SolderFlux
DesolderingWick
Breadboard
WireStripper
CommonICs
VoltageRegulators(78XX),
LM324,IC555etc
MAX232
ULN2003/ULN2803
TSOP17XX
L298,L293,L293D
ICProgrammers
Homemade(Supportfewerdevices,
supportonlyserialprogramming,notas
rugged)
Eg:PonyProg(http://www.lancos.com)

90
150
300
425
1.50to15ormore
1.50
15.00
1.00perpin
8.00
150(typical)to400
25.00
5.00
5.00
80.00to100.00
25.00
5.00
20.00
14.50
17.00
70.00
20.00to80.00
2500to25000

Readymade(Supportmanychips,support
parallelprogramming,easytouse,
rugged)
WirelessModules
ParallelPort/SerialPortAddonCardfor
PC
UniversalPCB

700.00
500.00
10.00to50.00

Youmaydropyourfeedbackatpriyank.patil@gmail.com.
PriyankPatil
KJSomaiyaCoEInformationTechnology
VidyaVihar,
Mumbai

Page17of
17

You might also like