skip navigational linksPJRC
Shopping Cart Checkout Shipping Cost Download Website
Home MP3 Player 8051 Tools All Projects PJRC Store Site Map
You are here: Teensy Teensyduino Libraries SoftPWM

PJRC Store
Teensy 3.1, $19.80
Teensy 2.0, $16.00
Teensy++ 2.0, $24.00
USB Cable, $4.00
Teensy
Main Page
Teensy 3.1
Getting Started
How-To Tips
Code Library
Projects
Teensyduino
Reference

SoftPWM Library

SoftPWM, written by Brett Hagman, can create additional PWM outputs on any pin. The signal frequency is relatively slow (default 30 Hz), which is mostly useful for controlling the brightness of LEDs.

Download: SoftPWM.zip

Hardware Requirements

SoftPWM can control up to 20 pins. Any digital pin may be used.

TODO: photo of Teensy 2.0 with 5 RGB LEDs

SoftPWM is compatible with Teensy 2.0, Teensy 3.0, and Teensy++ 1.0 & 2.0. Teensy 1.0 is not supported.

BoardPWM Pins Disabled
Teensy 2.010
Teensy 3.0-
Teensy++ 1.01, 24
Teensy++ 2.01, 24

TODO: photo of Teensy 3.0 with 20 red LEDs

Basic Usage

SoftPWMBegin(SOFTPWM_NORMAL);

Begin using SoftPWM. This function must be called first. The input can be SOFTPWM_NORMAL or SOFTPWM_INVERTED.

SoftPWMSet(pin, value);

Write the PWM on a pin. The value may be between 0 to 255. If fade times have been defined, the PWM will change gradually.

SoftPWMSetFadeTime(pin, fadeUpTime, fadeDownTime);

Set speed a PWM pin changes. fadeUpTime and fadeDownTime may be between 0 to 4000 milliseconds.

SoftPWMSet(pin, polarity);

Change the polarity on a single pin.

Example Program

SoftPWM comes with several examples. Open the from File > Examples > SoftPWM.

CPU Usage & Interrupt Latency

SoftPWM uses a timer and interrupt routine, which consumes considerable CPU time.

TODO: actually measure CPU usage (maybe try changing the number of channels...)

Latency sensitive libraries, like Servo and NewSoftSerial, will likely be disrupted by SoftPWM.

More Details

Please refer to the official SoftPWM documentation for more details.