My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
SoftPWMLibraryDocumentation  
Arduino/Wiring SoftPWM Library.
Updated Oct 14, 2011 by rogue.bh...@gmail.com

Arduino/Wiring SoftPWM Library



Download

SoftPWM Library - Latest Version

Installation

See: InstallingArduinoLibraries

Description

An Arduino and Wiring 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 the microcontroller to generate up to 16 PWM channels.

Features

  • Arbitrary output pins.
  • Up to 16 different channels can be created.
  • True zero level, i.e. off = off
  • Separate fade rates for on and off.

Notes:

This library hasn't been tested with servos, so beware that it may not work as you may expect if used to control servos.

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()

  • Initializes the library - sets up the timer and other tasks.

SoftPWMSet(pin, value)

  • pin is the output pin.
  • value is a value between 0 and 255 (inclusive).

SoftPWMSetPercent(pin, percent)

  • pin is the output pin.
  • percent is a value between 0 and 100 (inclusive).

SoftPWMSetFadeTime(pin, fadeUpTime, fadeDownTime)

  • pin is the output pin.
  • fadeuptime is the time in milliseconds that it will take the channel to fade from 0 to 255.
    • Range: 0 to 4000
  • fadedowntime is the time in milliseconds that it will take the channel to fade from 255 to 0.
    • Range: 0 to 4000

Notes

  • You can use ALL in place of the pin number to have the function act on all currently set channels.
    • e.g. SoftPWMSetFadeTime(ALL, 100, 400) - this will set all created channels to have a fade-up time of 100 ms and a fade-down time of 400.

Demonstrations


Arduino Duemilanove LED Blink example - available as library example:


rDuino LEDHead Bounce example - available as library example:


More demos:

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


Comment by danielf...@gmail.com, Dec 30, 2010

I tried and read your code: Seems you can optimize the ISR for speed a lot by using pointers and not using arrays: Arrays use a multiplication in the indexing slowing down the search An efficiency is achievd by doing:

softPWMChannel & currchannel = softpwm_channelsi?;
if (currchannel.fadeuprate > 0 || currchannel.fadedownrate > 0)
{
// we want to fade to the new value

etc another idea would be a pointer to the beginning of the array for (softPWMChannel currchannel = &softpwm_channels0?; and even having a pointer to the base saved somewhere...

Danny

Comment by chiv...@gmail.com, Mar 17, 2011

I downloaded and installed the library. When I verify the code it says "softpwmbegin () not declared in this scope" using an arduino duemil.

Comment by project member rogue.bh...@gmail.com, Mar 17, 2011

Make sure you use SoftPWMBegin() instead of softpwmbegin()

Comment by frank.le...@gmail.com, Apr 8, 2011

Great Library! I used this one to control some LEDs mounted on my staircase. Unfortunately, digital pin 0 can not be accessed although I used the fixed r116 version of the library. SoftPWMSet(ALL, 0); just sets any other pin to zero, but pin 0 is still in a high state. Is there any quick fix available for this issue? I would really like to use every digital pin ;)

Comment by project member rogue.bh...@gmail.com, Apr 8, 2011

More than likely, you'll need to do Serial.end(); in setup(). This will remove the hardware control on pin 0 and 1.

Try it, and let us know how it goes.

Comment by freekkal...@gmail.com, Apr 10, 2011

Why can you only go up to 16 channels?

Comment by project member rogue.bh...@gmail.com, Apr 10, 2011

No direct reason - it was chosen arbitrarily. You could do some math to figure out what the limit would be. It's just a calculation for the main interrupt loop - as you add more loops, the more time is taken in the interrupt routine, leaving less time in the main application.

@ 16 channels, approximately 20% of CPU cycles are spent in the interrupt routine. I'd have to spend some time looking at the routine in detail to get the exact figures, but that's a decent rough estimate.

Comment by dmacc...@gmail.com, Jul 19, 2011

Brett:

I don't know if you're still supporting this library, but if you are, could you look at my posting at Arduino.cc?

http://arduino.cc/forum/index.php/topic,66941.0.html

Thanks.

\dmc

Comment by joaoquar...@netcabo.pt, Jul 27, 2011

Hello Brett,

Have you plans to port this Library ChipKIT, I've tried and it doesn't work.

Best regards,

Joao Quaresma.

Comment by project member rogue.bh...@gmail.com, Jul 27, 2011

Probably not - Besides Rogue, Wiring is where I am putting all of my efforts these days. Wiring will eventually (within a year) have support for PIC, and hopefully others. The latest Wiring API hasn't been adopted by Arduino, and hence ChipKIT, so the current version of SoftPWM will require some work to get it functioning on ChipKIT.

For more information about where we are going with Wiring, check out http://wiring.org.co/

Comment by emiliano...@gmail.com, Jul 30, 2011

Hi, Im working on an RGB LED cube, with an Atmega644 with ArduinoBootloader?, I need 25 PWM channels, how can I get them with your library? will using 2 timers help? thanks!

Comment by m...@baseggio.ca, Oct 13, 2011

I just tried to use this with an attiny85 chip -- with a bunch of compile errors. Oh well, it was worth a shot :)

Any chance you'll be releasing one that is attiny85 compatible?

Comment by project member rogue.bh...@gmail.com, Oct 14, 2011

We are working on the Atmel ATtiny core series in Wiring. No ETA on the release yet.

Comment by div....@gmail.com, Nov 6, 2011

C:\Users\Ivan\Documents\Arduino\libraries\SoftPWM\SoftPWM.cpp: In function 'void SoftPWMBegin()': C:\Users\Ivan\Documents\Arduino\libraries\SoftPWM\SoftPWM.cpp:123: error: 'TIFR2' was not declared in this scope C:\Users\Ivan\Documents\Arduino\libraries\SoftPWM\SoftPWM.cpp:124: error: 'TCCR2B' was not declared in this scope C:\Users\Ivan\Documents\Arduino\libraries\SoftPWM\SoftPWM.cpp:125: error: 'TCCR2A' was not declared in this scope C:\Users\Ivan\Documents\Arduino\libraries\SoftPWM\SoftPWM.cpp:126: error: 'OCR2A' was not declared in this scope C:\Users\Ivan\Documents\Arduino\libraries\SoftPWM\SoftPWM.cpp:127: error: 'TIMSK2' was not declared in this scope C:\Users\Ivan\Documents\Arduino\libraries\SoftPWM\SoftPWM.cpp:127: error: 'OCIE2A' was not declared in this scope Why? Arduino IDE 0022. Code copypasted from the example.

Comment by stvan...@gmail.com, Dec 11, 2011

well done! and thank you

Comment by denleybi...@gmail.com, Jan 13, 2012

I am having the same problem with pin number 0. I can see in the SoftPWM.cpp file that it is a bug. On line 158 the code find the pins for pin<0 and then for pin>0 (it is missing the pin=0 case).

I am new to open source stuff so I will have a quick look to see if I can fix it myself but I dunno if I will be able to figure it out, so someone else may like to fix it or submit it as a bug report or whatever.

Comment by denleybi...@gmail.com, Jan 13, 2012

I submitted it under "Issues". Will have to just change my copy for now.

Comment by ronny.dl...@gmail.com, Jan 13, 2012

Hey, nice library!

It works really fine, but if I connect a xbee module on the Pins 0 and 1 the arduino crash every few seconds. Is there any chance to use this pins and the SoftPWM library?

Best Regards, Ronald Dlapka


Sign in to add a comment
Powered by Google Project Hosting