The PWM API supports writing analog values to pins using Pulse Width Modulation. Usually PWM is used for controlling LEDs, fans, vibration, etc.
PWM is characterized by a repeating digital signal of given pulse width (duration for value 1 in normal polarity and for value 0 in reverse polarity) and a total duration of the signal (period). Also, PWM is characterized by a duty cycle that is the ratio between the pulse width and the total signal period. For instance, a LED that is driven with a PWM signal with 50% duty cycle will be approximately half-bright.
The term "channel" is used as the numeric index of a PWM pin relative to the PWM controller, as described in the board documentation.
PWM functionality is exposed by an object that can be obtained by using the pwm()
method of the Board
API. See also the Web IDL. The API object exposes the following method:
Method | Description |
---|---|
open() |
synchronous open |
Configures a PWM pin using data provided by the options
argument. It runs the following steps:
- If
options
is a string or number, create a dictionaryinit
and use the value ofoptions
to initialize theinit.pin
property. - Otherwise if
options
is a dictionary, letinit
beoptions
. It may contain the followingPWM
properties, but at leastpin
pin
for board pin name, or PWM channel number, as defined by the board documentationreversePolarity
, with a default valuefalse
.
- If any of the
init
properties is specified, but has invalid value on the board, throwInvalidAccessError
. - Let
pwm
be thePWM
object representing the pin identified by theinit.pin
and request the underlying platform to initialize PWM for the given pin. In case of failure, throwInvalidAccessError
. - Initialize the
pwm.pin
property withinit.pin
. - Initialize the
pwm.reversePolarity
property withinit.reversePolarity
. - Return the
pwm
object.
Represents the properties and methods that expose PWM functionality. The PWM
object extends the Pin
object.
Property | Type | Optional | Default value | Represents |
---|---|---|---|---|
pin |
String or Number | no | undefined |
board name for the pin, or channel number |
reversePolarity |
boolean | yes | false |
PWM polarity |
write() |
function | no | defined by implementation | set and enable PWM signal |
stop() |
function | no | defined by implementation | stop the PWM signal |
close() |
function | no | defined by implementation | release the pin |
Method | Description |
---|---|
write() |
set and start a PWM signal |
stop() |
stop the PWM signal |
close() |
close the pin |
The pin
property inherited from Pin
can take values defined by the board documentation, either a pin name, or a channel number.
The reversePolarity
property tells whether the PWM signal is active on 0. The default value is false
.
Performs a synchronous write operation to define and enable the PWM signal. The argument value
MUST be a dictionary defined here.
Property | Type | Optional | Default value | Represents |
---|---|---|---|---|
period | double | no | undefined |
the total period of the PWM signal in milliseconds |
pulseWidth | double | no | undefined |
PWM pulse width in milliseconds |
dutyCycle | long | no | undefined |
the PWM duty cycle |
The write(value)
method runs the following steps:
- The argument
value
MUST have at least 2 properties specified:period
andpulseWidth
, orperiod
anddutyCycle
, orpulseWidth
anddutyCycle
. The third property ofvalue
is calculated based on the other two. If all properties are specified,dutyCycle
is recalculated based on the value ofperiod
andpulseWidth
.
- If
value
has invalid values for the given board, throwInvalidAccessError
. - Set up and enable the PWM signal based on
value
.
Disables the PWM signal on the pin. A new invocation of write()
is needed to restart the signal.
Called when the application is no longer interested in the pin. It invokes the stop()
method, then releases the pin.
try {
var pwm = require("board").pwm();
var pwm6 = pwm.open(6); // configure pin 6 as PWM
pwm6.write({ period: 2.5, pulseWidth: 1.5 }); // duty cycle is 60%
console.log("PWM duty cycle: " + pwm6.dutyCycle);
setTimeout(function(){
pwm6.stop(); // stop the PWM signal
pwm6.close();
}, 2000);
}.catch (error) {
console.log("PWM error: " + error.message);
};