/raspi-soft-pwm

Provides access to Software PWM on the Raspberry Pi from Node.js through pigpio

Primary LanguageJavaScriptMIT LicenseMIT

Raspi SOFT PWM

Gitter

Raspi Soft PWM is part of the Raspi.js suite that provides software PWM through pigpio.

Installation

npm install raspi-soft-pwm

Example Usage

var raspi = require('raspi-io');
var five = require('johnny-five');
var SoftPWM = require('raspi-soft-pwm').SoftPWM;
var board = new five.Board({
  io: new raspi()
});

board.on('ready', function() {
  var softPWM = new SoftPWM({pin: 'P1-26', range: 100, frequency: 200});
  dutyCycle = 0;
  
  setInterval(function () {
    softPWM.write(dutyCycle);

    dutyCycle += 10;
    if (dutyCycle > 100) {
      dutyCycle = 0;
    }
  }, 2000);
});

Pin Naming

The pins on the Raspberry Pi are a little complicated. There are multiple headers on some Raspberry Pis with extra pins, and the pin numbers are not consistent between Raspberry Pi board versions.

To help make it easier, you can specify pins in three ways. The first is to specify the pin by function, e.g. 'GPIO18'. The second way is to specify by pin number, which is specified in the form "P[header]-[pin]", e.g. 'P1-7'. The final way is specify the Wiring Pi virtual pin number, e.g. 7. If you specify a number instead of a string, it is assumed to be a Wiring Pi number.

Be sure to read the full list of pins on the supported models of the Raspberry Pi.

API

new SoftPWM(config)

Instantiates a new PWM instance on the given pin.

Arguments:

Argument Type Description
config (optional) Number | String | Object The configuration for the PWM pin. If the config is a number or string, it is assumed to be the pin number for the peripheral. If it is an object, the following properties are supported:
Property Type Description
pin (optional) Number | String The pin number or descriptor for the peripheral
range (optional) Number Sets the range register in the PWM peripheral. This value controls how many clock cycles are used in one period. Please refer to the pigpio documentation of [Gpio.pwmRange(range)](https://github.com/fivdi/pigpio/blob/master/doc/gpio.md#pwmrangerange). *Default: 255*
frequency (optional) Number Sets the frequency in the PWM peripheral. Please refer to the pigpio documentation of [Gpio.pwmFrequency(frequency)](https://github.com/fivdi/pigpio/blob/master/doc/gpio.md#pwmfrequencyfrequency). *Default: 800*

Instance Methods

write(value)

Sets the duty cycle for the PWM output.

Arguments:

Argument Type Description
value Number The duty cycle for the PWM to set, must be between 0 and range

Returns: None

Note: The PWM does not start outputting a signal until write is called for the first time.

Credits

This package was largely based on Bryan Hughes' work and advice. He is the creator of [raspi}(https://github.com/nebrius/raspi). Also, Brian Cooke, creator of pigpio, the package that actually does all the work.

License

The MIT License (MIT)

Copyright (c) 2016 Tiago Alves tralves@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.