bhagman / SoftPWM

A Wiring (and Arduino) Library to produce PWM signals on arbitrary pins.
MIT License
86 stars 31 forks source link

SoftPWM Library


What's New?

Version 1.0.1

Version 1.0.0

Description

A Wiring Framework (and Arduino) Library to produce PWM signals on any arbitrary pin.

It was originally designed for use controlling the brightness of LEDs, but could be modified to control servos and other low frequency PWM controlled devices as well.

It uses a single hardware timer (Timer 2 on AVR, or IntervalTimer on Teensy 3.x) on the microcontroller to generate up to 20 PWM channels.


Features


Download and Installation

You can use the Arduino Library Manager (Sketch -> Include Library -> Manage Libraries...) to download the library.

Alternatively, you can download the library directly, and install it yourself.

Unzip the folder and rename it to SoftPWM, then move it to your arduinosketchfolder/libraries/ folder.


Usage Example

#include "SoftPWM.h"

void setup()
{
  // Initialize
  SoftPWMBegin();

  // Create and set pin 13 to 0 (off)
  SoftPWMSet(13, 0);

  // Set fade time for pin 13 to 100 ms fade-up time, and 500 ms fade-down time
  SoftPWMSetFadeTime(13, 100, 500);
}

void loop()
{
  // Turn on - set to 100%
  SoftPWMSetPercent(13, 100);

  // Wait for LED to turn on - you could do other tasks here
  delay(100);

  // Turn off - set to 0%
  SoftPWMSetPercent(13, 0);

  // Wait for LED to turn off
  delay(500);
}

Function Descriptions

SoftPWMBegin([defaultPolarity])

SoftPWMSet(pin,value)

SoftPWMSetPercent(pin,percent)

SoftPWMSetFadeTime(pin,fadeUpTime,fadeDownTime)

SoftPWMSetPolarity(pin,polarity)

Notes


Demonstrations

Arduino Duemilanove LED Blink example - available as library example:

Arduino SoftPWM example

rDuino LEDHead Bounce example - available as library example:

rDuino LEDHead SoftPWM example

More demos:

https://www.youtube.com/view_play_list?p=33BB5D2E20609C52