Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

PIC code for triac light dimmer 1

Status
Not open for further replies.

fuseshut

Electrical
Oct 16, 2005
76
0
0
US
I am learning how to program a PIC. DOes anyone know where I can find sample code to learn from for a light dimmer project?
 
Replies continue below

Recommended for you

Start with something that doesn't involve line voltage, e.g. dimming an LED or an incandescent bulb run from a DC supply.

Post mortems are easier that way.



Mike Halloran
Pembroke Pines, FL, USA
 
The vendors supply code examples. Look under PWM or frequency modulation.

Ditto for the warnings of zeitghost and MikeHalloran.

You were asking for SW so I assume you know what your HW control and feedback mechanisms are?

You need to build a one shot timer. A PIC would be an interesting way of doing that.

zeithost has supplied an excellent detailed answer for you. Please, let us know if you need more info.
 
Hi.

Please accept the following personal-opinions or tips as a guide to help you understand and seek further researched and reliable references to this popular subject.

When dealing with mains, you should get assistance and perhaps get your circuit checked before powering it up and if available, use an AC supply running from isolated mains sources to reduce the risk of shock.
Always make sure someone knows you are experiementing with mains (in your room??)

The PIC micro can indeed be used to control a light dimming circuit.

You will need to have a good understanding of how your particular electronic circuit or system works. Then you can decide how you are going to drive it based on some intelligence input to your PIC.

The PIC provides all the 'hardware' (timers, i/o control pins under the control of your software). For Assembler programmers, each family of PIC PIC12xxx PIC14xxx .....PIC18xxx has what is called an Instruction Set for the appropriate RISC architecture. This is like a list of commands to control the hardware and manipulate data or signals to and from the PIC.


What your system needs to do is to be able to determine roughly where in the mains cycle the ac waveform is. Some electronic interface (resistive potential divider network **) interfacing the mains to your PIC. You can imagine this as a sinewave with 0 volts at t=0. Then we can determine the magnitude at any time using formula (1) below;

v = Vmax.Sin(w.t) - (1) note: w = omega

also, w = 2.pi.f - (2) note: pi = 3.14

_ f = 50Hz
/ \
/ \
| | |
t0 t1 /
\ /
-

Recall (from calculus) that 'the area under curve' of a sinusoid is INTEGRAL of Sin(x) with limits at t0 and t1.

So INTEGRAL = [INTEGRATE Sin(t1) - INTEGRATE Sin(t0)]

So if the switching on of the triac can be delayed by a finite time (by up to or >9mS)from the point of zero crossing (when t0=0), we can control the amount of power delivered to the lamp and so, fewer photons come out of the lamps' filament as t0 tends to t1, away from t=0 :)


The micro needs to be configured with;

1) Zero Cross detection / input pin (preferably to an interrupt pin (RB0). **

2) Triac Switch output(or what ever is actually switching the power to the light) **

3) i/o switches as input from Man Machine Interface, to increase and decrease brightness of the lamp.

You would usually use one of the PIC timers to start as soon as a zero cross is detected and when the timer overflows, you fire the triac - which automatically cuts out when the mains waveform reaches a minimum voltage (component dependent - several volts?).

The values you load into your timers depend on what oscillator frequecy you are running your PIC.

You could then get the PIC to poll a keypad (MMI) to change the brightness of the lamp.

Conclusion:
Once you understand the problem, draw a flow chart of your software.

Then write a Pseudo-Code of you program;

Intialize peripherals (timers, ports)
Wait until zero cross occurs.
When it does, start timer.
Wait until timer overflows.
when it does, turn on triac
wait several micro-seconds until Triac latches
Poll the MMI pins.
If pressed, change timer overflow value accordingly up to maximum value -


You can see that the 'software' is picking up shape already (in the form of the above Pseudo-code)and the rest is down to becoming familiar with the Instruction set of the PIC you're using and translating the pseudo-code into a PIC program.

Obviously, many poeople are using C-compilers these days and so Assembly may not be the way to go.

Try the PICs home for Student edition compilers and lots of examples and free development tools:-



Hope this helps.
Good luck!

____________________
**(Remember, this involves connecting pic i/o pin through appropriate electonics - seek advice)
 
Status
Not open for further replies.
Back
Top