Monday, February 13, 2012

Gadgeteer PWM

Pulse width modulation is a digital "trick" to fool analogue devices into thinking that a certain level of voltage is present on an otherwise digital IO pin.

Essentially, the system has a frequency, we'll say 100 hertz for argument's sake, and if the output pin remains high for the entire 100th of a second, that can be considered as the maximum output of the pin.
If however it remains high for only 50% of the time and is low the rest of the time than we can consider that the output is at 50% power.

Changing the pulse width within the 100th of a second time slice enables us to create varying levels of power and even synthesize waveforms if we so desire.

PWM on the gadgeteer is ridiculously simple. There are a number of ports that are PWM enabled. For example, the FEZ Spider has PWM on socket number 8 amongst others.

To set-up PWM on any PWM enabled pin we simply need to declare the PWM controller like so:
private PWMOutput pwm = null;

Then in "ProgramStarted" get the PWMOutput controller:

pwm = this.extender.SetupPWMOutput(Gadgeteer.Socket.Pin.Nine);

Then, to set the power of the pulses:

           
pwm.Set(x,p);

where X is the overall base frequency of the PWM signal and p is the value 0-100 of the percentage of power to set.


The video below shows Pulse Width Modulation in action on my oscilloscope.



I wired some LEDS up to the Gadgeteer and they changed brightness like so:








No comments: