Adjusting LED brightnes with PWM
Following the my adventure with capturing the RF signal with IR sensor, I had two ways to proceed. Go back to the shop and buy RF sensor. But the community cases(covid19) of Singapore is getting high again. Didn't feel like going out.
Thankfully there is another way to adjust the brightness of a LED. Pulse width modulation. I had a hunch that's how the existing light adjustments worked too(via RF remote) And I already have the necessary circuit items with me.
Adjusting the LED brightness via raspberry pi part 2.
Even though the end goal is to adjust the brightness of LED set with external power source, I set up to adjust the brightness of a single LED powered via raspberry pi GPIO pin it selt.
I configured the new circuit in bread board. It was just one resistor and one LED connected with jumping wires to GPIO pins.
It worked the first time!!!
Now for the complex circuit with external power source. (One LED is a poor application when come to lighting a room) Configured the circuit. Plugged one unit of LEDs we bought early.
And nothing happened!!! Well I am in a familiar ground again.
Had a hard look at the circuit. Looked for the pin layouts. Found out my knowledge of transistors are only valid for some simpler dimension. It's totally different when it comes to PNP and NPN!!!
Rewired the circuit.
Nope. Seems the transistor pin layout was not the only issue.
I started the piscope(It gives a live graph of GPIO volt levels) Well there is a problem there. It's not showing the volt level changes for the pin which circuit was connected.
I changed the GPIO pin. Updated the script. Executed it.
Houston, we have landed!!!
At first configurations I was using software GPIO and it was showing some jitters in the light. That's because the Linux is not a real time OS and it uses scheduling technique to operate in multi process environment. So when come to sending signals via GPIO in a high frequency it's possible to miss some due to scheduling.
But the raspberry pi is capable with hardware PWM and i found a nice library with python interface. It mentioned there are few GPIO pins which are capable driving hardware PWM. I tried with GPIO pin 18 and it worked.
With PWM it adjust the brightness by changing the duty cycle(in 50% duty cycle, one half of it is in hight volt state and other half is in low state, 75% is 3/4 in high and 1/4 in low) Also you can change the frequency. It defines how long is a duty cycle(high+low) in time dimension.
With low frequencies(~sub 100Hz) the blinking is visible to naked eye. When it increases(~100Hz - 1kHz) it's become invisible to naked eye but can be seen from camera. This the frequency range being used by the frequency in embedded circuit the LED set came with.
Bellow is a video recording(high frame rate) of the LED brightness being adjusted via circuit it came with.
But it's possible to further increase(more than 1kHz) the PWM frequency and hide the blinking from even hight frame rate camera captures. Upper bound for raspberry pi hardware PWM is ~20Mhz. I checked with a slow mo video from an iPhone SE 2020, with high frequency camera didn't see any blinking.
Well that's it for adjusting the LED brightness with raspberry pi.