I am reading shadows cast by an LED against a linear CDD. To make sure the pixels don't get overexposed, I have a calibration function that will raise the brightness of the LED by PWM until the reading reaches a given amount. This way the combination of ambient light and LED brightness give an ideal range of light.
The exposure time of the sensor is detemined by how long it takes to send 238 clocks though it. This could be considered equivalent to a shutter speed. Because the LED is flickering due to PWM, the reading can vary widely depending on how the PWM cycle and the sensor exposure line up. I've tried using the Timer1 library to set the period of the PWM cycle, and reset the PWM for the LED before each sensor read, but the results are inconsistent no matter how I try to sync the PWM cycle with the sensor read cycle.
Is there a way to smooth the PWM into a steady voltage at the LED, maybe using a capacitor or some other method?
regor2:
You should increase the frequency and put a small capacitor between the signal and ground.check here
Bad reference - on two counts
One is that the suggestion of using a capacitor is simply wrong, but the other is that a motor as such is somewhat inductive (especially if it has series field windings) and using a series inductance is the right answer!
A series inductor - and a "flyback" diode between the driver output and the load supply terminal (not across the load itself) essentially turns this into a switchmode converter which is exactly what you want. You can then improve the smoothing further with a capacitor across the load.
In the case of using PWM to generate an analog voltage, you generally use a resistor-capacitor filter for smoothing, given that the load is a relatively high impedance.
Setting the PWM frequency for Pin 9 (Timer1) to a divisor of 1 worked, making the frequency 31250 Hz. There is some variation from one reading to the next, but in testing with sunlight I found that the range of variation increases as the amount of light measured increases. With sunlight the amount of variation stayed fairly constant, while with PWM it was all over the place. Setting the frequency to 31250 made it respond to the LED more like sunlight.
If PWM for a given pin doesn't normally run at the base frequency, what is its default? If the PWM were directed at a motor, is there a benefit to increasing the frequency in that case?