Continue to Site

# PWM - LED fade out!!

Status
Not open for further replies.

#### Beth

##### New Member
Hi there

I am hoping someone may be able to offer some advice in regards to PWM.

I was wondering if it is possible to use PWM to fade out an LED. I am using a PIC16F628 and require an LED to turn on, and after 6 minutes or so gradually fade out until fully off. Is PWM modulation the best method for achieving this? Or is there a simpler alternative?

Cheers all

#### Nigel Goodwin

##### Super Moderator
Beth said:
Hi there

I am hoping someone may be able to offer some advice in regards to PWM.

I was wondering if it is possible to use PWM to fade out an LED. I am using a PIC16F628 and require an LED to turn on, and after 6 minutes or so gradually fade out until fully off. Is PWM modulation the best method for achieving this? Or is there a simpler alternative?

I think that would be the simplest way of doing it, the 628 is an ideal choice - cheap, eeprom technology, internal 4MHz oscillator, one channel of 10 bit hardware PWM. It will do everything you require with almost no other hardware required - a resistor to feed the LED, and perhaps a transistor as well - depending on the current you require for the LED.

All you need to do for the fadeout is gradually reduce the PWM ratio, reducing it by one every 3.5 seconds or so will go from 100% to 0% over roughly 6 minutes. I believe that the light output from LED's isn't linear, so it won't fade in a linear fashion - but presumably it won't matter for your application.

#### TheAnimus

##### New Member
PWM with LEDs is fine, most LEDs give a nice good-enough-to-be-linear response to it, but we as humans see light levels log.

As such your 10bit PWM, will only give you 10 clear log states without rounding errors.
(ie 0000 0001, 0000 0010 0000 0100 and so forth).

But in my experaince (way to much with playing with LEDs) i find you can get a good 45 states out of that without it looking jerky.

#### Rescyou

##### New Member
Linear PWM

I've found that using a power law algorithm works pretty decent at improving the non-linear light the led puts out when using PWM.

Something like this:

Duty = 255

START

PWM Output Duty to Pin

Duty = Duty * 6 / 7

GOTO START or UNTIL Duty < 0

------------------------------------------
And to have it fade slower:

Duty = 1024

START

PWM Output Duty to Pin

Duty = (Duty * 6 / 7) / 4

GOTO START or UNTIL Duty < 0

--------------------------------------

And to make it brighten:

Duty = 255

START

PWM Output Duty to Pin

Duty = 255 - (Duty * 6 / 7)

GOTO START or UNTIL Duty < 0

---------------------------------------------

And if your good enough with you PWM programming you can figure out the optimum current for the led and ensure your duty cycle never exceeds it so you probably won't even need a resistor but also keeping in mind the PIC pins are rated somewhere in the 20ma - 25ma range and the whole chip has a max as well, you can find that data in the PIC datasheet.

R.

#### Beth

##### New Member
Thank you

Hello

Thanks everyone for the replies. I'm putting together the code at the mo. Hopefully I will get it to work!

Cheers x

#### Wbdsgnr

##### New Member
asm code

Uhm.. I wanted to try the same, only to fade a led in, in about a sec (or two) I made this code, but it doesn't seem to work: (i'm a kinde newbee, but i really want to use asm)
Code:
		PROCESSOR 16F819
#include "p16f819.inc"
__config	0x3f14

ORG	0x00
goto		Main
ORG	0x04
goto		Main

Main	banksel	CCP1CON
clrf		CCP1CON				;everything zero
bsf		CCP1CON, CCP1M3		;set register
bsf		CCP1CON, CCP1M2		;on pwm
banksel	PR2
movlw	0xFF
movwf	PR2
banksel	TRISB
movlw	b'00000000'
movwf	TRISB

banksel	T2CON
clrf		T2CON
clrf		TMR2
bsf		T2CON, T2CKPS1    ;prescaler=16 / freq=3.05 kHz
bsf 		T2CON, TMR2ON		;timer2 on

movlw	 0x00
movwf	CCPR1L

clrf		TMR0
clrf		INTCON
banksel	OPTION_REG
movlw	b'00000110'	;Set prescaler timer0=32 so it takes 4.1 ms
movwf	OPTION_REG
banksel	INTCON
bsf		INTCON, TMR0IE
bsf		INTCON, PEIE
bsf		INTCON, GIE

Loop          btfss           INTCON, TMR0IF		;continue on overflow and
goto		Loop

incf		CCPR1L,1			;raise duty cycle
bcf		INTCON, TMR0IF		;turn flag off
goto		Loop					;return

End

could someone help?

Status
Not open for further replies.

Replies
3
Views
7K
Replies
5
Views
3K
Replies
178
Views
14K
Replies
4
Views
2K
Replies
16
Views
2K