You are on page 1of 54

CHAPTER ONE INTRODUCTION

1.1 General Overview


Line follower robot is a mobile machine that can detect and follow line which is drawn on the floor. The path can be visible like a black line on a white surface (or vice-versa) or it can be invisible like a magnetic field. Robot should read for identifying his position in some time, after that, the control system (AVR) will create for the line follower some orders to respect components in purposes of making correct movements of the line follower. Sensing a line and maneuvering the line follower to stay on course, while constantly correcting wrong moves using feedback mechanism forms a simple yet effective closed loop system. Although a Line Followers purpose in life is to follow a line, other issues have to be discussed for example, if the track that it is following is on a table, it should also have some form of detecting the edge of the table so it wont fall Off. If the robot is moving fast, it has to have fast reflexes so that if the line turns it will be able to compensate its direction so it wont leave the track. Line Follower needs light to move, but sometimes if an external light source is strong enough it might confuse the robot. If the robot is doing its preferred activity outdoors, a whole set of issues have to deal with, such as sunlight, dust, uneven terrain and other issues. The Line Follower in this project may not be able to handle all the conditions listed above, but as long as it follows the line it will be satisfied. Most, if not all Line Followers, are battery powered. They must have a way to define the direction in which they must go. They must also be able to accept input from the external world and use that input to decide what to do next.

1.2 Components
The block diagram below in figure 1.1 shows those basic four components to build the line follower which are: 1- The sensors array. 2- The microcontroller (ATmega16). 3- Motor driver (L293D). 4- Two Dc motors.

Figure1.1 the line follower block diagram

1.2.1 Sensors
The sensors are one of the most important components of the functional line follower robot ;they are robots eyes, ears, sense of touch, balance and only source of input from external environment The line follower uses IR sensors to sense the line, an array of 2 IR LEDs (Tx) and sensors (Rx), facing the ground. The output of the sensors is an analog signal which depends on the amount of light reflected back, this analog signal is connected to the microcontrollers pins that contain an analog to digital converter (ADC).

1.2.2 Microcontroller
A microcontroller often serves as the brain of a mechatronic system. It can be programmed to interact with both the hardware of the system and the user. Even the most basic microcontroller can perform simple math operations, control digital outputs, and monitor digital inputs. As the computer industry has evolved, so has the technology associated with microcontrollers. Newer microcontrollers are much faster, have more memory, and have a host of input and output features that dwarf the ability of earlier models. Most modern controllers have analog-to-digital converters, high-speed timers and counters, interrupt capabilities, outputs that can be pulse-width modulated, serial communication ports, etc. There are many types of microcontrollers but ATmega-16 is the one used in this project to control the robot movements. The ATmega16 is a low-power CMOS (complementary metal oxide semiconductor) 8-bit microcontroller based on the AVR enhanced RISC (reduced instruction set computing) architecture. By executing powerful instructions in a single clock cycle, the ATmega16 achieves throughputs approaching 1 MIPS per MHz allowing the system to optimize power consumption versus processing speed. The AVR core combines a rich instruction set with 32 general purpose working registers. All the 32 registers are directly connected to the Arithmetic Logic Unit (ALU), allowing two independent registers to be accessed in one single instruction executed in one clock cycle. The resulting architecture is more code efficient while achieving throughputs up to ten times faster than conventional CISC microcontrollers.[4]

1.2.3 Motor driver


Another component is the motor driver L293D which allows the microcontrollers to govern the movements of the line follower. Some of the microcontroller outputs are connected to the motor driver inputs that are how the microcontroller controls the motor movement throughout the output it gives to the motor driver. L293D is dual H-Bridge motor drive, so with one IC we can interface two DC motors which can be controlled in both clockwise and counter clockwise direction. L293D has output current of 600mA and peak output current of 1.2A per channel. Moreover for protection of circuit from back EMF output diods are included within the IC. The output supply has a wide range from 4.5V to 36V, which has made L293D the best choice for the DC motor driver.[7]

1.2.4 DC motors
The Direct Current (DC) motor is one of the first machines devised to convert electrical energy to mechanical power. Its origin can be traced to machines conceived and tested by Michael Faraday, the experimenter who formulated the fundamental concepts of electromagnetism. These concepts basically state that if a conductor, or wire, carrying current is placed in a magnetic field, a force will act upon it. The magnitude of this force is a function of strength of the magnetic field, the amount of current passing through the conductor and the orientation of the magnet and conductor. The direction in which this force will act is dependent on the direction of current and direction of the magnetic field. Electric motor design is based on the placement of conductors (wires) in a magnetic field. A winding has many conductors, or turns of wire, and the contribution of each individual turn adds to the intensity of the interaction. The

force developed from a winding is dependent on the current passing through the winding and the magnetic field strength. If more current is passed through the winding, then more force (torque) is obtained. In effect, two magnetic fields interacting cause movement: the magnetic field from the rotor and the magnetic field from the stators attract each other. This becomes the basis of both AC and DC motor design.

The figure1.2 below shows the state diagram for the line follower

Figure 1.2 state diagram of the line follower

The state diagram above in figure1.2 explains the strategy the line follower takes to deal with all possible conditions.

1.3 The Objectives of the Project


The main aim of the project is to design a line follower which is a simple robotics application, the follower designed for repetitive tasks which use the same path a lot and that helps to reduce the need for a human operator

1.4 The Applications of the Line Follower


The line follower is used in many applications, It might be used in a warehouse where the follower follows 'tracks' to and from the shelves they stock and retrieve from. The line follower is also can be used as a guider to guide

the visitors from the entrance to the main office. There are cases where
smarter versions of line followers are used to deliver mail within an office building and deliver medications in a hospital.

1.5 Project Layout


Chapter one represents a general overview of the line followers and its applications. Chapter two discusses the AVR microcontroller in general and ATmega16 microcontroller in details and the programming language used to write the control program. Chapter three shows architecture of the line follower and the components used in the design. Chapter four discusses the principle of operation of the line follower and explains the BASCOM code which controls the movement of the follower. Chapter five contains the recommendations and conclusion.

