Thread: help with code for a dimmer

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    6

    Smile help with code for a dimmer

    Hi im jenifer,

    Im doing a light dimmer with a PIC16f877.
    In my code i need to detect the rising edge of the scare signal thats enterring in my PIC. I don't know how to control the intensity of my light. I know that i need to change the delays to change the intensity.

    Any ideas on how to do all that?

    thanks alot

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, I take it you are using pulse-width modulation?
    What's a "scare-signal"?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    to detect the rising edge can be done by using external interrupt pin...
    you can set it to positive(rising up) or negative(going down) triggering...

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    thanks auralius!! i can detect my rising edge now!!

    Now what do I have to write in my code if I want to change the intensity in my light? I have one output connected to my light

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, what I think you are referring to with your "add delays" is something called pulse-width modulation - it is a digital method of controlling "voltage" by simply giving full voltage for some time, and then no voltage for some time. Depending on the proportion of on-time and off-time, it will appear as if you have a proportion of the full voltage. E.g. we want to give 25% of the full voltage, we have an on-time of 0.25 seconds, and an off-time of 0.75 seconds. Overall that will be 25% of the voltage. [You may want to use shorter times than that - probably best to stay under 20 milliseconds / 50 Hz to avoid flicker effects].

    Basically, what I'm saying is that if you flash (blink) the light quickly enough, you get the effect of a dimmer.

    So, let's say we have 20 milliseconds as our 'time-base', then we could make 25% light by having a 5 ms on-time, and 15 ms off-time. 75% of full light would be achieved by 15ms on, 5 ms off.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    yes you are right mats. thanks!

    I have to put some On, Off and delays it should work!

    something like that would work?

    ..
    Code:
    #INT_EXT
    interruption_EXT()
    {
    	delay_ms(200);
    	output_high(PIN_D1);   //on
    	delay_us(10);
    	output_low(PIN_D1);    //off
    }

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that would give you some 95% duty cycle 。 

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Quote Originally Posted by jenifer85 View Post
    I have to put some On, Off and delays it should work!

    something like that would work?

    ..
    Code:
    #INT_EXT
    interruption_EXT()
    {
    	delay_ms(200);
    	output_high(PIN_D1);   //on
    	delay_us(10);
    	output_low(PIN_D1);    //off
    }
    Of course, you'll need to put that in a loop. And, don't mix-up ms and us. (I'm not familiar with the PIC compiler or these special commands, but the concept looks good.)

    I'm not sure what you're doing with the interrupt input... Turning the dimmed light on/off?

    I assume you have an LED connected? You're not trying to control an AC lamp, right? With an AC lamp, you do need to sync to the 50Hz or 60Hz line frequency, and the interrupt can be used for that purpose. But, then there are hardware considerations that would affect your software design.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    yeah I have an AC lamp connected. thats why im using the interrupt input!

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    mmm i don't understand it's not working..

    Im not able to make it work after multiple attemps..

    Its only flashing the lamp always at the same intensity.. the intensity never change.

    someone can help plz??

    thanks

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you can see it flash, then the timing is far too slow. You need to use timing in the ten-twenty or so millisecond range to ensure that the eye doesn't see the blinking - and if I'm right that you are using pulse-width-modulation, then the idea really is to have it blink on at full effect and then go off again, but done so quickly that the integration circuit that is our eyes will think that it is "not full brightness, but not off either".

    If the idea is not pulse-width-modulation, then I have no idea what you are doing, or how you are supposed to solve it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    6
    yeah Im using the pulse-width-modulation. But when I put faster timing the lamp just don't do anything, it stays off.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by jenifer85 View Post
    yeah Im using the pulse-width-modulation. But when I put faster timing the lamp just don't do anything, it stays off.
    what other delay values have you tried? try 10ms off and 10ms on and see what happens.

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by jenifer85 View Post
    yeah Im using the pulse-width-modulation. But when I put faster timing the lamp just don't do anything, it stays off.
    You said you are using an AC lamp? I.e., an incandescent bulb? Such bulbs have an inductance, as well as a heat-up time, which will prevent them from turning on unless the current is held steady long enough. You can't "flash" an AC lamp at extreme frequencies.

    If the lamp is incandescent, the proper way to control its intensity is by varying the voltage, not pulse-width modulation.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by brewbuck View Post
    You said you are using an AC lamp? I.e., an incandescent bulb? Such bulbs have an inductance, as well as a heat-up time, which will prevent them from turning on unless the current is held steady long enough. You can't "flash" an AC lamp at extreme frequencies.
    the point is not to flash the bulb though. the idea is to have an average voltage that is less than the maximum by varying the duty cycle of the power supply. Also, a light bulb as an inductor is a pretty low-value one. it's essentially an air-core unit with many turns on a very small diameter. I can't imagine the inductance is much more than a few microhenries - an insignificant amount at 50-60Hz. In fact, assuming 1uH (1 microhenry), the reactance of the inductor would be about 3.77e-4 ohms at 60Hz, which is negligible. Electric motors are far more inductive than a light bulb, and very often have their speed controlled by means of pulse-width modulation as a means to vary the average voltage.

    If the lamp is incandescent, the proper way to control its intensity is by varying the voltage, not pulse-width modulation.
    pulse width modulation has the effect of varying the voltage by providing maximum voltage for a controlled duty cycle. assuming 120V, you would have 108V at 90% duty cycle, and only 12V at 10%. pulse width modulation should work just fine for dimming a light. your average wall-mounted light dimmer uses a form of pulse width modulation to accomplish its task, so there's no reason why it wouldn't work in this case.

    the point is to make the pulses short enough and often enough to avoid flashing and flickering.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM