Go Down

Topic: SoftPWM Library (Read 22361 times) previous topic - next topic


Mar 17, 2010, 07:06 am Last Edit: Mar 17, 2010, 07:13 am by bhagman Reason: 1
I created a new software PWM output library.  It was made for controlling LEDs.

A few features of this library:
  • 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.


Quick demos here:




Marvin Martian

That's neat - I was just writing some code to manage the fade up & down when switching the LEDs too - but using the built in PMW so was limited to the 6 lines. Yours is much better!


Jul 09, 2010, 07:50 pm Last Edit: Jul 09, 2010, 07:52 pm by Valygar Reason: 1
One question about the library. I tried to use it with a project that used already the 6 PWM ports, and it didn't work (there was no activity at the sofpwm port, or the hardware pwm ports)

I tried to disable the analogPWM hardware signals, pin outputs and then the leds controlled by softpwm worked.

So, if I use this library I have to control all pins with this library?

In any case, many thanks for this library!


The SoftPWM Library only uses Timer2.  So, unless there is something I'm missing, you just won't be able to use the two hardware PWM output pins for that timer (pins 3 and 11).  The others should work (hardware PWM on pins 5, 6, 9 and 10).



So, do i understand it right, that this Library makes me able to use the digital out pins on my duenmilanove (Or also the mini) as PWM Pins?


Thanks for all the hard work bhagman.

I have installed it and will have a play with it later.



Is there any relatively simple way to make the library use a different timer?

Or, another question to solve the issue I have:
If I start the softwarePWM library before I begin() some objects on the Tone library, will the Tones automatically take the next available timer?  I know they do this when there are multiple tone objects, but how about other libraries?
Soundcloud page: http://soundcloud.com/beefinator-2
Youtube channel: http://www.youtube.com/user/beefinator14
Old soundcloud page (ran out o


@zonkie: Yes.  That's exactly what the library is for.  Just a reminder though, that it is not intended for use with things like servos - although it will work, it is not recommended.  The reason being is that the SoftPWM range of values is over the entire pulse width, whereas a servo expects a pulse over a tight range (0.5 mS to 2.5 mS and repeated every 20 mS).  Hence, the range of usable values for a servo or such would be something like 40 to 50 resulting in big jumps in the servo position.

@sciguy: All the objects that use the hardware timers suffer from the problem of being tied to a specific timer.  I was thinking that the best solution (i.e. kludge) to this would be to make a library named for each timer, and thus you can pick which timer you want to use.  Would that make sense? e.g. SoftPWM1 for timer 1, SoftPWM2 for timer2, etc.



i'm getting this when i try to compile a sketch that uses your library.

Code: [Select]

C:\Program Files (x86)\Arduino\arduino-0018\libraries\SoftPWM\SoftPWM.cpp: In function 'void SoftPWMBegin()':

C:\Program Files (x86)\Arduino\arduino-0018\libraries\SoftPWM\SoftPWM.cpp:123: error: 'TIFR2' was not declared in this scope

C:\Program Files (x86)\Arduino\arduino-0018\libraries\SoftPWM\SoftPWM.cpp:124: error: 'TCCR2B' was not declared in this scope

C:\Program Files (x86)\Arduino\arduino-0018\libraries\SoftPWM\SoftPWM.cpp:125: error: 'TCCR2A' was not declared in this scope

C:\Program Files (x86)\Arduino\arduino-0018\libraries\SoftPWM\SoftPWM.cpp:126: error: 'OCR2A' was not declared in this scope

C:\Program Files (x86)\Arduino\arduino-0018\libraries\SoftPWM\SoftPWM.cpp:127: error: 'TIMSK2' was not declared in this scope

C:\Program Files (x86)\Arduino\arduino-0018\libraries\SoftPWM\SoftPWM.cpp:127: error: 'OCIE2A' was not declared in this scope

i just tried to include the headers for the library.i get the same error when i am trying to compile one of the examples.
the chip is an atmega8, for 328 works fine.

Go Up