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.
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.
Board | PWM Pins Disabled | Timer Used |
---|---|---|
Teensy 3.5, 3.6 | - | PIT / IntervalTimer |
Teensy 3.0, 3.1, 3.2 | - | PIT / IntervalTimer |
Teensy LC | - | PIT / IntervalTimer |
Teensy 2.0 | 10 | 4 |
Teensy++ 1.0 | 1, 24 | 2 |
Teensy++ 2.0 | 1, 24 | 2 |
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.