Reg-Sv: Light Dimming Regulation

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

Reg-sv

++381 13 812 932

July, 1999.

Light Dimming Regulation


Prepared by:
Nikola Tonev, B.Sc.EE
Purpose
This device regulates the light dimming. It regulates
the light intensity of standard bulbs. This is an
illustration how we can use microcontrollers in
applications such as controlling the intensity of light
dimming. In this article, you also can find out how to
connect a microcontroller to an AC power line, and to
a triac. We can design the same function in a simpler
way though, using (triac, diac, couple of resistors and
capacitors). There are numbers of electrical
schematics available on the net, describing how to do
this. My unit is powered directly from an AC line.
Powering from AC line enables the microcontroller to
directly trigger a triac (for example - triggering
without optotriac and similar things, which usually
characterize microcontroller and triac connection.)
Why to use a microcontroller? This unit tracks for
zerocrossing moment of AC line. Using this
information, we avoid error triggering at point of
maximum voltage AC (while the bulb fiber still being
cold). This is very important! Cold bulb fiber has a
couple of times less resistance than the hot one.
Turning on the cold bulb at maximum voltage AC (
220 * 2 ) high current will flow and the possibility
of damaging the fiber is high. Regulation with
microcontroller gives us dimmed light without
flickering. Regulation from full dimming (no
triggering at all) to minimal dimming (triggering at
start of AC period).
At this point, I would like to indicate the use of this
device in applications such as: heaters, with minor
modifications of course. Adding temperature sensors,
gives regulated heating power. More other
applications can be obtained performing time delay,
internal logic and others.

How does the device work?


Let's get back to our example. I designed it based on
existing sensor regulators. The turning on and off is
produced with short touch of sensor. Touching the
sensor a bit longer, makes the light power change
continually.
I used PIC 16F84-4MHz for this application. You can
also use other PIC controllers. Probably, the best and

cheapest solutions are some of those PICs in 8 pin


DIP.
In Yugoslavia, the AC line is 220V 50Hz. If this is not
always the case, the light regulator will still be
working properly. In case of 60Hz AC line, you should
make some corrections. Frequency of 50Hz makes the
amplitude of AC line to have zero value hundred times
per second. Thus, zero crossing comes every 10ms.
To control the power of lighting I decided to control
the phase shift. Triac should be triggered after phase
shift (time after zero crossing). If we turn on the triac
just after zero crossing time, the light will be
maximally strong. In addition, effective voltage on
light bulb will be 220V. In that case, current will flow
for half-period almost completely. On the other hand,
if we turn the triac on later, for example at the end of
half-period, the current will flow only for a little bit of
period. The result is dimmed light. This practically
means that delay regulation of triggering regulates
light dimming.

Calculation
Comfortable programming supposes we use 1 byte for
delay. In best case, 256 different dimming levels will
be available. Processors clock is 4MHz. It is
internally divided by 4. What is the value of
prescalling coefficient? 64. If we adopt 64 we can
get maximal dimming for nmax=(10ms/64s)157 (157
different dimming levels). In case of prescalling with
32, we get nmax312, (we need two bytes for
representing 312 different values of dimming). Finally,
it remains the case of prescalling with 128 n max79.
The best resolution we can get with one byte is 157
and prescalling coefficient is 64.
For estimated value of prescalling coefficient, we
must find the time minimum of triggering time delay.
Triggering the triac when amplitude of AC voltage is
too low, makes triggering insecure. Value for secure
triggering is taken as 20V.

min

1
20V
arcsin
311V 4
2 50
64 s

Reg-sv-1.1E 8/1

Instructions for use


Dimming is minimal for 4*64s delay time and
dimming is maximal for 157*64s.

Software & hardware


The program consists of 4 parts.
1.

Initialization

2.

The main part tracks the amplitude of AC line. In


this part, we measure the time after last zerocrossing. After certain time needed for dimming,
program turns the triac on.

3.

Turning the triac on

4.

Switching control. This part includes switch


debouncing. All changes such as turning on/off
the light, changing the size of dimming are
evaluated in this part of program.

Initialization (1) consists of setting the initial


