- Sacramento

advertisement
INFRARED SENSOR REMOTE CONTROLLED LINE FOLLOWING CAR
Chandra Sekhar Chukka
B.Tech, Jawaharlal Nehru Technological University, India, 2006
PROJECT
Submitted in partial satisfaction of
the requirements for the degree of
MASTER OF SCIENCE
in
ELECTRICAL AND ELECTRONIC ENGINEERING
at
CALIFORNIA STATE UNIVERSITY, SACRAMENTO
FALL
2010
INFRARED SENSOR REMOTE CONTROLLED LINE FOLLOWING CAR
A Project
by
Chandra Sekhar Chukka
Approved by:
__________________________________, Committee Chair
Jing Pang, Ph.D.
__________________________________, Second Reader
Preetham Kumar, Ph.D.
____________________________
Date
ii
Student: Chandra Sekhar Chukka
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
be awarded for the Project.
__________________________, Graduate Coordinator
Preetham Kumar, Ph.D.
Department of Electrical and Electronic Engineering
iii
________________
Date
Abstract
of
INFRARED SENSOR REMOTE CONTROLLED LINE FOLLOWING CAR
by
Chandra Sekhar Chukka
The main objective of this project is to design and build a car which can follow a given
path and which can be controlled remotely through infrared. In this report, the process of
building a car is explained in detail. The car is connected remotely to an IR
transmitter/receiver. The path detected by the sensors is transmitted to the processor. The
program used provides constant feedback to the processor to constantly correct its
movement. The car can be easily modified and enhanced for future applications. With the
widespread usage of modern technology, the usage of cars is increasing. Cars can be very
handy in day to day activities in future.
_______________________, Committee Chair
Jing Pang, Ph.D.
_______________________
Date
iv
ACKNOWLEDGMENTS
I would like to thank my project advisor, Dr. Jing Pang for her guidance and suggestions
for this project. Her time and support facilitated in completion of this project.
I would also like to thank Dr. Preetham Kumar for being my second reader. I would also
like to thank other faculty and staff of the Department of Electrical and Electronic
Engineering for their support.
I would also like to thank myself for being so patient during the course of this project.
Finally, I would like to thank my family and friends for their constant support and help
during this project.
v
TABLE OF CONTENTS
Page
Acknowledgments......................................................................................................... v
List of Tables ............................................................................................................ viii
List of Figures ............................................................................................................. ix
Chapter
1. INTRODUCTION ………………………………………………………………. 1
2. DESIGN OF MOTOR CAR ....................................................................................3
2.1 Structure of Motor Car ................................................................................ 3
2.2 Sensors .........................................................................................................6
2.3 Microcontroller…........................................................................................8
2.4 Voltage Regulator ......................................................................................10
2.5 Infrared Transmitter/Receiver…………………………………………...11
3. IMPLEMENTATION OF MOTOR CAR .............................................................15
3.1 PWM Speed Control ..................................................................................16
3.2 Sensor Circuit.............................................................................................19
3.3 Algorithm ...................................................................................................21
4. ATMEGA168 MICROCONTROLLER ............................................................... 23
4.1 Features ......................................................................................................23
4.2 Pin Description...........................................................................................25
4.3 I/O Ports .....................................................................................................26
4.4 8-bit Timer/Counter with PWM.................................................................27
4.4.1 Features ...................................................................................27
4.4.2 Fast PWM Mode .....................................................................28
4.4.3 Phase Correct PWM Mode .....................................................30
4.5 Analog to Digital Converter.......................................................................31
4.5.1 Features ...................................................................................31
vi
4.5.2 Conversion ..............................................................................33
4.5.3 Conversion Timing .................................................................34
4.5.4 Register Description................................................................36
5. CONCLUSION ......................................................................................................38
5.1 Conclusion .................................................................................................38
5.2 Future Work ...............................................................................................38
References ................................................................................................................... 39
vii
LIST OF TABLES
Page
Table 4.1 Port Pin Configurations………….……………………………….....27
Table 4.2 ADC Conversion……………………………………………………36
viii
LIST OF FIGURES
Page
Figure 2.1 PCB Layout………………….…………………………………….....4
Figure 2.2 Motors and Wheels…………………………………………………..5
Figure 2.3 Line Sensor Array……………..……………………………………. 6
Figure 2.4 Reflective Sensor……………………………………………………. 7
Figure 2.5 Schematic of Diode…………………………………………………..7
Figure 2.6 Block Diagram of L293D………………………………………….. ..9
Figure 2.7 Voltage Regulator…………………………………………………...10
Figure 2.8 Infrared Remote Protocol……..…………………………..………...11
Figure 2.9 Block diagram of the Receiver……………………………………...12
Figure 2.10 Complete Circuit of IR Remote Controller………………………..13
Figure 2.11 SIRC Timing Details………………………………………………14
Figure 3.1 Front and Rear Wheels…………..………………………………….15
Figure 3.2 Driver Schematic……………………………………………………16
Figure 3.3 PWM Signal………………………………………………………...17
Figure 3.4 Clockwise rotation…………………………………………………..18
Figure 3.5 Counter Clockwise rotation…………………………………………18
Figure 3.6 Unwanted Change in Line Width…………………………………...19
Figure 3.7 Schematic of Diode Sensor………………………………………….19
Figure 3.8 Relative Voltage Swing vs. Resistance………………...……………20
Figure 3.9 Path of Three Sensor Line Follower…………………….. …………22
ix
Figure 3.10 Path Cycle………………………………………………………….22
Figure 4.1 ATmega168 Block diagram………………………………………….24
Figure 4.2 Pin Configuration…………………………………………………….25
Figure 4.3 Fast PWM Mode, Timing Diagram…………………………………..29
Figure 4.4 Phase Correct PWM Mode, Timing Diagram………………………..31
Figure 4.5 Analog to Digital Converter Block Schematic Operation……………32
Figure 4.6 ADC Auto Trigger Logic…………………………………………….33
Figure 4.7 ADC Timing Diagram, First Conversion…………………………….34
Figure 4.8 ADC Timing Diagram, Free Running Conversion…………………..35
x
1
Chapter 1
INTRODUCTION
Purpose of the Study
A car is a machine that can follow the given directions. Line following car is a machine
that can follow the given path. The path can be visible like a black line on a white surface
or a white line on a black surface or invisible like a magnetic field.
You can build a car with just two transistors, LED and LDR as sensors. However, this
system will not have any memory or brain to control the car following on the track.
Moreover, the circuit is discrete. Car will not be able to follow the track smoothly;
instead, it will oscillate to the left and right. To get rid of this drawback we can enhance
the design by using a microcontroller that functions as the brain of the design. This makes
the line following car more robust, reliable and flexible that you could not have it with
the discrete electronics circuit without much change in electronic circuit design. The
advantage of using a microcontroller in the design is we can make use of the Arithmetic
Logic Unit of the microcontroller as PID control. Using the microcontroller in the design
helps the car in following the given path smoothly rather than oscillating to the left and
right. With the use of microcontroller, we also get an opportunity to constantly correct the
wrong moves using feedback mechanisms. Because of the use of microcontroller, you get
an opportunity as a programmer to teach the car how to follow the line thus giving it a
human-like property of responding to stimuli [1] [2].
The basic steps in building a line following car are as follows [3]:
2
1. Building the car including constructing the chassis of car, sensors, ADC and
sensor circuit, Motors and Wheels, Driver, power supply.
2. Capturing the position of line with the sensors mounted on the front end of car
3. Steering the car to track the line with any steering mechanism
4. Controlling the speed of car according to the condition of lane. The speed is
generally slow while passing the curve due to friction of the tire and the floor
3
Chapter 2
DESIGN OF MOTOR CAR
Line Follower Car Structure
The motor car can be divided into several parts. The main parts are chassis and body
structure, motors, wheels, sensors, microcontroller, driver, voltage regulator and infrared
transmitter/receiver [3].
2.1 Structure of motor car
There are some good materials for designing cars such as wood, plastic, aluminum and
brass alloys. While selecting the material we have to pay attention to the resistance,
weight and mechanical ability of the material. There are some agents that we can use
them to choose a good body, ability to perforate, incision, flexibility etc [5].
Aluminum has been used in my designed car for chassis because of its lightweight and
being strong enough to hold the motors and batteries and the circuitry. Figure 2.1 shown
below is the PCB and a graphical layout of the chassis board. This PCB functions as both
a chassis board and the PCB. You can see in the figure the position of the motors and the
skid. The line sensor is mounted on the front end of the chassis board. Line sensor should
be as far as possible from the drive wheels in this design so that the car functions
accurately. Using PCB layout software to design the chassis, as well as PCB techniques
to manufacture it, gives a lot of accuracy, which is very important for the mechanical
system to work correctly [4].
4
Figure 2.1 PCB Layout [4]
One of the important systems of a car is the movement system. The objective of the
movement system is how to move the car from one point to another one. Motors are used
to convert electrical energy to mechanical energy. Different kinds of motors are available
in the market and we should choose the best one. It depends on the car function, power
and precision [3].
5
D.C. gear motors are used in this project. The D.C. motors operate in the range of 0-12V
and have a speed of 2400rpm. The advantage of gear motors is that it has some gears and
its speed does not change towards downhill or a uphill. Two motors are used in this
project to support the two rear wheels.
Figure 2.2 Motors and Wheels [6]
There are two types of movement systems for cars. The two types are Wheel and Tank
system . Wheels are used in this project. Two wheels are used at the rear end and a castor
is used at the front end [3].
6
2.2 Sensors
Sensors are used to sense the line. IR sensors consist of two diodes, one for sending the
signal and the other for receiving the signal. IR emitter sends the signal and IR receiver
receives the signal. If the receiver receives the reflection ray, it means that the car is on
white and if it cannot receive it, so the car is on black. IR reflectance sensors contain a
matched infrared transmitter and infrared receiver pair [3].
Figure 2.3 Line Sensor Array[7]
The Line Senor Array, which is used in this design, is showed in figure 2.3. This device
has Indicator LED’s for each sensor to ensure correct working. The Indicator LED’s are
on the other side for better view. It has the operating voltage range of 6V-20V. It is less
sensitive to external ambient light. It can detect color difference [7].
IR reflectance sensors work accurately when shielded from ambient light. The accuracy
also depends on the distance between the sensors and the reflective surface. The distance
between sensors and the ground surface must be 3 or 2mm. The positioning of the sensors
7
Figure 2.4 Reflective sensor [8]
is also important for good reception. IR reflectance sensors depend on the amount of light
that is reflected into the receiver. The reflection from white surface is good while the
reflection from the black surface is poor [3].
Figure 2.5 Schematic of Diode sensor [3]
If the reflected signal is strong then the sensor will be able to transfer the analog signal to
8
the microcontroller or else the senor will not be able to transfer the signal to the
microcontroller.
2.3 Microcontroller
Microcontroller is the processor of the design. It acts as a brain of the system. It reacts to
the input signals it receives and generates the output signals and then sends the generated
output signals to the concerned external components through output ports. There are a lot
of microcontroller’s available in the market. Atmel’s AVR ATmega168 microcontroller
is used in this design.
ATmega168 microcontroller is used because of its features like [9]:
1. In system self programmable flash
2. Inbuilt components like ADC, Internal oscillator, Analog comparator, Timer
3. On chip Pulse Width Modulation circuit
4. RISC processor
5. Consumes less power
Internal components help in reducing the number of external components and external
circuitry. AVR instructions help in decreasing the size of program whether the program is
written in C or Assembly. 8-bit RISC CPU combined with In System Self programmable
Flash makes the Atmel ATmega168, a powerful microcontroller that is highly flexible
and used in many embedded control applications [3] [9].
A motor driver is used to control the motors. Microcontroller sends the signal to the
motor driver and the driver sends the output signal to the motor. If the signal received by
9
the motor driver is high, it will rotate the motor and if the signal is low, the driver will not
rotate the motor [3].
Microcontroller sends the signal to the driver, which gives the voltage required by the
motor to rotate. We cannot connect a microcontroller directly to the motor because it
cannot provide enough current to drive the motors. Motor driver works as a switch as
well as a current enhancing device. Therefore, we use a motor driver between the motor
and the microcontroller [10].
Motor driver used in this design is L293D. It controls two motors. It has two channels.
One channel for each motor. If the enable pin is high then the corresponding channel is
activated [10] [12].
Figure 2.6 Block diagram of L293D [12]
10
L293D is used in switching applications at frequencies up to 5KHz. L293D is assembled
in a 16 lead plastic package which has 4 center pins connected together and used for heat
sinking [12].
2.4 Voltage Regulator
D.C. voltage needs to be supplied for the design. But the supply has to be of constant
magnitude for all the components to function efficiently and to avoid any damage to the
components in the circuitry because of excess voltage. For the supply voltage to be of
constant magnitude, a voltage regulator is used in the design.
Voltage regulator L7805C is used in this design. This Voltage regulator supplies voltage
of constant magnitude to the circuit design irrespective of the external power supply,
temperature. Voltage regulators supply either a fixed voltage or an adjustable voltage [8].
Figure 2.7 Voltage Regulator [8]
11
Two voltage sources are required by the circuit. For the digital IC components +5V is
supplied and the motors get 12V supply. The motors get unregulated power supply
whereas the IC components get perfect ripple free +5V to function properly [8].
2.5 Infrared Transmitter/Receiver
Infrared is a mode of transmission of light. The wavelength of the infrared is too long to
see. Infrared signal used in remote control has three layers. The three layers are the
infrared, modulation and serial data. The modulation layer modulates the infrared signal.
The information regarding the command to be executed is given to the serial data layer.
The information given to the serial data layer is in the form of infrared bursts. A long
burst is treated as ‘1’ and a short gap is treated as ‘0’ [13].
The infrared transmitter sends out infrared light in the form of binary codes by the IR
receiver. The binary codes are decoded by the IR receiver into zeroes and ones.
According to the data received, the microcontroller then executes the command [13].
Figure 2.8 Infrared Remote Protocol [13]
12
The code which is sent by the IR transmitter consists of 12 bits. The sent code starts with
a header of 2.4 milli second. It is then followed by 7 command bits and 5 address bits.
The gap between two transmitted codes is 40ms. Least significant bits are transmitted
first [13].
The IR receiver circuit has a photodiode, preamplifier and a demodulator circuit. The
band pass filter in the preamplifier of the IR receiver lowers the receiver’s sensitivity.
The output at the IR receiver is a binary bit-stream [13].
Figure 2.9 Block Diagram of the Receiver [13]
The IR remote control used in this design follows Serial Infrared Control (SIRC)
protocol. This protocol is most common in remote controls. The data transmission in the
Serial Infrared Control takes place using the pulse width modulation of the infrared
signal. A single start bit is transmitted before the Serial Infrared Control transmission.
This helps in preventing an incomplete transmission. A software flag is set to 1, after
receiving the start bit to allow the rest of the transmission [13].
13
Figure 2.10 Complete Circuit of IR Remote Controller [13]
14
The Serial Infrared Remote data has either 0 or 1. There is a 0.6ms pause after every data
pulse. The pulse length is measured by the falling edge of the waveform [13].
Figure 2.11 SIRC Timing Details [13]
15
Chapter 3
IMPLEMENTATION OF MOTOR CAR
In this chapter, the implementation of motor car is discussed. Certain features like Pulse
width modulation speed control system, sensor circuit and algorithm, which are used in
implementing the motor car are discussed in this section.
In the car steering system two wheels are mounted on a single axis at the rear end and a
passive castor is used at the front end as wheel. As the two wheels are connected to two
motors, which makes them independently powered and controlled, it provides both drive
and steering [8].
Figure 3.1 Front and rear wheels [3]
The car moves in straight line, if both the drive wheels move in tandem. The car follows
a curved path, if one wheel turns faster than the other. The car revolves, if both the
wheels move at equal speeds but in opposite direction [8].
After establishing the steering system, the algorithm for controlling the car can be easily
developed. The assumption made here is that the wheels maintain a steady velocity. The
trajectory of the car becomes more complicated if the steady velocity is not maintained.
16
The speed changes instantly where mass of the platform is small. The path that the car
follows may not be truly circular, but close to circular [8].
3.1 PWM Speed Control
Microcontroller after receiving the input data from sensors, processes the data and then
sends the instructions to the driver. According to the inputs, the driver gives voltage to
the motors. One of the motors get positive voltage and the other motor gets negative
voltage from the driver [3].
Figure 3.2 Driver Schematic [3]
Motors are bidirectional. The direction of rotation of motors can be changed by reversing
the polarity. When the EN input is high, then the motors behave as per the direction
control [8].
Based on the direction of the motors, there are three states [3]:
1. Move forward; both the motors are turned on and rotate forward simultaneously
2. Move left; right motor is turned on and left motor is turned off
17
3. Move right; left motor is turned on and right motor is turned off
Pulse Width Modulation is a technique used for controlling the electrical devices. Switch
is turned on and off at a fast pace for controlling the average value of voltage and current
fed to the load. If the switch is on for a longer duration then it is off, then more power is
supplied to the load [5].
Figure 3.3 PWM Signal [8]
PWM Duty Cycle Equation [8]
Where ρ is duty cycle of the PWM control signal. The above equation shows that the
speed can be controlled by varying the duty cycle of the PWM control signal, which in
turn varies the voltage supplied to the motors [8].
18
Figure 3.4 Clockwise rotation [8]
Figure 3.5 Counter Clockwise rotation [8]
The advantage of PWM is that the power loss is low. The switch is either on or off unlike
the normal regulations which aids in low power loss. PWM works well with digital
circuits because of its on/off nature. The disadvantage of PWM is that life of the motors
is shortened because the pulsed power puts more stress on D.C. motor bearings and
windings [5] [10].
19
Figure 3.6 Unwanted Change in line Width [3]
3.2 Sensor Circuit
When IR light falls on the sensor, the resistance of the sensor decreases. A good sensor
will have near zero resistance in presence of light and very large resistance in absence of
light. A potential divider is formed using this property of the sensor. Maximum change in
voltage between no-light and bright-light conditions will be given by a good sensor [3].
Figure 3.7 Schematic of Diode Sensor [3]
20
The R1 shown in the above figure should be chosen wisely to get large voltage
difference. If RX sensor =c when no light falls on it and RX sensor = d when light falls
on it, then the difference between two potentials is [3]:
Vcc*{c/(c+R1) – d/(d + R1)}
Relative voltage swing = actual voltage swing/Vcc
= Vcc*{c/(c+R1) – d/(d + R1)}/Vcc
= c/(c+R1) – d/(d + R1)
If we plot the relative voltage swing with a range of values of resistor, then it would like
the below figure. From the graph, it is understood that maximum swing is obtained at
R1= 150k. However, the current will be very small with high resistance and it may be
distorted by noise. Therefore, we have to balance between sensitivity and noise
immunity[2].
Figure 3.8 Relative voltage swing vs. Resistance [2]
21
3.3Algorithm
The smoothness of the car following the path depends on the algorithm we choose. So the
algorithm is very important in a line follower car. If L1, C1 and R1 are the left, center
and right sensors and if the sensor reads 0 when a sensor is on line and the sensor reads 1
when it is not on the line, then the algorithm I choose is [3] [2]:
1. If C1 =0 then move forward
2. If C1=1then
If L1=0 then move left
If R1=0 then move right
3. If all the sensors read 1 then go to step 4
Else
If L > R move left
If L < R move right
If L = R move forward
4. Move clockwise if line was last seen on right
Move counterclockwise if line was last seen on left
Repeat step 4 until line is found
5. Go to step 1
Path for a three-sensor line array is shown in figure 3.9
22
Figure 3.9 path of three-sensor line follower [3]
Figure 3.10 Path Cycle [3]
23
Chapter 4
ATMEGA168 MICROCONTROLLER
4.1 Features
Microcontroller is the brain of RF remote controlled car. It has RISC architecture. It has
16K bytes of In-System Self Programmable Flash memory with read write capabilities. It
has 512 bytes EEPROM, 1K bytes SRAM, 23 general purpose I/O lines, 32 general
purpose working registers. It has three flexible Timer/Counters with Compare modes. It
has internal and external interrupts, a serial Programmable USART. It has a byte-oriented
2-wire serial Interface, SPI serial port. It has a 6-channel 10-bit ADC, a programmable
watchdog Timer with internal oscillator. It has five power saving modes[9].
When the power is down, the Power-down mode saves all the contents of the register and
disables all other chip functions until next hardware reset. The program memory can be
reprogrammed In-System through an SPI interface with the on-chip ISP Flash. Many
program and system development tools like C compilers, Macro Assemblers, Program
simulators and In-circuit Emulators are supported by ATmega168 AVR. The ISP Flash
memory is divided into Application program and Boot program. Any interface like a
serial port or a parallel port is used to download the application program into Application
Flash memory. The block diagram of AVR ATmega168 microcontroller is shown in
figure 4.1 [9].
24
Figure 4.1 ATmega168 Block diagram [9]
25
4.2 Pin Description
ATmega168 microcontroller has 28 pins. The pins are divided into Port C, Port B, Port
D, GND and VCC. The pin configuration is shown in the below figure.
Figure 4.2 Pin Configuration [9]
Port B(PB7:0): It has 8-bit bi-directional I/O ports. PB6 can be used as input to the
inverting oscillator amplifier depending on the clock selection settings. PB7 can be used
as output from the inverting oscillator amplifier depending on the clock settings. PB7 can
also be used as clock pin for crystal oscillator. When it is a clock pin, it cannot be used as
I/O pin.DDB7, PORTB7 will read 0, if PB7 is used as a clock pin [9].
Port C(PC5:0): It has 7 bit bi-directional I/O ports. PC6 differs in electrical characteristics
from the rest of the pins of port C. PC6 is used as an I/O pin if RSTDISBL is
26
programmed. PC5, PC4, PC3 can be used as ADC input channels. PC5, PC4, PC3 and
PC2 can serve as external interrupt sources [9].
Port D(PD7:0): Port D has 8-bit bi-directional I/P ports. While PD7 serves as AIN1,
Analog comparator negative input, PD6 serves as AIN0, Analog comparator positive
input. PD4 serves as XCK, USART external clock [9].
The supply voltage pin for A/D converter, PC3:0 and ADC7:6 is AVcc. Whether or not
ADC is used, AVcc should be connected to Vcc. It should be connected to Vcc through a
low-pass filter, if an ADC is used. The analog reference pin for A/D converter is
AREF[9].
4.3 I/O PORTS
All I/O ports have true Read-Modify-Write functionality, which means the direction of
one port can be changed without actually changing the direction of any other pin. This
also applies when changing drive value or when enabling/disabling of pull-up
resistors[9].
Each port is allocated a Data Register-PORTx, Data Direction Register-DDRx and the
Port Input Pins-PINx, a total of three I/O memory address locations for each port. The
I/O memory locations, Data Register and the Data Direction Register are read/write while
the Port Input Pins is read only. Making use of alternate functions on some port pins will
not affect the use of other port pins as general digital I/O [9].
27
As per the above figure, the DDXn bits, PORTxn bits and PINxn bits can be accessed at
DDRx I/O address, PORTx I/O address, PINx I/O address respectively. The DDxn bit
selects the direction of the pin in the DDRx Register. If DDxn is logic one or logic zero,
Pxn is configured as output pin or input pin respectively [9].
Table 4.1 Port Pin Configurations [9]
4.4 8-bit Timer/Counter with PWM
4.4.1 Features
1. It has two independent output compare units
2. Glitch free Phase Correct Pulse Width Modulation
3. Variable PWM Period
4. Frequency Generator
Timer/Counter is a general-purpose 8-bit Timer/Counter module. The Timer/Counter
(TCNT0) and Output Compare Registers (OCR0A and OCR0B) are 8-bit registers.
28
Timer/Counter can be clocked either internally or externally, internally through the
prescaler, externally through an external clock source on the T0 pin. The clock source can
be selected by the Clock Select logic. Clock Select logic can be controlled by Clock
Select bits(CS02:0). When no clock source is selected, the Timer/Counter stops [9].
The Timer/Counter value is compared with the double buffered Output Compare
Registers (OCR0A and OCR0B) and the result of the compare is used to generate a PWM
or variable frequency output by the Waveform Generator. The PWM output is generated
on the Output Compare pins (OC0A and OC0B). At each timer clock (clk T0), the counter
is either cleared, incremented or decremented depending on the mode of operation
used[9].
4.4.2 Fast PWM Mode
The combination of Waveform Generation mode (WGM02:0) and Compare Output
mode(COM0x1:0) defines the mode of operation. The Compare Output mode bits do not
affect the counting sequence. However, Waveform Generation mode bits affect the
counting sequence. The PWM output can be either inverted or non-inverted depending on
the COM0x1:0 bits [9].
The fast Pulse Width Modulation provides high frequency PWM. The difference between
other PWM options and fast PWM is that the fast PWM is single-slope operation. In fast
PWM mode, the counter counts from BOTTOM to TOP and then restarts from
BOTTOM. On comparing between TCNT0 and OCR0x, the Output Compare (OC0x) is
cleared in non-inverting Compare Output mode and set at BOTTOM. Whereas the
29
Output Compare is cleared at BOTTOM and set on compare match in inverting mode.
The operating frequency in fast PWM mode is high due to single slope. Because of the
high frequency, the fast PWM mode can be used in applications like power regulation,
rectification and DAC applications. High frequency reduces total system cost [9].
Figure 4.3 Fast PWM Mode, Timing Diagram [9]
The counter will keep incrementing in fast PWM mode until its value matches the TOP
value and then cleared at the following clock cycle. Each time the counter value matches
the TOP value, the Timer/Counter Overflow Flag (TOV0) is set. PWM waveforms are
generated on the OC0x pins. A non-inverted PWM is produced when COM0x1:0 bits are
set to two and an inverted PWM is produced when COM0X1:0 is set to three [9].
The output PWM frequency in fast PWM is calculated by the following equation, where
N is a variable [9] :
30
4.4.3 Phase Correct PWM Mode
The phase correct PWM mode generates phase correct PWM waveform. This mode has
dual-slope operation. The counter counts from BOTTOM to TOP and then from TOP to
BOTTOM. In this mode, because of the symmetry of dual slope, phase correct PWM can
be used in motor control applications. In this mode, the counter keeps incrementing until
it reaches the TOP value and it changes the count direction when it reaches TOP value[9].
Whenever the counter reaches BOTTOM, the Timer/Counter Overflow Flag (TOV0) is
set. The generation of PWM waveform is allowed on the OC0x pins. A non-inverted
PWM is produced when COM0x1:0 bits is set to two and an inverted PWM is produced
on setting the COM0x1:0 to three [9].
The output PWM frequency in phase correct PWM is calculated by the following
equation, where N is a variable [9] :
31
Figure 4.4 Phase Correct PWM Mode, Timing Diagram [9]
4.5 Analog to Digital Converter
4.5.1 Features [9]
1. 10-bit Resolution
2.13µs-260µs conversion time
3. Single conversion mode
4. Interrupt on ADC conversion complete
5. Noise canceller
The ADC in Atmega168 has the 10-bit successive approximation feature. Eight single
ended voltage inputs from Port C are connected to 8-channel Analog Multiplexer. The
sample and hold circuit present in the ADC helps in making sure the input voltage is at
32
Figure 4.5 Analog to Digital Converter Block Schematic Operation [9]
constant level during conversion. ADC also has a separate analog supply voltage, AVcc.
The difference between Vcc and AVcc must not be more than ±0.3V. The conversion of
33
an analog input voltage to a 10-bit digital value takes place through successive
approximation [9].
Decoupling of the internal voltage reference at AREF pin with an external capacitor helps
in improving the noise immunity. The selection of the analog input channel is done by
writing the MUX bits in ADMUX. Setting the ADC Enable bit, ADEN in ADCSRA
enables the ADC. The 10-bit result generated by the ADC is stored in ADC Data
Registers, ADCH and ADCL. When a ADC conversion is completed, ADC’s own
interrupt is triggered [9].
4.5.2 Conversion
A single conversion will be started by enabling the ADC start conversion bit, ADSC and
disabling the Power Reduction ADC bit, PRADC. The ADSC bit stays high until the
conversion is completed and is cleared by hardware when the conversion is completed[9].
Figure 4.6 ADC Auto Trigger Logic [9]
34
A conversion can also be triggered automatically. Enabling the ADC Auto Trigger
Enable bit, ADATE in ADCSRA enables auto triggering. A new conversion starts when a
positive edge occurs on the selected trigger signal. Conversions can be started at fixed
intervals with this method. If a trigger source receives another positive edge during a
conversion, the positive edge will be ignored [9].
4.5.3 Conversion Timing
In Free running mode, the ADC constantly samples and updates the ADC Data Register.
The first conversion is started by enabling the ADSC bit on ADCSRA. The ADC
performs successive conversion in this mode. To determine if a conversion is in progress,
the ADSC bit can be used [9].
Figure 4.7 ADC Timing Diagram, First Conversion [9]
35
Figure 4.8 ADC Timing Diagram, Free Running Conversion [9]
It takes 13 ADC clock cycles for a normal conversion. The first conversion takes 25 ADC
clock cycles for initializing the analog circuitry. In a First conversion, the sample and
hold takes place after 13.5 ADC clock cycles and in a normal conversion, it takes place
after 1.5 ADC clock cycles. A new conversion starts immediately after a conversion is
completed in Free Running mode [9].
36
Table 4.2 ADC Conversion Time [9]
4.5.4 Register Description
ADMUX – ADC Multiplexer Selection Register [9]

