SlideShare a Scribd company logo
1 of 20
Fading LEDs via PWM:
The LED is probably the most popular component used in hobby electronics. LEDs offer a way to
light things uniquely like in POV clocks and they are great for quick debugging when you want to
double check that something is working. Another cool feature of LEDs is that their light intensity
can be varied by sending it specific digital pulses.
This article will focus on how to use digital PWM (Pulse width Modulation) to vary the
light intensity of an LED in order to create an awesome fading effect. A small microcontroller
will be used to send out the custom PWM signal for fading the led light intensity in and out.
Purpose & Overview of this project:
The goal of this project is to create an LED controller that can fade the brightness
level of an LED (light-emitting-diode) with a smooth transition. Since light intensity
increases in a logrithmic fashion, we'll need to pay extra attention to the theory section of
this article to make sure the brightness transitions move smoothly.
To build this LED controller we will use only the most basic of parts to ensure that
anyone can duplicate this controller with minimal investment of time and money. The
PIC 18F1330 microcontroller will be used for all the intelligent digital work and some
standard LEDs will be used for testing out how well the LED fader works. Continue to
the next section to see the complete parts list.
Parts:
PIC 18F1330 Microcontroller
PICkit2
7805 +5v Regulator
2x 10kΩ Resistor
100Ω Resistor
47uF Capacitor
Assorted LEDs
2N2222
Breadboard
Wires
SIPS
9v Connector
Battery Holder
Parts List Details:
The parts list above shows everything I used to build this project. Below I have
detailed out additional information on some of the more important parts. Take a look if
you're curious.
PIC 18F1330
The PIC will be used as our intelligent LED controller. It will be programmed with
some firmware that will send out the digital PWM signal to change the brightness level of
the LED. The internal 32 MHz (8 MHz + x4 PLL) oscillator will be used, so you won't
need to buy an additional crystal!
7805 +5 Voltage Regulator
This +5v regulator will be used create the power supply source for all of the
electronics used in this circuit. Since the circuit is very small the actual load placed on
this regulator will be small, maybe 100 miliamp.
PICkit 2
The PICkit 2 is a programmer for the Microchip PIC microcontroller and it has a
legacy for being one of the best, even with the 3rd version already available. You will
need some programmer to get your programs onto the PIC and this is the one that I
suggest.
2N2222 General Purpose Transistor
This 2N2222 transistor will be used to control the LED's state between on and off.
The transistor will be connected directly to the microcontroller which will send a signal
telling the transistor what state it should be in: on or off.
Assorted LEDs
A variety of LEDs is nice to have to test the system out, even though you really only
need 1 led to see if everything is working. Your choice here.
Jumper Wires & Breadboard
I prefer to use a breadboard with the projects in my articles because they offer
maximum flexibilty with circuit design with minimal downtown between changes. In
addition to the breadboard you will need jumper wire to get everything connected
together.
Schematic Overview:
The schematic for this project was kept as simple as possible so that the focus
could remain on how the touch screen works and how we interface to it. You can see the
completed schematic for this project below. The main parts in the schematic are
theLM7805, 18F1330 and Assorted LEDs.
View Full Schematic
Schematic Specifics:
Power Regulator
The 7805 regulation circuit is nothing special. Connect a battery to one side, that
has +7.5v or more and the output is +5v. A 47uF dc filtering cap is added to allow the PIC
to operate flawlessly.
LED Circuit
The LED circuit consists of the connection from the microcontroller to the 2N2222
transistor. The transistor provides a ground for the LED when it is switched on, allowing
the led to be turned on. When the transistor is in the off state, the LED has no ground so
current cannot flow through it, therefore the LED is off.
PWM Theory:
In order to control the actual brightness of the LED we will send it a PWM signal
PWM stands for pulse width modulation. Any PWM signal has three core qualities:
1 Frequency
2 Duty Cycle
3 Amplitude
These three qualities tell us what type of PWM signal to expect and we will then be able
to project it's effect on our system. Below are a few examples of PWM signals with their
associated qualities listed out.
Some PWM Examples
The type of PWM signal we'll be generating will ultimately look like the pictures
above. We'll be shooting for a 60-120 Hz frequency range, with an amplitude of +5v
(because our system uses +5v). The duty cycle, however, will vary between 0% (fully-off
state) and 100% (fully-on state).
Lighting an LED via PWM
So, if we send any of the signals seen above into an LED, what do we expect should
happen? Well, the LED turns on for the short duration of the pulse. Since we will choose
an output frequency of between 60-120 Hz the LED will appear to be a solid color
because of persistence of vision. The brightness, however, will be controlled purely by the
duty cycle %. The animation below gives you an idea of the effect of different PWM duty
cycles input into an LED.
So, you might be thinking...great! we can control LED brightness by changing the
duty cycle, sweet let's get it on!...and you'd be right. There is, however, one small caveat.
Light brightness in LEDs does not increase proportionally with duty cycle increases. This
means if we varied duty cycle 0% to 100% back and forth the LED would fade in and out,
however it would disproportionately be in the 'fully-on' state most of the time. So let's
move forward and take a look at how we can use a logarithms to get a linear fade-in and
fade-out with our LEDs.
Adding A Logrithmic Filter
A logarithm is a type of reverse exponential function, but all we need to know is
that light intensity increases in a logarithmic way. So, how do we use this information?
Well, let's suppose we want a linear set of 10 points that represent everything from the
LED being fully off and the LED being fully on. To do this we use the logarithmic
function with base 10, which yields the following values (note I used excel):
Even though the final numbers output that we should use for the duty cycle seem
one-sided, they actually correlate to even steps from one LED brightness level to the
next. If you needed a larger set of values, for example 20, you could simply use a
logarithmic function with base 20 to get those values.
Hardware Design:
Now the fun part begins, we get to actually build the project. All the steps seen
below follow the schematic and I added a power LED to know when the power is
connected.
Building The Circuit:
Get your parts together and follow the schematic. I assembled the circuit in stages
as you can see below...
·First the power regulator circuit is connected.
·Next the PIC circuit is wired up on the breadboard.
·The 2N2222 and resistors are connected to the PIC.
·Lastly, the LED and resistor is connected.
The Software Design:
The code is too long to include everything on this page, so I'll just cover the two
important specifics:
- Main Loop Brightness Control
- Interrupt Level Counting
The program has two parts and two purposes. The first part is the main function
Setting Brightness Intensity:
------------« Begin Code »------------
.. ... while(1){ // Gradually Become Brighter for(i=0;i<21;i++){
led_intensity = clr_array[i]; Delay10KTCYx(20); } // Gradually Become
Darker for(i=20;i>0;i--){ led_intensity = clr_array[i]; Delay10KTCYx(20);
} ... ..
------------« End Code »------------
The interrupt portion of the firmware is triggered every time that the counter hits
the 0xFFFF value. When the interrupt occurs, the ISR (interrupt sub-routine)
increments an internal count value and checks to see what state the PWM output should
be in, to achieve the proper duty cycle, which correlates to the LED brightness level.
Digital Timer Interrupt Controls PWM Duty Cycle:
------------« Begin Code »------------
.....void InterruptHandlerHigh(){
if(INTCONbits.TMR0IF) { count++;
//Every 100 Timer0 Interrupts Reset Counter if(count >= 100) count = 0;
//Set Brightness According To Count Value //This Will Be The Duty Cycle
if(count <=led_intensity &&led_intensity > 0) PORTAbits.RA0 = 1; else
PORTAbits.RA0 = 0;
WriteTimer0( 0xFF00 ); INTCONbits.TMR0IF = 0; //Clear TMR0 Flag
}
INTCONbits.GIE = 1; //Re-enable all interrupts}.....
------------« End Code »------------
Each axis of the touch screen requires a similar process for testing to see if it is
currently being touched and where. First the Power and Ground lines need to be turned
on for the appropriate axis, all other connections should be high-impedance so as not to
interfere. Then the touch screen output line should be read via the A/D converter. Do
this for both X and Y axis and you have a 10-bit value signifying where the screen was
touched.
LED_Fading.c:
/********************
Description:
This program is meant to output a varying PWM signal to
light an LED at different brightness levels. The end effect
of the output signal will be a fading in and fading out of
the LED brightness. This is done through the two for loops
seen in the forever while loop inside the main function.
********************/
#include <p18f1330.h>
#include <delays.h>
#include <pwm.h>
#include <timers.h>
//Use Internal Oscillator
#pragma config OSC = INTIO2
//Watchdog Timer Off
#pragma config WDT = OFF
void InterruptHandlerHigh (void);
unsigned int count = 0;
unsigned int led_intensity = 0;
//Logarithmic (Base 20) Values From 1->100
unsigned int clr_array[] = {1,2,3,5,6,8,10,12,14,16,19,22,25,29,33,38,43,51,65,80,97};
void main(void){
unsigned int i = 0;
//32 MHz Clock Setup (8 MHz + 4x PLL), 8 MHz Instruction Execution
OSCCON = 0x70;
OSCTUNEbits.PLLEN = 1;
//Oscillator Good To Go?
while(OSCCONbits.IOFS != 1){
Delay1KTCYx(1);
}
TRISA = 0x00;
PORTA = 0x00;
//Setup Interrupts
INTCON = 0b10100000;
//Setup Timer0
OpenTimer0( TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_1 );
//Initialize Timer0
WriteTimer0( 0xFF00 );
while(1){
// Gradually Become Brighter
for(i=0;i<21;i++){
led_intensity = clr_array[i];
Delay10KTCYx(20);
}
// Gradually Become Darker
for(i=20;i>0;i--){
led_intensity = clr_array[i];
Delay10KTCYx(20);
}
}
}
//INTERRUPT CONTROL
#pragma code InterruptVectorHigh = 0x08 //interrupt pointer address (0x18 low
priority)
void InterruptVectorHigh (void)
{
_asm //assembly code starts
goto InterruptHandlerHigh //interrupt control
_endasm //assembly code ends
}
#pragma code
#pragma interrupt InterruptHandlerHigh //end interrupt control
void InterruptHandlerHigh()
{
if(INTCONbits.TMR0IF)
{
count++;
//Every 100 Timer0 Interrupts Reset Counter
if(count >= 100)
count = 0;
//Set Brightness According To Count Value
//This Will Be The Duty Cycle
if(count <= led_intensity && led_intensity > 0)
PORTAbits.RA0 = 1;
else
PORTAbits.RA0 = 0;
WriteTimer0( 0xFF00 );
INTCONbits.TMR0IF = 0; //Clear TMR0 Flag
}
INTCONbits.GIE = 1; //Re-enable all interrupts
}
LED_Fading.hex:
:020000040000FA
:06000000A5EF01F0120063
:020006001200E6
:060008008BEF00F0120076
:02000E000100EF
:0C00100074030000C00000002E0000007F
:04001C00D9CFE6FF53
:10002000E1CFD9FF020EE126DE6ADD6A700ED36EE3
:100030009B8CD3B406D0010EE66E95EC01F0E55230
:10004000F8D7926A806AA00EF26E9F0EE66E62EC9E
:1000500001F0E552E66AE66846EC01F0E552E55249
:10006000DE6ADD6ADECF02F0DDCF03F0150E025C42
:10007000000E03581CE2DECFE9FFDDCFEAFF010EE0
:10008000D890E936EA36E806FBE1C40EE926000E10
:10009000EA22EECFC2F0EDCFC3F0140EE66E7EEC96
:1000A00001F0E552DF2A010E01E3DB2ADBD7140E53
:1000B000DE6EDD6AD9CFE9FFDACFEAFF000ED88025
:1000C000EE54000EED541CE2DECFE9FFDDCFEAFF77
:1000D000010ED890E936EA36E806FBE1C40EE926BF
:1000E000000EEA22EECFC2F0EDCFC3F0140EE66EA2
:1000F0007EEC01F0E552DF06010E01E2DB06DAD705
:10010000AFD7020EE15C02E2E16AE552E16EE55230
:10011000E7CFD9FF1200DACFE4FFE2CFDAFFE9CF71
:10012000E4FFEACFE4FFF6CFE4FFF7CFE4FFF5CF3B
:10013000E4FFF3CFE4FFF4CFE4FFFACFE4FF00EEF7
:1001400000F0020EE80403E3EECFE4FFFBD700EE7D
:1001500002F0020EE80403E3EECFE4FFFBD7E65221
:10016000F2A420D00001C02B000EC123640EC05D9C
:10017000000EC15902E3C06BC16BC051C25DC151D9
:10018000C35908E3000ED880C255000EC35502E2E1
:10019000808001D08090E66AE66846EC01F0E55286
:1001A000E552F294F28EE55200EE03F0020EE804FE
:1001B00003E3E5CFEDFFFBD700EE01F0020EE8040C
:1001C00003E3E5CFEDFFFBD7E5CFFAFFE5CFF4FF83
:1001D000E5CFF3FFE5CFF5FFE5CFF7FFE5CFF6FF7E
:0E01E000E5CFEAFFE5CFE9FFE5CFDAFF11003A
:0201EE000E0EF3
:1001F000F66E000EF76E000EF86E00010900F55065
:10020000F36F0900F550F46F03E1F36701D03DD0BF
:100210000900F550EE6F0900F550EF6F0900F55039
:10022000F06F09000900F550E96E0900F550EA6E1B
:10023000090009000900F550F16F0900F550F26F4F
:1002400009000900F6CFF5F0F7CFF6F0F8CFF7F098
:10025000EEC0F6FFEFC0F7FFF0C0F8FF0001F1536A
:1002600002E1F25307E00900F550EE6EF107F8E203
:10027000F207F9D7F5C0F6FFF6C0F7FFF7C0F8FFB1
:0C0280000001F307000EF45BBFD7120072
:04028C00D9CFE6FFE1
:10029000DACFE6FFE1CFD9FFE2CFDAFFE652E6524E
:1002A000FC0EDBCFDEFFDBCFDDFF010EDB50D76EB8
:1002B000DF50D66EE552E552E552E5CFDAFFE7CFE3
:0402C000D9FF120050
:0C02C400D9CFE6FFDACFE6FFE1CFD9FF8B
:1002D000E2CFDAFFFD0EDB507F0BD56ED76AD66A10
:1002E000F294FD0EDBAE02D0F28A01D0F29AD58EE6
:0C02F000E552E5CFDAFFE7CFD9FF12009E
:0402FC00FF0EE350BE
:10030000056EEF0E01D0F30EE76EE72E85EF01F0DC
:100310000C0E046EE76AE72E8BEF01F0042E8AEFD5
:0A03200001F0052E83EF01F012003A
:06032A00FF0EE350056E1A
:10033000480E01D04C0EE76EE72EFED7E76AE72E97
:0A034000FED7052EF7D700001200CB
:06034A0010EE80F020EE31
:1003500080F0F86A019CF7EC00F003EC00F00EEC82
:0603600000F0FBD71200C3
:0A03660000EE00F00F0EEE6AEA62EE
:04037000FDD71200A3
:0C03740000000000010002000300050072
:10038000060008000A000C000E0010001300160002
:1003900019001D00210026002B00330041005000F1
:0203A0006100FA
:020000040030CA
:0100010008F6
:010003001EDE
:00000001FF
Data & Observations:
After getting the firmware loaded onto the PIC and building up the hardware, let's
take a look at how this thing actually works. The following video will demonstrate how
the system works with various low and high output LEDs.
You probably noticed that putting a ping-pong ball over the high intensity LEDs more
evenly distributed the light. This was just a small trick to help demonstrate the results for
the video. The different LEDs all faded in and out with equal transitions with time,
exactly as planned.
An Overview Of The Fading LEDs via PWM:
Fading LED brightness levels is definitely more involved than it might appear from
the beginning, but once you understand how it works and what is going on, things
become much easier. The system created in this article offered 20 unique levels of
brightness for any LED and the brightness increments were linear! Feel free to apply this
theory to all kinds of LEDs.
What To Do Now:
Once you know how to control the LED brightness intensity you can move to
funner topics like dual and tri-color LEDs, also known as RGB LEDs. RGB (Red-Green-
Blue) LEDs can output ANY COLOR of the rainbow with the right combinations of Red-
Blue-Green color intensity. Take a look at a color wheel or look at microsoft paint's
advanced color feature to see how the r-g-b color intensities mix together to get an idea
of how you could use what you've just learned in this article with them.
Conclusion:
Hopefully this article brought to you a new idea of what it means to flash LEDs.
There's a whole gigantic world of capability beyond just turned LEDs on and off. The
project turned out quite successful and hopefully the two theory sections didn't overload
your brain with information.
If you have any further questions, I implore you...don't be shy, take a look at
theforums or ask a question there. I check them out regularly and love getting comments
& questions.

