You are on page 1of 62

See

discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/270903472

Industrial miniature model of Bottle filling


plant using Microcontroller based Embedded
System
THESIS JULY 2013
DOI: 10.13140/2.1.3682.5922

READS

822

1 AUTHOR:
Debarghya Dutta
Indian Institute of Technology Delhi
1 PUBLICATION 0 CITATIONS
SEE PROFILE

Available from: Debarghya Dutta


Retrieved on: 22 February 2016

A Thesis submitted in partial fulfillment of the requirement for the degree of


M.Tech in Mechatronics Engineering under West Bengal University of
Technology.

INDUSTRIAL MINIATURE MODEL OF BOTTLE FILLING PLANT


USING MICROCONTROLLER BASED EMBEDDED SYSTEM

By
DEBARGHYA DATTA
[Roll No.- 16004411010]
[Registration No.- 111600410056 of 2011-2012]

DEPARTMENT OF ELECTRICAL ENGINEERING


NATIONAL INSTITUTE OF TECHNICAL
TEACHERS TRAINING AND RESEARCH
(Established by the MHRD, Govt. Of India)
Block FC, Sector III, Salt Lake City, Kolkata-700106

ACKNOWLEDGEMENT

It is my privilege to express my profound and sincere heartiest gratitude to my


Respected Project Supervisor and Research Guide Asst. Prof. Dr. S.K. Mandal, for his
liable guidance, support and encouragement. Working with him on the Present problem
has been a rewarding and pleasurable experience that has greatly benefited me through
the course of work.
My special thanks and deep regards to Dr.S. Chattopadhyay, Asso. Prof. and Dr.
Mrs. Sagarika Pal for their valuable teaching and advices during tenure of my M.Tech
course. I am thankfull to all the staffs of Electrical Engineering Department for various
assistance to complete the work.
I would like to extend my deep gratitude to all of my associated friends and other
students for their support in undergoing the M.Tech course.

______________
Debarghya Datta

ii

ABSTRACT

Automatic bottle filling machine (ABM) wills a new innovation manufacturing industry
especially sector filling. Automatic bottle filling machine is commercial refilling system
that involves Motor Drives and microcontroller. The system operates automatically in
three stations, which are refilling, checking, and feedback. Filling system operated by
automatic follow bottle height respectively. Motor Drive operates to control motor as
disc through signal forwarded by IR sensor. Microcontroller will obtain signal from IR
sensor and data will be displayed on Liquid Crystal Display (LCD).
The objective of the thesis is to explore the approach of designing a
microcontroller based closed loop controller with on line calculation in order to keep
better flexibility and versatility. Hence the design of a closed loop bottle filling model
using a 12V D.C unipolar Stepper Motor and a 4.8-6V Servomotor have been presented.
The hardware & software are validated in real time by considering different step
settings. The interface circuit and software are all designed with consideration of a small
sampling time to achieve better performance. Implementation of the controller has been
done through a ATMEGA8 AVR microcontroller assembly language programming.

iii

TABLE OF CONTENT
CHAPTER I: INTRODUCTION
1.1
1.2
1.3
1.4

Introduction
Literature Review
Problem Statement Of The Dissertation
Goal Of The Dissertation

1
2
4
5

CHAPTER II: OVERVIEW


2.1
2.2
2.3
2.4
2.5
2.6
2.7

Embedded Control And Its Need


Advantages And Important Features Of Embedded Controller
Components Of Microcontroller Based Embedded system
Microcontrollers And Their Use
Operation Of Microcontroller
Sensors And Vision
Power Supply

6
6
8
9
9
10
10

CHAPTER III: DESIGN AND DEVELOPMENT


3.1
3.2

3.3
3.4
3.5
3.6
3.7
3.8
3.9

Working Procedure
Requirements
3.2.1 Hardware Requirement
3.2.2 Software Requirement
Block Diagram
Block Diagram Description
Circuit Diagram
Flow Chart
Flow Chart Description
LCD Used In This Dissertation
Software Approach

11
12
12
29
32
32
33
35
36
36
37

CHAPTER IV: EXPERIMENTAL RESULTS

40

CHAPTER V: CONCLUSION AND FUTURE SCOPE

42

REFERENCES

43

APPENDIX

44-56

iv

List of Figures
Figure 1. Power supply .................................................................................................................. 10
Figure 2. Rotating Disc .................................................................................................................. 12
Figure 3.Stepper motor .................................................................................................................. 13
Figure 4. Stepper motor connection ............................................................................................. 14
Figure 5. Servo Motor .................................................................................................................... 15
Figure 6.Servo Motor Connection ................................................................................................. 16
Figure 7.Servo motor ..................................................................................................................... 16
Figure 8.Servo Motor .................................................................................................................... 18
Figure 9.IR Sensor Working Principle .......................................................................................... 18
Figure 10.IR Sensor Array............................................................................................................. 19
Figure 11.Ultrasonic Sensor .......................................................................................................... 19
Figure 12.Microcontroller Layout ................................................................................................. 22
Figure 13.Oscillator ....................................................................................................................... 22
Figure 14.Serial Communication ................................................................................................... 24
Figure 15. General Overview ......................................................................................................... 27
Figure 16.Pin Configuration ........................................................................................................... 28
Figure 17.AVR Studio Setup ......................................................................................................... 29
Figure 18.AVR Chip Burning Steps .............................................................................................. 30
Figure 19. Selecting The Chip for Burning ................................................................................... 30
Figure 20.Building The Project ..................................................................................................... 31
Figure 21.Block Diagram .............................................................................................................. 32
Figure 22.Circuit Diagram ............................................................................................................. 33
Figure 23.Project Layout ................................................................................................................ 34
Figure 24.LCD ............................................................................................................................... 36
Figure 25.Program Writing Screenshot ......................................................................................... 37
Figure 26. Selecting the Chip for burning process ........................................................................ 38
Figure 27. Selecting the Hex file for Burning Process .................................................................. 38
Figure 28.Final Step while burning ................................................................................................ 39

CHAPTER I: INTRODUCTION
1.1 INTRODUCTION
Many works have been done on the application of Microcontroller based bottle
filling plant in engineering application as compared to web and data base application.
This chapter presents the works regarding the use of AVR based automatic refilling in
engineering fields.
Control is important for most industrial process to avoid disturbances, which
degrade the overall process performance, and hence a great deal of work is being done in
this field. Electronic controllers, first introduced many years ago, have gradually excelled
in performance over their predecessors both in terms of performance and its economy.
They have reached the highest level of sophistication due to the rapid advances made in
the industry.
The methods of Bottle filling system using controllers like microcontroller or
microprocessor are normally simpler and less expensive than that of some other methods
used in the industry now-a-days. Robotic manipulators are also of wide demand in this
particular area of work due to their precise, wide, simple and continuous control
characteristics, and moreover, for certain advantages over conventional analog servos.
In the present investigation, attempts have been made to design and development
of Microcontroller based Embedded system miniature model of a bottle filling plant. As
all other good things, this powerful component is basically very simple and is obtained
by uniting tested and high- quality "ingredients" (components) as per following receipt:
the simplest computers processor is used as a "brain" of the future system. An
ATMEGA8 controller has been used in our dissertation for control purpose and
execution.
The Stepper Motor and the Servo motor offers the accurate control of the disc or
belt carrying the disc of bottles which is to be filled. Stepper motors consist of a
permanent magnet rotating shaft, called the rotor, and electromagnets on the stationary
portion that surrounds the motor, called the stator.

A command signal which is issued from the user's interface panel comes into the
servo's "positioning controller". The positioning controller is the device which stores
information about various jobs or tasks. It has been programmed to activate the
motor/load, i.e. change speed/position. The Servo motor actually controls the position of
the filler arm which is coupled to it. A reservoir can be incorporated with the system for
larger applications of the same.
The overall project has been designed with the Motor drives with a microcontroller
and typical Sensors for further operations. IR sensor and Ultrasound Sensor acts as the
sensing and actuating part which is directly connected with the driver circuit and the
embedded kit used for the purpose.

1.2 LITERATURE REVIEW


1.

In the year 1971 A.ALEXANDROVITZ and Z.ZABZR have proposed analog


computer simulation of a commutation switch used in microcontroller based filling
system. In this scheme, a simulation method is outlined, a mathematical model is
developed for the whole system and results are reported for a specific example.

