Upload
ahmedeee
View
36
Download
0
Tags:
Embed Size (px)
DESCRIPTION
implemented project
Citation preview
1
University of Information Technology and Sciences
School of Computer Science and Engineering
A Microcontroller Based System of Intelligent Light Emitting
Diode Lamp
This project is presented in partial fulfillment of the requirement for the degree of
Bachelor of Science in Electrical and Electronic Engineering (EEE)
Dhaka 2011
2
University of Information Technology and Sciences
GA-37/1 Progati Sarani, Baridhara J- Block, Gulshan, Dhaka 1212, Bangladesh.
Submitted By
Supervisor
-------------------------------
Sheik Md. Kazi Nazrul Islam
Lecturer, Department of Electronic and Communication Engineering
University of Information Technology and Sciences
Name ID
Taslim Ahmed 07510059
Samar Chowdhury 10310155
Khokan Das 07510036
3
University of Information Technology and Sciences
Candidates’ Declaration
It is declared hereby that this project paper or any part of it has not been submitted to anywhere else
for the award of any degree.
………………………… ……………………… …………………………
Taslim Ahmed Samar Chowdhury Khokan Das
4
University of Information Technology and Sciences
Certificate
This is to certify that the project entitled “A Microcontroller Based System of Intelligent Light
Emitting Diode Lamp” by TASLIM AHMED (07510059), SAMAR CHOWDHURY (10310155),
KHOKON DAS (07510036) have been carried out under my supervision.
Supervisor
………………………………………………….
Sheik Md. Kazi Nazrul Islam
Lecturer
Department of Electronic and Communication Engineering
University of Information Technology and Sciences
5
Abstract
This project presents various control of a Light Emitting Diode (LED) lamp with a built in alarm
system and an LCD for information display which is automatically controlled by the
microcontroller Atmega8. A Light Dependent Resistor (LDR) and a variable resistor (POT) is used
to provide individual input data for comparison, and producing a pre-programmed output to operate
the LED lamp such as LED ON/OFF, LED blinking, LED color changing and alarming. The
system required 0.98µA and 23.5mA current, and 4.88mW and 117.5mW power, during standby
and automated blinking sequences (Yellow-Off-Red) respectively; and the buzzer consumed only
0.49mW power for alarming. Proteus ISIS 7.7 is used for system design and simulation, and Code
Vision AVR (software) is used to write the program code and burning the microcontroller ATmega8.
Finally, Proteus ARES 7.7 is used to design the printed circuit board (PCB), for practical
implementation.
6
Acknowledgement
It gives us a great pleasure to have the privilege of expressing our indebtedness and gratitude to our
supervisor Mr. Sheik Md. Kazi Nazrul Islam, Lecturer, Department of Electronic and
Communication Engineering (ECE), University of Information Technology and Sciences (UITS)
for his deep interest, advice and encouragement throughout the project work.
We also sincerely wish to acknowledge our gratitude to our faculty members of UITS for extending
their help and assistance to work on this project.
Taslim Ahmed (07510059)
Samar Chowdhury (10310155)
Khokon Das (07510036)
7
Contents
Page No.
Introduction ---------------------------------------------------------------------------------- 8
Chapter 1. An Overview of Microcontroller based System Components ------ 11
1.1 Features of Microcontroller ATmega8 --------------------------------------- 11
1.2 Light Dependent Resistor (LDR) as Light Sensor-------------------------- 13
1.3 Light Emitting Diode (LED) -------------------------------------------------- 15
1.4 Buzzer (alarm unit) and Liquid Crystal Display (LCD) ------------------- 16
1.5 Voltage Regulator --------------------------------------------------------------- 18
Chapter 2. Concepts for Developing the Intelligent LED (lamp) System -------- 19
2.1 Block Diagram of Microcontroller Based LED Lamp --------------------- 19
2.2 Full Wave Rectifier (DC Converter) ----------------------------------------- 20
2.3 Power Supply and Display Section-------------------------------------------- 21
2.4 Sensor and Comparator Section ----------------------------------------------- 23
2.5 Alarm Section ------------------------------------------------------------------- 24
2.6 Load Section --------------------------------------------------------------------- 25
Chapter 3. Design and Construction of the Intelligent LED (lamp) System ---- 26
3.1 Circuit components ------------------------------------------------------------- 26
3.2 Schematic diagram and Circuit Configuration of Microcontroller
Based Intelligent LED (lamp) System ---------------------------------------
26
3.3 Program Code for the Operation of Microcontroller (ATmega8) -------- 28
3.4 Programming the Microcontroller ATmega8 (using Code Vision AVR)-- 33
3.5 Working Principle of the Intelligent LED System ------------------------- 39
Chapter 4. Simulation and Practical Implementation ------------------------------- 41
4.1 Operational Analysis ----------------------------------------------------------- 41
4.2 Transient Analysis -------------------------------------------------------------- 44
4.3 Practical Results of System Operation and Control ------------------------ 46
4.4 Test and Results ----------------------------------------------------------------- 50
4.5 Design of Printed Circuit Board (PCB) ------------------------------------- 51
4.6 View of Practically Implemented System ----------------------------------- 53
Findings ---------------------------------------------------------------------------------------- 54
Conclusion ------------------------------------------------------------------------------------ 56
References ------------------------------------------------------------------------------------- 57
Appendix (Microcontroller ATmega8 Datasheet) ------------------------------------ 59
8
Introduction
The application of microcontroller based devices are continuing to rise with its greater processing
speed and flexible control; and the electrical appliances are getting more miniaturized, less costly
and low power consuming. Microcontrollers reduces the number of chips and the amount of wiring
and circuit board space, that would be needed to produce equivalent systems using separate chips.
Furthermore, each pin of a microcontroller interfaces several internal peripherals, with the pin
function selected by software. This allows a wider variety of applications than single specific
functions. The world of microchip and microcontroller has left the human races wondering with its
incredible intelligence and control, in numerous application such as cellular phone, automobile
engine, control system, remote controls, office machines, appliances, programmable interval timer,
power tools and toys and analog to digital or digital to analog converters, etc. [1].
Thus, this programmable device (microcontroller) provides a unique tool to interface the ‘Nature
and Device’, to add a Hi-Tech dimension in the fashion of our everyday life. This phenomenal
aspect of opportunity led our thoughts to a vision of interfacing the Light (nature) and the LED
(device). To interface the light, an LDR is used which has a negative coefficient of resistance and
this property is utilized for the intelligent control of an LED, correspond to a time varying light
intensity. But to design a system with artificial intelligence with higher data processing rate, small
size, low power consuming and most importantly cost efficient; is always a prior goal for system
designers. And in this challenge, microchip or microcontrollers are able to deliver powerful
features that would otherwise be impossible, or too costly to implement. LED lamps are gaining
more and more popularity for their use in many applications and appliances, due to their relative
low power consumption, low cost, size, durability, wide operating range and controllable intensity
of illumination.
Microcontroller based system makes an LED lamp programmable and intelligent. The advanced
features of the Microcontroller ATmega8, provides a flexible control of LED operation with an
aspect of higher precision, which is very complex to achieve with some discrete component
electronics. More on the accuracy and lower power consumption makes the microcontroller based
LED system more Hi-Tech and efficient, regarding operation control and energy management.
Through these inspirations, an LED lamp controller is an ambition for optical signaling,
communication and agro-electronics. Some works been done on this interest and their contributions
left a lot of scope to meet.
9
This project implements this vision of designing and controlling an LED’s ON/OFF state and its
illumination pattern (blinking and color changing) according to the change in light intensity; using
microcontroller ATmega8. The turning ON operation state, of LED is annotated with a Buzzer by
alarming sound. More on an LCD is interfaced to display the information related to light intensity.
Objectives
The implementation of intelligent control of an LED lamp comprises some precise objectives and
goals, which are:
To design a microcontroller based LED (lamp) control system.
To control the ON/OFF state of LED (lamp).
To alarm when the LED (lamp) is turned ON.
To control its blinking capability.
To control its color changing.
To simulate and debug the system design.
To integrate the whole system compactly in a PCB (Printed Circuit Board).
Scopes of the Project
Intelligent lighting is the ability to reduce the amount of light or energy is used in such, that only
the right amount of light is delivered exactly where it is needed. ‘Digitally controlled lighting’,
using microcontrollers (MCUs) allows the engineers to take advantage of unique characteristics of
LEDs in an efficient and flexible way, for intelligent lighting. With digital control, designers can
scale and easily adjust designs to multiple applications, maximizing reuse and decreasing design
time. Flexible control and multidimensional application features of intelligent LED lamp are
implemented by using ATmega8; with a built in alarm system. The area and purpose of application
can be categorized in four major fields:
Communication
Security
Navigation
Agro-electronics
The combination of microcontroller and LED based lamp makes an intelligent support for using the
technology in various applications such as bridge indicator, high rise structure indicator,
telecommunication and transmission line towers, BTS tower and so on. This lamp can also be
installed on the runway of an airport for secured landing or for navigation purpose during hours of
darkness. This project can be implemented in measurement and instrumentation such as counter;
10
where a constant applied beam from a Laser on LDR will be interrupted and the number of object
will be equal to the number of alarm produced by the buzzer and consequently will work as a
counter. Beside of many innovative and interesting applications this project may also can be
implemented for intelligent security purpose in such fashion that whenever the continuous flux on
the LDR will be interrupted by any intruders, then an alarm in audible signal and the light in optical
signal will be produced and the necessary safety will be ensured in an efficient and skilled manner.
In agro-electronics a pH meter interfaced with the microcontroller can be used to vary the required
illumination intensity for the growth of plants, etc.
Methodology
Automated and intelligent controlling can be done in various ways according to electronics design.
But to avoid the complexity and achieve an extended dimension in controlling, microcontroller
based control system is used in this project and among a wide range of available microcontrollers,
ATmega8 is used which is a 28-pin low power CMOS 8-bit microcontroller – based on the AVR
RISC architecture. Along with the microcontroller ATmega8, the complete controlling system is
comprised of some discrete passive and active electronic components as peripherals (reading data
and driving the outcome). Because of mixed-signal operation in the system, among the six available
ADC (analog-to-digital converter) port of the ATmega8, four of them are used. As a light sensor -
an LDR (Light Dependent Resister) is used which has a negative coefficient of resistance. Another
two pins are used to drive the LED according to the comparison value of light sensor and a POT. A
Piezo buzzer is used for alarming purpose. A 16-segment LCD is also interfaced. The entire
operation of the controlling system is done by software and hardware. In case of software
simulation “Proteus 7.7 professional” is used where the ‘ISIS’ - a built in schematic tool is used to
draw the Schematic Diagram and the ‘ARES’ – another tool of Proteus is used for PCB design and
the simulated result is implemented by practically integrating the devices on the PCB board.
11
Chapter 1. An Overview of Microcontroller based LED (lamp) System
Components
To design an electronic system with Artificial Intelligence we require microcontroller which can
process an assigned task with a greater precision and control. A microcontroller is a computer on a
chip, containing processor, memory and input/output function. In addition to the usual arithmetic
and logic element of general purpose microcontroller, it integrates additional elements such as read-
write memory for data storage, read only memory for program storage, EEPROM (electronically
erasable programmable read-only memory) for data storage, peripheral device, and input-output
interface. A few MHz clock speed microcontroller often operates at very low speed compared to
the modern microprocessor, but this is adequate for typical applications. It consumes relatively
little power (mw); it has sleep and wake up options etc. By reducing the size, cost, and power
consumption compared to a design using a separate microprocessor or discrete component
electronics - microcontrollers make it economical to electronically control many more processes
[2].
1.1 Features of Microcontroller ATmega8
A micro-controller is a single integrated programmable circuit. The ATmega8 is a low-power
CMOS 8-bit microcontroller based on the AVR RISC architecture. By executing powerful
instructions in a single clock cycle, the ATmega8 achieves throughputs approaching 1 MIPS per
MHz, allowing the system designed to optimize power consumption versus processing speed. The
ATmega8 is a 28-pin embedded micro chip featuring electronically erasable programmable read-
only memory (EEPROM). Some important features and the pin diagram of ATmega8 are shown in
Figure 1.1 [3].
Figure 1.1 Pin diagram of microcontroller (ATmega8).
12
Some important features of the microcontroller ATmega8 are as following:
1. High-performance, low-power AVR 8-bit microcontroller.
2. Advanced RISC architecture:
130 powerful instructions – most single-clock cycle execution
32 x 8 general purpose working registers
fully static operation
Up to 16 MIPS throughput at 16 MHz
On-chip 2-cycle multiplier
3. High endurance non-volatile memory segments:
8K bytes of in-system self-programmable flash program memory
512 bytes EEPROM
1K byte internal SRAM
Write/Erase cycles: 10,000 flash/100,000 EEPROM
Data retention: 20 years at 85°C/100 years at 25°C
Optional boot code section with independent lock bits
In-system programming by on-chip boot program
4. True read-while-write operation-Programming lock for software security
5. Peripheral features:
Two 8-bit Timer/Counters with separate Prescaler, one compare mode
One 16-bit Timer/Counter with separate Prescaler, compare mode, and capture Mode
Real time counter with separate oscillator
Three PWM channels
8-channel ADC in TQFP and QFN/MLF package
Eight channels 10-bit accuracy
6-channel ADC in PDIP package
Six channels 10-bit accuracy
Programmable serial USART
Master/Slave SPI serial interface
Programmable Watchdog Timer with separate on-chip oscillator
On-chip analog comparator
6. Special microcontroller features:
Power-on Reset and programmable Brown-out Detection
Internal calibrated RC oscillator
13
External and internal interrupt sources
Five sleep modes: Idle, ADC noise reduction, Power-save, Power-down, and Standby
7. I/O and Packages:
23 programmable I/O lines
28-lead PDIP, 32-lead TQFP, and 32-pad QFN/MLF
8. Operating Voltages:
2.7 - 5.5V (ATmega8L)
4.5 - 5.5V (ATmega8)
9. Speed Grades:
0 - 8 MHz (ATmega8L)
0 - 16 MHz (ATmega8)
10. Power consumption at 4 Mhz, 3V, 25°C
11. Active: 3.6 mA
12. Idle mode: 1.0 mA
13. Power-down mode: 0.5 μA
And the more other features are illustrated and described in the Appendix (Microcontroller
ATmega8 Datasheet).
1.2 Light Dependent Resistor (LDR) as Light Sensor
A light dependent resistor (LDR) is used as a light sensor which is a photo conductive cell and
interfaced with a microcontroller to provide the information of light intensity. An LDR - as its
name suggests, offers resistance in response to the incident light. The resistance decreases as the
intensity of incident light increases, and vice versa. In the absence of light, LDR exhibits a
resistance to the order of mega-ohms which decreases to a few hundred ohms in the presence of
light. It can act as a sensor, since a varying voltage drop can be obtained in accordance with the
varying light. It is made up of cadmium sulphide (CdS). An LDR has a zigzag cadmium sulphide
track. It is a bilateral device which conducts in both directions in same fashion. To measure
analogue values, the microcontroller must contain an 'analogue to digital converter (ADC)’ and the
programming software must support the use of this ADC. By observing the change of resistance,
the LDR can sense day and night and the controller can take decision to “ON/OFF” the LED lamp.
The view of an LDR is shown in Figure 1.2 [4].
14
Figure 1.2 Light dependent resistors (LDR).
LDRs are used in automatic street lamps to switch them on at night and off during the day. They
are also used within many alarm and toys to measure light levels. The LDR is a type of analogue
sensor. An analogue sensor measures a continuous signal such as light, temperature or position
(rather than a digital on-off signal like a switch). The analogue sensor provides a varying voltage
signal. This voltage signal can be represented by a number in the range 0 and 255 (e.g. very dark =
0, bright light = 255). The characteristics curve of an LDR is shown in Figure 1.3 [4].
Figure 1.3 Characteristics curve of a light dependent resistor (LDR)
15
1.3 Light Emitting Diode (LED)
A light emitting diode (LED) is an electronic device which is different than normal diodes. It has
three pins and operated in forward biased. Due to the forward current through the diode, the LED
emits light. A bicolor LED is a 3-pin device which has two different single color built in LED in
parallel to each other. The anode of two diodes is common at +ve pin as shown in the figure above
and the cathodes of the two diodes are grounded by two other pins. Due to current through two
different color emitting diode – the bicolor LED emits two different colors corresponding to the
internal emission. LEDs are mainly used as indicator lights. Red and green LEDs are commonly
used on electronic appliances like televisions to show if they are switched on or in 'standby' mode.
LEDs are available in many different colors, including red, yellow, green and blue. Special 'ultra
bright' LEDs are used in safety warning devices such as the 'flashing lights'. Infra-red LEDs
produce infra-red light that cannot be seen by the human eye but can be used in devices such as
video remote-controls. The view of a single color and bicolor LED is shown in Figure 1.4 and the
internal structure in Figure 1.5 [5].
Figure 1.4 Generic single color LED (on left) and bicolor LED (on right)
Figure 1.5 Bicolor LED internal structure
16
1.4 Buzzer (alarm unit) and Liquid Crystal Display (LCD)
The generation of electric potential by the application of pressure, strain or any force is known as
the ‘Piezoelectric Effect’. A piezo buzzer produces sound based on reverse of the piezoelectric
effect. The generation of pressure vibration or strain by the application of electric potential across a
piezoelectric material is the underlying principle. These buzzers is used to alert a user of an event
corresponding to a switching action, counter signal or sensor input. They are also used in alarm
circuits. The buzzer produces a same noisy sound irrespective of the voltage variation applied to it.
It consists of piezo crystals between two conductors. The view of a piezo buzzer is shown in
Figure 1.6 [6].
Figure 1.6 Piezo buzzer
When there is dark, the resistance of LDR becomes very high and conduction does not takes place,
so high voltage is retained and the buzzer alarms; there by operates as a dark activated sensor. In
presence of light the resistance of LDR becomes low and the buzzer remains silent. A piezo also
requires less current and the sound that the piezo makes can be changed by altering the electronic
signals provided by the microcontroller.
An LM016L is a 16x2 LCD which can display 16 characters per line and there are 2 such lines. It is
a 14-pin device and has 8-Data lines, 3-Control Lines and 3-Power Lines. This LCD has two
registers, namely, Command and Data. The command register stores the command instructions
given to the LCD. A command is an instruction given to LCD to do a predefined task like
initializing it, clearing its screen, setting the cursor position, controlling display etc. The data
register stores the data to be displayed on the LCD. The pin diagram and pin configuration of the
LCD (LM041L) are shown in Figure 1.7 and Table 1.1 [7].
17
Figure 1.7 Pin diagram of LCD (LM041L)
Table 1.1 Pin configuration of LCD (LM041L)
Pin No. Name Function Information
1 Vss Ground Connected to 0V or Ground
2 Vdd +ve supply Connected to the +ve supply
3 Vee Contrast
adjustment
Control pin - to alter the contrast and connected to the
variable voltage supply or 0V.
4 RS Register Select,
H/L
L: Selects command register for instruction code input
H: Selects data register for data input
5 R/W Read/Write, H/L H: Data read (LCD module MCU)
L: Data write(LCD module MCU)
6 E Enable, H, H L Sends data to data pins when a high to low pulse is
given
7 D0 Data bit 0 Data Bus (not used in 4-bit mode) - LSB
8 D1 Data bit 1 Data Bus (not used in 4-bit mode)
9 D2 Data bit 2 Data Bus (not used in 4-bit mode)
10 D3 Data bit 3 Data Bus (not used in 4-bit mode)
11 D4 Data bit 4 LSB in 4-bit mode
12 D5 Data bit 5 Data Bus
13 D6 Data bit 6 Data Bus
14 D7 Data bit 7 MSB
An LCD LM041L can be operated in two different modes: 4-bit mode and 8-bit mode. In 8-bit
mode, pins 7-14 of the LCD are connected to eight I/O pins on the microcontroller; while in 4-bit
mode, pins 11-14 on the LCD are connected to four I/O pins on the microcontroller. The advantage
to operating in 8-bit mode is that the programming is a bit simpler and data can be updated more
quickly. The obvious reason to operate in 4-bit mode is to save four I/O pins on the ATmega8
microcontroller.
18
1.5 Voltage Regulator
7805 is a voltage regulator integrated circuit. It is a member of 78xx series of fixed linear voltage
regulator ICs. The voltage source in a circuit may have fluctuations and would not give the fixed
voltage output. The voltage regulator IC maintains the output voltage at a constant value. The xx in
78xx indicates the fixed output voltage it is designed to provide. 7805 provides +5V regulated
power supply. The pin diagram and the pin configuration of voltage regulator 7805 are shown in
Figure 1.8 and Table 1.2, respectively [8].
Figure 1.8 Pin diagram of 7805
Table 1.2 Pin configuration of 7805
Pin No Name Operating Value Function
1 Input (5V-18V) Input voltage
2 Ground (0V) Ground
3 Output 5V (4.8V-5.2V) Regulated output
19
Chapter 2. Concepts for Developing the Intelligent LED (lamp)
System
In some earlier efforts; a ‘Light Controller’ was build using microcontroller PIC16F873. They
provided an important source of information to understand this project easily and also explained
various types of chip, interfaced with hardware and software combination. There, the circuit was
operated only by direct electric current (dc) [9]. A ‘LED Flasher’ was also build using PIC16F84A
microcontroller and kept LED with five types blinking pattern options. This circuit controlled the
blinking of eight LEDs and the blinking patterns were changed with five switches [10].
But all those contributions left a lot of interest and scopes to meet. Using the application of
software Proteus 7.7 Professional an overview of the entire system sections are designed and
simulated. The schematic form and the block diagram of the system components and concepts for
system development are narrated in this chapter through a series of sections. The component list is
also included for references.
2.1 Block Diagram of Microcontroller Based LED Lamp
The block diagram in Figure 2.1 depicts the overall concepts and configuarations, for developing a
microcontroller based system of intelligent LED lamp.
Figure 2.1 Block diagram of an LED-based intelligent lamp using microcontroller
20
2.2 A Full Wave Rectifier (DC Converter)
A full wave rectifier is used as a dc converter to supply required power consumed by the different
circuit components. The circuit diagram of that rectifier is shown in (Figure 2.2) [11]. During the
positive half cycle, D1 and D2 are forward biased; D3 and D4 are open and the current flows
through D1 first and then through RL and then through D2 back to the ground. During the negative
half cycle D4 and D3 are forward biased and they conduct. The current flows from D3 through RL
to D4. Hence the direction of current is the same. So we get full wave rectified output The Output
is taken across the Resistor RL. The input and output voltage is shown in Figure 2.3(a) and Figure
2.3(b), respectively; which describes the transfer characteristics of a full wave rectifier.
Figure 2.2 Circuit diagram of a full wave bridge rectifier.
Figure 2.3(a) Input voltage Figure 2.3(b) Output voltage
Some advantages of using full wave rectifier are:
The peak inverse voltage (PIV) across each diode is Vm and not 2Vm as in the case of FWR.
Hence the Voltage rating of the diodes can be less.
Centre tapped transformer is not required.
There is no dc current flowing through the transformer since there is no centre tapping and
the return path is to the ground. So the transformer utilization factor is high.
ac input
ac input
RL
D1
D2
+VM
D4
D3
-VM
V0
t t
21
Some important specifications of a full wave rectifier are given in Table 2.1.
Table 2.1 Specifications of a full wave rectifier
Features Value
Peak Inverse Voltage Vm
No Load Voltage 2Vm
Ripple factor 0.482
Number of Diodes required 4
Ratio of Rectification Pdc/Pac 0.812
0.812
2.3 Power Supply and Display Section
A power supply section is used to a +5V dc power; comprised of a ‘full wave rectifier’ and a
‘voltage regulator’. The capacitors C1 (1000uF) and C2 (220uF) are connected before and after of a
regulator IC (7805), to provide necessary filter; and a transformer is used to step-down the voltage
from 220V ac to 12V ac. This power supply unit regulates necessary biasing for the microcontroller
ATmega8 and other circuit components. The Figure 2.4 shows the schematic view of a +5V dc
power supply, drawn in Proteus 7.7 Professional.
Figure 2.4 Circuit diagram of a +5V dc power supply
dc supply delivered to the Load
ac ratings of transformer
secondary
TUF =
22
Liquid Crystal Display (LCD) screen is an electronic display module and find a wide range of
applications. A 16x2 LCD display - is very basic module and is very commonly used in various
devices and circuits. These modules are preferred over seven segments and other multi segment
LEDs. The reasons being, LCDs are economical, easily programmable, have no limitation of
displaying special and even custom characters (unlike in seven segments), animations, and so on
[7].
To facilitate this feature into the intelligent LED (lamp) system, a 14-pin 16*4 character LCD
LM041L is interfaced with the microcontroller ATmega8 for displaying information of both the
predefined and real time variable data. The LCD LM041L is deployed in 4-bit operating mode and
the interconnection in-between the LCD and the ATmega8 is shown in Figure 2.5.
Figure 2.5 Connection diagram of interfacing LCD and microcontroller ATmega8
23
2.4 Sensor and Comparator Section
A light dependent resistance (LDR) is used as a light sensor with a series resistance, to operate an
LED according to the predefined algorithm. A VAR/POT is also used for providing input voltage
as analog data. The LDR and the POT are connected with two ADC (analog to digital) port of the
microcontroller ATmega8. The connection diagram of senor and comparator are shown in Figure
2.6.
Figure 2.6 Connection diagram of interfacing LDR and POT with microcontroller ATmega8
The POT and the LDR are interfaced with the PC3/ADC3 and PC4/ADC4 ports of the
microcontroller ATmega8 respectively. The microcontroller ATmega8 converts the analog input
voltages retrieved from the POT and LDR; and compares in-between them to produce a pre-
programmed output. As the intensity of light increases the LDR resistance decreases and the
voltage across the resistor R2 (in Figure 2.5) or the retrieved voltage from the LDR at port ADC4
increases and eventually forth ahead than the voltage of ADC3 port or POT voltage. As the
24
intensity of light decreases the LDR resistance increases and the voltage across the resistor R2 (in
Figure 2.6) or the retrieved voltage from the LDR at port ADC4 decreases and eventually fall
behind the voltage of ADC3 port or POT voltage. By manually setting the POT, the operating states
of the LED is maintained that when to ON or OFF, etc.
2.5 Alarm Section
A buzzer is used as an alarm unit which is drived by the microcontroller ATmega8, with a
predefined logic set by program code. A transistor (BJT) is connected (in Figure 2.7) to the pin PB1
of ATmega8, with its base. The buzzer used for alarming, is a piezo buzzer which is operated by the
reverse piezo electric effect and even a nano-ampere level of current can produce an alarm which
makes it more sensitive and low power consuming.
When, the LED turns ON; then a pulse generated by the microcontroller reaches to the base of the
transistor and turns on, then the current flows through the buzzer and it alarms.
Figure 2.7 Connection diagram of interfacing a buzzer with microcontroller ATmega8
25
2.6 Load Section
LEDs only require a small amount of current to operate, which makes them much more efficient
than bulbs; and are used as load. If too much current is passed through an LED it will be damaged
and so LEDs are normally used together with a 'series' resistor that protects the LED from too much
current [5]. The connection diagram is shown in Figure 2.8.
Figure 2.8 Connection diagram of interfacing a buzzer with microcontroller ATmega8
The LED requires only a small amount of current to operate; thus, it can be directly connected
between 5V and the microcontroller output pin (with the series protection resistor). When the
output pin of a microcontroller (ATmega8) is low or 0V, then the LED turns ON, and when the
output pin of a microcontroller (ATmega8) is high or 5V, then the LED turns ON. For a bicolor
LED (referring Figure 1.5) since current through two different cathodes produces two different
illumination of color; the two cathodes are connected with two different pin of ATmega8.
26
Chapter 3. Design and Construction of the Intelligent LED (lamp)
System
To control the operation features of an LED lamp intelligently, a microcontroller based system is
designed and constructed; where the components are directly or indirectly interfaced with the
microcontroller to retrieve inputs and drive outputs according to the instructions assigned inside the
program code of microcontroller ATmega8.
3.1 Circuit Components
A microcontroller ATmega8 is used as the central unit of the system and a light dependent resistor
(LDR) as a light sensor. The lamp used is light emitting diode (LED). The components used to
construct the microcontroller based intelligent LED (lamp) system are listed in Table 3.1.
Table 3.1 Components of the microcontroller based intelligent LED (lamp) system.
Component Name Quantity Reference Value
Resistor 5 R1,R4, R2,R3,R5,
POT/VAR
330,10k,100,1k,
10k
Capacitor 2 C1,C2 1000u, 220u
Integrated Circuit 2 U1, U2 Atmega8,7805
Transistors 1 Q1 BC337
Diodes 7 D1-D7 1N4007 (4), LED-Yellow, LED-red (2)
Miscellaneous 5 Buzzer, LCD, LDR,
Transformer
LCD LM016L, step-down transformer
( 220V ac to 12V dc)
3.2 Schematic diagram and Circuit Configuration of Microcontroller Based
Intelligent LED (lamp) System
A power supplier is constructed to establish a dc voltage of +12V and +5V (Figure 3.1); to provide
necessary biasing of microcontroller ATmega8 (in the center of Figure 3.1) and other circuitry. A
bicolor light emitting diode - LED (red) is connected with the +5V terminal. A light dependent
resistor (LDR) and a series resistor as a voltage divider are connected with the rectifier output
voltage (+5V) and the divided voltage is interfaced with the ADC (4) of ATmega8. A buzzer is
grounded with a series npn transistor and the transistor drived with the interfaced port PB1 (pin
27
no.2 of Port B). A VAR/POT is connected to the +5V and the ADC (3) of ATmega8. A bicolor
LED (yellow and red) with series resistor R3, R4 – is connected as load or lamp which is drived by
the microcontroller ATmega8 through the port ADC (2) and ADC (1). A liquid crystal display
(LCD) is connected with the Port D of ATmega8. The schematic diagram of the system is shown in
Figure 3.1.
Figure 3.1 Practical schematic diagram of an LED-based intelligent lamp using microcontroller
28
3.3 Program Code for the Operation of Microcontroller (ATmega8)
To make a microcontroller based system artificially intelligent, a program code must be written
inside of a microcontroller. A program code consists of a set of organized instructions or
commands, which is followed by microcontroller. Regarding this method of creating artificial
intelligence, a program code is produced by the help of software Code Vision AVR version 2.05.0
Professional. The generated program is then modified according to the operation and control of the
featuring system. The modified and re-written program (shown below) is then used for
programming the microcontroller ATmega8.
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.0 Professional
Automatic Program Generator
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project : UITS Intelligent Lamp
Version :
Date : 11/25/2011
Author : NeVaDa
Company : Evil Genius
Comments : Hello World
Chip type : ATmega8L
Program type : Application
AVR Core Clock frequency : 8.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega8.h>
#include <stdio.h>
#include <delay.h>
unsigned int adc=0;
// Alphanumeric LCD Module functions
29
#include <alcd.h>
unsigned char lcd[16];
#define ADC_VREF_TYPE 0x00
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
int m=0;
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=Out
// State7=T State6=T State5=T State4=T State3=T State2=T State1=0 State0=0
PORTB=0x00;
DDRB=0x02;
// Port C initialization
// Func6=In Func5=Out Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=0 State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x06;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
30
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
31
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// USART disabled
UCSRB=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 125.000 kHz
// ADC Voltage Reference: AREF pin
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x86;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
// Alphanumeric LCD initialization
// Connections specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTD Bit 0
32
// RD - PORTD Bit 1
// EN - PORTD Bit 2
// D4 - PORTD Bit 4
// D5 - PORTD Bit 5
// D6 - PORTD Bit 6
// D7 - PORTD Bit 7
// Characters/line: 16
lcd_init(16);
lcd_clear();
while (1)
{
// Place your code here
adc=read_adc(4)/10;
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("UITS Int. LAMP ");
sprintf(lcd,"LDR Voltage=%d%c",adc,37);
lcd_gotoxy(0,1);
lcd_puts(lcd);
delay_ms(500);
if(adc>=(read_adc(3)/10))
{PORTB.1=0;PORTC.1=1;PORTC.2=1;m=0;}
if(adc<(read_adc(3)/10)&&m==0)
{PORTB.1=1;delay_ms(1000);PORTB.1=0;delay_ms(1000);m=1;}
if(adc<(read_adc(3)/10))
{
PORTC.1=1;PORTC.2=0;delay_ms(1000);
PORTC.1=1;PORTC.2=1;delay_ms(1000);
PORTC.1=0;PORTC.2=1;delay_ms(1000);
PORTC.1=1;PORTC.2=1;delay_ms(500);
}
}
}
33
3.4 Programming the Microcontroller ATmega8 (using Code Vision AVR)
The ATmega8 uses the built in feature EEPROM which erases the written program inside of
microcontroller, without ultraviolet light. The software “Code Vision AVR” is used to write the
program for ATmega8 (section 3.3) and the following steps are followed, and the Figure 3.2 to
Figure 3.12 shows the entire process from programming to burning procedures of ATmega8 [12].
Step 1: Using the software ‘Code Vision AVR’, the program is typed; then it is compiled and after
being successful, a HEX file is generated. The HEX file is generation is shown in Figure 3.2.
Step 2: Using the software the chip signature is checked which ensures the chip is ATmega8 and
using the EEPROM (electrically erasable programmable read-only memory) feature of ATmega8
the microcontroller is erased. The chip signature check and the erasing process are shown through
the Figure 3.3 to Figure 3.8.
Figure 3.2 Program writing and HEX file generation, using Code Vision AVR.
34
Figure 3.3 Pressing the ‘Chip Programmer’ tab of the toolbar
Figure 3.4 Selecting the chip (ATmega8) and program fuse bits
35
Figure 3.5 Reading the chip signature
Figure 3.6 The chip signature is identified.
36
Figure 3.7 Choosing the ‘Erase Chip’ option under the ‘Program’ tab, from ‘Title bar’
Figure 3.8 The chip ATmega8 is being erased
37
Step 3: Using the chip programming feature of the software the generated HEX file is written into
the ATmega8. This step is called "Burning" and shown through the Figure 3.9 to Figure 3.11.
Figure 3.9 Choosing the ‘FLASH’ option of ‘program’ tab, to write program into the ATmega8
Figure 3.10 The chip is being written into the ATmega8, (software view)
38
Figure 3.11 The program code is being written into the ATmega8, (hardware view).
Step 4: The burnt ATmega8 is then inserted into the designed circuit, powered up and executed so
as to verify whether the program worked as expected. This step is called "Dropping" the chip and
shown in Figure 3.12.
If the program didn’t work as desired, the program is again edited going through Step 1 and
debugged to find out the problems (if any), reprogrammed and then burning and dropping are done
as usual.
Figure 3.12 The burnt ATmega8 is inserted into the designed circuit and powered up.
39
3.5 Working Principle of the Intelligent LED System
Referring to the Figure 3.1, it is found that there are two different inputs in two different ADC
ports, amongst which the microcontroller ATmega8 compares and takes a pre-programmed
decision. One input is obtained from the VAR/POT through ADC (3) which is read by ATmega8 as
the “SET Voltage”. Another voltage is obtained across the R2 through ADC (4) which is varied by
the change in intensity of light – is read by ATmega8 as the “Real Time Voltage (RTV)”. When the
incident lights on the LDR increases, the resistance of the LDR decreases and the voltage across the
series resistance R2 increases. Thus the value of the RTV also increases and when the incident light
on the LDR decreases, the resistance of the LDR increases and the voltage across the series
resistance R2 decreases. Thus the value of the RTV also decreases. Now the instruction logic is set
by the program code inside of ATmega8 is such that,
while (1)
{
// Place your code here
adc=read_adc(4)/10;
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("UITS Int. LAMP ");
sprintf(lcd,"LDR Voltage=%d%c",adc,37);
lcd_gotoxy(0,1);
lcd_puts(lcd);
delay_ms(500);
if(adc>=(read_adc(3)/10))
{ PORTB.1=0;PORTC.1=1;PORTC.2=1;m=0;}
if(adc<(read_adc(3)/10)&&m==0)
{ PORTB.1=1;delay_ms(1000);PORTB.1=0;delay_ms(1000);m=1;}
if(adc<(read_adc(3)/10))
{ PORTC.1=1;PORTC.2=0;delay_ms(1000);
PORTC.1=1;PORTC.2=1;delay_ms(1000);
PORTC.1=0;PORTC.2=1;delay_ms(1000);
PORTC.1=1;PORTC.2=1;delay_ms(500);
}
}
40
Here, the program code is a “While-loop”. According to the Code RTV=adc= read_adc(4)/10, and
SET voltage= read_adc(3)/10. When RTV>=SET Voltage,
then {PORTB.1=0;PORTC.1=1;PORTC.2=1;m=0;} that means when it is Day Light then there is
no voltage differences across the LED (bicolor, with series resistance). So, the LED is OFF and the
buzzer also will not alarm. Here ‘m’ is a local variable which is declared in the main program code.
Again if RTV<SET Voltage and m=0,
then {PORTB.1=1;delay_ms(1000);PORTB.1=0;delay_ms(1000);m=1;} that means the buzzer
will alarm and the value of the local variable (m) will be updated to m=1.
And finally when RTV<SET Voltage,
then{PORTC.1=1;PORTC.2=0;delay_ms(1000);PORTC.1=1;PORTC.2=1;delay_ms(1000);PORC.
1=0;PORTC.2=1;delay_ms(1000);PORTC.1=1;PORTC.2=1;delay_ms(500);}(as shown above)
that means the D6 is ON or Yellow when D7 is OFF with a 1 sec of delay and the D7 is ON or Red
when D6 is OFF again with a 1 sec of delay.
Since both the D6 and D7 are two internal diodes of the bicolor LED; consequently the LED will
blink with a 1 sec of delay while altering the color. Here the delay can be extended or shortened by
changing the delay command of program code. By changing the value of VAR/POT, the SET
Voltage and consequently the LED (Lamp) operation state corresponding to light intensity is
changed.
41
Chapter 4. Simulation and Practical Implementation
To verify the circuit operation, simulation is done in two steps. The First step comprises the
verification of operation by observing the results according to internal circuitry. And the second
step comprises the verification of operation by analyzing the results according to transient behavior.
4.1 Operational Analysis
To check the operation and characteristic behaviors of the system - the designed circuit is simulated
by the software ‘Proteus 7.7 professional’. And the result found by simulation - is verified
according to the expected outcomes theoretically approved by program code and working principle.
The snapshots of the simulation (circuit operation) are added here in a sequence of executed
operations and shown in Figure 4.1(a) to 4.1(f). When the Real Time Voltage (RTV) is greater than
the Set Voltage (SV) then the LED lamp is OFF and no alarm sounds and when the Set Voltage is
greater than the Real Time Voltage then the buzzer alarms for 1s; then the LED lamp turns ON
blinking into the pattern of ‘Yellow-OFF-Red’ for 1s each and the operation continues repeatedly
as long the Set Voltage is greater than the Real Time Voltage, excluding the alarm only sounds for
once before the blinking of LED; are shown through Figure 4.1(a) to Figure 4.1(e), respectively.
Figure 4.1(a) RTV (51%) > SV; LED lamp is OFF (50%)’ and no alarm sounds
42
Figure 4.1(b) RTV (51%) < SV (52%); buzzer alarms for 1 second
Figure 4.1(c) RTV (51%) < SV (52%); LED lamp is ON (Yellow) for 1 second
43
Figure 4.1(d) RTV (51%) < SV (52%); LED lamp is OFF for 1s
Figure 4.1(e) RTV (51%) < ‘SV (52%); LED lamp is ON (Red) for 1s
44
And the above operation continues repeatedly as long the Set Voltage is greater than the Real Time
Voltage, excluding the alarm only sounds for once before the blinking of LED. By varying the
POT, the Set Voltage can be changed as well as the operation of the LED lamp; is shown in Figure
4.1(f).
Figure 4.1(f) RTV (34%) > SV (33%); LED lamp is OFF by varying the POT or Set Voltage
4.2 Transient Analysis
The system operation (transient) is verified by plotting the input and output variables and
parameters. The results obtained by simulation are shown in Figure 4.2 (a) to Figure 4.2 (c).
When the Set Voltage is greater than the Real Time Voltage then the buzzer alarms; and when the
Set Voltage is greater than the Real Time Voltage then the LED turns ON blinking into the pattern
of ‘Yellow-OFF-Red’ for 1s each and the operation continues repeatedly as long the Set Voltage is
greater than the Real Time Voltage, excluding the alarm only sounds for once before the blinking
of LED; the resultant transients are shown in Figure 4.2(a) and Figure 4.2(b), respectively. But
when the Real Time Voltage is greater than the Set Voltage then the LED turs OFF and no alarm
sounds; that transient result is depited in Figure 4.2(c).
45
Figure 4.2(a) RTV (2.49V or 51%) < SV (2.59V or 52%); the buzzer alarms for 1s
Figure 4.2(b) RTV (2.49V) < SV (2.59V); LED bicolor is ON (blinking and color changing) for 1s
Buzzer Current
SET Voltage
Real Time Voltage
Real Time Voltage
SET Voltage
LED (Yellow)
LED (Red)
Voltage (V) Current (uA)
Current (mA)
Time (s)
Time (s)
Voltage (V)
46
Figure 4.2(c) ON RTV (4.998V) > SV (2.499V); LED and buzzer, both are OFF
The Simulation results (both operational and transient) are summarized below in brief:
The LED is “OFF” (initially), while the incident light on the LDR is Maximum.
The more the incident light decreases on the LDR or it is ‘Dark; the voltage across the R2
decreases and when it crosses below the SET voltage, then the Buzzer alarms and the LED
turns “ON” in the fashion of color changing and blinking with 1sec of delay.
The more the incident light on the LDR increases or it is ‘Day’; the voltage across the R2
increases and when it crosses over the SET voltage, then the LED turns “OFF”. That means
when it is “not DARK” (or Day Light) then the LED turns “OFF”.
The operating point of LED ON state is controlled by changing the Set Voltage varying
POT.
The results obtained both by ‘Operational Analysis” and ‘Transient Analysis”, shows that
the operation and control behavior of the system are in the sequence, as instructed in the
program code.
4.3 Practical Results of System Operation and Control
The practical operation of the system after implementing on a PCB board; is represented through
the Figure 4.3(a) to Figure 4.3(f). When the Real Time Voltage (RTV) is greater than the Set
Voltage (SV) then the LED lamp is OFF and no alarm sounds and when the Set Voltage is greater
SET Voltage
Real Time Voltage
LED (Red)
LED (Yellow)
Buzzer Current
Current (nA)
Time (s)
Voltage (V)
47
than the Real Time Voltage then the buzzer alarms for 1s; then the LED lamp turns ON blinking
into the pattern of ‘Yellow-OFF-Red’ for 1s each and the operation continues repeatedly as long
the Set Voltage is greater than the Real Time Voltage, excluding the alarm only sounds for once
before the blinking of LED; are shown in Figure 4.1(a), Figure 4.1(b), Figure 4.1(c), Figure 4.1(d)
and Figure 4.1(e), respectively.
Figure 4.3(a) Three series bicolor LEDs are OFF when the light intensity is high
Figure 4.3(b) The buzzer alarms before the LEDs are turned ON when the light intensity is low.
48
Figure 4.3(c) The LEDs are ON (Yellow), when the light intensity is low or dark.
Figure 4.3(d) The LEDs are OFF between of blinking, when the light intensity is low or dark.
49
Figure 4.3(e) The LEDs are ON (Yellow), when the light intensity is low or dark.
The Set Voltage as well as the operation of the LED lamp, can be changed by varying the POT or
VAR; is depicted in Figure 4.1(f).
Figure 4.3(f) The LEDs are OFF after varying the POT and consequently the Set Voltage.