More Related Content

What's hot

Bidirectional Visitor Counter with Automatic ON-OFF Switch2
Bidirectional Visitor Counter with Automatic ON-OFF Switch2Bidirectional Visitor Counter with Automatic ON-OFF Switch2
Bidirectional Visitor Counter with Automatic ON-OFF Switch2DIPAN GHOSH
 
Bidirectional visitor counter & home automation by Jitendra Dhaka
Bidirectional visitor counter & home automation by Jitendra DhakaBidirectional visitor counter & home automation by Jitendra Dhaka
Bidirectional visitor counter & home automation by Jitendra DhakaNIT srinagar
 
Basic arduino sketch example
Basic arduino sketch exampleBasic arduino sketch example
Basic arduino sketch examplemraziff2009
 
Bi-Directional Visitor Counter & Home Automation revised strictly to pattern ...
Bi-Directional Visitor Counter & Home Automation revised strictly to pattern ...Bi-Directional Visitor Counter & Home Automation revised strictly to pattern ...
Bi-Directional Visitor Counter & Home Automation revised strictly to pattern ...Roshan Mani
 
GSM based elevator alarm and Panic Detection
GSM based elevator alarm and Panic DetectionGSM based elevator alarm and Panic Detection
GSM based elevator alarm and Panic DetectionAhmedNazir18
 
