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.6, $29.25
Teensy 3.5, $24.25
Teensy 3.2, $19.80
Teensy LC, $11.65
Teensy 2.0, $16.00
Teensy++ 2.0, $24.00
Main Page
Getting Started
How-To Tips
Code Library

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: Included with the Teensyduino Installer

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 3.6, Teensy 3.5, Teensy 3.2, Teensy 3.1, Teensy 3.0, Teensy LC, Teensy 2.0, and Teensy++ 1.0 & 2.0.

SoftPWM does not work with Teensy 1.0.

BoardPWM Pins DisabledTimer Used
Teensy 3.5, 3.6-PIT / IntervalTimer
Teensy 3.0, 3.1, 3.2-PIT / IntervalTimer
Teensy LC-PIT / IntervalTimer
Teensy 2.0104
Teensy++ 1.01, 242
Teensy++ 2.01, 242

TODO: photo of Teensy 3.0 with 20 red LEDs

Basic Usage


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.