220V AC Lamp dimmer with PIC16F877A and TRIAC

This post shows how to control 220V AC lamp brightness using PIC16F877A microcontroller and one TRIAC. The TRIAC is a three terminal component (terminal 1, terminal 2 and gate) that is used to control the current following to the load. The TRIAC is fired by applying a pulse to the gate.

Hardware Required:

  • PIC16F877A microcontroller
  • BT136 TRIAC  —  datasheet
  • 220V AC lamp
  • LM393 (or LM339) comparator
  • Optocoupler (MOC3020, MOC3021, MOC3022, MOC3023 or equivalent)  —  datasheet
  • 2 x 1N4007 diode (or 1N4001)
  • 2 x 220k ohm resistor
  • 10k ohm potentiometer
  • 2 x 10k ohm resistor
  • 470 ohm resistor
  • 180 ohm resistor
  • 100 ohm resistor
  • 0.01µF capacitor
  • 5V source
  • Breadboard
  • Jumper wires

PIC16F877A dimmer with triac AC lamp dimmer

AC Lamp dimmer with PIC16F877A and TRIAC circuit:
The following image shows project circuit schematic diagram.

PIC16F877A light dimmer BT136 TRIAC 220V AC lamp

All grounded terminals are connected together.

In this project I used the LM393 (dual comparator IC) for zero crossing detection, the LM339 quad comparator IC also can be used. An optocoupler can be used for the same purpose( zero crossing detection) but I think the comparator is much better. The two diodes which are connected between the non-inverting input (+) and the inverting input (-) of the comparator are used to limit the voltage across the 2 pins. The output of the LM393 (or LM339) is an open collector, so I added a 10k ohm resistor between +5V and comparator output (pull-up resistor).
The output of the analog comparator is connected to RB0 pin of the PI16F877A which is also external interrupt pin of the microcontroller (INT).

The optocoupler (MOC302x) is used for firing the BT136 triac, its LED is connected to PIC16F877A RD2 pin through 180 ohm resistor. I chose the value 180 ohm in order to get a current of about 20mA (current passes through the optocoupler LED).

The 10k potentiometer is used to control the firing angle and therefore the brightness of the lamp.

In this project the PIC16F877A runs with crystal oscillator of 20MHz.

AC Lamp dimmer with PIC16F877A and TRIAC C code:
The C code below is for CCS C compiler, it was tested with version 5.051.

The frequency of my AC source is 50Hz which means the period is 20ms, so the half wave period is 10ms = 10000µs.
The resolution of PIC16F877A microcontroller ADC module is 10 bits which means the digital value can vary between 0 and 1023.

I used PIC16F877A external interrupt (INT) to detect the zero-crossing events, so when there is a zero crossing event (falling or rising), the PIC16F877A interrupted and after some delay (angle alpha) it fires the triac.

AC Lamp dimmer with PIC16F877A and TRIAC video:

One comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.