The following shows PWM module APIs available for each platform.
Linux (Ubuntu) |
Tizen (ARTIK530) |
Raspbian (Raspberry Pi) |
NuttX (STM32F4-Discovery) |
TizenRT (ARTIK053) |
|
---|---|---|---|---|---|
pwm.open | X | O | O | O | O |
pwm.openSync | X | O | O | O | O |
pwmpin.setPeriod | X | O | O | O | O |
pwmpin.setPeriodSync | X | O | O | O | O |
pwmpin.setFrequency | X | O | O | O | O |
pwmpin.setFrequencySync | X | O | O | O | O |
pwmpin.setDutyCycle | X | O | O | O | O |
pwmpin.setDutyCycleSync | X | O | O | O | O |
pwmpin.setEnable | X | O | O | O | O |
pwmpin.setEnableSync | X | O | O | O | O |
pwmpin.close | X | O | O | O | O |
pwmpin.closeSync | X | O | O | O | O |
configuration
{Object} Configuration object which can have the following properties.pin
{number} The pin number to use with this PWM object (mandatory configuration).chip
{number} The PWM chip number (only on Linux). Default:0
.period
{number} The period of the PWM signal, in seconds (positive number).dutyCycle
{number} The active time of the PWM signal, must be within the0.0
and1.0
range.
callback
{Function} Callback function.err
{Error|null} The error object ornull
if there were no error.pwmpin
{Object} An instance of PWMPin.
- Returns: {Object} An instance of PWMPin.
Opens PWM pin with the specified configuration.
To correctly open a PWM pin one must know the correct pin number:
- On Linux,
pin
is a number which is0
or1
. - On Tizen,
pin
is a number which is2
. (Only ARTIK530 board support PWM.) - On NuttX, you have to know pin name. The pin name is defined in target board module. For more module information, please see below list.
Example
var pwm = require('pwm');
var config = {
pin: 0,
period: 0.1,
dutyCycle: 0.5
}
var pwm0 = pwm.open(config, function(err) {
if (err) {
throw err;
}
});
configuration
{Object} Configuration object which can have the following properties.pin
{number} The pin number to use with this PWM object (mandatory configuration).chip
{number} The PWM chip number (only on Linux). Default:0
.period
{number} The period of the PWM signal, in seconds (positive number).dutyCycle
{number} The active time of the PWM signal, must be within the0.0
and1.0
range.
- Returns: {Object} An instance of PWMPin.
Opens PWM pin with the specified configuration.
To correctly open a PWM pin one must know the correct pin number:
- On Linux,
pin
is a number which is0
or1
. - On NuttX, you have to know pin name. The pin name is defined in target board module. For more module information, please see below list.
Example
var pwm = require('pwm');
var config = {
pin: 0,
period: 0.1,
dutyCycle: 0.5
}
var pwm0 = pwm.openSync(config);
period
{number} The period of the PWM signal, in seconds (positive number).callback
{Function}err
{Error|null} The error object ornull
if there were no error.
The setPeriod
method allows the configuration of the period of the PWM signal in seconds.
The period
argument must specified and it should be a positive number.
Configuration is done asynchronously and the callback
method is invoked after the
period is configured to the new value or if an error occured.
Example
pwm0.setPeriod(1, function(err) {
if (err) {
throw err;
}
console.log('done');
});
// prints: do
console.log('do');
// prints: done
period
{number} The period of the PWM signal, in seconds (positive number).
The setPeriodSync
method allows the configuration of the period of the PWM signal in seconds.
The period
argument must specified and it should be a positive number.
Configuration is done synchronously and will block till the period is configured.
Example
pwm0.setPeriodSync(1);
// prints: done
console.log('done');
frequency
{integer} In Hz (positive integer).callback
{Function}err
{Error|null} The error object ornull
if there were no error.
The setFrequency
method configures the frequency of the PWM signal.
frequency
is the measurement of how often the signal is repeated in a single period.
Configuration is done asynchronously and the callback
method is invoked after the
frequency is configured to the new value or if an error occured.
Example
pwm0.setFrequency(1, function(err) {
if (err) {
throw err;
}
console.log('done');
});
// prints: do
console.log('do');
// prints: done
frequency
{integer} In Hz (positive integer).
The setFrequencySync
method configures the frequency of the PWM signal.
frequency
is the measurement of how often the signal is repeated in a single period.
Configuration is done synchronously and will block till the frequency is configured.
Example
pwm0.setFrequencySync(1);
// prints: done
console.log('done');
dutyCycle
{number} Must be within the0.0
and1.0
range.callback
{Function}err
{Error|null} The error object ornull
if there were no error.
The setDutyCycle
method allows the configuration of the duty cycle of the PWM signal.
The duty cycle is the ratio of the pulse width in one period.
Configuration is done asynchronously and the callback
method is invoked after the
duty cycle is configured to the new value or if an error occured.
Example
pwm0.setDutyCycle(1, function(err) {
if (err) {
throw err;
}
console.log('done');
});
// prints: do
console.log('do');
// prints: done
dutyCycle
{number} Must be within the0.0
and1.0
range.
The setDutyCycleSync
method allows the configuration of the duty cycle of the PWM signal.
The duty cycle is the ratio of the pulse width in one period.
Configuration is done synchronously and will block till the duty cycle is configured.
Example
pwm0.setDutyCycleSync(1);
// prints: done
console.log('done');
enable
{boolean}callback
{Function}err
{Error|null} The error object ornull
if there were no error.
The setEnable
method can turn on/off the generation of the PWM signal.
If the enable
argument is true
then the PWN signal generation is turned on.
Otherwise the signal generation is turned off.
After enabling/disabling the signal generation the callback
is invoked.
Example
pwm0.setEnable(true, function(err) {
if (err) {
throw err;
}
console.log('done');
});
// prints: do
console.log('do');
// prints: done
enable
{boolean}
The setEnableSync
method can turn on/off the generation of the PWM signal.
If the enable
argument is true
then the PWN signal generation is turned on.
Otherwise the signal generation is turned off.
Example
pwm0.setEnableSync(false);
// prints: done
console.log('done');
callback
{Function}err
{Error|null} The error object ornull
if there were no error.
The close
method closes the PWM pin asynchronously.
The callback
method will be invoked after the PWM device is released.
Example
pwm0.close(function(err) {
if (err) {
throw err;
}
console.log('done');
});
// prints: do
console.log('do');
// prints: done
The closeSync
method closes the PWM pin synchronously.
Example
pwm0.closeSync();
// prints: done
console.log('done');