The pwm
module supports PWM(Pulse Width Modulation). Use require('pwm')
to access this module.
An instances of PWM
represents a PWM object.
pin
<number>
The pin number which can support PWM function.
frequency
<number>
The PWM frequency in Hz. Default: 490
Hz
duty
<number>
The PWM duty cycle between 0.0
and 1.0
. Default: 1.0
// Generate 1000 Hz 50% duth PWM signal on the pin 1.var PWM = require('pwm').PWM;var pwm = new PWM(1, 1000, 0.5); // Create the PWM instance with pin 1pwm.start(); // Generate PWM signal// ...pwm.stop(); // Stop PWM signalpwm.close(); // Close PWM port.
Start to generate PWM signal.
// Generate 1000 Hz 30% duth PWM signal on the pin 1.var PWM = require('pwm').PWM;var pwm = new PWM(1, 1000, 0.3); // Create the PWM instance with pin 1pwm.start(); // Generate PWM signal
Stop to generate PWM signal.
// Generate 1000 Hz 50% duth PWM signal on the pin 1.var PWM = require('pwm').PWM;var pwm = new PWM(1, 1000, 0.5); // Create the PWM instance with pin 1pwm.start(); // Generate PWM signaldelay(100); // Wait 100ms.pwm.stop(); // Stop PWM signal
Returns: <number>
PWM frequency of the PWM instance.
Get the PWM frequency.
// Generate 1000 Hz 50% duth PWM signal on the pin 1 and print the frequencyvar PWM = require('pwm').PWM;var pwm = new PWM(1, 1000, 0.5); // Create the PWM instance with pin 1console.log(pwm.getFrequency()); // Print current PWM frequency.
frequency
<number>
PWM frequency of the PWM instance.
Set the new PWM frequency.
// Generate 1000 Hz 50% duth PWM signal on the pin 1 and print the frequencyvar PWM = require('pwm').PWM;var pwm = new PWM(1, 1000, 0.5); // Create the 1000 Hz PWM instance with pin 1pwm.setFrequency(500); // Change the PWM frequency to 500 Hz.
Returns: <number>
PWM duty of the PWM instance.
Get the PWM duty.
// Generate 1000 Hz 50% duth PWM signal on the pin 1 and print the dutyvar PWM = require('pwm').PWM;var pwm = new PWM(1, 1000, 0.5); // Create the PWM instance with pin 1console.log(pwm.getDuty()); // Print current PWM duty.
duty
<number>
PWM duty of the PWM instance.
Set the new PWM duty.
// Generate 1000 Hz 50% duth PWM signal on the pin 1 and print the dutyvar PWM = require('pwm').PWM;var pwm = new PWM(1, 1000, 0.5); // Create the PWM instance with pin 1pwm.setDuty(0.7); // Change the PWM duty to 70%.
Close the PWM port.
// Generate 1000 Hz 50% duth PWM signal on the pin 1.var PWM = require('pwm').PWM;var pwm = new PWM(1, 1000, 0.5); // Create the PWM instance with pin 3pwm.start(); // Generate PWM signal// ...pwm.stop(); // Stop PWM signalpwm.close(); // Close PWM port.