Top Banner

of 21

Arduino Controlled Light Dimmer

Jun 02, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/10/2019 Arduino Controlled Light Dimmer

    1/21

  • 8/10/2019 Arduino Controlled Light Dimmer

    2/21

    Switching an AC load with an Arduino is rather simpel: either a mechanicalrelay or a solid state relay with an optically isolated Triac. (if you use an8051 or !C1"#8$$A microcontroller% there is stu& for you too here.'

    !t ecomes a it more tric)y if one wants to dim a mains AC lamp with anarduino: *ust limiting the current through e.g. a transistor is not reallypossile due to the large power the transistor then will need to dissipate%resulting in much heat and it is also not e+cient from an energy use point of,iew.

    -ne way of doing it is through phase control with a Triac: the Triac then isfully opened% ut only during a part of the sinus AC wa,e. This is calledleading edge cutting.

    -ne could let an Arduino *ust open the Triac for a numer of microseconds%ut that has the prolem that it is unpredictale during what part of thesinus wa,e the triac opens and therefore the dimming le,el is unpredictale.-ne needs a reference point in the sinus wa,e.

    #or that a ero crossing detector is necessary. This is a circuit that tells theArduino (or another micro controller' when the sinus/wa,e goes throughero and therefore gi,es a dened point on that sinus wa,e.

    -pening the Triac for a numer of microseconds delay starting from the erocrossing therefore gi,es a predictale le,el of dimming.

    Another way of doing this is y ulse S)ip odulation. 2ith S% one ormore full cycles (sinuswa,es' are transferred to the load and then one ormore cycles are not. Though e&ecti,e% it is not a good way to dim lights asthere is a chance for 3ic)ering. Though it might e tempting% in S oneshould always allow a full sinuswa,e to e passed to the load% not a halfsinus as in that case the load will e fed factually from 4C which is not agood thing for most AC loads. The di&erence etween leading edge cuttingand S is mainly in the software: in oth cases one will need a circuit thatdetects the ero crossing and that can control a triac.

    Such a circuit is easy to uild: The ero crossing is directly deri,ed from therectied mains AC lines ,ia an optocoupler ofcourse/ and gi,es a signale,ery time the wa,e goes through ero. 6ecause the sine wa,e rst goesthrough doule phased rectication% the ero/crossing signal is gi,enregardless whether the sinus wa,e goes up through ero or down throughero. This signal then can e used to trigger an interrupt in the Arduino.

    !t goes without saying that there needs to e a gal,anic separation etween

    the Arduino side of things and anything connected to the mains. #or thosewho do not understand 7gal,anic separation7 it means 7no metal connections7

  • 8/10/2019 Arduino Controlled Light Dimmer

    3/21

    thus /// opto/couplers. 69T% if you do not understand 7gal,anic separation7%maye you should not uild this.

    The circuit pictured here does *ust that. The mains 0;olt ,oltage is led

    through two 5 opto/coupler. The ?@4 in this opto/coupler thusgoes low with a freuency of 100B and the signal on the collector is goinghigh with a freuency of 100B% in line with the sinusoid wa,e on the mainsnet. The signal of the =>5 is fed to an interrupt pin in the Arduino (or othermicroprocessor'. The interrupt routine feeds a signal of a specic length toone of the !- pins. The !- pin signal goes ac) to our circuit and opens the?@4 and a -C

  • 8/10/2019 Arduino Controlled Light Dimmer

    4/21

    no clue or if you are douting aout what you do% chances are you are goingto e 4@A4E

    aterials

    Gerocrossing

    =>5 H0.5 or B11AA1 or !?50% !?51% !?5% ?T;81= (see teIt in the neItstep'

    Fesistor 10) H0.10

    ridge rectier =00 ;olt H0.

  • 8/10/2019 Arduino Controlled Light Dimmer

    5/21

    Dou will nd two pictures for the C6: my rst one% that ! lea,e here fordocumentation purposes and a slightly altered new one. The di&erence isthat ! left out the enerdiode as it is not really necessary and ! ga,e the ?@4

    itJ own (1)' resistor: it is no longer in series with the -ptocoupler% that nowhas a =$0 -hm resistor. ! made the C6 ,ia direct toner transfer and then

  • 8/10/2019 Arduino Controlled Light Dimmer

    6/21

    etched it in a hydrochloric acidBydrogenperoIide ath. There are plenty ofinstructales telling how to do that. Dou can use the attached print design todo the same. opulating the print is uite straightforward. ! used !C feet forthe opto/couplers and the ridge rectier.

    >ote: Dou need #riting for this. #or the direct toner transfer% the printed sideof the printed pdf le% goes directly against the copper layer for transfer.-nce it is transferred% you will e loo)ing at the in) from the other side andthus see the teIt normal again. ! made slight alterations in theC6: !remo,ed the enerdiode and the ?@4 is no longer in series with theoptocoupler.

    ! used a T!C0". That can deli,er = amperes. Keep in mind though that thecopper trac)s of the C6 will not e ale to withstand = Amperes. #or anyserious load% solder a piece of copper installation wire on the trac)s leading

    from the TF!AC to the connectors and on the trac) etween the twoconnectors.

    !n case it is not clear what the inputs are: from top to ottom on the secondpicture:

    L5;olts

    !nterrupt signal (going to 4 on arduino'

    Triac signal (coming from 4< on Arduino'

    Mround

    >-T@:

    !f you ha,e an B11AA1or !? 50% 51 or 5 opto/coupler then you do notneed the ridge rectier. These ha,e two anti/parellel diodes and thus canhandle AC. !t is pin compatile with the =>5% *ust pop it in and solder wire/ridges etween F5 and L and F$ and /. The ?T;81= is notpincompatile

  • 8/10/2019 Arduino Controlled Light Dimmer

    7/21

    The presented circuit is suited for pure resisti,e loads such as incandescentlamps.

    Should you want to use it for inducti,e loads% then a snuer circuit isnecessary. The gure shows the modications for use with !nducti,e loads.ind you% this is not something ! tried as ! *ust wanted to dim lamps% ut it isased on eIamples and theory a,ailale on the internet. Dou would ha,e toadapt the pro,ided C6

    The top gure shows the circuit as is% for dimming a lamp. !t is in all itssimplicity *ust a resistor to trigger the gate ,ia the diac in the optocoupler.

    The ,alue of 1) may e changed as discussed in the teIt efore.

    The ottom gure gi,es an omnipresent circuit for use in inducti,e loads.

    !t consists of an additional resistor and capacitor. The gate current is elow15mA. !f you are using a less sensiti,e triac to control the inducti,e load%reduce the resistor from .=)N to 1.)N% pro,iding more current to dri,e thetriac and increase the capacitor to 00n#. This snuer circuit is there toprotect the triac from the high ,oltage generated from an inducti,e load.

    The feedac) may cause some prolem for non/inducti,e load. The smalllea)age can e signicant enough to turn on small load (for eIample alamp'.

  • 8/10/2019 Arduino Controlled Light Dimmer

    8/21

    There are other snuer circuits% e.g. a resistor and capacitor in seriesdirectly o,er the load

    !f you could care less aout the theory% ut *ust want the software% go to theneIt step

    The way to use an AC dimmerfader is uite simple once you understand theasics:

    !n AC the power fed to the lamp is directly related to the total surface of thesinuswa,e% the lamp can e regulated y only allowing a predictale part ofthat sinuswa,e to 3ow through the lamp.

    As such we need a reference point on that sinus from where we calculatewhen the lamp has to e switched on.

    The easiest reference point to use is the so called 7ero crossing7: themoment that the light goes through ero.

    After each ero crossing there is one full half of the sinuswa,e a,ailale tosend through the lamp.

  • 8/10/2019 Arduino Controlled Light Dimmer

    9/21

    So what the software needs to do is to detect the erocrossing% and thenwait for a set amount of time on that sinuswa,e to switch on the TF!AC.

    There are ma*or net freuencies in the world: 100B in @urope and most of

    Asia and Africa and 10 B in the America7s (and parts of the Cariean'.There are ma*or ,oltages in the world: 110/10; and 0/=0; ut theyare not important for the mathematics here.

    #or ease of use ! will ta)e the 50B freuency as an eIample:

    50B is 50 wa,es per second.

    @ach sinuswa,e thus ta)es 1000ms50O0ms (miliseconds'

    As there are sinuspea)s in a wa,e that means that after e,ery ero

    detection there is a 10ms period that we can regulate.

    Should we ignite the lamp directly at the eginning of that period% the lampwill recei,e full power% should we do it at the end of that 10ms period thelamp will recei,e no ower and should we do it halfway% the lamp will recei,ehalf power.

    As we are using TF!ACs% what the software needs to do is to wait for the eropoint at the sinuscur,e% ta)e note of that and then wait a specied amountof time within that 10ms period to send a pulse to the TF!AC.

    !f it sends that pulse at 5ms% the lamp will only urn at half power.

    !n the Circuit% the ero detection is done y the iphase optocoupler and isa,ailale as the P/signal on the oard.

    There are asically ways for a microcontroller to detect that signal:

    1/a continuous 7polling7 of the Gero Crossing pin

    /using an interrupt to tell the program that there was a ero crossing

    The main di&erence etween the two is that in 7polling7 e,erytime the

    computer goes through it7s main loop it needs to chec) the pin. !f yourprogram is usy doing a lot of other things% it might e too late in chec)ingthe ero crossing pin% while when using an interrupt% it does not matter whatthe program is usy with. The interrupt is sort of 7tapping it on the shoulder7saying QBey loo)% something came up that you need to attend to >-2Q.

    After the ero crossing is detected the program needs to wait for a speciedamount of time and then switch on the TF!AC.

    Also here% that waiting can e done in two di&erent ways

    1/ y issuing a 7wait7 command

  • 8/10/2019 Arduino Controlled Light Dimmer

    10/21

    /y using a timer interrupt

    Again% oth these methods ha,e their pro7s and con7s. The 7wait7 command(7delay7 in Arduino language' literally let7s the computer wait for the reuired

    time and it cant do anything else in the mean time. if the lamp is urning atlow power y letting the computer wait say Rms% that means that e,ery10ms the computer is told to wait Rms: ergo it will e idle R0 of the time.

    That is ne if your controller is only used to control the lamp% ut if it needsto do other stu& then little time is left.

    9sing a timer interrupt sol,es that. 6asically what that does is that theprogram tells the timer: Bey% ! *ust heard we ha,e a ero crossing% ! got todo something else% ut you *ust wait =.5ms and then switch on the TriacQ Sothe program goes on it7s merry way and =.5ms (as an eIample' after it wasgi,en notice there was a 0/crossing% the timer switches on the TF!AC.

    olling: (note this is a rough eIample for illustration of polling% o,iously itneeds some enhancement'

    int ACU?-A4O

  • 8/10/2019 Arduino Controlled Light Dimmer

    11/21

    ,oid setup('

    W

    pinode(ACU?-A4% -9T9T'V Set AC ?oad pin as output

    attach!nterrupt(0% eroUcrosssUint% F!S!>M'V Choose the ero cross interrupt Y

    from the tale ao,e

    X

    What this says is that the interrupt is attached to interrupt 0, it goes to a

    function called "zero_crosss_int" and it reacts to a rising flank on the pin.

    n the !ero_cross_int function that the progra #ups to after the

    interrupt we deterine the tie we need to wait $efore firing the T%A&.

    We will also add a $it of functionality. We don't #ust want one le(el set that

    the lap $urns on, we are going to add soe functionality to regulate the

    light le(el in steps.

    )or that ha(e chosen the fully ar$itrary aount of *+ steps. That

    eans that e(ery step is *0s-*+ *0000us-*+/us 1in fact it is /,

    $ut get to that later2. The total ditie then is calculated fro /31* to

    *+2. The nu$er $etween *4*+, which deterines our le(el of diing,

    we assign to the (aria$le integer 'diing'

    ,oid eroUcrosssUint(' function to e red at the ero crossing to dim the light

    W

    int dimtime O ($5Zdimming'V #or "0B O"5

    delayicroseconds(dimtime'V -& cycle

    digital2rite(ACU?-A4% B!MB'V triac ring

    delayicroseconds(10'V triac -n propagation delay (for "0B use 8.

  • 8/10/2019 Arduino Controlled Light Dimmer

    12/21

    to know for sure the T%A& is on, so we wait *0us. 6ow 1*00004*02-*+

    is still / $ut i found / to work well. )eel free to use / though.

    The only thing then left to do in the ain progra is to set the le(el atwhich we want the lap to $urn:

    ,oid loop(' W

    for (int iO5V i [O 18V iLL'W

    dimmingOiV

    delay(10'V

    X

    What happens here is a siple loop that regulates the lap up in a *+

    steps. ha(e chosen not to start at * $ut at $ecause at that le(el there

    could $e soe tiing issues that could cause the lap to flicker.

    The a$o(e progra is only an e3aple of how to control the lap,

    o$(iously you want to add soe other functionality rather than #ust ha(e a

    lap go up and down in $rightness.

    7sing a tier:

    f you want your progra to $e tie efficient you will need to use an

    interrupt for the zero4crossing detection and a tier to deterine the

    aount of tie to wait.

    %oughly a progra would look as follows:

    nitialize

    8et up the (arious constants and (aria$les you need and include the

    li$raries used 1such as the TierOne 9i$rary2

    8etup

    8etp the pins and the + interrupts

    The zero4crosssing interrupt points to a function and so does the tierinterrupt

  • 8/10/2019 Arduino Controlled Light Dimmer

    13/21

    !ero4cross functie

    8et a $oolean indicating if a zero cross has occurred

    Tier function

    f we regulate the $rightness again in *+ steps, then the tier function is

    set up to $e called whene(er the tie of a step has passed 1e.g. /us2

    and then checks if the nu$er of steps passed is e5ual to the nu$er of

    steps set. f that is the case, the Triac is switched on

    As discussed in the pre(ious theoretical page, the software is fairly easy.

    f you want to de(elop your own software all you need to do is:

    Wait for the zerocrossing

    Wait a specific tie $etween 0 and 00 icroseconds 100*0.0004*02

    switch on yr T%A&

    Wait for a$out *0us 1that is the tie you need to ake sure the T%A& is

    on2

    switch off yr T%A& 1in fact, you only reo(e the triggersignal to the

    T%A&, the T%A& will stay on till the ne3t zerocrossing2

  • 8/10/2019 Arduino Controlled Light Dimmer

    14/21

    #ust $riefly sketch the flow of the progra that used:

    The zero ;4ing signal generates an interrupt.

    At 0> s or >>> u8

    The interrupt routine then switches on the Triac after a specific tie. That

    tie is set in the ain progra loop.

    As the progra (aries the diing fro )ull to Off in *+ steps 1that is

    #ust a choice that was ade, could $e *00 steps as well2, at 0 T1 on the ?eonardo

    < ^ 1 ^ All /6ut it is !>T0 on the ?eonardo

  • 8/10/2019 Arduino Controlled Light Dimmer

    15/21

    18 ^ 5 ^ Arduino ega -nly

    1R ^ = ^ Arduino ega -nly

    0 ^ < ^ Arduino ega -nly

    1 ^ ^ Arduino ega -nly

    0 ^ 0 ^ ?eonardo

    1 ^ < ^ ?eonardo

    $ ^ = ^ ?eonardo

    The Arduino 4ue has no standard interrupt pins as an iterrupt can e attached to

    almosty any pin.

    !n the program pin is chosen

    Z

    int ACU?-A4 O

  • 8/10/2019 Arduino Controlled Light Dimmer

    16/21

    int dimtime O ($5Zdimming'V #or "0B O"5

    delayicroseconds(dimtime'V 2ait till ring the TF!AC

    digital2rite(ACU?-A4% B!MB'V #ire the TF!AC

    delayicroseconds(10'V triac -n propogation delay (for "0B use 8.o longer trigger the TF!AC (the neIt ero

    crossing will swith it o&' TF!AC

    X

    ,oid loop(' W

    for (int iO5V i [O 18V iLL'W

    dimmingOiV

    delay(10'V

    X

    X

    A$out the software: theoretically in the loop you could let (aria$le 'i' start

    fro '0'.

  • 8/10/2019 Arduino Controlled Light Dimmer

    17/21

    found another piece of 8oftware that allows controlling the lap (ia the

    serial port. ha(e not tested it yself yet, $ut see no reason why it

    should not work. t trigger son the falling edge of the zero4crossing signal,

    so the tiing is a $it different, $ut presue that was worked out in this

    software.

    int ACUpin O M from B!MB to

    ?-2% run light procedureE

    X

    ,oid light(' W

    if (Serial.a,ailale('' W

    dim O Serial.read('V

    if (dim [ 1' W

    Turn TF!AC completely -## if dim is 0

    digital2rite(ACUpin% ?-2'V

    X

    if (dim 5=' W Turn TF!AC completely -> if dim is 55

    digital2rite(ACUpin% B!MB'V

    X

    X

    if (dim 0 __ dim [ 55' W

    4imming part% if dim is not 0 and not 55

    delayicroseconds(

  • 8/10/2019 Arduino Controlled Light Dimmer

    18/21

    delayicroseconds(500'V

    digital2rite(ACUpin% ?-2'V

    X

    X

    ,oid loop(' W

    X

    @,en more software [a

    hrefOQhttp:wi)i.dIarts.washington.edugroupsgeneralwi)i=dd"RACU4immerUCirc

    uit.htmlQ relOQnofollowQhere[a

    8tep =: Arduino controlled light dier: The software

    The code $elow has $een confired to work on the 9eonardo

    Z

    AC ?ight Control

    9pdated y Foert Twomey

    Changed ero/crossing detection to loo) for F!S!>M edge rather

    than falling. (originally it was only chopping the negati,e half

    of the AC wa,e form'.

  • 8/10/2019 Arduino Controlled Light Dimmer

    19/21

    Also changed the dimUchec)(' to turn on the Triac% lea,ing it on

    until the eroUcrossUdetect(' turn7s it o&.

    Adapted from s)etch y Fyan c?aughlin

    http:www.arduino.cccgi/inyaDa66.pl`numO1

  • 8/10/2019 Arduino Controlled Light Dimmer

    20/21

    1000000 us 10 B O 8M'V Attach an !nterupt to in

    (interupt 0' for Gero Cross 4etection

    Timer1.initialie(freStep'V !nitialie Timer-ne lirary for the fre

    we need

    Timer1.attach!nterrupt(dimUchec)% freStep'V

    9se the Timer-ne ?irary to attach an interrupt

    to the function we use to chec) to see if it is

    the right time to re the triac. This function

    will now run e,ery freStep in microseconds.

    X

    ,oid eroUcrossUdetect(' W

    eroUcross O trueV set the oolean to true to tell our dimming function

    that a ero cross has occured

    iO0V

    digital2rite(ACUpin% ?-2'V turn o& TF!AC (and AC'

    X

    Turn on the TF!AC at the appropriate time

    ,oid dimUchec)(' W

    if(eroUcross OO true' W

    if(iOdim' W

    digital2rite(ACUpin% B!MB'V turn on light

    iO0V reset time step counter

    eroUcross O falseV reset ero cross detection

    X

  • 8/10/2019 Arduino Controlled Light Dimmer

    21/21

    else W

    iLLV increment time step counter

    X

    X

    X

    ,oid loop(' W

    dimLOincV

    if((dimO18' ^^ (dim[O0''

    incZO/1V

    delay(18'V

    X

    8tep /: Arduino controlled lightdier: result @ e3pansin

    his circyut can also $e used for an %BC i3er, al$eit you need two

    additional T%A& circuits. Dou could use this circuitEF&Cin duplo for that,

    $ut ake sure you use a rando cycle opto4coupler suah as the

    GO&>0+*. Ho not use a zerocrossing Opto4coupler such as the

    GO&>0?*.

    The zero4crossing circuit is ofcourse only needed once.

    perhaps this is still soething for tradition 1call it Old fashioned2 34as

    tree lights that work directly on ++0 1or **02 Iolts. different color

    lap strings in the tree and regulate the with this circuit e3panded with

    two T%Ac circuits

    http:--www.instructa$les.co-id-Arduino4controlled4light4dier4The4

    circuit-Jstep0

    http://www.instructables.com/id/Small-Triac-Switch/http://www.instructables.com/id/Arduino-controlled-light-dimmer-The-circuit/#step0http://www.instructables.com/id/Arduino-controlled-light-dimmer-The-circuit/#step0http://www.instructables.com/id/Arduino-controlled-light-dimmer-The-circuit/#step0http://www.instructables.com/id/Arduino-controlled-light-dimmer-The-circuit/#step0http://www.instructables.com/id/Small-Triac-Switch/