CHAPTER TWO MICROCONTROLLER


2.1 Introduction
A microcontroller is a computer-on-a-chip or a single-chip computer. Micro suggests that the device is small, and controller tells that the device might be used to control objects, processes, or events. Another term to describe a microcontroller is embedded controller, because the microcontroller and its support circuits are often built into, or embedded in, the devices they control. Microcontrollers involves in all kinds of things these days. Any device that measures, stores, controls, calculates, or displays information is a candidate for putting a microcontroller inside. The largest single use for microcontrollers is in automobiles just about every car manufactured today includes at least one microcontroller for engine control, and often more to control additional systems in the car. In desktop computers, microcontrollers, inside keyboards, modems, printers, and other peripherals, in test equipment, microcontrollers make it easy to add features such as the ability to store measurements, to create and store user routines, and to display messages and waveforms. Consumer products that use microcontrollers include cameras, video recorders, compact-disk players, and ovens. And these are just a few examples. A microcontroller is similar to the microprocessor inside a personal computer. Examples of microprocessors include Intels 8086, Motorolas 68000, and Zilogs Z80. Both microprocessors and microcontrollers contain a (Central Processing Unit) CPU. The CPU executes instructions that perform the basic logic, math, and data moving functions of a computer. To make a complete
7

computer, a microprocessor requires memory for storing data and programs, and input/output (I/O) interfaces for connecting external devices like keyboards and displays. Microcontrollers are characterized by how many bits of data they process at once, with a higher number of bits generally indicating a faster or more powerful chip. Eight-bit chips are popular for simpler designs, but 4-bit, 16-bit, and 32-bit architectures are also available. All microcontrollers have a defined instruction set, which consists of the binary words that cause the CPU to carry out specific operations. For example, the instruction 0010 0110 tells an 8052 to add the values in two locations. The binary instructions are also known as operation codes (Opcodes) for short. The opcodes perform basic functions like adding, subtracting, logic operations, moving and copying data, and controlling program branching. Inside, microcontrollers are little more than a carefully designed array of logic gates and memory cells, but modern fabrication processes allow thousands of these to fit on a single chip. Since the basic functions of a microcontroller performing arithmetic, logic, data-moving, and program branching functions are common ones that are useful in many applications, its practical to design a chip that performs these functions. The user accesses the abilities of the microcontroller by writing a program that performs the desired functions. The microcontroller contains all of the elements of a computer on a single chip. Using a microcontroller can reduce the number of components and thus the amount of design work and wiring required for a project. [4]

2.2 Architectures of Microcontrollers


Every microcontroller must have memory space to store a program (code) and data. While code provides instructions to the CPU, the data provides the information to be processed. The CPU uses buses (wire traces) to access the code ROM (Read Only Memory) and RAM (Random Access Memory) memory space. There are two types of microcontroller architectures Harvard architecture and von Neumann architecture.

2.2.1 Von Neumann architecture


In von Neumann Architecture the process of accessing the code or data could cause them to get in each other's way and slow down the processing speed of the CPU, because each had to wait for the other to finish fetching. The data and instructions use the same buses.

Figure 2.1 von Neumann architecture

2.2.2 Harvard architecture


To speed up the process of program execution, some CPUs use Harvard architecture. In Harvard architecture, the instruction memory and the data memory are separate, have separate bus for the code and data memory, and sometimes have different sizes. [3]

Figure 2.2 Harvard architecture

2.3 Atmel AVR


The AVR is a modified Harvard architecture 8-bit RISC (Reduced Instruction Set Computer) single chip microcontroller which was developed by Atmel in 1996. The AVR was one of the first microcontroller families to use onchip flash memory for program storage, as opposed to One-Time Programmable ROM, EPROM(Erasable Programmable Read Only Memory) , or EEPROM( Electrically Erasable Programmable Read Only Memory) used by other microcontrollers at the time. The Atmel AVR and the Microchip PIC are very similar (Harvard architecture) lines of microcontrollers.

2.3.1 Advantages of Atmel AVR


1. Cost. At the moment, the very lowest-price microcontroller available from any manufacturer is the Atmel AVR ATtiny116 MHz FLASH. 2. Speed: Not only are most AVRs capable of 20MHz (even really cheap ones like the ATtiny25/45/85 and ATmega48), but they actually run at near 20 MIPS; the PIC chip only run at 5 MHz with a 20 MHz oscillator frequency. In addition, with the better addressing modes and registers of
10

the AVRs, most operation can be done in only one instruction, where it often takes more than one instruction to do the same thing on a PIC. 3. Peripherals: Many Atmel AVR controllers, like many Microchip PIC controllers, have a built-in 10 bit ADC. Some have LCD (Liquid Cristal Display) or USB drivers. 4. One big advantage of AVRs is that they are supported by the GNU Compiler Collection (GCC)."

2.3.2 Disadvantages of Atmel AVR


1. Single source: only available from Atmel. 2. Power: Atmel AVRs have fairly low power, but the TI MSP430 series has the lowest power of any microcontroller. 3. Speed: near 20MIPS is great, but the SX chips regularly run at near 50MIPS and can run at 75 or even 100 (in rare cases)

2.3.3 Common misperceptions


Some people think the fastest Microchip PIC (20 MHz 4:1 pipeline) is as fast as the fastest Atmel AVR (20 MHz 1:1 pipeline). They are wrong. The PIC is actually processing instructions at 5 MIPS and the AVR is at just under 20 MIPS. Also, the AVR instruction set is larger, so in many cases, it is getting more done in fewer cycles. [9]

11

2.3.4 Groups of Atmel AVR


The AVR line is divided into two major groups; Tiny and Mega, with some additional special function versions for specific applications such as lighting, automotive, battery management, and LCD drivers. 2.3.4.1 Mega AVR These are powerful microcontrollers with more than 120 instructions and lots of different peripheral capabilities, which can be used in different designs. Table 2.1 shows the types of mega AVR. Some of their characteristics are as follows: Program memory: 4K to 256K bytes. Package: 28 to 100 pins. Extensive peripheral set. Extended instruction set: They have rich instruction set.