Bit 7:6 REFS1:0: Reference Selection Bits
Reference voltage for ADC is selected using these two pins. If a change occurs to
these pins during a conversion, the change will take place only after the conversion is
complete.

Bit 5 ADLAR
ADLAR stands for ADC Left Adjust Register. This bit controls the presentation of
the result in ADC Data Register. A one on ADLAR adjusts the result to the left or
else the result is adjusted to the right.

Bit 4 Res: Reserved Bit
This bit is not used and will always be read as zero.

Bit 3:0 MUX3:0 Analog Channel Selection Bits
37
The analog inputs to be connected to ADC are selected by these bits. If a change
occurs to the values of these pins during a conversion, the change will take place only
after the conversion is complete.
38
Chapter 5
CONCLUSION
5.1 Conclusion
This project deals with building and designing a car. With this project, I have learnt about
AVR ATmega168 microcontroller, STK500 and AVR Studio. I have learnt how to
program the AVR microcontroller. I have also learnt about C compiler. Initially I
collected all the hardware parts required for the design and then started building each
component, starting with chassis and then connecting the wheels, motors and power
supply. The design uses feedback control system. After testing the design, I was
impressed with the functionality of the microcontroller in designs like this.
5.2 Future Work
The design can be easily modified for future applications. In future, the car can be
designed with higher speed. This design uses four sensors. More number of sensors can
be used for accurate precision. Even lighter cars can be designed so that the stress on the
motors is reduced. More complex control theories can be used in the design.
39
REFERENCES
[1] rwb, “Build Your Own Microcontroller Based PID Control Line Follower Robot
(LFR) –Second Part”, Aug, 2009, retrieved from world wide web, Feb. 2010
http://www.ermicro.com/blog/?p=1163
[2] Priyank Patil, “Line Following Robot”, retrieved from world wide web, March 2010
http://www.kmitl.ac.th/~kswichit/CAR/Follower.pdf
[3] Mehran Pakdaman, M. Mehdi Sanaatiyan, “Design and Implementation of Line
Follower Car”, 2009 Second International Conference on Computer and
Electrical Engineering, pp. 585-590, Jan. 2010
[4] Ibrahim Kamal, “Mini line follower Robot”, April 15, 2008, retrieved from world
wide web, April 2010
http://ikalogic.com/proj_mini_line_folower.php
[5] “Pulse Width Modulation”, April 2009, retrieved from world wide web, June 2010
http://en.wikipedia.org/wiki/Pulse-width_modulation
[6] DFCAR, “3PA Assembly Guide”, retrieved from world wide web, Jan. 2010
http://www.yecar.com/products/manual/3PA%20Assembly%20Guide.pdf
[7] “Line Sensor Array”, retrieved from world wide web, Feb. 2010
http://robokits.co.in/shop/index.php?main_page=product_info&cPath=11&products_id=4
3
[8] Amithash E. Prasad, “Line Following Robot”, retrieved from world wide web, March
2010
http://eces.colorado.edu/~prasadae/LFR_Report.pdf
[9] Atmel Corporation, “8-bit Microcontroller with 8K Bytes In-System Programmable
Flash”, atmega168 datasheet, pp. 1-6,72-74,79,82,85,88-90,92,95,97-99,245-250,
256, Sep 2007
[10] Mayur Agarwal, Prashant Agrawal, Krishna Nand Gupta, Hitesh Meghani, “Line
Follower Robot”, Robotics workshop, March 2008
[11] Atmel Corporation, “AVR STK500 User Guide”, STK500 datasheet, pp. 5, 9-10, 15,
19, 42,44, March 2003
[12] ST SGS-Thomson Microelectronics Corporation, “Push-Pull Four Channel Driver
with diodes”, L293D datasheet, 1996
40
[13] Sanda Win, Tin Shein, Khin Maung Latt, “Design and Construction of PIC-based IR
Remote Control Moving Robot”, World Academy of Science, Engineering and
Technology 48 2008, pp 390-395, March 2010
Download