PWM: Pulse-Width Modulation
a Wiki quote, for those unfamiliar with PWM:
“Pulse-width modulation (PWM), or pulse-duration modulation (PDM), is a commonly used technique for controlling power to inertial electrical devices, made practical by modern electronic power switches.
The average value of voltage (and current) fed to the load is controlled by turning the switch between supply and load on and off at a fast pace. The longer the switch is on compared to the off periods, the higher the power supplied to the load is.
The PWM switching frequency has to be much faster than what would affect the load, which is to say the device that uses the power. Typically switchings have to be done several times a minute in an electric stove, 120 Hz in a lamp dimmer, from few kilohertz (kHz) to tens of kHz for a motor drive and well into the tens or hundreds of kHz in audio amplifiers and computer power supplies.”
As you probably know, PWM is also used to fade LEDs on and off, without increasing/decreasing the actual voltage. (That’s more fun, but it’s probably bad for the LED. Feel free to let me know in the comments.)
Unfortunately, the NXT doesn’t really regard the lamp in the Light Sensor as an output, so you lack cool features like PWM.
But I didn’t like that.
So, I present: the NXT, now with 100% more PWM!
It took a little bit of playing around to get it working, and it’s still not perfect, but it’s almost done. As Wiki points out,
Some types of light sources such as light-emitting diodes (LEDs), however, turn on and off extremely rapidly and would perceivably flicker if supplied with low-frequency drive voltages. Perceivable flicker effects from such rapid response light sources can be reduced by increasing the PWM frequency. If the light fluctuations are sufficiently rapid, the human visual system can no longer resolve them and the eye perceives the time average intensity without flicker (see flicker fusion threshold).
Currently I have only gotten about 20 levels of PWM (0%, 5%, 10%, 15%, 20%, 25%, 30%, 35%, 40%, 45%, 50%, 55%, 60%, 65%, 70%, 75%, 80%, 85%, 90%, 95%, 100%) to be “sufficiently rapid [so that] the human visual system can no longer resolve them and the eye perceives the time average intensity without flicker”.
Here are two links, they are the two programs I’ve made so far.
The first one simply makes the LED blink faster/slower, depending on how far you turn the motor. (It basically reads the TachoCount from the motor and uses that as wait times in between commands that turn the LED on/off. When the wait value is approaching zero, you essentially get a limited form of PWM: the light is on at 50%.)
The second program uses two motors (for two separate TachoCounts) to individually change the on and off times.
A third program is in the works that will fade the LED in and out automatically, it will be coming soon.
First program, second program.