Table 2.1 types of mega AVR Code ROM 8K 16K 32K 64K 128K Data RAM 1K 1K 2K 4K 8K Data EEPROM 0.5K 0.5K 1K 2K 4K I/O pins 23 32 32 54 86

Part Num. AT mega 8 AT mega 16 AT mega 32 AT mega 64 AT mega 1280

ADC Timers 8 8 8 8 16 3 3 3 4 6

12

2.3.4.2 Tiny AVR As its name indicates, the microcontrollers in this group have less instructions and smaller packages in comparison to mega family. Some characteristics are as follows: Program memory: 1K to 8K bytes. Package: 8 to 28 pins. Limited peripheral set. Limited instruction set: the instruction sets are limited. For example, some of them do not have the multiply instruction.[3]

2.4 ATmega16
The Atmel AT mega 16 is equipped with 32 general purpose 8-bit registers that are tightly coupled to the processors arithmetic logic unit within the CPU. Also, the processor is designed following the Harvard Architecture format. That is, it is equipped with separate, dedicated memories and buses for program and data information. The register-based Harvard Architectures coupled with the RISC-based instruction set allows for fast and efficient program execution and allows the processor to complete an assembly language instruction every clock cycle. Atmel indicates the AT mega 16 can execute 16 million instructions per second when operating at a clock speed of 16 MHz.

13

2.4.1 Atmega16 architecture overview


The AT mega 16 has external connections for power supplies (Vcc, GND, Avcc, and AREF), an external time base (XTAL1 and XTAL2) input pins to drive its clock, processor reset(active low RESET), and four 8-bit ports (PA0PA7, PB0-PB7, PC0-PC7, PD0-PD7), which are used to interact with the external world, these ports may be used as general purpose digital input/output (I/O) ports or they may be used for the alternate function. The ports are interconnected with the AT mega 16s CPU and internal subsystems via an internal bus. The AT mega 16 also contains a timer subsystem, an analog to digital converter (ADC), an interrupt subsystem, memory components, and a communication subsystem. 2.4.1.1 Analog to digital converter The AT mega16 is equipped with an eight-channel ADC. The ADC converts an analog signal from the outside world into a binary representation suitable for use by the microcontroller. The AT mega16 ADC has 10-bit resolution. This means that an analog voltage between 0 and 5V will be encoded into one of 1024 binary representations between (000)16 and (3FF)
16.

This

provides the AT mega16 with a voltage resolution of approximately 4.88 Mv.[2]

14

2.5 Pin Description


Figure 2.3 below shows the pins of at mega 16 microcontroller:

Figure 2.3 pin description of Atmega16

VCC Digital supply voltage. GND Ground.

Port A (PA7..PA0) Port A serves as the analog inputs to the digital (ADC). Port A also serves as an 8-bit bi-directional I/O port, if the (ADC) is not used. Port pins can provide internal pull-up resistors (selected for each bit). The Port A output buffers have symmetrical drive characteristics with both high sink and source capability.

15

When pins PA0 to PA7 are used as inputs and are externally pulled low, they will source current if the internal pull-up resistors are activated. The Port A pins are tri-stated when a reset condition becomes active, even if the clock is not running.

Port B (PB7..PB0)

Port B is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port B output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port B pins that are externally pulled low will source current if the pull-up resistors are activated. The Port B pins are tri-stated when a reset condition becomes active, even if the clock is not running. Port B also serves the functions of various special features of the Atmega16 as listed on table below.

Port C (PC7..PC0)

Port C is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port C output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port C pins that are externally pulled low will source current if the pull-up resistors are activated. The Port C pins are tri-stated when a reset condition becomes active, even if the clock is not running. If the JTAG interface is enabled, the pull-up resistors on pins PC5(TDI), PC3(TMS) and PC2(TCK) will be activated even if a reset occurs. Port C also serves the functions of the JTAG interface and other special features of the Atmega16 as listed on table below.

16

Port D (PD7..PD0)

Port D is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port D output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port D pins that are externally pulled low will source current if the pull-up resistors are activated. The Port D pins are tri-stated when a reset condition becomes active, even if the clock is not running. Port D also serves the functions of various special features of the Atmega16 as listed on table below.

RESET Reset Input. A low level on this pin for longer than the minimum pulse length will generate a reset, even if the clock is not running. Shorter pulses are not guaranteed to generate a reset.

XTAL1 Input to the inverting Oscillator amplifier and input to the internal clock operating circuit.

XTAL2 Output from the inverting Oscillator amplifier.

AVCC AVCC is the supply voltage pin for Port A and the ADC. It should be externally connected to VCC, even if the ADC is not used. If the ADC is used, it should be connected to VCC through a low-pass filter.

17

AREF AREF is the analog reference pin for the ADC. [10]

2.6 Power Saving Modes

Idle Mode stops the CPU while allowing the USART, Two-wire interface, SRAM (Static Random Access Memory), Timer/Counters, SPI port, and interrupt system to continue functioning.

Power-Down Mode saves the register contents but freezes the Oscillator, disabling all other chip functions until the next external interrupt or Hardware Reset.

Power-Save Mode in which the Asynchronous Timer continues to run, allowing to maintain a timer base while the rest of the device is sleeping.

ADC Noise Reduction Mode stops the CPU and all I/O modules except Asynchronous Timer, to minimize switching noise during A/D conversions.

Standby Mode in which the crystal/resonator Oscillator is running while the rest of the device is sleeping. This allows very fast start-up combined with low-power consumption.

Extended Standby Mode in which both the main Oscillator and the Asynchronous Timer continue to run.[9]

2.7 Writing the Control Program


When its time to write the program that controls the project, the options include using machine code, assembly language, or a higher-level language.

18

2.7.1 Machine code


