---
+ Post New Thread
Results 1 to 18 of 18
  1. #1
    Junior Member level 1
    Points: 566, Level: 5

    Join Date
    Jan 2010
    Location
    dar es salaam
    Posts
    16
    Helped
    0 / 0
    Points
    566
    Level
    5

    Microcontroller based light dimmer using mikro c codes

    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);
    }
    }

  2. #2
    Full Member level 5
    Points: 1,903, Level: 10
    demetal's Avatar
    Join Date
    May 2011
    Location
    Kingdom Of Kochi
    Posts
    275
    Helped
    19 / 19
    Points
    1,903
    Level
    10

    Re: Microcontroller based light dimmer using mikro c codes

    you have set here a PWM coding.... u need additional circuit to do this.....

    try this forum

    Control the dimming of lights using microcontroller



  3. #3
    Junior Member level 1
    Points: 566, Level: 5

    Join Date
    Jan 2010
    Location
    dar es salaam
    Posts
    16
    Helped
    0 / 0
    Points
    566
    Level
    5

    Re: Microcontroller based light dimmer using mikro c codes

    what addition circuit do I need,



    •   Alt11th June 2011, 12:38

      advertising

        
       

  4. #4
    Administrator
    Points: 58,311, Level: 58
    Awards:
    3rd Helpful Member
    bigdogguru's Avatar
    Join Date
    Mar 2010
    Location
    Southwest, USA
    Posts
    8,305
    Helped
    2155 / 2155
    Points
    58,311
    Level
    58
    Blog Entries
    3

    Re: Microcontroller based light dimmer using mikro c codes

    Can you post a schematic of your circuit and the entire code listing?



  5. #5
    Junior Member level 1
    Points: 566, Level: 5

    Join Date
    Jan 2010
    Location
    dar es salaam
    Posts
    16
    Helped
    0 / 0
    Points
    566
    Level
    5

    Re: Microcontroller based light dimmer using mikro c codes

    [IMG]C:\Users\user\Danyel\Xul\Project\Proj.Rep\report\c kt coloured.pdf[/IMG]
    and codes are as shown above in my first comment.



  6. #6
    Administrator
    Points: 58,311, Level: 58
    Awards:
    3rd Helpful Member
    bigdogguru's Avatar
    Join Date
    Mar 2010
    Location
    Southwest, USA
    Posts
    8,305
    Helped
    2155 / 2155
    Points
    58,311
    Level
    58
    Blog Entries
    3

    Re: Microcontroller based light dimmer using mikro c codes

    Where's

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

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

    [IMG]C:\Users\user\Danyel\Xul\Project\Proj.Rep\report\c kt coloured.pdf[/IMG]



  7. #7
    Junior Member level 1
    Points: 566, Level: 5

    Join Date
    Jan 2010
    Location
    dar es salaam
    Posts
    16
    Helped
    0 / 0
    Points
    566
    Level
    5

    Re: Microcontroller based light dimmer using mikro c codes



    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);
    }
    }



  8. #8
    Full Member level 5
    Points: 1,903, Level: 10
    demetal's Avatar
    Join Date
    May 2011
    Location
    Kingdom Of Kochi
    Posts
    275
    Helped
    19 / 19
    Points
    1,903
    Level
    10

    Re: Microcontroller based light dimmer using mikro c codes

    Quote Originally Posted by danyel View Post
    what addition circuit do I need,
    Its already in that forum link... You have to refer it...



    •   Alt11th June 2011, 17:32

      advertising

        
       

  9. #9
    Junior Member level 1
    Points: 566, Level: 5

    Join Date
    Jan 2010
    Location
    dar es salaam
    Posts
    16
    Helped
    0 / 0
    Points
    566
    Level
    5

    Re: Microcontroller based light dimmer using mikro c codes

    is there any mikro c codes instead of adding more circuits



  10. #10
    Junior Member level 1
    Points: 566, Level: 5

    Join Date
    Jan 2010
    Location
    dar es salaam
    Posts
    16
    Helped
    0 / 0
    Points
    566
    Level
    5

    Re: Microcontroller based light dimmer using mikro c codes

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



  11. #11
    Administrator
    Points: 58,311, Level: 58
    Awards:
    3rd Helpful Member
    bigdogguru's Avatar
    Join Date
    Mar 2010
    Location
    Southwest, USA
    Posts
    8,305
    Helped
    2155 / 2155
    Points
    58,311
    Level
    58
    Blog Entries
    3

    Re: Microcontroller based light dimmer using mikro c codes

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

    Dimming AC Incandescent Lamps Using A PIC10F200/TRIAC

    Appnote in PDF Form and Source Code.



  12. #12
    Junior Member level 1
    Points: 566, Level: 5

    Join Date
    Jan 2010
    Location
    dar es salaam
    Posts
    16
    Helped
    0 / 0
    Points
    566
    Level
    5

    Re: Microcontroller based light dimmer using mikro c codes

    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.



  13. #13
    Administrator
    Points: 58,311, Level: 58
    Awards:
    3rd Helpful Member
    bigdogguru's Avatar
    Join Date
    Mar 2010
    Location
    Southwest, USA
    Posts
    8,305
    Helped
    2155 / 2155
    Points
    58,311
    Level
    58
    Blog Entries
    3

    Re: Microcontroller based light dimmer using mikro c codes

    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 by bigdogguru; 18th June 2011 at 04:37.



  14. #14
    Junior Member level 1
    Points: 566, Level: 5

    Join Date
    Jan 2010
    Location
    dar es salaam
    Posts
    16
    Helped
    0 / 0
    Points
    566
    Level
    5

    Re: Microcontroller based light dimmer using mikro c codes

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



  15. #15
    Administrator
    Points: 58,311, Level: 58
    Awards:
    3rd Helpful Member
    bigdogguru's Avatar
    Join Date
    Mar 2010
    Location
    Southwest, USA
    Posts
    8,305
    Helped
    2155 / 2155
    Points
    58,311
    Level
    58
    Blog Entries
    3

    Re: Microcontroller based light dimmer using mikro c codes

    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.



  16. #16
    Advanced Member level 5
    Points: 35,749, Level: 46
    Tahmid's Avatar
    Join Date
    Jun 2008
    Location
    Cornell University, Ithaca, New York, USA (from Dhaka, Bangladesh)
    Posts
    4,730
    Helped
    1697 / 1697
    Points
    35,749
    Level
    46
    Blog Entries
    19

    Re: Microcontroller based light dimmer using mikro c codes

    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.



  17. #17
    Junior Member level 1
    Points: 566, Level: 5

    Join Date
    Jan 2010
    Location
    dar es salaam
    Posts
    16
    Helped
    0 / 0
    Points
    566
    Level
    5

    Re: Microcontroller based light dimmer using mikro c codes

    Quote Originally Posted by Tahmid View Post
    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,



  18. #18
    Advanced Member level 5
    Points: 35,749, Level: 46
    Tahmid's Avatar
    Join Date
    Jun 2008
    Location
    Cornell University, Ithaca, New York, USA (from Dhaka, Bangladesh)
    Posts
    4,730
    Helped
    1697 / 1697
    Points
    35,749
    Level
    46
    Blog Entries
    19

    Re: Microcontroller based light dimmer using mikro c codes

    Yes, you need to change your code. Have a look here:

    mikroElektronika &bull; View topic - Triac Control
    mikroElektronika &bull; View topic - Touchdimmer

    In hardware, you have to add a zero cross detector. Check here:
    Zero Cross Detector circuit and program using AVR?

    Another approach is pulse skipping modulation (PSM), but you would still need to make the same changes to the hardware and make changes to the software as well.

    Hope this helps.
    Tahmid.



+ Post New Thread
Please login