2.

In 1977 A.K. LIN and W.W. KOEPSEL proposed a little advanced microprocessor
based filling scheme. The digital system consisting of random logic circuits and an
Intel 8080 microcomputer offers overall advantages in price, performance,
flexibility, and reliability and power requirements.

3. In 1988 J.B. PLANT, S.J. JORNA and Y.T. CHAN has proposed a suitable method
for microprocessor implementation of controlling the conveyor belt. The derivation
of a simple control law is given next, followed by the stability and error analysis of
the controller.
4. In 1989 ZIYAD SALAMEH and SUNWAY WANG investigate the design and
implementation of a firing scheme using the Intel Microcontroller to control the
speed of Control motors, viz. Servo and Stepper motors.
5. In 1991 J.GORDON KETTLEBOROUGH,

IVOR R. SMITH, VINOD

V.VADHER, and FERNANDO L. M. ANTUNES describe a microprocessor based


bottle filler for a system fed from a dc source, which incorporates spillover field to
provide smooth and precise control from standstill to general value.
6. S.P CHOWDHURY, DR. S.K BASU and R. MONDAL in 1992 have reported on
the development of a laboratory model of an experimental microcomputer-based
2

controller. The motor drive system consists of a small Stepper motor. A torque
measuring system has also been incorporated.
7. In 1997 F.I. AHMED, A.M.EI-TOBSHY, A.A. MAHFOUZ and M.M.S.
IBRAHIM have introduced the traditional Proportional- Integral (P-I) controller
system for controlling the conveyor belt mechanism. Some improvements in this
field like theoretical studies for (P-I) and (I-P) controllers in the S-domain are
presented and the transfer functions for both are derived.
8. The paper Design of an environment for physical phenomena simulation:
application to visualization and animation of electric field and potential authored
by CASADO-REVUELTA E, MARTINEZ-JIMENEZ, BLANCA-PANCORBO
A. implements the environment towards orientation of automatic filler system.
9. ZAATAR W. and NASR G.E. presents in the paper An implementation scheme
for a microcontroller emulator a general method for defining a microcontroller
emulator, applicable in any high level programming language.
10. COUGHLAN K.L. and P.J. discusses in the paper Developing special purpose
simulators under Microsoft windows AVR the benefits of using Win AVR to
construct special purpose simulators. The development of a special purpose
simulator for the Irish Electricity supply board is described.
11. In 2004 A.H.M.S. UL utilized the SCADA model for the bottle filling entire
mechanism. The control algorithms are stored and implemented by the
microprocessor of the microcomputer. The system employs the use of Motor driver
implemented on the computer.
12. In 2007 T.MAITY, A.GHOSH and S.K. BHUNIA developed a software program
which is an integrated, automated and efficient control, using microcontroller based
embedded systems high level programming languages, through which the system
was successfully compiled and run, thus getting a more reliable autonomous filler
plant.
13. APARAJITH, S. ET AL. (2010) shows that Technology does not drive change, it
enables change. The primary purpose of Technology is its implementation in dayto-day life wherein it could enhance the lifestyle. The use of a robotic arm has been
implemented to design the conveyor belt system.
14. GOGATE, C.A. ET AL. (2004) explains that At present any consignment carried by
a conveyor belt system having a robotic arm which is further manipulated by a

indigenous DC motor can be implemented to pick an place the water filling bottles
in a sequential manner.
15. XING JIANPING ET AL. (2005) proposed a method of combining the two types of
procedures, i.e. a robotic arm with manipulator and SCADA technique for the bottle
filling process. It was initially generalized for bulky industrial systems.
16. DAI, Z. ET AL. (2007) describes the performance of the autonomous milk or water
filling systems with the help of a Proportional and Integral controller. For further
work Proportional-Integral-Derivative controller was also implemented. Numerical
results were Satisfactory.

17.HUSSAIN, M. ET AL. (2011) A novel approach for creating an advisory/regulatory


environment to limit the maximum running speed of the conveyor is presented. This
paper deals with creating an onboard speed regulation module for manipulators
which can monitor as well as control their instantaneous speed in comparison with
the maximum permissible speed of the system. The location of the initial filler is
obtained using position tracking technology. The work discusses the unique position
matching algorithm developed and design details of the proposed on-board module
for pick an place bottle. The algorithm continuously compares the actual speed of
the conveyor belt with its corresponding location based limits obtained through the
developed database and thus provides: (a) An advisory signal to the driver about the
need for a reduction in speed of Stepper motor. (b) An automatic restriction of the
speed below the prescribed limits.

1.3 PROBLEM STATEMENT OF THE DISSERTATION


To make an autonomous plant such that bottles can be filled with least human
interference.
Infra-Red Sensor (IR sensor), which will get the input by the reflected beam of
light and it will determine the starting position of the rotating disc. A person will keep
normally Empty bottles at the starting position. Then the bottle will be moved with a
moving mechanism (a circular disc) driven by stepper motor.
Second IR sensor and the emitting & receiving mechanism will sense the bottle to
the filling point. In addition, the microcontroller will command the moving mechanism to
stop. Then manually we will fill the bottle.
An Ultrasound Sensor fitted at the top of the system mechanism will sense the
presence of the filled bottle at the end point and the microcontroller will command the
driver circuit to stop the circular disc.
A servo motor will allow to fill up the bottles after a time delay once a bottle has
been filled. The ATMEGA8 microcontroller itself will initialize the movement of the
servo arm. The movement of the servo arm will be governed by the motor driver circuit.
Finally the LCD which has been interfaced in the microcontroller kit will show
the various positions of the bottle filled rotating disc and will indicate the subsequent
instructions to run the system accordingly.

1.4 GOAL OF THE DISSERTATION


The objective of this project is to design construct and test hardware and software to
create an autonomous prototype of bottle filling plant. In the present times, most
automated manufacturing tasks are carried out by specialized system designed to perform
predetermined functions in a manufacturing process. The inflexibility and generally high
cost of these machine often called hand automation systems in fulfilment of these
interests , initiate the use of system which are capable of performing a variety of
manufacturing functions in more flexible to perform the process control and object
transfer routine as in modern industrial production system. It implies to fill the liquid in
bottle on a rotating disc done by the stepper motor after a definite time delay with a servo
motor aiding the filling process.

CHAPTER-II: OVERVIEW
2.1 EMBEDDED CONTROL AND ITS NEED
An embedded control is done by a special-purpose computer system designed to
perform one or a few dedicated functions, often with real-time computing constraints. It
is usually embedded as part of a complete device including hardware and mechanical
parts. In contrast, a general-purpose computer, such as a personal computer, can do many
different tasks depending on programming. Embedded systems control many of the
common devices in use today.
Embedded controllers are often the heart of an industrial control system or a
process control application. The majority of computer systems in use today is embedded
in other machinery, such as automobiles, telephones, appliances, and peripherals for
computer systems. While some embedded systems are very sophisticated, many have
minimal requirements for memory and program length, with no operating system, and
low software complexity. Typical input and output devices include switches, relays,
solenoids, LEDs, small or custom LCD displays, radio frequency devices, and sensors for
data such as temperature, humidity, light level etc. Embedded systems usually have no
keyboard, screen, disks, printers, or other recognizable I/O devices of a personal
computer, and may lack human interaction devices of any kind.
2.2 ADVANTAGES AND IMPORTANT FEATURES OF EMBEDDED
CONTROLLER
The main differentiating feature of an embedded controller is that external PC
controls not all system operation. In fact, the CPU running the system is actually built
into the I/O system itself. While some type of general purpose Personal Computer
complete with mouse, monitor and other human interface devices (HID) hosts a typical,
slaved data acquisition system, an Embedded Controller's processor is usually dedicated
to controlling the I/O system and often does not provide any direct human interface.
Differences between an embedded controller and a standard PC are easily
observed. However, the differences in software are equally noticeable. While most PCs
operating systems for our desktop and laptop computer are large (in terms of RAM and
hard drive space needed), operating systems developed for embedded systems are likely

to be smaller and have been developed without all of the built-in GUIs as well as much of
office equipment peripheral support.
Since embedded processors are usually used to control devices, they sometimes
need to accept input from the device they are controlling. This is the purpose of the
analog to digital converter. Since processors are built to interpret and process digital data,
i.e. 1's and 0's, they won't be able to do anything with the analog signals that may be
being sent to it by a device. So the analog to digital converter is used to convert the
incoming data into a form that the processor can recognize. There is also a digital to
analog converter that allows the processor to send data to the device it is controlling .
In addition to the converters, many embedded microprocessors include a variety
of timers as well. One of the most common types of timers is the Programmable Interval
Timer, or PIT for short. A PIT just counts down from some value to zero. Once it
Reaches zero, it sends an interrupt to the processor indicating that it has finished
counting. This is useful for things such as thermostats, which periodically test the
temperature around them to see if they need to turn the air conditioner on, the heater on,
etc.
In the earliest years of computers in the 1930-40s, computers were sometimes
dedicated to a single task, but were far too large and expensive for most kinds of tasks
performed by embedded computers of today. Over time however, the concept of
programmable controllers evolved from traditional electromechanical sequencers, via
solid state devices, to the use of computer technology.
Since these early applications in the 1960s, embedded systems have come down
in price and there has been a dramatic rise in processing power and functionality. The
first microprocessor for example, the Intel 4004 was designed for calculators and other
small systems but still required many external memory and support chips. In 1978
National Engineering Manufacturers Association released a "standard" for programmable
microcontrollers, including almost any computer-based controllers, such as single board
computers, numerical, and event-based controllers.
Physically, embedded systems range from portable devices such as digital
watches and MP3 players, to large stationary installations like traffic lights, factory
controllers, or the systems controlling nuclear power plants. Complexity varies from low,
with a single microcontroller chip, to very high with multiple units, peripherals and
networks mounted inside a large chassis or enclosure.

We can summarize the important features of embedded controller as follows


Embedded systems are designed to do some specific task, rather than be a
general-purpose computer for multiple tasks. Some also have real-time performance
constraints that must be met, for reasons such as safety and usability; others may have
low or no performance requirements, allowing the system hardware to be simplified to
reduce costs.
Embedded systems are not always separate devices. Most often they are
physically built-in to the devices they control.
The software written for embedded systems is often called firmware, and is
stored in read-only memory or Flash memory chips rather than a disk drive. It often runs
with limited computer hardware resources: small or no keyboard, screen, and little
memory.
2.3 COMPONENTS OF MICROCONTROLLER BASED EMBEDDED SYSTEM
Central processing unit - ranging from small and simple 4-bit processors to complex 32or 64-bit processors
Discrete input and output bits, allowing control or detection of the logic state of an
individual package pin
Serial input/output such as serial ports (UARTs)
Other serial communications interfaces like IC, Serial Peripheral Interface and
Controller Area Network for system interconnect
Peripherals such as timers, event counters, PWM generators, and watchdog
Volatile memory (RAM) for data storage
ROM, EPROM, EEPROM or Flash memory for program and operating parameter
storage
Clock generator - often an oscillator for a quartz timing crystal, resonator or RC circuit
Many include analog-to-digital converters
In-circuit programming and debugging support

2.4 MICROCONTROLLERS AND THEIR USE


Depending on the producer, it is added: a bit of memory, a few A/D converters,
timers, input/output lines etc. It is all placed in one of standard packages.
Simple software that will be able to control it all and about which everyone will
be able to learn has been developed. Three things have had a crucial impact on such a
success of the microcontrollers- Powerful and intelligently chosen electronics embedded in the microcontrollers
can via input/output devices (switches, push buttons, sensors, LCD displays, relays)
control various processes and devices such as industrial automatics, electric current,
temperature, engine performance etc.
A very low price enables them to be embedded in such devices in which, until
recent time it was not worth embedding anything, the world is overwhelmed today with
cheap automatic devices and various intelligent appliances.
Prior knowledge is hardly needed for programming. It is sufficient to have any kind
of PC (software in use is not demanding at all and it is easy to learn to work on it) and
one simple device (programmer) used for transferring completed programs into the
microcontroller.

2.5 OPERATION OF MICROCONTROLLER


Even though there is a great number of various microcontrollers and even greater
number of programs designed for the microcontrollers use only, all of them have many
things in common. A typical scenario on whose basis it all functions is as follows-1) Power supply is turned off and everything is so still,chip is programmed,
everything is in place, and nothing indicates what is to come.
2) Power supply connectors are connected to the power supply source and
everything starts to happen at high speed. The control logic registers what is going on
first. It enables only quartz oscillator to operate.
3) Voltage level has reached its full value and frequency of oscillator has
become stable. The bits are being written to the SFRs, showing the state of all peripherals
and all pins are configured as outputs. Everything occurs in harmony to the pulses
rhythm and the overall electronics starts operating. Since this moment, the time is
measured in micro and nanoseconds.

4) Program Counter is reset to zero address of the program memory. Instruction


from that address is sent to instruction decoder where its meaning is recognized and it is
executed with immediate effect.
5) The value of the Program Counter is being incremented by 1 and the whole
process is being repeated,several million times per second.

2.6 SENSORS AND VISION


Sensors provide intelligence to the manipulator for its motion control, joint link position,
velocity, torque is required to be sensed and the end effectors position and orientation is
required. Sensors may be proximity, range, contact or non-contact, tactile or non-tactile,
or a vision system.
Controller: Robot controller generally performs three functions1) Initiation and termination of the motion of components of the manipulators
desired sequence.
2) Storage of position and data sequence
3) Interfacing of robot with outside world via sensors.
Controllers may be Simple step sequencer, Pneumatic Logic System or microcontroller
based.

2.7 POWER SUPPLY


Two things within the circuit that take care of the microcontroller power supply
are worth attention

Figure 1. Power supply

10

CHAPTER III: DESIGN AND DEVELOPMENT

3.1 WORKING PROCEDURE


In the Bottle Filling Plant System when the supply is given through the adapter,
the sensor checks the position of circular disc and a signal is given by the microcontroller
to first stepper motor to move the disc containing the containers to its desired initial
position. The movement of the arm will be governed by the combination of the Servomotor .
When the container is put in the belt, it interrupts the IR Sensor, which senses the
home position with the help of a acceptor and reflector mechanism, i.e. it seeks the initial
position of the circulating disc by receiving the amount of reflected light from a whitish
surface which is marked in a particular container and it gives the signal to the
microcontroller through amplifier circuit. A person will keep normally Empty
bottles/containers at the starting position. The presence of the bottles/container will be
sensed by using Light. Thus, the circuit will be broken, the microcontroller will direct the
stepper motor driver to move the conveyor belt through stepper motor, and the bottle will
be moved with a moving mechanism.
At a certain distance, there is a filling point where the container will be filled.
Next when the container is being filled, a certain time delay is given. An Ultrasound
sensor is attached to the top of the system to indicate the amount of filler filled in the
containers. It works on the principle of Doppler effect. As soon as it receives the signal, it
immediately sends a feedback signal to the microcontroller to stop the filler arm of the
servomotor.
In the next instance the LCD which is used to show the display will read as
Filling Bottle 1 or Filling bottle 2 and so on. It will display Seeking home position
at the very beginning. In my project I have used six containers to show the overall
mechanism. After the bottle is filled, the conveyor belt will move again until the bottle
reaches the end point. There again the IR mechanism will sense the presence of the filled
bottle and then the microcontroller will command the driver circuit to stop the belt.
Microcontroller will give the command to the second Motor driver circuit to operate the
stepper motor. The level of the liquid filled in by the filler will be initiated by a pair of
Ultrasound sensors. In this way six containers are filled in subsequently.
11

3.2 REQUIREMENTS
3.2.1. HARDWARE REQUIREMENT
The Hardware of the Bottling Plant consists of Three parts :1. Moving System
2. Sensing System
3. Controlling System

MOVING SYSTEMRotating Disc :-- A circular disc in which all the bottles are fitted one after another is
driven by a stepper motor. The stepper motor is defined at a specific time intervals which
allows it to rotate the disc according to the signals generated from the Sensors.

Figure 2. Rotating Disc

12

Stepper Motor :-

Figure 3.Stepper motor