The most fundamental program form is machine code, the binary instructions that cause the CPU to perform the desired operations. Machine code or machine language is a system of instructions and data executed directly by a computers central processing unit. Machine code may be regarded as a primitive (and cumbersome) programming language or as the lowest-level representation of a compiled and/or assembled computer program. Programs in interpreted languages are not represented by machine code, however, their interpreter (which may be seen as a processor executing the higher level program) often is. Machine code is sometimes called native code when referring to platform-dependent parts of language features or libraries. Machine code should not be confused with so called byte code, which is executed by an interpreter. Every processor or processor family has its own machine code instruction set. Instructions are patterns of bits that by physical design correspond to different commands to the machine. The instruction set is thus specific to a class of processors using (much) the same architecture.

2.7.2 Assembly language


One step removed from machine code is assembly language, where abbreviations called mnemonics (memory aids) substitute for the machine codes. The mnemonics are easier to remember than the machine codes they stand for. For example, in the 8052s assembly language, the mnemonic CLR C means clear the carry bit, and is easier to remember than its binary code (11000011). Since machine code is ultimately the only language that a CPU understands, you need some way of translating assembly-language programs into machine code. For very short programs, you can hand assemble, or translate the mnemonics
19

yourself by looking up the machine codes for each abbreviation. Another option is to use an assembler, which is software that runs on a desktop computer and translates the mnemonics into machine code. Most assemblers provide other features, such as formatting the program code and creating a listing that shows both the machine-code and assembly-language versions of a program side by-side.

2.7.3 Higher-level languages (H-L-L)

A programming language such as C, FORTRAN, or Pascal that enables a programmer to write programs that are more or less independent of a particular type of computer. Such languages are considered high-level because they are closer to human languages and further from machine languages. In contrast, assembly languages are considered low-level because they are very close to machine languages. The main advantage of high-level languages over low-level languages is that they are easier to read, write, and maintain. Ultimately, programs written in a high-level language must be translated into machine language by a compiler or interpreter. The first high-level programming

languages were designed in the 1950s. Now there are dozens of different languages, including Ada, Algol, BASIC, COBOL, C, C++, FORTRAN, LISP, Pascal, and Prolog. Higher-level languages also simplify programming by allowing you to do in one or a few lines what would require many lines of assembly code to accomplish. Interpreters and compilers are two forms of higher-level languages. An interpreter translates a program into machine code each time the program runs, while a compiler translates only once, creating a new, executable file that the computer runs directly, without re-translating. As a rule, interpreters are very convenient for shorter programs where execution speed
20

isnt critical. With an interpreted language, you can run your program code immediately after writing it, without a separate compile or assembly step. A compiler is a good choice when a program is long or has to execute quickly. A single language like BASIC may be available in both interpreted and compiled versions. [4]

2.7.3.1 BASCOM-AVR BASCOM-AVR is a type of high level languages is four programs in one package, it is known as an IDE (Integrated Development Environment) it includes the program editor, the compiler, the programmer and the simulator all together. BASCOM-AVR is a compiler that uses a version of Basic very similar to QBASIC to produce programs for the AVR. BASCOM-AVR uses IDE which allows writing and editing programs, compiling them, testing them with a simulator and finally writing the program to the microcontroller for use in a circuit all from one program. Microcontrollers, such as the AVR, are controlled by software and they do nothing until they have a program inside them. The AVR programs are written on a PC (Personal Computer) using the BASCOM-AVR. This software is a type of computer program called compiler.[8]

- Programming template ----------------------------------------------------------------- 1. Title Block Author: Date:


21

File Name: ----------------------------------------------------------------- 2. Program Description: ----------------------------------------------------------------- 3. Compiler Directives (these tell Bascom things about our hardware) $regfile = attiny26.dat the micro we are using $crystal = 1000000 the speed of the micro ----------------------------------------------------------------- 4. Hardware Setups setup direction of all ports Config Porta = Output LEDs on portA Config Portb =Input switches on portB 5. Hardware Aliases Led0 alias portb.0 6. 22nitialize ports so hardware starts correctly Porta = &B11111111 turns off LEDs ----------------------------------------------------------------- 7. Declare Variables 8. Initialise Variables 9. Declare Constants ----------------------------------------------------------------- 10. Program starts here Do Loop End end program

22

CHAPTER THREE ARCHITECTURE OF THE LINE FOLLOWER


This chapter consist the design of the line follower, the car which used is a simple toy car that modified by taking out the most of it is component and just leave the main body which contains two dc motors, one in the front (for the front wheels) and the other one (for the rear wheels). This body is the basic construction that the circuit built on it. First of all the circuit built on a breadboard so as known, in electronics some experimentation is required to prototype (trial) specific circuits. A prototype circuit is needed before a PCB is designed for the final circuit. A breadboard is used to prototype the circuit. It has holes which components can be inserted and has electrical connections between the holes as per the figure 3.1 below shows that. Using a breadboard means no soldering and a circuit can be constructed quickly and modified easily before a final solution is decided upon.

Figure 3.1 breadboard


23

3.1 Motor Driver (L293D)


One of the first realizations in robotics is that making something move isnt an easy task. Simply cant take a brain circuit and connect it to a motor and expect anything to happen. The motor will simply not working at the puny output signal from the brains, and stay stationary. What the brain needs is an enforcer (Muscle). Something to convince the motor to do things the way the brains want it to be done. There are many ways to strengthen (buffer) a signal so its strong enough to drive a large load like a motor. Transistors H-bridges circuit, buffer chips, and dedicated motor driving chips are all suitable choices. A motor driver is a device or group of devices that serves to govern in some predetermined manner the performance of an electric motor. A motor driver include automatic means for starting and stopping the motor, selecting forward or reverse rotation, selecting and regulating the speed, regulating or limiting the torque, and protecting against overloads and faults.

3.1.1 (L293D) Description


This device is a monolithic integrated high voltage, high current four channel driver. Drive inductive loads (such as relays solenoids, DC and stepping motors and switching power transistors). To simplify use as two bridges each pair of channels is equipped with an enable input. A separate supply input is provided for the logic, allowing operation at a lower voltage and internal clamp diodes are included. This device is suitable for use in switching applications at frequencies up to 5 kHz. The L293D is assembled in a 16 lead plastic package which has 4 center pins connected together and used for heat sinking. L293D is dual H-Bridge motor drive, so with one IC can interface two DC motors which can be controlled in both clockwise and counter clockwise direction. L293D has output current of 600Ma and peak output current of 1.2A
24

per channel. Moreover for protection of circuit from back EMF output diods are included within the IC. The output supply has a wide range from 4.5V to 36V, which has made L293D a best choice for DC motor driver.

3.1.2 Working theory of H-Bridge


The name H-Bridge is derived from the actual shape of the switching circuit which controls the motion of the motor. It is also known as (Full Bridge). Basically there are four switching elements in the H-Bridge as shown in figure 3.2 below.

Figure 3.2 H-Bridge circuit

As seen in the figure 3.2 above there are four switching elements named as High side left, High side right, Low side right, Low side left. When these switches are turned on in pairs motor changes its direction accordingly. Like, if we switch on High side left and Low side right then motor rotate in forward direction, as current flows from (+) power supply through the motor coil goes to ground via switch low side right. Similarly, when you switch on low side left and high side right, the current flows in opposite direction and motor rotates
25

in backward direction. This is the basic working of H-Bridge. So we have seen that using simple switching elements we can make our own HBridge, or other option we have is using an IC based Hbridge driver. [7]

3.1.3 Pin configurations


Figure 3.3 and table 3.1 below shows the pin configuration of L239D.

Figure 3.3 pin configurations of (L293D)

26

Table 3.1 pin configuration of L293D Pin Number 1 2 3 4 5 6 7 8 9 11 11 12 13 14 15 16 Function Enable pin for Motor 1; active high Input 1 for Motor 1 Output 1 for Motor 1 Ground (0V) Ground (0V) Output 2 for Motor 1 Input 2 for Motor 1 Supply voltage for Motors; 9-12V (up to 36V) Enable pin for Motor 2; active high Input 1 for Motor 1 Output 1 for Motor 1 Ground (0V) Ground (0V) Output 2 for Motor 1 Input 2 for Motor 1 Supply voltage; 5V (up to 36V) Name Enable 1,2 Input 1 Output 1 Ground Ground Output 2 Input 2 Vcc 2 Enable 3,4 Input 3 Output 3 Ground Ground Output 4 Input 4 Vcc 1

27

3.2 Sensors
A sensor is often defined as a device that receives and responds to a signal or stimulus. This definition is broad. In fact, it is so broad that it covers almost everything from a human eye to a trigger in a pistol. Sensors that are used in artificial systems must speak the same language as the devices with which they are interfaced. This language is electrical in its nature. Thus, it should be possible to connect a sensor to an electronic system through electrical wires. Hence, we use a somewhat narrower definition of sensors, which may be phrased as: A sensor is a device that receives a stimulus and responds with an electrical signal. The stimulus is the quantity, property, or condition that is sensed and converted into electrical signal. The purpose of a sensor is to respond to some kind of an input physical property (stimulus) and to convert it into an electrical signal which is compatible with electronic circuits. The sensors considered as a translator of a generally nonelectrical value into an electrical value. The sensors output signal may be in the form of voltage, current, or charge. These may be further described in terms of amplitude, frequency, phase, or digital code. This set of characteristics is called the output signal format. Therefore, a sensor has input properties (of any kind) and electrical output properties.

3.2.1 IR LEDs
A light-emitting diode (LED) is a semiconductor diode that emits incoherent narrow-spectrum light when electrically biased in the forward direction of the p-n junction. The basic function of the emitter is to convert electricity into light. It works on the principle of recombination of the electron-hole pair. As in the conduction band of a diode, electrons are the majority carrier and in the valence band, holes are majority carrier. So when an electron from a conduction band recombines with a hole
28

of valance band, some amount of energy is released and this energy is in the form of light. The amount of energy released is depends upon the forbidden energy gap. The color of the emitted light depends on the composition and condition of the semiconducting material used, and can be infrared (IR LED), visible, or nearultraviolet. LEDs are often used as small indicator lights on electronic devices and increasingly in higher power applications such as flashlights and area lighting. The IR Led has two legs, the leg which is longer is positive and other leg is negative as seen in figure 3.4 below.

Figure 3.4 IR LED (Tx)

3.2.2 Photodiodes
The photodiode is a p-n junction diode which is connected in reverse bias direction. The basic function of the detector is to convert light into electricity. As its name implies that it works effectively only when the certain number of photon or certain amount of light falls on it. When there is no fall of light on the photodiode it has an infinite resistance and act as an open switch but as the light starts falling on the photodiode, the resistance becomes low and when the full intensity of light fall on the photodiode then its resistance becomes zero and it starts act like a closed switch. The IR receiver shows in figure 3.5 below: [6]

29

Figure 3.5 photodiode (Rx)

3.2.3 Circuit diagram of IR sensors


The circuit diagram is shown below in Figure 3.6 and only one set of emitter/detector sensor is depicted. The remaining two are constructed in the same manner.

Figure 3.6 circuit diagram of IR sensor

A line sensor in its simplest form is a sensor capable of detecting a contrast between adjacent surfaces, such as difference in color. The theory of operation is simple when light shines on a white surface; most of the incoming light is reflected away from the surface. In contrast, most of the incoming light is
30

absorbed if the surface is black. Therefore, by shining light on a surface and having a sensor to detect the amount of light that is reflected, a contrast between black and white surfaces can be detected. Figure 3.7 shows an illustration of the basics just covered.

Figure 3.7 the basic IR function

By making use the reflecting light from the black and white surfaces, the objective of tracking a black line is simple and can be achieved by using the appropriate sensors. The sensors array consists of two IR transmitters (Tx) and receivers (Rx) pasted on the front of the car as shown in figure 3.8 below:

Figure 3.8 the sensors positions

31

3.3 Interfacing the Components with Microcontroller


The components can be connected with microcontroller directly or through a certain devices.

3.3.1 IR sensor interface with the microcontroller


The IR sensors array consist of: two LDEs (Tx) and two photo diodes (Rx), the positive legs of the LEDs are connected to the source through (2K) resistance to reduce the input voltage, and the negative legs are connected to the ground. By this way the LEDs emitting infrared rays. The photo diodes also having two legs, the positive legs are grounded and the negative legs are connected with the microcontroller to pins (A0, A1).

3.3.2 Motor interface with the microcontroller


The microcontroller sends a signal to L239D that acts as a switch. If the signal received by the L239D is high it will rotate the motor or else it wont do so. Note that microcontroller only sends a signal to a switch which gives the voltage required by the motor to rotate. Here using L293D which can be used to control two motors. Pin connections for L239D shown below:

En1 & En2 are given logic 1 from microcontroller or give 5V from outside and are used to activate/deactivate one half of the H-bridge.

V is the voltage supply to the motor(s). Vcc is the logic 1 or 5V. GND There are two DC motors front motor for front wheels and rear motor for

rear wheels. The motors receive a signal from the microcontroller through the motor driver. The dc motors rotation depends on the polarity or the current
32

direction, thats why the motor driver is used. It has four available inputs and outputs, only two outputs are needed for each motor. The inputs of L239D are connected to the microcontroller ports (D1, D2, D3), D1 and D2 are connected to (PIN1, PIN2) respectively to drive the front motor bidirectional, D3 connected to(PIN4) to drive the rear motor. When the microcontroller sends a signal to the motor driver, this signal actually enables one of the outputs of the motor driver, so L293D allows the motor to rotate in the desired direction. The rear motor only goes forward tracing the line, so it doesnt need to run bidirectional and that is the reason of not connecting PIN5 with the microcontroller. The L293D-DCmotor-Interface can be represented in figure 3.9 below bellow:

Figure 3.9 L293D and DC-motors interfacing

33

Table 3.2 Truth table of L293D IN1 1 0 1 1 IN2 1 1 0 1 Description Motor stops or breaks Motor runs anti-clock wise Motor runs clockwise Motor stops or breaks

For the above truth table, the enable (EN) has to be set (1).

34

CHAPTER FOUR DESIGN AND PROGRAMMING OF THE LINE FOLLOWER


4.1 Circuit Connection
According to the main function of the line follower, which is to detect the path that specified by the user, two pair of IR transmitters and receivers has been connected to the terminals, transmitters connected to the power supply and the receivers connected to microcontroller(portA.0, portA.1) in order to send signal to the microcontroller, with this signal the car can define which case that it goes through, then it gives the suitable output for the motors through the L293D that takes the car back to the black line. The model can be represented in figure 4.1 below. As it is clear, the microcontroller is the main object that the whole components of the circuit are connected to. The devices can be divided into two groups, first; the inputs of the Atmega16 microcontroller which are; the IRs transmitter and receivers, only the receivers are connected directly to the pins of the microcontroller, and the two other Irs transmitters are supplied from the power supply (+ 5v). The other group is the output devices; they are; the front motor, the rear motor, multi colors LEDs (Light Emitting Diodes), motor driver (L293D).

35

Figure 4.1 the circuit diagram


36

4.2 The Principle of Operation of the Line Follower


The principle of operation of the follower is to send and receive signals from the right and left IR receivers which are located in the center of the car model, and send another signal to the microcontroller to take an action according to the input. The car should be put where the path (black line) is exactly above the right and the left IR receivers. While the microcontroller pins receives a specific signal from which is fed from both receivers Port A as (ADC) value of(+ 2.5v) that means the car should go forward, that happens when the black line is right under the IR receivers, the rear motor will receive (+ 5v) through the motor driver L293D, whenever one of the two IR receivers got out of the path to a plain surface (a white surface was taken in consideration), it directly sends a signal to the microcontroller reporting that the car got out of the desired path. So the front motor will rotate in the direction of the line in order to take the car back to its path. LEDs are used only to clarify the movement of the car, the two yellow front LEDs are showing that the car is in the right path, when the power is connected to the car, they flash ten times showing that the car is in the standby mode. There is a LED with a red color that lights when the car got out of the desired path which is already specified. Also there are two other LEDs; a right LED flashes when the front wheel rotates to the right direction, and another left LED also flashes when it goes to the left direction.

37

4.3 Programming of the Microcontroller


The program has been written by BASCOM-AVR programming language, its one of the simplest and easiest high-level programming languages that deal with Atmel-AVR microcontrollers, once it is a high-level language, the instructions of the program shows the logical statements that the car should physically execute according to the cases that the car might face in its way going forward. The following flowchart in figure 4.2 below clarifies the main idea of the program.

38

Start
FLASH THE TWO YELLOW LEDs TEN TIMES (STANDBY MODE)

BOHT R. IR & L. IR INSIDE THE LINE?

NO

YES
RUN THE REAR MOTOR AND THE TWO YELLOW LEDs

R. IR DETECTS LINE & L. IR OUT OF LINE?

NO

YES
ROTATE FRONT MOTOR TO THE RIGHT & RUN REAR MOTOR + RIGHT BLUE LED

2
39

1
BOHT R. IR & L. IR OUTSIDE THE PATH

NO

YES
STOP ALL MOTORS AND LIGHT RED LED
L. IR DETECTS LINE & R. IR OUT OF LINE?

NO

YES

ROTATE FRONT MOTOR TO THE LEFT & RUN REAR MOTOR + LEFT BLUE LED

2
Figure 4.2 Flowchart of the possible cases

40

4.3.1 The cases might face the follower


