Pulse Width Modulation

Pulse Width Modulation allows you to create an 'average' analog value using only a digital output. It does this by outputting a square wave, where the pulse is high for the a certain proportion of the time. If you want a 'real' analog output, see DAC.

In Espruino, you can perform Pulse Width Modulation using the following function:

analogWrite(PIN, value)

eg. analogWrite(A0, 0.2) (Pulse high 20% of the time, low 80%)

Note: Not all pins are capable of PWM. See the Reference for your board and look for PWM. Some pins are also capable of proper Analog DAC outputs and for these, analogWrite will use the DAC by default.

To force Espruino to use PWM (instead of the DAC) or to specify a certain frequency, use:

analogWrite(PIN, value, { freq : my_freq_in_hz } )

Using PWM

This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.