SoftPWM from Brett Hagman

Hello, everyone,

I use Brett Hagman's SoftPWM library on an Arduino Mega to control the brightness of 18 LED strips independently of each other (lighting of stairs)

Now I ask myself what frequency is the PWM running at?

Can I possibly even determine that myself?

Thank you for your answers!

LG Breschdlingsxelz

Does the documentation or source code commentary not talk about that?

@anon75951627, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for questions about your project. See
About the Installation & Troubleshooting category.

If I had your question, I would dig through the library; there might be some explanations in there.

The documentation doesn't talk about it at all. But it's 60Hz:

#define SOFTPWM_FREQ 60UL

Thanks for moving it and thanks for your fast replies!

Ok, right, it says the 60Hz.

Can I change them without making the program unusable?

Let's say to 120Hz?

Many greetings

The fastest way to find out is to try.

Seems to me that by the time I got around to needing to control 18 LEDs (especially with individual brightness settings), I'd switch over to an addressable LED type -- for example WS2812B or APA102. Both are available as preassembled multi-LED strips and individual components. You could control all 18 (and many more) using one (WS2812B) or two (APA102) digital pins -- no PWM required.

You can change the frequency, but have to modify SOFTPWM_FREQ or SOFTPWM_OCR in the library source code SoftPWM.cpp#L54-L60.

#if F_CPU
#define SOFTPWM_FREQ 60UL
#define SOFTPWM_OCR (F_CPU/(8UL*256UL*SOFTPWM_FREQ))
#else
// 130 == 60 Hz (on 16 MHz part)
#define SOFTPWM_OCR 130
#endif

You can also try my new AVR_Slow_PWM Library, where you can easily change frequency and/or duty-cycle by a line in your code, such as

ISR_PWM.setPWM(PWM_Pin, PWM_Freq, PWM_DutyCycle);

Check in GitHub

Thank you very much!

The library is really cool!
Exactly what I actually need.

But does it also support 18 outputs?
Because I read 16 as the maximum.

Many greetings

The number 16 is just defined arbitrarily to be sure not so many channels are used and time wasted to check unused channels

It's easy to change to larger or smaller number by changing this line AVR_Slow_PWM_ISR.h#L142 to the number you'd like to have

For example from

#define MAX_NUMBER_CHANNELS        16

to

#define MAX_NUMBER_CHANNELS        18

also change in the example these lines ISR_8_PWMs_Array.ino#L97-L116 to match your actual connections and requirements

// You can assign pins here. Be careful to select good pin to use or crash, e.g pin 6-11
uint32_t PWM_Pin[] =
{
  LED_BUILTIN, PIN_D0, PIN_D1,  PIN_D2,  PIN_D3,  PIN_D4,  PIN_D5,  PIN_D6
};

#define NUMBER_ISR_PWMS         ( sizeof(PWM_Pin) / sizeof(uint32_t) )


// You can assign any interval for any timer here, in Hz
uint32_t PWM_Freq[NUMBER_ISR_PWMS] =
{
  1,  2,  3,  4,  5,  6,  7,  8
};

// You can assign any interval for any timer here, in Microseconds
uint32_t PWM_DutyCycle[NUMBER_ISR_PWMS] =
{
  5, 10, 20, 25, 30, 35, 40, 45
};

Wow, I'm really overwhelmed by how much help I'm getting here!

As soon as my Arduino Mega arrives, I will use your library and modify it as you explained.

It would be too cool if it actually works then :smiley:

Thanks again!