Stepper Motors work under a very similar principle to DC motors, except they have
many coils instead of just one. So to operate a stepper motor, one must activate these
different coils in particular patterns to generate motor rotation. So stepper motors need to
be sent patterned commands to rotate. These commands are sent (by a microcontroller) as
high and low logic over several lines, and must be pulsed in a particular order and
combination. Steppers are often used because each 'step,' separated by a set step angle,
can be counted and used for feedback control. For example, a 10-degree step angle
stepper motor would require 36 commands to rotate 360 degrees. However, external
torque can force movement to a different step, invalidating feedback. Therefore, external
torque must never exceed the holding torque of a stepper.
Stepper motors consist of a permanent magnet rotating shaft, called the rotor,
and electromagnets on the stationary portion that surrounds the motor, called the stator.
Figure 5 illustrates one complete rotation of a stepper motor. At position 1, we can see
that the rotor is beginning at the upper electromagnet, which is currently active (has
voltage applied to it). To move the rotor clockwise (CW), the upper electromagnet is
deactivated and the right electromagnet is activated, causing the rotor to move 90 degrees
CW, aligning itself with the active magnet. This process is repeated in the same manner
at the south and west electromagnets until we once again reach the starting position.
In the above example, we used a motor with a resolution of 90 degrees or
demonstration purposes. In reality, this would not be a very practical motor for most
applications. The average stepper motor's resolution -- the amount of degrees rotated per
pulse -- is much higher than this. For example, a motor with a resolution of 5 degrees
would move its rotor 5 degrees per step, thereby requiring 72 pulses (steps) to complete a
full 360-degree rotation.
13

Figure 4. Stepper motor connection

Voltage- Polarized

typically from 5-12V, but can range to extremes in special

application motors. Higher voltages generally mean more torque, but they also require
more power. Steppers can run above or below rated voltage (to meet other design
requirements) most efficient at rated voltage.
Current-In case of a motor, we would consider Stall Current, Holding Current and
Operating Current (maximum and minimum).
Stall Current - The current that a stepper motor requires when powered but held so that
it does not rotate.
Holding Current - The current that a stepper motor requires when powered but not
signaled to rotate.
Operating Current - The current drawn when a stepper motor experiences zero
resistance torque.It is best to determine current curves relating voltage, current, and
required torque for optimization. When a stepper motor experiences a change in torque
(such as motor reversal), expect short-lived current spikes. Current spikes can be up to
2x the stall current, and can fry control circuitry if unprotected.
Power (Voltage x Current) - Running motors close to Stall Current often, or reversing
current frequently under high torque, can cause motors to melt Heat Sink.

14

Torque In case of a motor, we would consider Stall Torque and Operating Torque
(maximum and minimum).
Stall Torque - The torque a stepper motor requires when powered but held so that it does
not rotate.
Holding Torque - The torque a stepper motor requires when powered but not signaled to
rotate.
Operating Torque - The torque a stepper motor can apply when experiencing zero
resistance torque. Changing the voltage will change the torque.
Velocity - Motors run most efficient at the highest possible speeds. Gearing a motor
allows the stepper motor to run fast, yet have a slower output speed with much higher
torque.
Efficiency - Stepper motors are most efficient at rated voltage. They are less efficient
than DC motors due to non-continuous stepping.
Control Methods - Stepper Motors require a special stepper controller (driver) to
prevent loss of torque. It has a more precise control than a DC motor.

SERVO MOTOR:-

Figure 5. Servo Motor

This is not easily defined nor self-explanatory since a servomechanism, or servo drive,
does not apply to any particular device. It is a term which applies to a function or a task.
The function, or task, of a servo can be described as follows. A command signal which is
issued from the user's interface panel comes into the servo's "positioning controller". The
positioning controller is the device which stores information about various jobs or tasks.
It has been programmed to activate the motor/load, i.e. change speed/position.

15

Figure 6.Servo Motor Connection

The signal then passes into the servo control or "amplifier" section. The servo control
takes this low power level signal and increases, or amplifies, the power up to appropriate
levels to actually result in movement of the servo motor/load.
These low power level signals must be amplified: Higher voltage levels are needed to
rotate the servo motor at appropriate higher speeds and higher current levels are required
to provide torque to move heavier loads.
This power is supplied to the servo control (amplifier) from the "power supply" which
simply converts AC power into the required DC level. It also supplies any low level
voltage required for operation of integrated circuits.

Figure 7.Servo motor

16

Therefore, a servo involves several devices. It is a system of devices for controlling some
item (load). The item (load) which is controlled (regulated) can be controlled in any
manner, i.e. position, direction, speed. The speed or position is controlled in relation to a
reference (command signal), as long as the proper feedback device (error detection
device) is used. The feedback and command signals are compared, and the corrections
made. Thus, the definition of a servo system is, that it consists of several devices which
control or regulate speed/position of a load.

A servo is a mechanical motorized device that can be instructed to move the output shaft
attached to a servo wheel or arm to a specified position. Inside the servo box is a DC
motor mechanically linked to a position feedback potentiometer, gearbox, electronic
feedback control loop circuitry and motor drive electronic circuit.

Servo Ratings-- The most common details available on a servo are its speed and torque
rating. Nearly all servo packages are listed with brand name, model name/ number,
speed, and torque output at 4.8 volts and 6.0 volts. Some information about metal, plastic
gears or ball bearings may also be listed.

Servo Speed-- Servo Speed is measured by the amount of time (in seconds)
it takes a 1 inch servo arm to sweep left or right through a 60 degree arc at either 4.8 or
6.0 volts. A servo rated at 0.22 seconds/60 degrees takes 0.22 seconds to sweep through a
60 degree arc. Some of the fastest servos available move in the 0.06 to 0.09 second
range. In some servos, faster speeds may
lower torque available.

Servo Torque-- Servo Torque is measured by the amount of weight (in ounces) that a
servo can hold at 1-inch out on the servo output arm in the horizontal plane, again at
either 5.0 or 6.0 volts to see when the servo stalls as it tries to lift the weight horizontally.

Servo Power-- Servo operate from 4.5 to 6.0 volts DC. At the higher voltage servos tend
to be faster and sometimes stronger, but can heat up faster when stalled or in a hold
position with stress forces against the servo output shaft. Some servo controllers require a
separate power source from the control source to deliver the higher 6.0 Vdc.

17

Figure 8.Servo Motor

SENSING SYSTEM
IR Sensor- - Infra-Red sensor consists of an emitter (LED) and detector (photo
diode). Emitter sends IR pulses. Position calculation is done through intensity of
reflected light received by the detector. Ambient interference is negligible in IR sensors.

Figure 9.IR Sensor Working Principle

18

Figure 10.IR Sensor Array

Ultrasonic sensors- - Parallax PING ultrasound Sensors can accurately measure distance
up to 3 meters. Connection is very easy requiring only 3 lines: +5V, GND and one Port
pin of Microcontroller.

Figure 11.Ultrasonic Sensor

CONTROLLING SYSTEM

Microcontroller Basics
Obviously, everything that occurs in the microcontroller occurs at high
speed and quite simple, but it would not be so useful if there were no special interfaces,
which make it, complete. Text below refers to that (in short).

19

Program Memory (ROM)


The Program Memory is a type of memory, which permanently stores a
program being executed. Obviously, the maximal length of the program that can be
written to depends on the size of the memory. Program memory can be built in the
microcontroller or added from outside as a separate chip, which depends on type of the
microcontroller. Both variants have advantages and disadvantages: if added from outside,
the microcontroller is cheaper and program can be considerably longer. At the same time,
a number of available pins is decremented as the microcontroller uses its own
input/output ports to be connected to the memory. The capacity of Internal Program
Memory is usually smaller and more expensive but such a chip has more possibilities of
connecting to peripheral environment. Program memory size ranges from 512B to 64KB.

Data Memory (RAM)


Data Memory is a type of memory used for temporary storing and keeping
different data and constants created and used during operating process. The content of
this memory is erased once the power is off. For example: when the program performs
addition, it is necessary to have a register presenting what in everyday life is called a
sum. For that purpose, one of the registers in RAM is named as such and serves for
storing results of addition. Data memory size goes up to a few KBs.

EEPROM Memory
The EEPROM Memory is a special type of memory which not all the types
of the microcontrollers have. Its content can be changed during program execution
(similar to RAM), but it is permanently saved even after the power goes off (similar to
ROM). It is used for storing different values created and used during operating process
and which must be saved upon turning off the device (calibration values, codes, values to
count up to etc.). A disadvantage of this memory is that programming is relatively slowmeasured in milliseconds.

SFRs (Special Function Registers)


