LED Dimmer Soft On / Off
Modern LED's are ideally suited for many illuminating tasks. In most cases, the LED is simply turned on and off.

This software implements a more sophoisticated SoftON/OFF Dimmer, to obtain a very smooth turning on and turning off.

You could use this circuit to obtain a very small PCB that fits nearly everywhere!

Complete source code available on request here.

The Main loop of the Program only generates a software PWM for the LED's.

The Timer Interrupt Service Routine polls the pushbutton and changes the brightness

Push the button for about 0.5s:

Turns the LED's ON or Off.

If the Brightness was changed beforehand, the new Value is stored in nonvolatile EEPROM

Hold Pushbutton pressed:

Brightness slowly changes from 0 to 100% and back If the desired brightness is reached, release button.

The Change Rate alters in different speeds, to emulate the logarithmic sensitivity of the human eye.

Hold pushbutton down while powering up the circuit:

You can use this feature to select if the LED's are automatically turned on after power-up

OSCCAL-Value should be stored in EEPROM-Cell $3f when programming!

;;Dimmer Soft ON/OFF

;(c) 2004 Markus Vohburger

;Gewerbliche Nutzung untersagt!

;Definitionen für Tiny12 Laden

.include "tn12def.inc"

.def temp1 = r16

.def temp2 = r17

.def output = r18

.def swcount0 = r19

.def output = r20

.def pwmcount = r21

.def swcount1 = r22

.def conbuf = r23

.def direction = r25

.def control=r26

.def pwmvalue = r27

.def dirty = r28

.def onoff = r29


;reset handler

.org $0000

rjmp main

;timer overflow interrut handler

.org OVF0Addr

rjmp OVF0Handler

.org $0010


;stack setup

;read osccal byte

ldi temp1,EE_OSCCAL

rcall readeeprom

out osccal,temp1

;port setup

;port b input with pullups

ldi temp1,$01e

out ddrb,temp1

;port b output off, pullups on

ldi temp1,$1f

out portb,temp1

ldi temp1,$1e

out ddrb,temp1

;Einstellungen aus EEProm laden

ldi temp1,$00

rcall readsettings

mov conbuf,temp1

ldi direction,$00

ldi dirty,$00

ldi temp1,$01

rcall readeeprom

mov onoff,temp1

andi onoff,$01

;Dimmer An/Aus nach Reset umschalten, wenn Taste gedrückt

sbic pinb,0

rjmp notoggle

ldi temp1,$01

eor onoff,temp1

;ins EEProm schreiben


sbic eecr,eewe

rjmp waittoggle

out eedr,onoff

sbi eecr,eemwe

sbi eecr,eewe



;Timer zurücksetzen

ldi temp1,$00

out tcnt0,temp1

;Timer interrupts an

ldi temp1,(1<<toie0)

out timsk,temp1

;Timer starten

ldi temp1,$03

out tccr0,temp1

ldi swcount0, $00

ldi swcount1,$00

ldi pwmcount,$00

ldi pwmvalue,$00


;Hauptprogrammschleife, macht nur PWM


;leds aus

ldi output,$1f

cp pwmcount,pwmvalue

brsh writeoutput

;leds an

ldi output,$01


out portb,output

inc pwmcount

rjmp loop

;Timer Interrupt Handler


;Status sichern

in r1,sreg

;SoftON bzw. Off abhandeln

mov control,conbuf

cpi onoff,$01

breq nooff

ldi control,$00


cp pwmvalue,control

breq endcontrol

brlo doinc

dec pwmvalue

rjmp endcontrol


inc pwmvalue

;wenn taster gedrückt, dann pressedhandler


sbis pinb,0

rjmp pressedhandler

;war taster zu kurz gedrückt?

cpi swcount0,$02

brlo exitreleasehandler

;helligkeit wurde geändert

cpi swcount0,$1f

brsh exitreleasehandler

;turn on / Off

;und einstellungen abspeichern

ldi temp1,$01

eor onoff,temp1

rcall writesettings

rjmp exitreleasehandler


ldi swcount0,$00


out sreg,r1


;zähler nur bis zum maximum erhöhen


cpi swcount0,$20

brsh noinc

inc swcount0


cpi swcount0,$20

brlo exit_ovf0handler

inc swcount1

cpi swcount1,$08

brne exit_ovf0handler

clr swcount1

rcall updown

rjmp exit_ovf0handler

;Änderungsgeschwindigkeit je nach Helligkeit anpassen


mov pwmvalue,conbuf

;merken, dass sich was geändert hat

ldi dirty,$ff

;heller oder dunkler werden ?

sbrs direction,0

rjmp uphandler

ldi temp1,$01

cpi conbuf,$f0

brsh more1

ldi temp1,$02

cpi conbuf,$e8

brsh more1

ldi temp1,$08



sub conbuf,temp1

brcc exitsub

ldi conbuf,$00

cbr direction,1

rjmp exitsub


ldi temp1,$01

cpi conbuf,$f0

brsh more2

ldi temp1,$02

cpi conbuf,$e8

brsh more2

ldi temp1,$08



add conbuf,temp1

brcc exitsub

ldi conbuf,$ff

sbr direction,1



;die aktuelle helligkeit aus EEprom lesen


ldi temp1,$00

rcall readeeprom

mov conbuf,temp1




;nur schreiben, wenn sich was geändert hat




Complete sourcecode available on request here

Additional Information,Basics,DIY Kit, Links for Schematic
Sponsored Ads:

You are Visitor Nr. 185766