Development of a Low Cost, Reliable & Scalable Home Automation System.
Development of a Low Cost, Reliable & Scalable Home Automation System.Development of a Low Cost, Reliable & Scalable Home Automation System.
Development of a Low Cost, Reliable & Scalable Home Automation System.imtiyazEEE
 
56211728 automatic-room-light-controller-with-bidirectional-visitor-counter
56211728 automatic-room-light-controller-with-bidirectional-visitor-counter56211728 automatic-room-light-controller-with-bidirectional-visitor-counter
56211728 automatic-room-light-controller-with-bidirectional-visitor-counterAnn Francis Olita
 
ELECTRONICS PROJECT REPORT OF HOME AUTOMATION CUM BUILDING SECUIRITY
ELECTRONICS PROJECT REPORT OF HOME AUTOMATION CUM BUILDING SECUIRITYELECTRONICS PROJECT REPORT OF HOME AUTOMATION CUM BUILDING SECUIRITY
ELECTRONICS PROJECT REPORT OF HOME AUTOMATION CUM BUILDING SECUIRITYEldhose George
 
Automatic control of electrical Appliances
Automatic control of electrical AppliancesAutomatic control of electrical Appliances
Automatic control of electrical AppliancesShubham Sachan
 
Automatic room light intensity based window blinds control system
Automatic room light intensity based window blinds control systemAutomatic room light intensity based window blinds control system
Automatic room light intensity based window blinds control systemEcwayt
 
