Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
31 lines (19 sloc) 1.35 KB

Pulse Width Modulation

⚠️ Please view the correctly rendered version of this page at Links, lists, videos, search, and other features will not work correctly when viewed on GitHub ⚠️

  • KEYWORDS: Peripheral,Peripherals,Pulse Width Modulation,PWM,Pulse,Analog,Built-In

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