There are four cases that the car might face on its way; all of them should be completely defined in order to make the microcontroller able to deal with them. The first case is when the car is completely out of the path (the black line), at this case both of the IR transmitters is out of the path. The second case is when the car is inside the desired path, which means both of the IR transmitters are detecting the black line. The third when the car turns right of the line, leaving the path in its left, then only the left IR transmitter detects the black line, while the other detects the color of the surface (white surface). The fourth and the last case is, when the car turns left of the line, leaving the line in its right, at this case the right IR transmitter detects the black line, while the other detects the (white) surface. To be able to understand the conditions that the car may go through, it is essential to define the ports of the microcontroller; table 4.1 below identifies each port interface and when it should give an output signal:

41

Table 4.1 Ports connections

Port

I/O

connected to L293(IN 1) L293(IN 2) L293(IN 3) Red LED Left led Right led Forward yellow right LED Forward yellow left LED Right IR detector

Runs when Power is on Power is on Power is on The car got out of path The wheel turn left The wheel turn right At standby mode

Period always always always 30 msec

Portd.1 O/P Portd.2 O/P Portd.3 O/P Portc.0 O/P

Portb.0 O/P Portb.1 O/P Portb.2 O/P

Portb.3 O/P

At standby mode Always to detect the path Always to detect the path

30 msec

Porta.1

I/P

Porta.0

I/P

Left IR detector

42

4.3.2 The explanation of the code


The code starts with defining the micro type ($regfile = M16DEF.DAT) which means Atmega16, and specify the frequency used ($crystal = 10000000), then defining the ports as follows: Ports (D0, D1, D2, and D2) defined as an output ports that connected to L293D to control the motors. Port C0 defined as an output port that connected to the red led. Ports (B0, B1, B2, and B3) defined as an output ports connected to LEDs also. Part (a) of the code shows that after defining the ports, a variable has been reserved in the micro memory to detect a loop in which the forward yellow LEDs flashes for 10 times (ON for 30ms and OFF for 30ms) when the power ON, as the detectors shows that the car is in the standby mode. Next step a port A is configured as an ADC to receive the analog signal from the IR receivers. $regfile = M16DEF.DAT $crystal = 10000000 Config Portd.0 = Output Config Portd.1 = Output Config Portd.2 = Output Config Portd.3 = Output Config Portc.0 = Output Config Portb.0 = Output Config Portb.1 = Output Config Portb.2 = Output Config Portb.3 = Output Dim Y As Byte For Y = 1 To 10 Portb.2 = 1 Portb.3 = 1 Waitms 30 Portb.2 = 0
43

Portb.3 = 0 Waitms 30 Next Config Adc = Single , Prescaler = 8 , Reference = Internal Enable Interrupts Start Adc Part (a) of the code The next part started with holding a word in the memory for two variables; Right1 and Left1, the first variable refers port A1, and the second refers to port A0, which are the right and the left IR transmitter connected to the ports mentioned respectively. The while-loop shows that when both right and left IR detectors at the same time detect less than 300 (ADC value) or about (+ 2.5v), then the microcontroller will give a signal (+ 5v) to the red LED that is connected to port C0. In other words, when the car is completely out of path, only the red LED will light up. Dim Left1 As Word Dim Right1 As Word Dim A As Byte M: Do Left1 = Getadc(0) Right1 = Getadc(1) While Right1 < 300 And Left1 < 300 Left1 = Getadc(0) Right1 = Getadc(1) Portc.0 = 1 Portb.2 = 0 Portb.3 = 0 Portd.0 = 0 Portd.1 = 0 Portd.2 = 0 Portd.3 = 0 Waitms 300 Wend Portc.0 = 0
44

Part (b) Part ( c) is an If condition used to represent that when the left Rx read less than 400(digitally) then the program jump to loop defined as L, and if the right Rx read less than 400(digitally) then the program jump to loop defined as R. In case the conditions an above are not confirmed the port B2=1and port B3=1 which are connected to the forward yellow LEDs and a port D1=1, which connected to (input1) of the L293driver meaning that the rear motor is running, and all other ports are 0. To control the speed of the rear motor for the specific design, the micro sends signal (1) for 5ms and stop (0) for16ms. If Left1 < 400 Then Gosub L End If If Right1 < 400 Then Gosub R End If Portb.2 = 1 Portb.3 = 1 Portd.0 = 0 Portd.1 = 1 Portd.2 = 0 Portd.3 = 0 Waitms 5 Portd.0 = 0 Portd.1 = 0 Portd.2 = 0 Portd.3 = 0 Waitms 16 Loop End Part (c) In part (d) the loop L and R which mentioned in previous conditions are defined, in loop L port B0=1 which mean the left led flashes, port D1=1 and port

45

D3=1 to enable the L293D to rotate the forward motor to the right because the left receiver is out of path and need to rotate right to be in the path. For the loop R port B1=1 meaning that the right led flashes, port D (1 and 2) equal 1to enable the L293D to rotate the front motor to the left because the right receiver is out of the path and need to rotate left to stay on the track. In both loop L and R the rear motor runs for 5ms and stops for 16ms to drive the rear motor by specific speed. L: For A = 1 To 3 Portb.0 = 1 Portd.0 = 0 Portd.1 = 1 Portd.2 = 0 Portd.3 = 1 Waitms 5 Portb.0 = 0 Portd.0 = 0 Portd.1 = 0 Portd.2 = 0 Portd.3 = 1 Waitms 16 Next Gosub M R: For A = 1 To 3 Portb.1 = 1 Portd.0 = 0 Portd.1 = 1 Portd.2 = 0 Portd.3 = 1 Waitms 16 Next Gosub M R: For A = 1 To 3
46

Portb.1 = 1 Portd.0 = 0 Portd.1 = 1 Portd.2 = 1 Portd.3 = 0 Waitms 5 Portb.1 = 0 Portd.0 = 0 Portd.1 = 0 Portd.2 = 1 Portd.3 = 0 Waitms 16 Next Gosub M Part (d)

4.3.3 The logical statements explanations of the code


