Need help with varying brightness of LED using PWM

Discussion in 'The Projects Forum' started by christineee, Jul 3, 2013.

  1. christineee

    Thread Starter New Member

    Jul 3, 2013
    6
    0
    I am trying to create a circuit that is able to adjust the brightness of the LED accordingly to different light level. I want the light brightness vary with the surrounding brightness. If the surrounding brightness is low, the light will produce more brightness and vice versa.
    For example:
    100% brightness: dark surrounding
    70% brightness: dim surrounding
    0%(off) brightness: lighted surrounding

    So far i am only able to make the LED turn on/off according to light/dark surrounding. I have also tried to use PWM command but my LED will not vary the brightness but dimming it till it turned off and repeat.

    BTW, i am using pbasic programming.
    Below is the codes i did. I am unable to post the image of my schematic cause i dont know how.

    Code ( (Unknown Language)):
    1.  
    2. ' What's a Microcontroller - testing circuit 6.bs2
    3. ' Change digitized analog phototransistor measurement to a binary result.
    4. ' Turn an LED on and off.
    5. ' Dimming LED.
    6.  
    7. ' {$STAMP BS2}
    8. ' {$PBASIC 2.5}
    9.  
    10. valMax         CON        4000
    11. valMin         CON        168
    12.  
    13. time           VAR        Word
    14. n              VAR        Byte
    15.  
    16. PAUSE 1000
    17.  
    18. DO
    19.  
    20.   GOSUB Rc_Time
    21.   GOSUB Display
    22.  
    23. Rc_Time:
    24.  
    25.   HIGH 2
    26.   PAUSE 100
    27.   RCTIME 2, 1, time
    28.  
    29. Display:
    30.  
    31.   time = time MAX valMax MIN valMin
    32.  
    33.   DEBUG HOME, "time = ", DEC5 time
    34.   IF time > (valMax - valMin) / 4 * 3 THEN
    35.     DEBUG CR, "Turn light onn"
    36.   FOR n = 254 TO 179
    37.     PWM 14, n, 100
    38.     NEXT
    39.   ELSEIF time < (valMax - valMin) / 4 THEN
    40.     DEBUG CR, "Turn light off"
    41.     LOW 14
    42.   ENDIF
    43.  
    44. LOOP
    45.  
     
    Last edited by a moderator: Jul 4, 2013
  2. donpetru

    Active Member

    Nov 14, 2008
    186
    25
    What type of microcontroller you want to use?
    Secondly, you said:
    but in three cases, what is the RMS voltage of the PWM signal and the PWM duty cycle? Have you calculated / measured this values before starting to design software?
     
  3. christineee

    Thread Starter New Member

    Jul 3, 2013
    6
    0
    I am using the Parallax INC (Rev E) microcontroller.
    I am not sure how i am suppose to get the RMS voltage.
    Is it : 0.707 * Vdd = 0.707 * 5 = 3.535V ?
    Also, how do i get the PWM signal?
    As for the duty cycle, 100% brightness = 254 , 70% brightness = 179, 0% brightness = 0
    Sorry for all these questions but its my first time applying PWM command into my board.

    Thanks in advance.
     
  4. christineee

    Thread Starter New Member

    Jul 3, 2013
    6
    0
    I decided to add in another LED for my circuit.
    So when it is in :
    bright surrounding, both pin 14 and 13 will be low.
    dim surrounding, pin 13 will be high and pin 14 will be low.
    dark surrounding, both pin 14 and 13 will be high.

    My new code is as follow:

    Code ( (Unknown Language)):
    1. ' What's a Microcontroller - testing circuit 7.bs2
    2. ' Change digitized analog phototransistor measurement to a binary result.
    3. ' Turn an LED on and off.
    4. ' Switching LEDs to operate on different light level.
    5.  
    6. ' {$STAMP BS2}
    7. ' {$PBASIC 2.5}
    8.  
    9. valMax         CON        4000                  'variable declaration
    10. valMin         CON        168
    11.  
    12. time           VAR        Word
    13.  
    14. PAUSE 1000
    15.  
    16. DO
    17.  
    18.   GOSUB Rc_Time
    19.   GOSUB Display
    20.  
    21. Rc_Time:                                        'to charge the capacitor.
    22.  
    23.   HIGH 2
    24.   PAUSE 100
    25.   RCTIME 2, 1, time
    26.  
    27. Display:
    28.  
    29.   time = time MAX valMax MIN valMin
    30.  
    31.   DEBUG HOME, "time = ", DEC5 time
    32.   IF time > (valMax - valMin) / 4 * 3 THEN
    33.     DEBUG CR, "Turn LED 100% on"                  'when time = more then 3/4 of the "turning led on" value.
    34.     HIGH 14
    35.     LOW 13
    36.   ELSEIF time > (valMax - valMin) / 4 * 2 THEN
    37.     DEBUG CR, "Turn LED 70% onn"                  'when time = more than 2/4 of the "turning led on" value.
    38.     PWM 13, 190, 5000
    39.     LOW 14
    40.   ELSEIF time < (valMax - valMin) / 5 THEN
    41.     DEBUG CR, "Turn LED 0% offf"                  'when time = less than 1/4 of the "turning led on" value.
    42.     LOW 13
    43.     LOW 14
    44.   ENDIF
    45.  
    46. LOOP
    However, now the problem i encounter is that the pin 13 when activated, it keeps flashing/flicker. Which part of my coding did i make a mistake? For the pin 14, there's no such problem.
    Also, by adding that PWM command in my coding, does that make my led in pin 13 to light up to 75% brightness?

    Thanks all in advance.
     
    Last edited: Jul 5, 2013
  5. bountyhunter

    Well-Known Member

    Sep 7, 2009
    2,498
    507
    You could do this with one op-amp and a photoresistor device.

    If you want to use a PWM pulse train to control it, just run it into an RC filter and use the resulting DC level as input to the op amp.
     
  6. christineee

    Thread Starter New Member

    Jul 3, 2013
    6
    0
    Hi. May i know how do i connect the op-amp to my circuit? I am sorry but i have never been taught on how to apply it. And as for the photoresistor, i am not gonna use it as phototransistor would be a better choice for me due to its high sensitivity for light.
     
  7. LDC3

    Active Member

    Apr 27, 2013
    920
    160
    I'm not familiar with your MPU, but I am guessing that this statement in your code is setting a high on pin 13 for 190 ms, followed by a low for 5000 ms. This will repeat until you turn it off. This will turn on the LED for a little under 0.2 seconds and turn it off for 5 seconds. If you want to use the PWM to dim the LED, you will need to use much smaller numbers, such as:
    Code ( (Unknown Language)):
    1.  
    2.     PWM 13, 10, 10
    3.  
    You may see this flicker at 50 Hz, so the numbers will need to be smaller. If you don't see the flickering, then as long as the 2 number add to 20, you can change the numbers to see how bright the LED looks.
     
  8. christineee

    Thread Starter New Member

    Jul 3, 2013
    6
    0
    How do i go about changing the frequency? I still see the flickering.
    Thanks.
     
  9. LDC3

    Active Member

    Apr 27, 2013
    920
    160
    Like I said, the number will need to be smaller. "PWM 13, 8, 8" will give a frequency of 62.5Hz. "PWM 13, 7, 7" will give a frequency of 71.4Hz. You shouldn't see the LED flicker at these frequencies.
     
  10. christineee

    Thread Starter New Member

    Jul 3, 2013
    6
    0
    Sorry for asking, but may i know how do you calculate to get those frequencies?
    And btw, I tried changing from PWM 13, 10, 10 to PWM 13, 9, 9 and so on till PWM 13, 2, 2 i still see the LED flickering just that now the light is very very dim.
     
Loading...