Need help with varying brightness of LED using PWM

Thread Starter

christineee

Joined Jul 3, 2013
6
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.

Rich (BB code):
' What's a Microcontroller - testing circuit 6.bs2
' Change digitized analog phototransistor measurement to a binary result.
' Turn an LED on and off.
' Dimming LED.

' {$STAMP BS2}
' {$PBASIC 2.5}

valMax         CON        4000
valMin         CON        168

time           VAR        Word
n              VAR        Byte

PAUSE 1000

DO

  GOSUB Rc_Time
  GOSUB Display

Rc_Time:

  HIGH 2
  PAUSE 100
  RCTIME 2, 1, time

Display:

  time = time MAX valMax MIN valMin

  DEBUG HOME, "time = ", DEC5 time
  IF time > (valMax - valMin) / 4 * 3 THEN
    DEBUG CR, "Turn light onn"
  FOR n = 254 TO 179
    PWM 14, n, 100
    NEXT
  ELSEIF time < (valMax - valMin) / 4 THEN
    DEBUG CR, "Turn light off"
    LOW 14
  ENDIF

LOOP
 
Last edited by a moderator:

donpetru

Joined Nov 14, 2008
185
What type of microcontroller you want to use?
Secondly, you said:
100% brightness: dark surrounding
70% brightness: dim surrounding
0%(off) brightness: lighted surrounding
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?
 

Thread Starter

christineee

Joined Jul 3, 2013
6
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?
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.
 

Thread Starter

christineee

Joined Jul 3, 2013
6
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:

Rich (BB code):
' What's a Microcontroller - testing circuit 7.bs2
' Change digitized analog phototransistor measurement to a binary result.
' Turn an LED on and off.
' Switching LEDs to operate on different light level.

' {$STAMP BS2}
' {$PBASIC 2.5}

valMax         CON        4000                  'variable declaration
valMin         CON        168

time           VAR        Word

PAUSE 1000

DO

  GOSUB Rc_Time
  GOSUB Display

Rc_Time:                                        'to charge the capacitor.

  HIGH 2
  PAUSE 100
  RCTIME 2, 1, time

Display:

  time = time MAX valMax MIN valMin

  DEBUG HOME, "time = ", DEC5 time
  IF time > (valMax - valMin) / 4 * 3 THEN
    DEBUG CR, "Turn LED 100% on"                  'when time = more then 3/4 of the "turning led on" value.
    HIGH 14
    LOW 13
  ELSEIF time > (valMax - valMin) / 4 * 2 THEN
    DEBUG CR, "Turn LED 70% onn"                  'when time = more than 2/4 of the "turning led on" value.
    PWM 13, 190, 5000
    LOW 14
  ELSEIF time < (valMax - valMin) / 5 THEN
    DEBUG CR, "Turn LED 0% offf"                  'when time = less than 1/4 of the "turning led on" value.
    LOW 13
    LOW 14
  ENDIF

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:

bountyhunter

Joined Sep 7, 2009
2,512
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.
 

Thread Starter

christineee

Joined Jul 3, 2013
6
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.
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.
 

LDC3

Joined Apr 27, 2013
924
Rich (BB code):
    PWM 13, 190, 5000
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:
Rich (BB code):
    PWM 13, 10, 10
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.
 

Thread Starter

christineee

Joined Jul 3, 2013
6
Rich (BB code):
    PWM 13, 10, 10
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.
How do i go about changing the frequency? I still see the flickering.
Thanks.
 

LDC3

Joined Apr 27, 2013
924
How do i go about changing the frequency? I still see the flickering.
Thanks.
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.
 

Thread Starter

christineee

Joined Jul 3, 2013
6
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.
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.
 
Top