Notes arduino workshop_15
Notes arduino workshop_15Notes arduino workshop_15
Notes arduino workshop_15Faiz Lazim
 
Project report on the Digital clock using RTC and microcontroller 8051
Project report on the Digital clock using RTC and microcontroller 8051Project report on the Digital clock using RTC and microcontroller 8051
Project report on the Digital clock using RTC and microcontroller 8051Maulik Sanchela
 
Automatic room light controller with bi directional visitor counter using Ard...
Automatic room light controller with bi directional visitor counter using Ard...Automatic room light controller with bi directional visitor counter using Ard...
Automatic room light controller with bi directional visitor counter using Ard...PRASENJITMORE2
 
International Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentInternational Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentIJERD Editor
 
Automatic room-light-controller-visitor-counter
Automatic room-light-controller-visitor-counterAutomatic room-light-controller-visitor-counter
Automatic room-light-controller-visitor-counterMohit Awasthi
 

What's hot (20)

Visitor counter
Visitor counterVisitor counter
Visitor counter
 
Bidirectional Visitor Counter with Automatic ON-OFF Switch2
Bidirectional Visitor Counter with Automatic ON-OFF Switch2Bidirectional Visitor Counter with Automatic ON-OFF Switch2
Bidirectional Visitor Counter with Automatic ON-OFF Switch2
 
Bidirectional visitor counter & home automation by Jitendra Dhaka
Bidirectional visitor counter & home automation by Jitendra DhakaBidirectional visitor counter & home automation by Jitendra Dhaka
Bidirectional visitor counter & home automation by Jitendra Dhaka
 
Basic arduino sketch example
Basic arduino sketch exampleBasic arduino sketch example
Basic arduino sketch example
 
Bi-Directional Visitor Counter & Home Automation revised strictly to pattern ...
Bi-Directional Visitor Counter & Home Automation revised strictly to pattern ...Bi-Directional Visitor Counter & Home Automation revised strictly to pattern ...
Bi-Directional Visitor Counter & Home Automation revised strictly to pattern ...
 
GSM based elevator alarm and Panic Detection
GSM based elevator alarm and Panic DetectionGSM based elevator alarm and Panic Detection
GSM based elevator alarm and Panic Detection
 
Development of a Low Cost, Reliable & Scalable Home Automation System.
Development of a Low Cost, Reliable & Scalable Home Automation System.Development of a Low Cost, Reliable & Scalable Home Automation System.
Development of a Low Cost, Reliable & Scalable Home Automation System.
 
56211728 automatic-room-light-controller-with-bidirectional-visitor-counter
56211728 automatic-room-light-controller-with-bidirectional-visitor-counter56211728 automatic-room-light-controller-with-bidirectional-visitor-counter
56211728 automatic-room-light-controller-with-bidirectional-visitor-counter
 
Home Automation System
Home Automation SystemHome Automation System
Home Automation System
 
ELECTRONICS PROJECT REPORT OF HOME AUTOMATION CUM BUILDING SECUIRITY
ELECTRONICS PROJECT REPORT OF HOME AUTOMATION CUM BUILDING SECUIRITYELECTRONICS PROJECT REPORT OF HOME AUTOMATION CUM BUILDING SECUIRITY
ELECTRONICS PROJECT REPORT OF HOME AUTOMATION CUM BUILDING SECUIRITY
 
Final Presentation
Final PresentationFinal Presentation
Final Presentation
 
Automatic control of electrical Appliances
Automatic control of electrical AppliancesAutomatic control of electrical Appliances
Automatic control of electrical Appliances
 
Automatic room light intensity based window blinds control system
Automatic room light intensity based window blinds control systemAutomatic room light intensity based window blinds control system
Automatic room light intensity based window blinds control system
 
Notes arduino workshop_15
Notes arduino workshop_15Notes arduino workshop_15
Notes arduino workshop_15
 
Manual QUBINO zmnhwd1
Manual QUBINO zmnhwd1Manual QUBINO zmnhwd1
Manual QUBINO zmnhwd1
 
Project report on the Digital clock using RTC and microcontroller 8051
Project report on the Digital clock using RTC and microcontroller 8051Project report on the Digital clock using RTC and microcontroller 8051
Project report on the Digital clock using RTC and microcontroller 8051
 
Deepak
DeepakDeepak
Deepak
 
Automatic room light controller with bi directional visitor counter using Ard...
Automatic room light controller with bi directional visitor counter using Ard...Automatic room light controller with bi directional visitor counter using Ard...
Automatic room light controller with bi directional visitor counter using Ard...
 
International Journal of Engineering Research and Development
International Journal of Engineering Research and DevelopmentInternational Journal of Engineering Research and Development
International Journal of Engineering Research and Development
 
Automatic room-light-controller-visitor-counter
Automatic room-light-controller-visitor-counterAutomatic room-light-controller-visitor-counter
Automatic room-light-controller-visitor-counter
 

Similar to Fading leds via pwm

IRJET - Automatic Mechanism for LED Parameters Testing & Checking
IRJET -  	  Automatic Mechanism for LED Parameters Testing & CheckingIRJET -  	  Automatic Mechanism for LED Parameters Testing & Checking
IRJET - Automatic Mechanism for LED Parameters Testing & CheckingIRJET Journal
 
electronic voting machine by rfid
electronic voting machine by rfidelectronic voting machine by rfid
electronic voting machine by rfidSaurabh Uniyal
 
Counter digital electronics
Counter digital electronics Counter digital electronics
Counter digital electronics Sooraj Maurya
 
summer training report (2)
summer training report (2)summer training report (2)
summer training report (2)Kavya Gupta
 
smart home automation system
smart home automation systemsmart home automation system
smart home automation systemMdSaifuddinQuader
 
PIC Microcontroller
PIC MicrocontrollerPIC Microcontroller
PIC MicrocontrollerDivya Bansal
 
Home automation control system using dtmf technology
Home automation control system using dtmf technologyHome automation control system using dtmf technology
Home automation control system using dtmf technologyGovind Ekshinge
 
Tachometer using AT89S52 microcontroller with motor control
Tachometer using AT89S52 microcontroller with motor controlTachometer using AT89S52 microcontroller with motor control
Tachometer using AT89S52 microcontroller with motor controlSushil Mishra
 
Report (Electromagnetic Password Door Lock System)
Report (Electromagnetic Password Door Lock System)Report (Electromagnetic Password Door Lock System)
Report (Electromagnetic Password Door Lock System)Siang Wei Lee
 
DESIGN OF TEMPERATURE BASED FAN SPEED CONTROL and MONITORING USING ARDUINO
DESIGN OF TEMPERATURE BASED FAN SPEED CONTROL and MONITORING USING ARDUINODESIGN OF TEMPERATURE BASED FAN SPEED CONTROL and MONITORING USING ARDUINO
DESIGN OF TEMPERATURE BASED FAN SPEED CONTROL and MONITORING USING ARDUINORatnesh Kumar chaurasia
 
Street light controlling using Microcontroller
Street light controlling using MicrocontrollerStreet light controlling using Microcontroller
Street light controlling using Microcontroller9989476539
 
Coin based mobile charger project report
Coin based mobile charger project reportCoin based mobile charger project report
Coin based mobile charger project reportkaushal chaubey
 
Arduino Workshop (3).pptx
Arduino Workshop (3).pptxArduino Workshop (3).pptx
Arduino Workshop (3).pptxHebaEng
 
STREET LIGHT THAT GLOWS ON DETECTING VEHICLE MOVEMENT
STREET LIGHT THAT GLOWS ON DETECTING VEHICLE MOVEMENTSTREET LIGHT THAT GLOWS ON DETECTING VEHICLE MOVEMENT
STREET LIGHT THAT GLOWS ON DETECTING VEHICLE MOVEMENTm sivareddy
 

Similar to Fading leds via pwm (20)

Bidirect visitor counter
Bidirect visitor counterBidirect visitor counter
Bidirect visitor counter
 
IRJET - Automatic Mechanism for LED Parameters Testing & Checking
IRJET -  	  Automatic Mechanism for LED Parameters Testing & CheckingIRJET -  	  Automatic Mechanism for LED Parameters Testing & Checking
IRJET - Automatic Mechanism for LED Parameters Testing & Checking
 