SFRs are a particular part of memory whose purpose is defined in advance by
the producer. Each of these registers has its name and controls some of interfaces within
the microcontroller. For example, by writing zero or one to the SFR controlling some

20

input/output port, each of the port pins can be configured as input or output (each bit in
this register controls the purpose of one single pin).

Program Counter
Program Counter is an engine which starts the program and indicates the
address in memory where next instruction to execute is found. Immediately after its
execution, the value of the counter is incremented by 1. For this automatic increment, the
program executes one instruction at a time as it is written. However ,the program counter
value could be changed at any moment, which will cause jump to a new location in the
program memory. This is how subroutines or branch instructions are executed.

CPU (Central Processor Unit)


As its name tells, this is the unit who monitors and controls all operations
being performed within the microcontroller and the user cannot affect its work. It
consists of several smaller units. The most important are- Instruction decoder - a part of electronics, which recognizes program
instructions and based on which runs other circuits.
Arithmetical Logical Unit (ALU) - performs all mathematical and logical
operations with data. The features of this circuit are described in the "instruction set"
which differs for each type of the microcontroller.
Accumulator - is a special type of the SFR closely related to operating mode of
the ALU. It is a kind of desk on which all data needed to perform some operation on are
set (addition, shift etc.). It also contains a result, ready to be used further in operation.
One of the SFRs, called the Status Register, is closely related to the accumulator,
showing at any time the "status" of a number being in the accumulator (the number is
greater than or less than zero etc.).

Bit - This word indicates whether the voltage is applied to an electrical conductor or not.
In the first case, a logical one is present on the pin, i.e. the bits value is 1. Otherwise, if
the voltage level is 0 V, i.e. a logical zero is present on the pin, the bits value is 0
Input/output ports (I/O Ports)
The microcontroller cannot be of any use without being connected to
peripheral devices. For that reason, each microcontroller has one or more registers
connected to its pins (called ports in this case).
21

Figure 12.Microcontroller Layout

Oscillator

Figure 13.Oscillator

It is commonly configured to use quartz crystal or ceramics resonator for frequency


stabilization. Besides, it can often operate without elements for frequency stabilization
(like RC oscillator). It is important to know that instructions are not executed at the rate
22

ordered by oscillator but several times slower. The reason for this is that each instruction
is executed in several steps (In some microcontrollers execution time of all instructions is
equal, while in others microcontrollers execution time differs for different instructions).
Consequently, if our system uses quartz crystal of 20MHz, execution time of a program
instruction is 200, 400 or even 800 nS.

Timers/Counters
Most programs use in some way these miniature electronic "stopwatches". They
are mostly 8- or 16-bit SFRs whose value is automatically incremented with each coming
pulse. Once the register is completely "filled up"- an interrupt is generated.
If the registers use internal oscillator for its operating then it is possible to measure
the time between two events ( if the register value is T1 at the moment measuring has
started, and T2 at the moment measuring has finished, then the time that has passed is
equal to the value gained by their subtraction T2-T1 ). If the registers for its operating use
pulses coming from external source then such a timer is converted to counter. This is a
very simple explanation used to describe the essence of the operating. It is a bit more
complicated in practice.

Register is another name for a memory cell. Beside 8 bits available to the user, each
register has also addressing part usually not visible to the user. It is important to know-All registers in ROM as well as those in RAM memory identified as generalpurpose registers are mutually equal. During programming, each register can be assigned
a name, which makes operating much easier.
All SFRs have their own names that are different for different types of the
microcontrollers and each of them has a particular role.

Brown out is potentially dangerous state coming up now the microcontroller is being
turned off or in situations when due to powerful disturbances, voltage supply comes to
the lowest limit. As the microcontroller consists of several circuits, which have different
operating voltage levels, this can cause its "out of control" performance. In order to
prevent that, a circuit for brown out reset is usually embedded. When the voltage level
drops below the lower limit then this circuit immediately resets the whole electronics.

23

Reset pin is usually identified as MCLR (Master Clear Reset) and serves for "external"
reset of the microcontroller by applying logical zero or one depending on type of the
microcontroller. In case the brown out is not embedded, a simple external circuit for
brown out reset can be connected to this pin.

Serial communication

Figure 14.Serial Communication

Connection between the microcontroller and peripheral devices established through I/O
ports is an ideal solution for shorter distances- up to several meters. But, when it is
needed to enable communication between two devices on longer distances or when for
any other reason it is not possible to use "parallel" connection,in such and similar
situations, communication through pulses, called serial communication is the most
appropriate to use.
Serial communication problem has been resolved a long time ago and nowadays several
different systems enabling this kind of connection are embedded as a standard equipment
into most microcontroller.
One of the most important things concerning the use of serial communication is to
strictly observe the Protocol. It is a set of rules, which must be applied in order to enable
devices to recognize the data being exchanged. Fortunately, the microcontrollers
automatically take care of it, which leads to a reduction of the programmers work to
simple writing and reading data.

Byte - 8 bits next to each other make entity called a program word or a byte. If the bit is a
digit then it is logical that bytes are numbers. All mathematical operations can be
performed upon them, just like with usual decimal numbers and they are performed in the
ALU. It is important to note that byte ( as each number) has two sides, i.e. digits a byte
24

consists of are not of equal significance. The highest value has a digit on the far left
called the most significant bit (MSB). A digit on the far right has the least value and is
called the least significant bit (LSB). As 8 digits can be combined in 256 different ways,
the greatest decimal number that can present one byte is 255 (zero is also presented with
one combination).

Program
Unlike other integrated circuits, which only need to be connecting to other components
and then powered on, the microcontrollers need to be programmed too prior to turning
the power on.

Interrupt
Electronics is usually faster than physical process in environment it should keep under
control. Thats why the microcontroller spends the most of its time waiting for something
to happen or execute. In order to avoid continuous checking for logical state on input
pins and in internal registers, the interrupt is generated. It is a signal interrupting regular
program execution. Since several events can cause interrupt, when it occurs, the
microcontroller immediately stops operating and checks for the cause. If it is needed to
perform some action, a current state of the program counter is pushed on the Stack and
the appropriate program is executed (so called interrupt routine).

Stack
It is a part of RAM used for storing the current state of the program counter
(address).This address lets the controller know where to return after the subroutine has
been executed. Stack can consist of several levels. This enables subroutines nesting, i.e.
calling one subroutine from another.
Alphabetical List Of Instructions
ACALL- Absolute Call
ADD, ADDC- Add Accumulator (With Carry)
AJMP- Absolute Jump
ANL- Bitwise AND
CJNE- Compare and Jump if Not Equal
25

CLR- Clear Register


CPL- Complement Register
DA- Decimal Adjust
DEC- Decrement Register
DIV- Divide Accumulator by B
DJNZ- Decrement Register and Jump if Not Zero
INC- Increment Register
JB- Jump if Bit Set
JBC- Jump if Bit Set and Clear Bit
JC- Jump if Carry Set
JMP- Jump to Address
JNB- Jump if Bit Not Set
JNC- Jump if Carry Not Set
JNZ- Jump if Accumulator Not Zero
JZ- Jump if Accumulator Zero
LCALL- Long Call
LJMP- Long Jump
MOV- Move Memory
MOVC- Move Code Memory
MOVX- Move Extended Memory
MUL- Multiply Accumulator by B
NOP- No Operation
ORL- Bitwise OR
POP- Pop Value From Stack
PUSH- Push Value Onto Stack
RET- Return From Subroutine
RETI- Return From Interrupt
RL- Rotate Accumulator Left
RLC- Rotate Accumulator Left Through Carry
RR- Rotate Accumulator Right
RRC- Rotate Accumulator Right Through Carry
SETB- Set Bit
SJMP- Short Jump
SUBB- Subtract From Accumulator With Borrow
26

SWAP- Swap Accumulator Nibbles


XCH- Exchange Bytes

The AVR microcontroller is computer-on -chip is the brain of Bottle Filling Plant, which
makes decisions and controls all the functions of plant to make it autonomous along with
its circuitry. ATMEGA8 IC is used in which program is burned In its RAM.

Figure 15. General Overview

Specifications
Operating Voltage- 4.5V to 5.5V
Speed Grades- 0 to 16MHz
Power Consumption at 4MHz, 3V, 25oC
Active- 3.6mA
Idle mode- 1mA
Power down mode- 0.5A

27

Pin Configuration

Figure 16.Pin Configuration

