Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

PIC code for triac light dimmer

PIC code for triac light dimmer

PIC code for triac light dimmer

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?

RE: PIC code for triac light dimmer

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

RE: PIC code for triac light dimmer


But the shortcuts are on another computer...

And what Mike says above is very true...

RE: PIC code for triac light dimmer

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.

RE: PIC code for triac light dimmer


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 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 smile

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.

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)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close