electronic voting machine by rfid
electronic voting machine by rfidelectronic voting machine by rfid
electronic voting machine by rfid
 
Embedded notes.iet.trichy
Embedded notes.iet.trichyEmbedded notes.iet.trichy
Embedded notes.iet.trichy
 
Embedded notes.iet.trichy
Embedded notes.iet.trichyEmbedded notes.iet.trichy
Embedded notes.iet.trichy
 
Counter digital electronics
Counter digital electronics Counter digital electronics
Counter digital electronics
 
Embedded system
Embedded systemEmbedded system
Embedded system
 
Arduino Thermometer
Arduino ThermometerArduino Thermometer
Arduino Thermometer
 
summer training report (2)
summer training report (2)summer training report (2)
summer training report (2)
 
smart home automation system
smart home automation systemsmart home automation system
smart home automation system
 
PIC Microcontroller
PIC MicrocontrollerPIC Microcontroller
PIC Microcontroller
 
Home automation control system using dtmf technology
Home automation control system using dtmf technologyHome automation control system using dtmf technology
Home automation control system using dtmf technology
 
Tachometer using AT89S52 microcontroller with motor control
Tachometer using AT89S52 microcontroller with motor controlTachometer using AT89S52 microcontroller with motor control
Tachometer using AT89S52 microcontroller with motor control
 
Report (Electromagnetic Password Door Lock System)
Report (Electromagnetic Password Door Lock System)Report (Electromagnetic Password Door Lock System)
Report (Electromagnetic Password Door Lock System)
 
DESIGN OF TEMPERATURE BASED FAN SPEED CONTROL and MONITORING USING ARDUINO
DESIGN OF TEMPERATURE BASED FAN SPEED CONTROL and MONITORING USING ARDUINODESIGN OF TEMPERATURE BASED FAN SPEED CONTROL and MONITORING USING ARDUINO
DESIGN OF TEMPERATURE BASED FAN SPEED CONTROL and MONITORING USING ARDUINO
 
Street light controlling using Microcontroller
Street light controlling using MicrocontrollerStreet light controlling using Microcontroller
Street light controlling using Microcontroller
 
Coin based mobile charger project report
Coin based mobile charger project reportCoin based mobile charger project report
Coin based mobile charger project report
 
Arduino Workshop (3).pptx
Arduino Workshop (3).pptxArduino Workshop (3).pptx
Arduino Workshop (3).pptx
 
STREET LIGHT THAT GLOWS ON DETECTING VEHICLE MOVEMENT
STREET LIGHT THAT GLOWS ON DETECTING VEHICLE MOVEMENTSTREET LIGHT THAT GLOWS ON DETECTING VEHICLE MOVEMENT
STREET LIGHT THAT GLOWS ON DETECTING VEHICLE MOVEMENT
 
Manual fsr'14
Manual fsr'14Manual fsr'14
Manual fsr'14
 

Recently uploaded

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 

Recently uploaded (20)

Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 

