How to make an AC dimmer with PIC12F675 and TRIAC

Published by MKDas on

An dimmer circuit is one of the basic circuit in power electronics people want to make. In this article, lets learn how to make an AC dimmer circuit. In this project, we will use PIC12F675 micro-controller and a TRIAC as the switching device. Lets start!

Disclaimer: Electricity is always dangerous. Skill required to work with electricity. Do work with your own risk. Author will not be responsible for any misuse or harmful act. This website contents is also copyright protected. Anything copied and directly posted in your website and claiming it as yours is prohibited and nonsensical. Author published all the articles as opensource to help you making your project and learning purpose only. Learn and make one for yourself. If need any help feel free to ask the author. Author will be helpful to you. Thanks.

How an AC dimmer circuit works:

There are 3 basic ways to make an AC dimmer circuit.

  • Changing the voltage with VARIAC/variable transformer
  • Using PWM signal
  • Using TRIAC with firing angle control

Variable transformer:

Variable transformers are transformers that can put out differing amounts of voltage from the same input voltage. There are trademarked versions of these transformers and there are versions that are simply sold as variable transformers, some of which are built to custom specifications.

This type of transformer is mostly used in laboratories to change the AC voltage from fixed input voltage. But using this large device is not good sign to make a dimmer for a small load. But if the load is large too, sometimes these are the most suitable.

Using PWM signal:

Sometimes, PWM signals are used in AC dimmers but as this signal generates harmonics and EMI, use of this technique is very few. I’ll discuss about this technique in another article.

PWM signal

Using TRIAC with firing angle control:

For the AC dimmer circuit, TRIAC or SCRs are the best choice for almost any load. Using Thyristor makes the circuit small as well as loss is very low. In this method, the output is regulated controlling the firing angle of the Thyristor which generates an AC signal like this in the output load:

That means, the firing angle can be controlled from 0-180º to deliver 0-100% to the output. This way, almost any AC load can be dimmed according this firing angle. Each time we can set a pulse to trigger the thyristor from the zero-crossing point to get the exact amount of output. The working principle will be more easy to understand if you see this animated image of firing angle control:

Firing angle control of AC waveshape

If we want to deliver the full sine wave to the load then we have to use TRIAC and if we want to deliver only the positive half cycle, then we can use SCR. But in that case, output will be DC rather than AC signal.

Zero-crossing point:

To know more about zero-crossing: Wiki.

Now, lets see what is TRIAC.

TRIAC:

TRIAC is a generic trademark for a three terminal electronic component that conducts current in either direction when triggered. Its formal name is bidirectional triode thyristor or bilateral triode thyristor._wiki

BTA12 TRIAC

You can imaging this as two anti-parallel SCRs in one device. In SCR, the output is triggered only when the input is positive half and in TRIAC, it can trigger in both ways.

In this project, we will use a very common TRIAC: BTA12.

TRIAC driver:

Only using a TRIAC will not solve everything. We have to use a driver for this TRIAC. Yes, we can drive using different ways but using Opto-coupler will be the best way to drive.

We can use MOC3021 for this purpose.

MOC3021 opto-coupler

Now, we have TRIAC and a driver for this TRIAC. But what else we need? Yes, we need to find the zero-crossing point of each cycle of sine wave. That is why, we need a zero-crossing circuit.

Zero-crossing circuit:

There are lots of ways to design this circuit. But I like using transistor to make it simple. As most of the circuits we use transformers so we can detect the zero-crossing point easily using this circuit:

A simple zero-crossing point detector circuit

But in practical I use this concept and modified my circuit in my ways.

Circuit diagram:

Simple dimmer circuit diagram

As you can see, I used only two resistors and a transistor and an extra diode to make the zero crossing circuit.

The other part of the circuit is not so complex at all. The gate driver circuit is very easy one. Although the datasheet provide different values, but after doing more practical works I changed the values which works excellent.

A resistor (100Ω) and a capacitor (0.1µF/400V) in series which is connected in parallel with TRIAC’s MT1 and MT2 pin is a snubber circuit. Snubbers are energy-absorbing circuits used to suppress the voltage spikes caused by the circuit’s inductance when a switch, electrical or mechanical, opens. The most common snubber circuit is a capacitor and resistor connected in series across the switch

snubber circuit is an useful circuit for any switching device. I’ll discuss about this later in another article. But for now, take it in simple way.

Here we used a POT variable resistor which is used as the tuner to tune the output. And a voltage regulator LM7805 gives us a regulated 5V DC supply to run our micro-controller.

Here, we used PIC12F675 8pin micro-controller which is very small and pretty for our project. As there is an Weak-pull up option in each pin of this micro-controller so we used that for our zero-crossing circuit.