Pin Descriptions
VCC -- Digital supply voltage.
GND-- Ground.
Port B (PB7 to PB0)
XTAL1/XTAL2/TOSC1/
TOSC2 -- 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.

28

3.2.2 SOFTWARE REQUIREMENT


Procedure of using the IDE
The sample IDE window is shown below-

Using the AVR kit and compiler For working with the ATMEGA kit, we first need to
install some software to write your program, compile them and burn it in our hardware
kit.
Softwares are:1. winAVR (free GNU compiler)
2. AVR Studio 4.0 (free IDE)
3. Extreme Burner
Running the IDE for the first time we need to follow the subsequent steps to run the IDE
for the first time:* Click on the AVR Studio 4 in desktop shortcut or in the All Programs Menu.
* On opening this dialog box should come. Click on New project.

Figure 17.AVR Studio Setup

29

Select AVR GCC, Select a suitable location (preferably desktop & Create new
folder there). Giving a project name then we have to click Next

Figure 18.AVR Chip Burning Steps

Select AVR Simulator, Atmega8 from the device list. Click on finish

Figure 19. Selecting The Chip for Burning

30

Right click on topmost item (usually on the project name) in the left hand tab (see
below) and click on Edit Configuration options

Figure 20.Building The Project

Enter 8000000 in the Frequency tab, we have to click on OK.

31

3.3 BLOCK DIAGRAM

Figure 21.Block Diagram

3.4 BLOCK DIAGRAM DESCRIPTION


The power supply is given as 5V. It passes through the IR sensor and Ultrasonic
sensor for initializing and detecting the home position. The logic circuitry panel consists
of the motor driver circuit which acts when the Stepper motor and Servo motor comes
into play. The microcontroller which has been used here is the AVR ATMEGA8. The
programme is burned in it through the compiler and AVR studio. An LCD is used here to
display the various positions of the rotating containers in the circular disc.
The L293D is the motor driver circuit which is the heart of the overall system. It connects
the Stepper motor with the control block which is the microcontroller. The Ultrasonic
sensor is used for detecting the filler, while the Servo motor block will control the filler
arm during the operation of the system. Port A or Port C pins are nothing but the
combination of different microcontroller Pins. It can be stated as a closed loop control
system owing to the feedback mechanism provided by the IR sensor, Ultrasonic sensor,
Motor driver circuit blocks to the main microcontroller interfacing block.
32

3.5 CIRCUIT DIAGRAM

Figure 22.Circuit Diagram

33

OVERVIEW OF WORK

Figure 23.Project Layout

34

3.6 FLOW CHART

35

3.7 FLOW CHART DESCRIPTION


The Flow Chart shown above depicts the overall process of the automatic bottle filling
mechanism. The initial block denotes the START position, i.e. when supply is given to
the system. The disc begins to rotate in clockwise/anticlockwise direction containing the
bottles therein. This mechanism is governed by the Stepper motor. Next the IR Sensor
upon detecting the home position places the initial bottle in the predefined space. The
Ultrasound sensor which has been used then senses the filler mechanism. If it correctly
detects the said process, then the Stepper motor stops and the filler which is being
governed by the Servo motor comes into action. If it fails to detect the filling mechanism,
the Stepper motor again starts to rotate the disc.
A time delay of 3s has been given in our experimental process. After the filling
mechanism of the 1st bottle is completed, the stepper motor again starts to rotate the disc
containing the bottles. Again the IR sensor which is provided to initialise the home
position if detects the next bottle in the process, the stepper motor stops and in the same
sequential manner the whole process continues, which finally completes the required
objective and after the execution of the required process the whole system comes to a
halt. This has been represented by the block showing STOP.

3.8 LCD USED IN THIS DISSERTATION

Figure 24.LCD

The LCD which has been used is an alphanumeric LCD and it can display only numbers,
texts and symbols. It is very cost effective and also easy to interface with the system.
Typically there are 16 pins in a 16x2 LCD display. It means we can display upto 2 lines
and 16 characters in a line. Pin no. 1 and 2 are Ground and VDD respectively and are
36

connected with Gnd and +5V Bus. The Pin no. 3 is LCD contrast control pin. Pin no. 414 are needed for the interfacing with the microcontroller. These pins are directly
connected with the microcontroller port pins.

3.9 SOFTWARE APPROACH


After typing the program we have to save it, then click on Build -> Rebuild All

Figure 25.Program Writing Screenshot

Transferring the program


We have to click on Chip and ATMEGA8 from the list.
This has to be done first time only.

37

Figure 26. Selecting the Chip for burning process

Then we have to click on Open, then select the *.hex file inside the default folder
inside the project folder.

Figure 27. Selecting the Hex file for Burning Process

38

Figure 28.Final Step while burning

39

CHAPTER IV: EXPERIMENTAL RESULTS


The expected result of the working of the project has been achieved. The
autonomous bottle filling plant designed using the AVR microcontroller and the IRUltrasonic sensing mechanism has been run successfully according to the programming
done using the IDE software. The bottling plant as been verified with its entire
component thoroughly checked and the bottles are successfully sensed and filled
manually. In addition, the servo motor which is attached to the system for the filler to fill
the containers is running successfully.
In the very beginning upon giving power supply, the system seeks Home Position by the
help of IR sensor.

Then when filling is done in the 1st bottle, the LCD displays likewise

40

After the bottle has been filled, the LCD shows the following display

Next it seeks for the next bottle in progress

In the similar process following the 1st step it shows the display of the 2nd bottle

In this way the process continues in a sequential order, one after another. I have used six
bottles in my project for the purpose.
Thus we can see that our purpose of making a prototype version of a bottle filling system
with the help of microcontroller, motor drives, sensors and using LCD display are
working and functioning properly without any errors.

41

CHAPTER V : CONCLUSION AND FUTURE SCOPE

CONCLUSION

The prototype of bottle filling plant based on embedded system was implemented
and working successfully using microcontroller AVR.

The industrial prototype was built and the conclusions are as follows:

The system can perform the task of autonomous quality control system used in
industrial production.

The interfacing of microcontroller programming with hardware of prototype of


bottle filling plant was tested and working successfully.

Implementation of various sensors like IR with Ultrasonic and switches for the
control of rotating disc is functioning successfully.

FUTURE SCOPE
Though the autonomous bottling plant made using the AVR microcontroller is quite
simple and automated, but this realization can be made advanced and faster using PLC
(programmable logical control) which provides faster realization of the circuits. PLCs are
well adapted to a range of automation tasks. These are typically industrial processes in
manufacturing where the cost of developing and maintaining the automation system is
high, relative to the cost of the automation, and where changes to the system would be
expected during its operational life.
Even though this project is very much set as per the production because, we can only fill
and pick up the bottle. We can further modify it using various technologies and processes
like capping the filled bottles, putting the label, varying the height and even packing four
to six bottles in a carton. Thus, we can create a lot of scope for future working with
certain modifications.

42

REFERENCES
[1] Gopal K. Dubey, Fundamentals of Electric Drives, Narosa Publishing House, New
Delhi-1989.
[2] Kumara MKSC, Dayananda PRD, Gunatillaka MDPR, Jayawickrama SS, PC based
speed controlling, A final year report University of Moratuwa Illiniaus USA, 2001102.
[3] J Nicolai and T. Castagnet, A flexible Micro controller based chopper driving a
motor drive, The European Power electronics Application. 1993.
[4] J. Chiasson, Nonlinear Differential- Geometric Techniques for microcontroller, IEEE
Transactionson Control Systems Technology, Vol 2, Page 35-42, 1994.
[5] Peter Spasov, Microcontroller Technology: The 68HC11 Prentice Hall, 5th edition,
2004.
[6] LCD Interfacing, the Microcontroller and Embedded systems by Muhammad Ali
Mazidi, Janice Gillispie Mazidi, Rolin D. Mckinlay.
[7] The 8051 Microcontroller by Kenneth J.Ayala.
[8] The 8051 Microcontroller and Embedded Systems by M. A. Mazidi.
[9] Introduction to Robotics by Sayeed B. Nikku.
[10] Manual book, A beginners guide to AVR, version 2.1. Singapore: Omron.2001
[11] ADC devices ATMEGA8L data sheet.
[12] Lawrence A. Duarte. The Microcontroller Beginners Handbook. 2nd Edition,
USA, Prompt Publication. 3-5; 1998.
[13] Iovine John, Microcontroller Project Book, 2nd Edition. Singapore: McGraw-Hill.
121-123; 2000.
[14] D. Roy Chowdhury, Linear Integrated Circuits, New Age International (P) Ltd.,
2003.
[15] Julia Case Bradley, Anita C. Millspaugh. Programming in Virtual Studio, Version
6. New York: McGraw-Hill/Irwin, 2002.
[16] Sensors and Transducers by A.K. Shawney.
[17] Abu Zaharin Ahmad and Mohd. Nasir Taib. A study on the micro-controller
mechanism, Asia SENSE SENSOR, 2003, Page 359-364.
[18] P.C. Sen and M.L. MacDonald. IEEE Transactions on Energy Conversion, 1978,
Vol. IECI-25, No. 4: 347-354.