The program starts with the standby mode, the forward yellow LEDs should flash ten times by the time the power is on, as the detectors show that the car is in the standby mode, and it is ready to receive signals from the IR detectors. The number of the port of the LEDs has been referred to, previously in the table 4.1. The next loop started with holding a word in the memory for two variables; Right1 and Left1, the first variable refers to the ADC port no. 1, and the second refers to the ADC port no. 0, which are the right and the left IR transmitter connected to the ports mentioned respectively. The while-loop shows that when both right and left IR detectors at the same time detect less than 300 (ADC value) or about (+ 2.5v), then the microcontroller will give a signal (+ 5v) to the red LED that is connected to Portc.0. In other words, when the car is completely out of path, only the red LED will light up.

47

Else if the car got out of the path to the right, in case that the path is curved to the left of the car, then the right IR transmitter will send an ADC value more than 300 (more than + 2.5v) to the Porta.0, by the time the other left IR still in the path with an ADC value of more than 300, then the microcontroller will send a signal to the L293D to enable the output1 and output2 that control the front motor in order to rotate the wheels of the car to the left, taking the car back to the path that is in the left. Another two signals will be sent, one to the L293D for enabling the rear motor while the car is turning left back to the path, and other is sent to the left blue LED to flash while the car turning left. When the car got out of the path, but this time to the left in case that the black live is curved to the right, then the left IR transmitter will send an ADC value more than 300 to the Port1.1 of the microcontroller, by the time that the right IR still located in the right path, then the microcontroller will send three signals, one to the L293D to enable the output3 and output4 which control the rear motor in order to rotate the wheel of the car to the right,. The second signal will also be sent to the L293D to run the rear motor to go forward, and the other signal is for flashing the blue right LED. The last case, when the car is completely inside of the path (the black line), then, both of the IR transmitters will detect the line, and send this signal to the microcontroller, then, only the rear motor will be active by enabling the its two outputs of the L293D. And the two forward yellow LEDs will light as long as the car is in the right way.

48

CAPTER FIVE
5.1 Conclusion
A follower scheme has been done which mean that this van can run forward, turn right and turn left. (l293D) represents an interface unit with a microcontroller to control the movements of the two motors. BASCOM-AVR compiler is an easy language compared with the other high level language.

5.2 Recommendations
Smart versions of followers can be used in many life applications. Obstacle detectors can be add so that follower to stop when something crossover its way. The follower can be modifying to avoid things that might face it and return to right path.
Embedded magnets path can be used instead of drown line on

the surface, when its supplied with magnetic detectors, the follower follows the magnets path. The code can be modified and array of sensors can be increased to satisfy a specific cases(sensitivity) and when the follower totally gets out of the right path the control program command the to reverse its movement to return to the course.

49

References
[1]- Dhananjay V.Gadre, Programming and customizing the AVR microcontroller McGraw-Hill 2001. [2]- Steven F.Barrett, Daniel J.Pack, Atmel AVR microcontroller primer Programming and Interfacing, Morgan & Claypool 2008. [3]-Muhammad Ali Mazidi, Sarmad Naimi, Sepeher Naimi, The AVR microcontroller and embedded system using assembly and C, Prentice hall. [4]-Jan Axelson, The microcontroller idea book, Lakeview Research 1997. [5]- Priyank Patill, , K.J.Somaiya,Line following robot, Mumbai India. [6]- Jacob Fraden, Hand book of modern sensors (third edition), San Diego California [7]-Krishna Nand Gupta and others, Motor driver L293D. [8]- Programming in BASCOM-AVR, David Swinscoe www.davidswinscoe.com [9]-http://www.atmel.com/products/avr/ , may 2011 [10]- ATmega16 data sheet,Atmel Corporation 2003.

50

Appendices
The control program
$regfile = "M16DEF.DAT" $crystal = 10000000 Config Portd.0 = Output Config Portd.1 = Output Config Portd.2 = Output Config Portd.3 = Output Config Portc.0 = Output Config Portb.0 = Output Config Portb.1 = Output Config Portb.2 = Output Config Portb.3 = Output Dim Y As Byte For Y = 1 To 10 Portb.2 = 1 Portb.3 = 1 Waitms 30 Portb.2 = 0 Portb.3 = 0 Waitms 30 Next Config Adc = Single , Prescaler = 8 , Reference = Internal Enable Interrupts Start Adc Dim Left1 As Word Dim Right1 As Word Dim A As Byte M: Do Left1 = Getadc(0) Right1 = Getadc(1) While Right1 < 300 And Left1 < 300 Left1 = Getadc(0) Right1 = Getadc(1) Portc.0 = 1
51

Portb.2 = 0 Portb.3 = 0 Portd.0 = 0 Portd.1 = 0 Portd.2 = 0 Portd.3 = 0 Waitms 300 Wend Portc.0 = 0 If Left1 < 400 Then Gosub L End If If Right1 < 400 Then Gosub R End If Portb.2 = 1 Portb.3 = 1 Portd.0 = 0 Portd.1 = 1 Portd.2 = 0 Portd.3 = 0 Waitms 5 Portd.0 = 0 Portd.1 = 0 Portd.2 = 0 Portd.3 = 0 Waitms 16 Loop End L: For A = 1 To 3 Portb.0 = 1 Portd.0 = 0 Portd.1 = 1 Portd.2 = 0 Portd.3 = 1 Waitms 5 Portb.0 = 0 Portd.0 = 0
52

Portd.1 = 0 Portd.2 = 0 Portd.3 = 1 Waitms 16 Next Gosub M R: For A = 1 To 3 Portb.1 = 1 Portd.0 = 0 Portd.1 = 1 Portd.2 = 0 Portd.3 = 1 Waitms 16 Next Gosub M R: For A = 1 To 3 Portb.1 = 1 Portd.0 = 0 Portd.1 = 1 Portd.2 = 1 Portd.3 = 0 Waitms 5 Portb.1 = 0 Portd.0 = 0 Portd.1 = 0 Portd.2 = 1 Portd.3 = 0 Waitms 16 Next Gosub M

53

The line following van final design :

54

You might also like