How everything working:

The AC supply line is stepped down by a step-down transformer which is giving us 12V AC. Then using a bridge diode we made it into rectified DC signal. From this point, we are detecting the zero-crossing point using a transistor circuit. Then using a diode 1N4007, this rectified signal is connected to a Capacitor of 1000µF then voltage regulator. The regulated 5V is supplying to our MCU.

On the other hand, the AC supply is also supplied to our Load through the TRIAC. A set of circuit is forming the TRIAC circuit. A RC snubber, a gate driver circuit is the assisting part of the TRIAC.

Using the INT hardware interrupt feature of our MCU, the zero-crossing signal is sensed by the MCU. Then a timer interrupt is used to trigger the TRIAC through the Gate driver IC MC3021 keeping a precious timing.

Once the INT is triggered, triggering signal is zero and the timer interrupt starts. After the timer interrupt, trigger pin is high for 200µS then trigger pin is low and timer interrupt is disabled.

Using this basic concept, the dimmer circuit is working. So all we need to use INT hardware interrupt to detect the zero-crossing point. Then using a timer interrupt, we will generate a 200uS pulse after a precious time from that zero-crossing point. This delay is our main control variable to control the output firing angle. So we can tune this variable to get our controlled output.

A potentiometer is used and the voltage is read by ADC of micro-controller. From this value, we can set our controlled variable to get the controlled output.

Coding:

This is the mikroC code for our dimmer circuit. Very simple and easy to understand.

/*******************************************************************************
* Program for, "Simple Dimmer circuit using TRIAC, BTA12 & PIC12F675"          *
* Program Written by_ Engr. Mithun K. Das                                      *
* MCU:PIC12F675; X-Tal: 4MHz (internal); mikroC pro for PIC v7.6.0             *
* Date: 22-06-2020                                                             *
*******************************************************************************/

#define      Trigger     GP1_bit
int TMR_Value = 0;
unsigned int adc_val=0,i=0;

void Interrupt() iv 0x0004 ics ICS_AUTO 
{
   if(INTF_bit)//if there is any INT Interrupt
   {
      INTF_bit = 0;
      Trigger = 0;//reset trigger

      TMR0 = TMR_Value;
      TMR0IE_bit = 1;//enable timer
      TMR0IF_bit = 0;//clear flag
   }
   
   if (TMR0IF_bit)
   {
      TMR0IF_bit = 0;
      Trigger = 1;//set trigger
      Delay_us(200);
      Trigger = 0;
      TMR0IE_bit = 0;//disable timer
   }

}

void main() 
{
   TRISIO  = 0b00000101;// GP0 and GP2 as input, GP1 as output
   GPIO=0x00;
   ANSEL=0x11;//AN0 as ADC    with Fosc/8
   ADCON0=0x01; //AN0 activated
   CMCON = 0x07;//comparator off
   
   GIE_bit=1;// Global Interrupt enable
   PEIE_bit = 1;//periferal INT. enable
   
   OPTION_REG=0x04;//enable weak-pull-ups
   INTCON         = 0xA0; //Timer0 enable
   TMR0                 = 100;
   
   INTE_bit = 1;//enable INT Interrupt
   INTF_bit=0;//clear flag
   INTEDG_bit=0;//falling edge
   
   while(1)
   {
      //read POT
      adc_val=0;
      for(i=0;i<20;i++)
      {
         adc_val+=ADC_Read(0)/4;
      }
      adc_val/=20;
      
      //filter the values
      if(adc_val>255)adc_val=255;
      
      //TMR_Value = adc_val;  // this is for direct switching
            
      //for smooth change      
      if(TMR_Value<adc_val)TMR_Value++;
      else if(TMR_Value>adc_val)TMR_Value--;
      Delay_ms(5);
        
   }//end of while(1)
}//end of void main

Here I’ve used a timer interrupt to generate a trigger pulse at the right time which is started from each zero-crossing point. And the zero-crossing point is sensed by hardware interrupt INT.

I use USB license for mikroC, if you do not have a license your generated file may misbehave. That is why I’ve attached the hex file below so that you can use it in your circuit.

You can download the generated hex file from here.

Before we test the circuit in the real field, we should test it in simulation. Once it is ok in simulation, we can test the code in real-life circuits.

PCB:

A PCB helps a lot in electronic circuit to work with. I’ve designed a PCB for this project. Lets design the PCB:

PCB designing of our dimmer circuit

After designing and printing the PCB, this was the result:

Dimmer circuit

Here I kept a fuse as a protecting device in series with the TRIAC.

Testing with VR controlled dimmer:

As you see the dimmer is working very smoothly and the wave shape is also smooth. Here in my lab, the supply itself is not 100% pure sine wave. Little distortion is there. But its not a problem at all.

Dimmer with button controlled:

If you want to control the dimming or TRIAC firing angle with button then use this circuit:

Here GP4 & GP5 pins are used for tuning purpose. You can omit the VR RV1 for this case.

Coding with button:

/*******************************************************************************
* Program for, "Simple Dimmer circuit using TRIAC, BTA12 & PIC12F675"          *
* Program Written by_ Engr. Mithun K. Das                                      *
* MCU:PIC12F675; X-Tal: 4MHz (internal); mikroC pro for PIC v7.6.0             *
* Date: 22-06-2020                                                             *
*******************************************************************************/

#define      Trigger        GP1_bit
#define      High_button    GP4_bit
#define      Low_button     GP5_bit

bit mask1,mask2;
int TMR_Value = 0;
unsigned int adc_val=0,i=0;

void Interrupt() iv 0x0004 ics ICS_AUTO
{
   if(INTF_bit)//if there is any INT Interrupt
   {
      INTF_bit = 0;
      Trigger = 0;//reset trigger

      TMR0 = TMR_Value;
      TMR0IE_bit = 1;//enable timer
      TMR0IF_bit = 0;//clear flag
   }

   if (TMR0IF_bit)
   {
      TMR0IF_bit = 0;
      Trigger = 1;//set trigger
      Delay_us(200);
      Trigger = 0;
      TMR0IE_bit = 0;//disable timer
   }

}

void main()
{
   TRISIO  = 0b00110101;// GP0 and GP2 as input, GP1 as output
   GPIO=0x00;
   ANSEL=0x11;//AN0 as ADC    with Fosc/8
   ADCON0=0x01; //AN0 activated
   CMCON = 0x07;//comparator off

   GIE_bit=1;// Global Interrupt enable
   PEIE_bit = 1;//periferal INT. enable

   OPTION_REG=0x04;//enable weak-pull-ups
   INTCON         = 0xA0; //Timer0 enable
   TMR0                 = 100;

   INTE_bit = 1;//enable INT Interrupt
   INTF_bit=0;//clear flag
   INTEDG_bit=0;//falling edge

   while(1)
   {

      if(!High_button && mask1) //High button is pressed
      {
         mask1=0;//reset mask
         if(TMR_Value<250)TMR_Value+=5;
         Delay_ms(200);
      }
      if(High_button) mask1 = 1;//set mask
      
            
      if(Low_button && mask2) //Low button is pressed
      {
         mask2=0;//reset mask
         if(TMR_Value>5)TMR_Value-=5;
         Delay_ms(200);
      }
      if(!Low_button)mask2=1;


   }//end of while(1)
}//end of void main


// for eye friendly

You can download the hex file from here.

Conclusion:

This dimmer circuit can be used in AC lamps, Fan motor, etc. Based on load capacity, the TRIAC should be selected. If you use BTA12 in your circuit, it can easily drive up to 500Watts of resistive load and 300Watts of Motor. Keeping a safety factor of 3 is be the best choice for TRIACs because some Transient moments can kill the TRIAC if it can not tolerate it.

I hope this project was helpful to you. If you make one for yourself, it will be a great pleasure for me. Anywhere you need help, let me know. Please share this project and subscribe to my blog. Thank you.


MKDas

Mithun K. Das; B.Sc. in EEE from KUET. Blog: https://labprojectsbd.com

9 Comments

Chandana · July 31, 2020 at 12:07 am

Excellent Article! Many Thanks For Your Hard Works

CHANDRAMOHAN MOHAN · August 25, 2020 at 5:33 pm

Very Good Work…….sir

Makeup · November 1, 2020 at 1:59 am

I like the valuable info you provide to your articles. I bookmark your blog and take a look at once more here frequently. I am somewhat certain be informed lots of new stuff proper right here! Good luck for the next!

Hairstyles · November 3, 2020 at 2:21 am

Greetings! I know this is kind of off topic but I was wondering which blog platform are you using for this site? I’m getting sick and tired of WordPress because I’ve had problems with hackers and I’m looking at options for another platform. I would be fantastic if you could point me in the direction of a good platform.

Joy Majumder · November 29, 2020 at 1:44 pm

Sir please make pic12f675 ir Remote(Fixed Frequency) Control 3 light 1 fan(AC) with button mikro c pro code. I Request .Long time I ti try but fail.

dizi · February 9, 2021 at 10:45 am

Fastidious answers in return of this matter with genuine arguments and explaining all concerning that. Cynthie Rafaello Gratiana Eloisa Walker Yelich

Leave a Reply

Your email address will not be published. Required fields are marked *