43

APPENDIX
MICROCONTROLLER PROGRAMMING CODES
SERVO-MOTOR TEST
#include "lcd.h"
#include<util/delay.h>
#include<stdio.h>
#include"motor.h"
#include"adc.h"
#include<avr/interrupt.h>
uint8_t servo1_pos=45,servo1_temp;
int Read_Distance();
ISR(TIMER1_OVF_vect) // isr is called after 20ms ///
{
int k;
TCNT1=0xFD8F;
sbi(PORTC,1);
_delay_ms(1);
servo1_temp=servo1_pos;
for(k=0;k<135;k++)
{
_delay_us(7);
servo1_temp--;
if(servo1_temp==0) cbi(PORTC,1);
}
}

int main()
{
int i,j,level,zero_level;
LCD_Init();
DCM_Init();
TCCR1B=0x04;
TCNT1=0xFD8F;
sbi(TIMSK,TOIE1);
sei();
sbi(DDRC,1);
while(1)
{
LCD_Clrscr();
printf("Seeking \n Home ");
while(ADC_Read(1)>950)
44

STM_CW();
LCD_Clrscr();
servo1_pos=45;
_delay_ms(100);
zero_level=Read_Distance();
LCD_Clrscr();
level=0;
while(level<3)
{
LCD_Home();
printf("Filling Bottle 1 \nLevel = %dcm
level= zero_level- Read_Distance();
if(level<0) level=0;
}
LCD_Clrscr();
printf("Bottle Filled");
_delay_ms(2000);
servo1_pos=90;

",level);

for(j=2;j<7;j++)
{
LCD_Clrscr();
printf("Moving to\n Bottle - %d",j);
for(i=0;i<85;i++)
STM_CW();
servo1_pos=45;
_delay_ms(100);
zero_level=Read_Distance();
LCD_Clrscr();
level=0;
while(level<3)
{
LCD_Home();
printf("Filling Bottle %d \nLevel = %dcm
level=zero_level-Read_Distance();
if(level<0) level=0;
}
LCD_Clrscr();
printf("Bottle Filled");
_delay_ms(2000);
servo1_pos=90;
}
}
return 0;
}
#define in 0
#define out 1
int Read_Distance()
45

",j,level);

{
uint16_t dist;
sbi(DDRD,out);
cbi(DDRD,in);
sbi(PORTD,in);
TCCR0|=(1<<CS00);
TCNT0=214; // for 6us
cbi(PORTD,out);
_delay_us(10);
sbi(PORTD,out);
_delay_us(10);
cbi(PORTD,out);
dist=0;
cli();
while( (PIND & 0x01)==0);
while( ((PIND & 0x01)!=0) && (dist<30000))
{
while( (TIFR & (1<<TOV0)) == 0);
TCNT0=214;
TIFR |= (1<<TOV0);
dist++;
}
sei();
dist=dist/10;
return dist;
}
LCD TEST
#include "LCD.h"
#include <stdio.h>
#include<util/delay.h>
#include<compat/deprecated.h>

#define CLK 5
#define DATA 3
static int LCD_char(char data,FILE *stream);
static FILE uart_out= FDEV_SETUP_STREAM(LCD_char,
NULL,_FDEV_SETUP_WRITE);

void SPI_MasterInit(void)
{
46

/* Set MOSI and SCK output, all others input */


/* Enable SPI, Master, set clock rate fck/16 */
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
SPSR = (1<<SPI2X);
}
void CLOCK()
{
_delay_us(1);
sbi(PORTB,CLK);
_delay_us(1);
cbi(PORTB,CLK);
}
void SPI_MasterTransmit(char cData)
{
uint8_t mask=0x80,i;
for(i=0;i<8;i++)
{
if( (cData & mask)==0 )
cbi(PORTB,DATA);
else
sbi(PORTB,DATA);
CLOCK();
mask=mask>>1;
}
}

//----------------------------------------------------------------------------------------// FUNCTION: LCD_write


// PURPOSE: send a character or an instruction to the LCD
void LCD_write(uint8_t data,uint8_t rs)
{
//check write type
if (rs)
{
LCD_rs_high(); //write data
}
else
{
LCD_rs_low(); //write instruciton
}
SPI_MasterTransmit(data);
_delay_us(100);
47

LCD_e_high();
_delay_us(2); //sensitive when changing CPU MHz!!!!!!!!!!!!!
LCD_e_low();
_delay_us(2);
}
//----------------------------------------------------------------------------------------// FUNCTION: LCD_char
// PURPOSE: send a character to the LCD
static int LCD_char(char data,FILE *stream)
{
if (data=='\n')
{
if (g_nCurrentLine >= LCD_LINES - 1)
LCD_Setline(0);
else
LCD_Setline(g_nCurrentLine+1);
}
else
LCD_write(data,1);
return 0;
}

//----------------------------------------------------------------------------------------// FUNCTION: LCD_instr


// PURPOSE: send an instruction to the LCD
void LCD_instr(uint8_t instr)
{
LCD_write(instr,0);
}
//----------------------------------------------------------------------------------------// FUNCTION: LCD_init
// PURPOSE: Initialize LCD to 8 bit I/O mode
void LCD_Init()
{
// configure all port bits as output (LCD data and control lines on different ports
//SPI_MasterInit();
DDRB = (1<<3)|(1<<5)|(1<<7)|(1<<6);
_delay_ms(16);
LCD_instr(LCD_FUNCTION_8BIT_2LINES); // 4-bit interface, dual line, 5x7 dots
_delay_ms(2);
LCD_instr(LCD_ENTRY_INC_);//cursor move right, no shift display
_delay_ms(2);

48

LCD_instr(0X0C);// display on, cursor on, blink char


_delay_ms(2);
LCD_Home();//set cursor to home and clear the cursor
stdout = &uart_out;
}
//----------------------------------------------------------------------------------------// FUNCTION: LCD_newline
// PURPOSE: Move cursor on specified line
void LCD_Setline(uint8_t line)
{
uint8_t addressCounter = 0;
switch(line)
{
case 0: addressCounter = LCD_START_LINE1; break;
case 1: addressCounter = LCD_START_LINE2; break;
case 2: addressCounter = LCD_START_LINE3; break;
case 3: addressCounter = LCD_START_LINE4; break;
default:addressCounter = LCD_START_LINE1; break;
}
g_nCurrentLine = line;
LCD_instr((1<<LCD_DDRAM)+addressCounter);
}
int g_nCurrentLine = 0;
//----------------------------------------------------------------------------------------// FUNCTION: LCD_gotoxy
// PURPOSE: Set cursor to specified position
//
Input: x horizontal position (0: left most position)
//
y vertical position (0: first line)
void LCD_Gotoxy(uint8_t x, uint8_t y)
{
#if LCD_LINES==1
LCD_instr((1<<LCD_DDRAM)+LCD_START_LINE1+x);
#elif LCD_LINES==2
switch (y)
{
case 0:LCD_instr((1<<LCD_DDRAM)+LCD_START_LINE1+x);break;
case 1:LCD_instr((1<<LCD_DDRAM)+LCD_START_LINE2+x);break;
default: break;
}
#elif LCD_LINES==4
switch (y)
{
case 0:LCD_instr((1<<LCD_DDRAM)+LCD_START_LINE1+x);break;
case 1:LCD_instr((1<<LCD_DDRAM)+LCD_START_LINE2+x);break;
49

case 2:LCD_instr((1<<LCD_DDRAM)+LCD_START_LINE3+x);break;
case 3:LCD_instr((1<<LCD_DDRAM)+LCD_START_LINE4+x);break;
default: break;
}
#endif
}
MOTOR DRIVE TEST
#include<compat/deprecated.h>
#include <avr/io.h>
#define L_MOTOR_EN
1
#define R_MOTOR_EN
2
#define L_MOTOR_INA
0
#define L_MOTOR_INB
5
#define R_MOTOR_INA
6
#define R_MOTOR_INB
7
void DCM_Init()
{
DDRD|=0xE0; // set the motor drive pins as output ////
DDRB|=0X07;
}
void DCM_Forward()
{
sbi(PORTB,L_MOTOR_EN);
sbi(PORTB,R_MOTOR_EN);
sbi(PORTB,L_MOTOR_INA);
cbi(PORTD,L_MOTOR_INB);
cbi(PORTD,R_MOTOR_INA);
sbi(PORTD,R_MOTOR_INB);
}
void STM_CW()
{
//// sequence 1 //////////
sbi(PORTB,L_MOTOR_EN);
sbi(PORTB,R_MOTOR_EN);
cbi(PORTB,L_MOTOR_INA);
cbi(PORTD,L_MOTOR_INB);
cbi(PORTD,R_MOTOR_INA);
sbi(PORTD,R_MOTOR_INB);
_delay_ms(5);
//// sequence 2 //////////
sbi(PORTB,L_MOTOR_EN);
sbi(PORTB,R_MOTOR_EN);
cbi(PORTB,L_MOTOR_INA);
cbi(PORTD,L_MOTOR_INB);
sbi(PORTD,R_MOTOR_INA);
cbi(PORTD,R_MOTOR_INB);
_delay_ms(5);

50

//// sequence 3 //////////


sbi(PORTB,L_MOTOR_EN);
sbi(PORTB,R_MOTOR_EN);
cbi(PORTB,L_MOTOR_INA);
sbi(PORTD,L_MOTOR_INB);
cbi(PORTD,R_MOTOR_INA);
cbi(PORTD,R_MOTOR_INB);
_delay_ms(5);
//// sequence 4 //////////
sbi(PORTB,L_MOTOR_EN);
sbi(PORTB,R_MOTOR_EN);
sbi(PORTB,L_MOTOR_INA);
cbi(PORTD,L_MOTOR_INB);
cbi(PORTD,R_MOTOR_INA);
cbi(PORTD,R_MOTOR_INB);
_delay_ms(5);
}
void STM_CCW()
{
//// sequence 4 //////////
sbi(PORTB,L_MOTOR_EN);
sbi(PORTB,R_MOTOR_EN);
sbi(PORTB,L_MOTOR_INA);
cbi(PORTD,L_MOTOR_INB);
cbi(PORTD,R_MOTOR_INA);
cbi(PORTD,R_MOTOR_INB);
_delay_ms(10);
//// sequence 3 //////////
sbi(PORTB,L_MOTOR_EN);
sbi(PORTB,R_MOTOR_EN);
cbi(PORTB,L_MOTOR_INA);
sbi(PORTD,L_MOTOR_INB);
cbi(PORTD,R_MOTOR_INA);
cbi(PORTD,R_MOTOR_INB);
_delay_ms(10);
//// sequence 2 //////////
sbi(PORTB,L_MOTOR_EN);
sbi(PORTB,R_MOTOR_EN);
cbi(PORTB,L_MOTOR_INA);
cbi(PORTD,L_MOTOR_INB);
sbi(PORTD,R_MOTOR_INA);
cbi(PORTD,R_MOTOR_INB);
51

_delay_ms(10);
//// sequence 1 //////////
sbi(PORTB,L_MOTOR_EN);
sbi(PORTB,R_MOTOR_EN);
cbi(PORTB,L_MOTOR_INA);
cbi(PORTD,L_MOTOR_INB);
cbi(PORTD,R_MOTOR_INA);
sbi(PORTD,R_MOTOR_INB);
_delay_ms(10);
}
ADC TEST
#include<avr/io.h>
//HEADER FILE FOR AVR INPUT OUTPUT
#include<compat/deprecated.h> //HEADER FILE FOR FUNCTIONS LIKE SBI
AND CBI
#include<util/delay.h>
//HEADER FILE FOR DELAY

int ADC_Read(uint8_t channel)


{
uint16_t val;
sbi(ADCSRA,7);
ADCSRA=0X87;
DDRC&= ~(1<<(channel-1));
ADMUX=0X40 | (channel-1);
ADCSRA |= (1<<ADSC); // a transformation single conversion
while ( ADCSRA & (1<<ADSC) );
val=ADC;
return(val);
}
USART TEST
#include<avr/io.h>
OUTPUT
#include<compat/deprecated.h>
SBI AND CBI
#include<util/delay.h>
#include<stdio.h>
#include <avr/interrupt.h>

//HEADER FILE FOR AVR INPUT


//HEADER FILE FOR FUNCTIONS LIKE
//HEADER FILE FOR DELAY

void USARTInit(uint16_t ubrr_value);


static int USARTWriteChar(char data,FILE *stream);
char USARTReadChar();

52

static FILE uart_out= FDEV_SETUP_STREAM(USARTWriteChar,


NULL,_FDEV_SETUP_WRITE);
char USARTReadChar()
{
//Wait untill a data is available
while(!(UCSRA & (1<<RXC)))
{
//Do nothing
}
//Now USART has got data from host
//and is available is buffer
return UDR;
}
static int USARTWriteChar(char data, FILE *stream)
{
//Wait untill the transmitter is ready
if (data == '\n')
USARTWriteChar('\r', stream);
while(!(UCSRA & (1<<UDRE)));
//Now write the data to USART buffer
UDR=data;
return 0;
}
void USARTInit(uint16_t ubrr_value)
{
//Set Baud rate
UBRRL = ubrr_value;
UBRRH = (ubrr_value>>8);
/*Set Frame Format

>> Asynchronous mode


>> No Parity
>> 1 StopBit
>> char size 8
*/

53

UCSRA=(1<<U2X);
UCSRC=(1<<URSEL)|(3<<UCSZ0);
UCSRB=(1<<RXEN)|(1<<TXEN);
stdout = &uart_out;
}
AVR
#include<avr/io.h>
#include<compat/deprecated.h>
#include<util/delay.h>
int main()
{
DDRD=0b00011100;
DDRD=(1<<4)|(1<<3)|(1<<2);
DDRD=0x1C;
DDRD=28;
return 0;
A=~B;
y=A|B
Y=A&B
Y=A^B
PORTD=PORTD|(1<<4)|(1<<3);

PORTD=11111101
11101111
PORTD=PORTD&( ~(1<<4))
A|0=A 0 0 = 0
A|1=1 0 1 = 1
A&0=0; 1 0 = 1
A&1=A; 1 1 = 0
A^0=A
A^1=~A
DDRD=(1<<2);
while(1)
{
_delay_ms(1000);
PORTD^=(1<<2);
}

cbi(DDRD,2); /// set as input //


i= bit_is_set(PIND,2);
54

i= PIND & (00000100);


i= bit_is_clear(PIND,2);
if the pin is high, it will return
a nonzero number

#define LED 2
#define SWITCH 3
int main()
{
int is_blinking=0;
cbi(DDRD,SWITCH); // centre switch iP
sbi(DDRD,LED); // bottom LED OP
sbi(PORTD,SWITCH); // activate PULLUP
while(1)
{
if(bit_is_set(PIND,SWITCH)==0)
{
_delay_ms(100);
while(bit_is_set(PIND,SWITCH)==0); // wait until user releases the key
if(is_blinking==0)
is_blinking=1;
else
is_blinking=0;
}
if(is_blinking==1)
{
_delay_ms(100);
PORTD^=(1<<LED);
}
}
}

#include"lcd.h"
#include<stdio.h>
int main()
{
int i=0;
LCD_Init();
while(1)
{
LCD_Home();
printf("HELLO WORLD\n i= %d",i++);
55

_delay_ms(1000);
}
}

#include"adc.h"
#include"lcd.h"
#include<stdio.h>
#include<util/delay.h>
int main()
{
uint16_t val1, val2;
LCD_Init();
while(1)
{
val1=ADC_Read(1);
val2=ADC_Read(2);
printf("V1 = %4.4d\nV2 = %4.4d",val1,val2);
_delay_ms(100);
}
}

56

You might also like