Kaluma
Search…
PWM
The pwm module supports PWM(Pulse Width Modulation). Use require('pwm') to access this module.

Class: PWM

An instances of PWM represents a PWM object.

new PWM(pin[, frequency[, duty]])

  • pin <number> The pin number which can support PWM function.
  • frequency <number> The PWM frequency in Hz. Default: 490Hz
  • duty <number> The PWM duty cycle between 0.0 and 1.0. Default: 1.0
1
// Generate 1000 Hz 50% duth PWM signal on the pin 1.
2
const {PWM} = require('pwm');
3
const pwm = new PWM(1, 1000, 0.5); // Create the PWM instance with pin 1
4
pwm.start(); // Generate PWM signal
5
// ...
6
pwm.stop(); // Stop PWM signal
7
pwm.close(); // Close PWM port.
Copied!

pwm.start()

Start to generate PWM signal.
1
// Generate 1000 Hz 30% duth PWM signal on the pin 1.
2
const {PWM} = require('pwm');
3
const pwm = new PWM(1, 1000, 0.3); // Create the PWM instance with pin 1
4
pwm.start(); // Generate PWM signal
Copied!

pwm.stop()

Stop to generate PWM signal.
1
// Generate 1000 Hz 50% duth PWM signal on the pin 1.
2
const {PWM} = require('pwm');
3
const pwm = new PWM(1, 1000, 0.5); // Create the PWM instance with pin 1
4
pwm.start(); // Generate PWM signal
5
delay(100); // Wait 100ms.
6
pwm.stop(); // Stop PWM signal
Copied!

pwm.getFrequency()

  • Returns: <number> PWM frequency of the PWM instance.
Get the PWM frequency.
1
// Generate 1000 Hz 50% duth PWM signal on the pin 1 and print the frequency
2
const {PWM} = require('pwm');
3
const pwm = new PWM(1, 1000, 0.5); // Create the PWM instance with pin 1
4
console.log(pwm.getFrequency()); // Print current PWM frequency.
Copied!

pwm.setFrequency(frequency)

  • frequency <number> PWM frequency of the PWM instance.
Set the new PWM frequency.
1
// Generate 1000 Hz 50% duth PWM signal on the pin 1 and print the frequency
2
const {PWM} = require('pwm');
3
const pwm = new PWM(1, 1000, 0.5); // Create the 1000 Hz PWM instance with pin 1
4
pwm.setFrequency(500); // Change the PWM frequency to 500 Hz.
Copied!

pwm.getDuty()

  • Returns: <number> PWM duty of the PWM instance.
Get the PWM duty.
1
// Generate 1000 Hz 50% duth PWM signal on the pin 1 and print the duty
2
const {PWM} = require('pwm');
3
const pwm = new PWM(1, 1000, 0.5); // Create the PWM instance with pin 1
4
console.log(pwm.getDuty()); // Print current PWM duty.
Copied!

pwm.setDuty(duty)

  • duty <number> PWM duty of the PWM instance.
Set the new PWM duty.
1
// Generate 1000 Hz 50% duth PWM signal on the pin 1 and print the duty
2
const {PWM} = require('pwm');
3
const pwm = new PWM(1, 1000, 0.5); // Create the PWM instance with pin 1
4
pwm.setDuty(0.7); // Change the PWM duty to 70%.
Copied!

pwm.close()

Close the PWM port.
1
// Generate 1000 Hz 50% duth PWM signal on the pin 1.
2
const {PWM} = require('pwm');
3
const pwm = new PWM(1, 1000, 0.5); // Create the PWM instance with pin 3
4
pwm.start(); // Generate PWM signal
5
// ...
6
pwm.stop(); // Stop PWM signal
7
pwm.close(); // Close PWM port.
Copied!
Last modified 7mo ago