Fading leds via pwm

  • 1. Fading LEDs via PWM: The LED is probably the most popular component used in hobby electronics. LEDs offer a way to light things uniquely like in POV clocks and they are great for quick debugging when you want to double check that something is working. Another cool feature of LEDs is that their light intensity can be varied by sending it specific digital pulses. This article will focus on how to use digital PWM (Pulse width Modulation) to vary the light intensity of an LED in order to create an awesome fading effect. A small microcontroller will be used to send out the custom PWM signal for fading the led light intensity in and out. Purpose & Overview of this project: The goal of this project is to create an LED controller that can fade the brightness level of an LED (light-emitting-diode) with a smooth transition. Since light intensity increases in a logrithmic fashion, we'll need to pay extra attention to the theory section of this article to make sure the brightness transitions move smoothly. To build this LED controller we will use only the most basic of parts to ensure that anyone can duplicate this controller with minimal investment of time and money. The PIC 18F1330 microcontroller will be used for all the intelligent digital work and some standard LEDs will be used for testing out how well the LED fader works. Continue to the next section to see the complete parts list. Parts: PIC 18F1330 Microcontroller PICkit2 7805 +5v Regulator 2x 10kΩ Resistor 100Ω Resistor 47uF Capacitor Assorted LEDs 2N2222 Breadboard Wires SIPS 9v Connector Battery Holder Parts List Details: The parts list above shows everything I used to build this project. Below I have detailed out additional information on some of the more important parts. Take a look if you're curious. PIC 18F1330 The PIC will be used as our intelligent LED controller. It will be programmed with some firmware that will send out the digital PWM signal to change the brightness level of the LED. The internal 32 MHz (8 MHz + x4 PLL) oscillator will be used, so you won't need to buy an additional crystal!
  • 2. 7805 +5 Voltage Regulator This +5v regulator will be used create the power supply source for all of the electronics used in this circuit. Since the circuit is very small the actual load placed on this regulator will be small, maybe 100 miliamp. PICkit 2 The PICkit 2 is a programmer for the Microchip PIC microcontroller and it has a legacy for being one of the best, even with the 3rd version already available. You will need some programmer to get your programs onto the PIC and this is the one that I suggest. 2N2222 General Purpose Transistor This 2N2222 transistor will be used to control the LED's state between on and off. The transistor will be connected directly to the microcontroller which will send a signal telling the transistor what state it should be in: on or off. Assorted LEDs A variety of LEDs is nice to have to test the system out, even though you really only need 1 led to see if everything is working. Your choice here. Jumper Wires & Breadboard I prefer to use a breadboard with the projects in my articles because they offer maximum flexibilty with circuit design with minimal downtown between changes. In addition to the breadboard you will need jumper wire to get everything connected together. Schematic Overview: The schematic for this project was kept as simple as possible so that the focus could remain on how the touch screen works and how we interface to it. You can see the completed schematic for this project below. The main parts in the schematic are theLM7805, 18F1330 and Assorted LEDs.
  • 3. View Full Schematic Schematic Specifics: Power Regulator The 7805 regulation circuit is nothing special. Connect a battery to one side, that has +7.5v or more and the output is +5v. A 47uF dc filtering cap is added to allow the PIC to operate flawlessly. LED Circuit The LED circuit consists of the connection from the microcontroller to the 2N2222 transistor. The transistor provides a ground for the LED when it is switched on, allowing the led to be turned on. When the transistor is in the off state, the LED has no ground so current cannot flow through it, therefore the LED is off.
  • 4. PWM Theory: In order to control the actual brightness of the LED we will send it a PWM signal PWM stands for pulse width modulation. Any PWM signal has three core qualities: 1 Frequency 2 Duty Cycle 3 Amplitude These three qualities tell us what type of PWM signal to expect and we will then be able to project it's effect on our system. Below are a few examples of PWM signals with their associated qualities listed out. Some PWM Examples The type of PWM signal we'll be generating will ultimately look like the pictures above. We'll be shooting for a 60-120 Hz frequency range, with an amplitude of +5v (because our system uses +5v). The duty cycle, however, will vary between 0% (fully-off state) and 100% (fully-on state).
  • 5. Lighting an LED via PWM So, if we send any of the signals seen above into an LED, what do we expect should happen? Well, the LED turns on for the short duration of the pulse. Since we will choose an output frequency of between 60-120 Hz the LED will appear to be a solid color because of persistence of vision. The brightness, however, will be controlled purely by the duty cycle %. The animation below gives you an idea of the effect of different PWM duty cycles input into an LED. So, you might be thinking...great! we can control LED brightness by changing the duty cycle, sweet let's get it on!...and you'd be right. There is, however, one small caveat. Light brightness in LEDs does not increase proportionally with duty cycle increases. This means if we varied duty cycle 0% to 100% back and forth the LED would fade in and out, however it would disproportionately be in the 'fully-on' state most of the time. So let's move forward and take a look at how we can use a logarithms to get a linear fade-in and fade-out with our LEDs. Adding A Logrithmic Filter A logarithm is a type of reverse exponential function, but all we need to know is that light intensity increases in a logarithmic way. So, how do we use this information? Well, let's suppose we want a linear set of 10 points that represent everything from the LED being fully off and the LED being fully on. To do this we use the logarithmic function with base 10, which yields the following values (note I used excel):
  • 6. Even though the final numbers output that we should use for the duty cycle seem one-sided, they actually correlate to even steps from one LED brightness level to the next. If you needed a larger set of values, for example 20, you could simply use a logarithmic function with base 20 to get those values. Hardware Design: Now the fun part begins, we get to actually build the project. All the steps seen below follow the schematic and I added a power LED to know when the power is connected. Building The Circuit: Get your parts together and follow the schematic. I assembled the circuit in stages as you can see below...
  • 7. ·First the power regulator circuit is connected. ·Next the PIC circuit is wired up on the breadboard.
  • 8. ·The 2N2222 and resistors are connected to the PIC. ·Lastly, the LED and resistor is connected.
  • 9. The Software Design: The code is too long to include everything on this page, so I'll just cover the two important specifics: - Main Loop Brightness Control - Interrupt Level Counting The program has two parts and two purposes. The first part is the main function Setting Brightness Intensity: ------------« Begin Code »------------ .. ... while(1){ // Gradually Become Brighter for(i=0;i<21;i++){ led_intensity = clr_array[i]; Delay10KTCYx(20); } // Gradually Become Darker for(i=20;i>0;i--){ led_intensity = clr_array[i]; Delay10KTCYx(20); } ... .. ------------« End Code »------------ The interrupt portion of the firmware is triggered every time that the counter hits the 0xFFFF value. When the interrupt occurs, the ISR (interrupt sub-routine) increments an internal count value and checks to see what state the PWM output should be in, to achieve the proper duty cycle, which correlates to the LED brightness level.
  • 10. Digital Timer Interrupt Controls PWM Duty Cycle: ------------« Begin Code »------------ .....void InterruptHandlerHigh(){ if(INTCONbits.TMR0IF) { count++; //Every 100 Timer0 Interrupts Reset Counter if(count >= 100) count = 0; //Set Brightness According To Count Value //This Will Be The Duty Cycle if(count <=led_intensity &&led_intensity > 0) PORTAbits.RA0 = 1; else PORTAbits.RA0 = 0; WriteTimer0( 0xFF00 ); INTCONbits.TMR0IF = 0; //Clear TMR0 Flag } INTCONbits.GIE = 1; //Re-enable all interrupts}..... ------------« End Code »------------ Each axis of the touch screen requires a similar process for testing to see if it is currently being touched and where. First the Power and Ground lines need to be turned on for the appropriate axis, all other connections should be high-impedance so as not to interfere. Then the touch screen output line should be read via the A/D converter. Do this for both X and Y axis and you have a 10-bit value signifying where the screen was touched. LED_Fading.c: /******************** Description: This program is meant to output a varying PWM signal to light an LED at different brightness levels. The end effect of the output signal will be a fading in and fading out of the LED brightness. This is done through the two for loops seen in the forever while loop inside the main function.
  • 11. ********************/ #include <p18f1330.h> #include <delays.h> #include <pwm.h> #include <timers.h> //Use Internal Oscillator #pragma config OSC = INTIO2 //Watchdog Timer Off #pragma config WDT = OFF void InterruptHandlerHigh (void); unsigned int count = 0; unsigned int led_intensity = 0; //Logarithmic (Base 20) Values From 1->100 unsigned int clr_array[] = {1,2,3,5,6,8,10,12,14,16,19,22,25,29,33,38,43,51,65,80,97};
  • 12. void main(void){ unsigned int i = 0; //32 MHz Clock Setup (8 MHz + 4x PLL), 8 MHz Instruction Execution OSCCON = 0x70; OSCTUNEbits.PLLEN = 1; //Oscillator Good To Go? while(OSCCONbits.IOFS != 1){ Delay1KTCYx(1); } TRISA = 0x00; PORTA = 0x00; //Setup Interrupts INTCON = 0b10100000;
  • 13. //Setup Timer0 OpenTimer0( TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_1 ); //Initialize Timer0 WriteTimer0( 0xFF00 ); while(1){ // Gradually Become Brighter for(i=0;i<21;i++){ led_intensity = clr_array[i]; Delay10KTCYx(20); } // Gradually Become Darker for(i=20;i>0;i--){ led_intensity = clr_array[i]; Delay10KTCYx(20); } } }
  • 14. //INTERRUPT CONTROL #pragma code InterruptVectorHigh = 0x08 //interrupt pointer address (0x18 low priority) void InterruptVectorHigh (void) { _asm //assembly code starts goto InterruptHandlerHigh //interrupt control _endasm //assembly code ends } #pragma code #pragma interrupt InterruptHandlerHigh //end interrupt control void InterruptHandlerHigh() { if(INTCONbits.TMR0IF) { count++; //Every 100 Timer0 Interrupts Reset Counter if(count >= 100)
  • 15. count = 0; //Set Brightness According To Count Value //This Will Be The Duty Cycle if(count <= led_intensity && led_intensity > 0) PORTAbits.RA0 = 1; else PORTAbits.RA0 = 0; WriteTimer0( 0xFF00 ); INTCONbits.TMR0IF = 0; //Clear TMR0 Flag } INTCONbits.GIE = 1; //Re-enable all interrupts } LED_Fading.hex: :020000040000FA :06000000A5EF01F0120063 :020006001200E6 :060008008BEF00F0120076
  • 17. :10012000E4FFEACFE4FFF6CFE4FFF7CFE4FFF5CF3B :10013000E4FFF3CFE4FFF4CFE4FFFACFE4FF00EEF7 :1001400000F0020EE80403E3EECFE4FFFBD700EE7D :1001500002F0020EE80403E3EECFE4FFFBD7E65221 :10016000F2A420D00001C02B000EC123640EC05D9C :10017000000EC15902E3C06BC16BC051C25DC151D9 :10018000C35908E3000ED880C255000EC35502E2E1 :10019000808001D08090E66AE66846EC01F0E55286 :1001A000E552F294F28EE55200EE03F0020EE804FE :1001B00003E3E5CFEDFFFBD700EE01F0020EE8040C :1001C00003E3E5CFEDFFFBD7E5CFFAFFE5CFF4FF83 :1001D000E5CFF3FFE5CFF5FFE5CFF7FFE5CFF6FF7E :0E01E000E5CFEAFFE5CFE9FFE5CFDAFF11003A :0201EE000E0EF3 :1001F000F66E000EF76E000EF86E00010900F55065 :10020000F36F0900F550F46F03E1F36701D03DD0BF :100210000900F550EE6F0900F550EF6F0900F55039 :10022000F06F09000900F550E96E0900F550EA6E1B :10023000090009000900F550F16F0900F550F26F4F
  • 19. :10033000480E01D04C0EE76EE72EFED7E76AE72E97 :0A034000FED7052EF7D700001200CB :06034A0010EE80F020EE31 :1003500080F0F86A019CF7EC00F003EC00F00EEC82 :0603600000F0FBD71200C3 :0A03660000EE00F00F0EEE6AEA62EE :04037000FDD71200A3 :0C03740000000000010002000300050072 :10038000060008000A000C000E0010001300160002 :1003900019001D00210026002B00330041005000F1 :0203A0006100FA :020000040030CA :0100010008F6 :010003001EDE :00000001FF Data & Observations: After getting the firmware loaded onto the PIC and building up the hardware, let's take a look at how this thing actually works. The following video will demonstrate how the system works with various low and high output LEDs. You probably noticed that putting a ping-pong ball over the high intensity LEDs more evenly distributed the light. This was just a small trick to help demonstrate the results for the video. The different LEDs all faded in and out with equal transitions with time, exactly as planned.
  • 20. An Overview Of The Fading LEDs via PWM: Fading LED brightness levels is definitely more involved than it might appear from the beginning, but once you understand how it works and what is going on, things become much easier. The system created in this article offered 20 unique levels of brightness for any LED and the brightness increments were linear! Feel free to apply this theory to all kinds of LEDs. What To Do Now: Once you know how to control the LED brightness intensity you can move to funner topics like dual and tri-color LEDs, also known as RGB LEDs. RGB (Red-Green- Blue) LEDs can output ANY COLOR of the rainbow with the right combinations of Red- Blue-Green color intensity. Take a look at a color wheel or look at microsoft paint's advanced color feature to see how the r-g-b color intensities mix together to get an idea of how you could use what you've just learned in this article with them. Conclusion: Hopefully this article brought to you a new idea of what it means to flash LEDs. There's a whole gigantic world of capability beyond just turned LEDs on and off. The project turned out quite successful and hopefully the two theory sections didn't overload your brain with information. If you have any further questions, I implore you...don't be shy, take a look at theforums or ask a question there. I check them out regularly and love getting comments & questions.