prescalling factor (64), setting initial values of
variables, and defining input/output ports. We need to
turn off manually the WDT while programming, turn
on XT oscillator and turn on Power up timer (PWRT)
Turning the triac on (2) has to be done in sub program
Ukljuci. Pin RB0 has to be initialized as input pin. In
data latch of that pin sub-program puts 1. This value
will not be at the pin lead because this pin is defined
as input pin. At the time of turning on, this part of
program sets the pin RB0 as output pin for 8s. After
that RB0 is defined again as input pin. 8s is enough
for secure triggering the triac. If we want that kind of
triggering in both half-periods, it is necessary for triac
to be able to work in 4 quadrants, for example
(TIC206 Texas Instruments, Motorola's MAC15A,...).
If we try to put the triac that can not work in 4
quadrants, it will be turned on just in one half-period.
The main trick in whole project is this triggering.
Main program (2) tracks zero-crossing and consists of
two parts. One part tracks the positive half-period of
AC line, and the other tracks the negative half-period.
One pin is enough for this function realisation. Pins of
port A have built in protection diodes. The purpose of
these diodes is to protect the chip from extreme input
voltage. The AC line and port A pin are connected
with high resistance. If the input voltage is higher or
lower than nominal input range, this will turn one of
protection diodes on. While one half-period is one, a
capacity of PN connection will charge. That capacity
will be discharged slowly with the start of alternative
half-period when the AC voltage is still low. In this
case at the end of the positive half-period: the
controller will get the information that positive halfperiod is still going, but the negative half-period has
already started. In addition, in reality instead of
maximal dim we get minimal dim. There is an another
problem. Input pin senses everything below +2,5V as
logic zeroes. However, 2,0V is still in a positive halfperiod. Additional compensatory resistors connected

to the ground and VCC can solve the problem.


Naturally, software also can give us a solution. The
software solution is given in 4 lines in listing and can
be deleted when using a compensatory resistor (these
four lines are noted in listing). The software solution
gives better results when using a quartz oscillator. In
this example, this is a better solution. In case of using
smaller controller with RC oscillator makes the
software solution a little bit more complicated. The
values of additional compensatory resistors are
estimated experimentally. There is another diode
(LED diode) connected to port B (pin RB1). On this
diode, we get the PWM signal from this pin. In
contrast to triggering triac that lasts only 8s, PWM
signal turns on the diode and lasts through the rest of
the half-period. This LED diode is for testing only.
The most complicated part of the program is subprogram for taster switch control (3). Used variables
are:
Smer=1 (light dimming or turning on)
Smer=0 (light up or turning off)
Vrednost is a variable. It shows a value of dimming.
Minimal dim is set up as 4 and a maximal is set as 200
(not 157 but 200 to be sure the triac will not turn on
accidentally.
Stisak measures the time to pass keeping taster switch
pressed. If there is enough time passed, the power of
lighting will change gradually. Short time pressing
changes minimal and maximal lighting (turning
on/off).
Speedcontrol
is
dimming/lighting.

slow

down

speed

of

The rest of variables track the previous states of taster


switch between two subprogram calls (every 10ms). In
addition, debounce taster switch.

Possible future improvements


-

software solution in case of RC oscillator

triggering the triac which can not work in 4


quadrants

realization with serial connection to bulb

adding varistor

adding filter

current measuring and overload protection

better reset circuit

Something for the end


I wish to warn those who may try to assamble this
device. The whole circuit is under HIGH VOLTAGE
and you must be very careful when working. The
safety measures must be taken as well as keeping the
whole device in a grounded box. I suggest to test the
whole thing with small alternative voltage (about 25V,
Reg-sv-1.1E 8/2

Instructions for use


resistors R7, R8 =100K in this case). You can also
power the microcontroller with battery or increase
capacity of C1. First, check if there are 5V for
controller supply, and see if lighting of diode can be
changed. If all this is O.K. then try with an AC line
(220V).
This article is published by mikroElektronika
(http://www.mikroelektronika.co.yu). Reprinting is
granted as long as the entire document package stays
unchanged. (view Readme.txt) The author and
mikroElektronika assume no responsibility for the use
of this application

Microchip, PIC 16F8X data sheet, 1996


Microchip, AN521 - "Interfacing to AC Power
Lines"
3. Motorola, Motorola thyristor device data MAC15A series, rev 1
For all information, suggestion and help you can email
or call:
(mailto:ntoncev@hemo.net)
Tel: ++381 13 812 932, ++381 63 790 556

CAUTION

HIGH VOLTAGE

CON1
3
2
1

1.
2.

Electrical schematics

S2

Literature

C1
phase
zero
light bulbe

47R0.5W

100n240Vac

VDD

D1

R2

1N4007
D2

ZD1

R1

220V AC

120K

47uF/16V
EC1

1N4007

C4
100n

5.1V

gnd
RA1

TRI1
TIC206M

RA2
R8 R7
1M5 1M5

C
VDD

gnd
VDD

S1
taster switch

R3
33k

R6
220

C2
22p

Q1

For testing
U1
16
4
17
18
1
2
3
6

gnd

C1
22p

R4
20k

C3
22n

gnd

4Mhz

OSC1/CLKIN
MCLR
RA0
RA1
RA2
RA3
RA4/T0CKI
RB0/INT

OSC2/CLKOUT
RB1
RB2
RB3
RB4
RB5
RB6
RB7

15
7
8
9
10
11
12
13

RB1
R5
1k

LD1
LED

PIC16F84-04I/P(18)
R10 R9
4M7 4M7

Power supply: VDD i gnd


gnd

VDD gnd

remove in case of software compensation

Projekat

Regulator svetla
Projektovao

Datum

ANEL, Vrsac

28-Oct-1999

Fajl
D:\cuvanje\PROTEL\SCH\SEME\dim\dim.Sch
1

Reg-sv-1.1E 8/3

Instructions for use


Listing
Dim.asm
;**************************************************************************
;
;
;
Light Dimming Regulation
;
;
;
Start date : 28.02.1999.
;
Finish date : 15.03.1999.
;
;
Nikola Toncev B.Sc.
;**************************************************************************
list p=16f84, f=inhx8m,
INCLUDE P16f84.INC
;**************************************************************************
;
Defining: variables in RAM memory
;**************************************************************************
include d:\pic\dim\dimprom.asm
;**************************************************************************
;
Program code
;**************************************************************************
#define
#define
#define

Init

STISAK_KASNJENJE
VREDNOST_MAX .200
VREDNOST_MIN .4

.50

ORG 0000h

; Reset vector.

bcf
clrf
clrf
goto

; hardware initialization
; turn off interrupt
;
;

INTCON,GIE
STATUS
PORTB
InitNext

ORG 0004h
goto

; Interrupt vector.

Main_init

;**************************************************************************
;
Main program
;**************************************************************************
Main_init
movlw
movwf
movlw
movwf
movlw
movwf

0x03
PORTB
VREDNOST_MAX
Vrednost
0xff
Smer

;test call
;
goto

Tastaturamain
test

Reg-sv-1.1E 8/4

Instructions for use


Main_prvi
call
Tastaturamain
btfss PORTA,1
; waiting for histerezis
goto
Main_prvi
; initialization of first half-period
bcf
PORTB,1
clrf
TMR0
Prvapetlja
btfss PORTA,1
; check + half-period
goto
Main_drugi
movf
Vrednost,W
subwf TMR0,W
btfss STATUS,C
; is TMR0-Vrednpost <0 ?
goto
Prvapetlja
;turn on - PALI
bsf
PORTB,1
call
Ukljuci
Prvpetcekaj
btfsc PORTA,1
goto
Prvpetcekaj
bcf
PORTB,1

; + period is still going

Main_drugi
call
Tastaturamain
btfsc PORTA,2
goto
Main_drugi
;initialization of second half-period
bcf
PORTB,1
clrf
TMR0
Drugapetlja
btfsc PORTA,2
goto
Main_prvi
movf
Vrednost,W
subwf TMR0,W
btfss STATUS,C
goto
Drugapetlja
;turn on
bsf
call

PORTB,1
Ukljuci

Drugapetljacekaj
btfss PORTA,2
goto
Drugapetljacekaj
bcf
PORTB,1
goto
Main_prvi

Ukljuci
;software compensation starts here
movlw .150
subwf Vrednost,W
;compensation of histerezis
btfsc STATUS,C
return
;software compensation ends here, this 4 lines can be removed
movlw b'00001111'
movwf PORTB
movlw b'00000000'
bsf
STATUS,RP0
movwf TRISB
bcf
STATUS,RP0
nop
nop

Reg-sv-1.1E 8/5

Instructions for use


nop
nop
nop
nop
nop
movlw
bsf
movwf
bcf
return

b'00000001'
STATUS,RP0
TRISB
STATUS,RP0

;**************************************************************************
;
;
Sub-program for taster switch
;
;
;**************************************************************************
Tastaturamain
movf
PORTA,W
andlw b'00000001' ;filter for zero bit
movwf Sad
;puts in Sad and after that puts in Pre
andwf Pre,W
;this is for taster switch debouncing
movwf Stanje
;
movf
Sad,W
movwf Pre
;if two successive reading of RA0 are logical 1
btfss Juce,0
;taster-switch is pushed, then variable Stanje gets
goto
Tastkraj
;value 1;
btfss Stanje,0
goto
Tastprekinutpritisak
;check push button is released
;The new and previous reading of pushbutton switch are 1, pushbutton is pressed
movlw STISAK_KASNJENJE
subwf Stisak,W
;pushbutton is not released
btfss STATUS,C
;checks how long it was kept pushed
goto
Tastparanput ;even and odd, to slow down counting
;light on
decf
Stisak,F
movlw 0xff
xorwf Speedcontrol,F
movf
Speedcontrol,F
btfss STATUS,Z
goto
Tastparanput
;odd pass, only for slow down
btfss Smer,0
;check the positive direction
goto
Tastnegativansmer
;negative direction
;positive direction
movlw VREDNOST_MAX
subwf Vrednost,W
btfss STATUS,Z
goto
Tastnijemaxdim
;count off, change direction
clrf
Smer
decf
Vrednost,F
Tastnijemaxdim
incf
Vrednost,F
Tastparanput
incf
Stisak,F
Tastkraj
movf
Stanje,W
movwf Juce
Tastend
return
Tastnegativansmer
movlw VREDNOST_MIN
subwf Vrednost,W

Reg-sv-1.1E 8/6

Instructions for use


btfss STATUS,Z
goto
Tastnegativansmerkraj
movlw 0xff
;change direction in case of count off
movwf Smer
incf
Vrednost,F
;negative direction,
Tastnegativansmerkraj
decf
Vrednost,F
goto
Tastparanput
Tastprekinutpritisak
movlw STISAK_KASNJENJE
;taster is released, check time to pass
subwf Stisak,W
;keeping taster switch pressed
btfsc STATUS,Z
;
goto
Tastprekinutpritisakpreskok
movlw VREDNOST_MIN
movwf Vrednost
btfsc Smer,0
goto
Tastprekinutpritisakpreskok
movlw VREDNOST_MAX
movwf Vrednost
Tastprekinutpritisakpreskok
movlw 0xff
xorwf Smer,F
clrf
Stisak
goto
Tastkraj

;**************************************************************************
;
initialization
;**************************************************************************
InitNext

MoreMem

clrf
bsf
movlw
movwf
movlw
movwf
movlw
movwf
bcf
clrf
movlw
movwf

PORTA
STATUS,RP0
b'00000001'
TRISB
b'00011111'
TRISA
b'10000101'
OPTION_REG
STATUS,RP0
TMR0
b'00000000'
INTCON

movlw
movwf
clrf
incf
movlw
xorwf
btfss
goto

0ch
FSR
INDF
FSR,F
4fh
FSR,W
STATUS,Z
MoreMem

goto

Main_init

;
;
; RB1-RB7,are outputs, RB0 input
;
; RA0-RA4 are inputs.
; Configure OPTION register
;
;
; TMR0 = 0.
; Configure INTCON register
; 7-GIE, 6-EEIE, 5-T0IE, 4-INTE
; 3-RBIE, 2-TOIF, 1-INTF, RBIF
;
; clear RAM
;
;
;

;
;

Reg-sv-1.1E 8/7

Instructions for use


END

Dimprom.asm
; date: 28.02.1999.
cblock 0x1c
Vrednost
Speedcontrol
Stisak
Stanje
Juce
Smer
Sad
Pre
endc

Reg-sv-1.1E 8/8

You might also like