Microcontroller based light dimmer using mikro c codes

Status
Not open for further replies.

danyel

Junior Member level 1
Joined
Jan 29, 2010
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
dar es salaam
Activity points
1,371
please help me, I have to Dim an AC lamp using Micro-controller, but insteady of dimming, it is blinking, my circuit connection involves PIC,zero crossing detector and lamp connection. and am using mikro c codes below. where is the problem
can you help me please.....

unsigned Duty=0;
void InitMain()
{
TRISC=0x00;
PORTC=0x00;
PWM1_Init(5000);
}
void main()
{
InitMain();
//PWM1_Start();
while(1)
{
if (Duty==0)
{
PWM1_Start();
for (Duty=0;Duty<=255;Duty++)
{
PWM1_Set_Duty(Duty);
//Delay_ms (50);
}
}
Duty=255;
if (Duty==255)
{
PWM1_Start();
for (Duty=255;Duty>=0;Duty--)
{
PWM1_Set_Duty(Duty);
//Delay_ms (50);
}
}
 

Where's

PWM1_Start();
PWM1_Set_Duty();
PWM1_Init();

And this is going to help me much, an image file sitting on your computer:

 



and the code is as follows
unsigned Duty=0;
void InitMain()
{
TRISC=0x00;
PORTC=0x00;
PWM1_Init(5000);
}
void main()
{
InitMain();
//PWM1_Start();
while(1)
{
if (Duty==0)
{
PWM1_Start();
for (Duty=0;Duty<=255;Duty++)
{
PWM1_Set_Duty(Duty);
//Delay_ms (50);
}
}
Duty=255;
if (Duty==255)
{
PWM1_Start();
for (Duty=255;Duty>=0;Duty--)
{
PWM1_Set_Duty(Duty);
//Delay_ms (50);
}
}
 

is there any mikro c codes instead of adding more circuits
 

hey guys, Do Triac accept Pulses From PIC directly???
if not how can I solve it in order for the triac to accept it
 

Here's a Microchip appnote discussing the use of a TRIAC with PIC interface:

**broken link removed**

Appnote in PDF Form and Source Code.
 

thanks so much,
please,can you help mikro c codes. i want to use different duty cycles and it is controlled by one push button.
 

While I own MikroC Pro, I rarely use it except for legacy code maintenance.

However, MikroC has a PWM library with a fairly detailed help system and plenty of examples.

Here's an example which is similar to your specifications:

The example changes PWM duty ratio on RC1 and RC2 pins continually. If LED is connected to these pins, you can observe the gradual change of emitted light.

Code:
unsigned short current_duty, old_duty, current_duty1, old_duty1;

void InitMain() {
  ANSEL  = 0;                         // Configure AN pins as digital
  ANSELH = 0;
  C1ON_bit = 0;                       // Disable comparators
  C2ON_bit = 0;
  
  PORTA = 255;
  TRISA = 255;                        // configure PORTA pins as input
  PORTB = 0;                          // set PORTB to 0
  TRISB = 0;                          // designate PORTB pins as output
  PORTC = 0;                          // set PORTC to 0
  TRISC = 0;                          // designate PORTC pins as output
  PWM1_Init(5000);                    // Initialize PWM1 module at 5KHz
  PWM2_Init(5000);                    // Initialize PWM2 module at 5KHz
}

void main() {
  InitMain();
  current_duty  = 16;                 // initial value for current_duty
  current_duty1 = 16;                 // initial value for current_duty1

  PWM1_Start();                       // start PWM1
  PWM2_Start();                       // start PWM2
  PWM1_Set_Duty(current_duty);        // Set current duty for PWM1
  PWM2_Set_Duty(current_duty1);       // Set current duty for PWM2

  while (1) {                         // endless loop
    if (RA0_bit) {                    // button on RA0 pressed
      Delay_ms(40);
      current_duty++;                 // increment current_duty
      PWM1_Set_Duty(current_duty);
     }

    if (RA1_bit) {                    // button on RA1 pressed
      Delay_ms(40);
      current_duty--;                 // decrement current_duty
      PWM1_Set_Duty(current_duty);
     }

    if (RA2_bit) {                    // button on RA2 pressed
      Delay_ms(40);
      current_duty1++;                // increment current_duty1
      PWM2_Set_Duty(current_duty1);
     }

    if (RA3_bit) {                    // button on RA3 pressed
      Delay_ms(40);
      current_duty1--;                // decrement current_duty1
      PWM2_Set_Duty(current_duty1);
     }

    Delay_ms(5);                      // slow down change pace a little
  }
}


You'll have to change the port configuration according to your design, i.e., what pins your switches and led are attached.

Hope the example helps, check the MikroC compiler help system for details of their PWM library.

Ciao
 
Last edited:

sorry,i have tested it but push buttons are not working to change duty cycle ,
I have put button into port A
 

I've tested the code on an EasyPIC 5 board and it performed as expected. Please post or upload your code?

Did you test the code in simulation or actual hardware? If hardware, what model PIC are you using?

Did you disable the ADCs or Comparators that are usually active by default on PORTA?

If not this is most likely the problem.
 

Hi,
I don't get what you're doing here. Why are you using PWM? You have to use phase angle control or pulse skipping. If you use phase angle control, you can't use the opto-triac that you used, you have to use a regular opto-triac such as MOC3021.

Why are you using 5kHz? You have to fire the triac with respect to the line frequency (50 Hz or 60 Hz). You have to sense the zero-crossing, and then decide how much time to wait before firing the triac and you repeat this the next cycle.

Assuming you're using an MOC3021 or something similar, when you set PWM duty cycle (the traditional DC-DC controlling PWM) at 20%, the output is high for the first 2ms and low for 8ms. But as soon as the output goes high, the MOC3021 fires the triac, which gets latched (on) until the next zero crossing.
Take any duty cycle and you'll see that the lamp will always light upto full brightness.

What you have to do is, if you need 20% brightness:
Half period is 10ms. So, your output needs to be on for 20% first half, then 20% second half. So, that means 80% off, this 80% is "cut" through phase control. Wait for 80% of the half-time, ie, 8ms and THEN send a high pulse to the MOC for about 100us and then turn the pulse low. Repeat this for the second half as well, and THEN you will get 20% brightness.

Take a look at this:
Phase fired controllers - Wikipedia, the free encyclopedia


Hope this helps.
Tahmid.

---------- Post added at 17:00 ---------- Previous post was at 16:57 ----------

Plus, you really need to use a resistor between the LED and mains. Add a 100k resistor. Add a resistor between the opto-triac output and the triac gate.
 




thank you for your support
so do I need to change codes for this to happen(if yes which code do I have to change) or I have to change hardware parts,
 


Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…