Go Down

Topic: PWM frequency library (Read 381564 times) previous topic - next topic


This Library didn't work. The following message was appeared:
The library "Arduino PWM Frequency Library v_05" cannot be used.
Library names must contain only basic letters and numbers.
(ASCII only and no spaces, and it cannot start with a number)

Please, I need it!


Hi, could anyone help me?
I have noticed that there's no PWM.cpp file. The library wasn't installed.


You extracted the filename of the zip file directly into your library directory as "Arduino PWM Frequency Library v_05"
You should simply make a directory under libraries and call it PWM for example.
C:\Program Files\arduino-1.8.1\libraries\PWM

Then copy all the stuff in under the PWM directory into this new library PWM directory, and copy PWM_lib_example and PWM_lib_resolution_example into there as well.

It should look like this when your done ...

 Directory of C:\Program Files\arduino-1.8.1\libraries\PWM

11-May-2018  16:49    <DIR>          .
11-May-2018  16:49    <DIR>          ..
16-Aug-2012  16:55             2,208 keywords.txt
24-Sep-2012  15:24             4,873 PWM.h
11-May-2018  14:38    <DIR>          PWM_lib_example
11-May-2018  14:38    <DIR>          PWM_lib_resolution_example
11-May-2018  16:49                 0 showDir.txt
11-May-2018  14:38    <DIR>          utility
               3 File(s)          7,081 bytes
               5 Dir(s)  113,831,374,848 bytes free


I like to using this pwm.h librari variable phase shifting arduino between two pwm chanel.
I like the shifing variable 0-180 degrees.

Somebody doing same?



Cannot compile the code for Atmega8. While I tried compiling for Atmega168, the code uses 2720 bytes of space and compiles successfully and works fine. Anyone please help me.


post your code if you want help



I am trying to lower the Arduino Uno PWM frequencies to drive 4 x solenoid valves. Ideally, pins 5 & 6 are untouched to retain timer0 function. However, understand that creating custom frequencies would sacrifice 2 pins on Arduino, which I have found out to be pins 6 and 11. 

In the original post by runnerup:
"If you don't want to sacrifice any 8bit PWM pins, don't call the initialize function for that timer, try changing the prescaler to manipulate frequency instead. "

So if I would like to leave pins 5 and 6 untouched and change PWM frequency for pins 3, 9, 10 AND 11 to 31Hz? Do I simply use this setup to change the timer prescaler and the PWM frequencies for the 4 pins would be lowered to 31Hz?

Code: [Select]
void setup()

I tried doing that and alternatively using this function PwmFrequency to also change the prescaler. But neither works.

So is this a limitation such pins 6 and 11 cannot be used?

Appreciate some clarification/ advice, thanks!


Great Library

Some problems i am facing:
1- Cannot modify the frequency of pin 11, the bool doesn't return success
2- Can't get the resolution on pin 3 and 11 although the timer gives me resolution


The Leonardo has a different CPU architecture. That means different timers with different properties that I would have to wrap.
So far, Arduino microcontrollers use three main architectures 'groups'.  Arduino Mega and ADK are of the AVR mega 1280/1281/2560/2561 group, which are all identical except for the amount of memory they hold. The Leonardo uses ATmega32u4. And pretty much everything else uses 88/168/328 variants.
I have an Uno and a Mega, which means I was able to build and test for every architecture except the Leonardo. If there is enough interest in this project, I would be willing to try to get it working on the Leonardo. But I have no reliable way too test it in real life to make sure it works.
I am using Atmega 64/128/256 with MegaCore , your library is very necessary for me for one project. But it does not support this processor. Can you help me? Thanks.


I've tried implementing the library and i get an error, i thought this was supposed to work for all Arduinos?



Not ALL libraries are meant for ALL Arduinos.
Which Library the one in question already or a different one ?

What board are you trying to use your lib with ?

Ok less questions and more directions.
Please read the sticky posts at the top of forum sections and add the details they recommend.

It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.


This library is very useful to me.
I have a question.

Is there a way to select a frequency as a float rather than an integer?

I need a frequency of 10.5 Hz.

Thank you.


May 21, 2019, 12:42 pm Last Edit: May 22, 2019, 05:44 am by J-M-L
Is there a way to select a frequency as a float rather than an integer?
Not in the library API

I need a frequency of 10.5 Hz.
This frequency is low so this is achievable (at least approchable to a correct resolution) so you could manually play with the prescaler, Timer etc -> see this article for details and the very beginning of this current  discussion of the PWM library
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous


This would be my first attempt at using a library like this.
Can someone explain the limitations of creating a specific frequency when using this library?
I keep seeing the phrase"resolution" tossed around but I cannot really grasp what that means relative to a clock signal.
I am trying to generate two clocks of 104 khz and 165khz on a UNO.
When I run the following code I get close but there is some deviation.
Tweaking the two frequency variables slightly does nothing or it makes it jump past my target freq.
Per my oscilloscope I am getting 105.290 khz & 166.710 khz. This is within 1.25% of my target and would probably be fine for my application but I really want to know what I would have to do if my tolerances needed to be tighter.
Is this expected behavior or is my programming a hot mess?

Code: [Select]
#include <PWM.h>
#include <FastLED.h>

#define LED_PIN     4
#define NUM_LEDS    300
#define BRIGHTNESS  64
#define LED_TYPE    WS2811


int fosc1 = 9;                // the pin that the timer1 is attached to
int fosc2 = 3;                // the pin that the timer2 is attached to
int32_t frequency1 = 165000;   //frequency (in Hz)
int32_t frequency2 = 104000;   //frequency (in Hz)


CRGBPalette16 currentPalette;
TBlendType    currentBlending;

extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;

void setup() {
    delay( 3000 );     // power-up safety delay
    FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
    FastLED.setBrightness(  BRIGHTNESS );
    currentPalette = RainbowColors_p;
    currentBlending = LINEARBLEND;

    //initialize all timers except for 0, to save time keeping functions

    //sets the frequency for the specified pin
    bool success1 = SetPinFrequencySafe(fosc1, frequency1);
    bool success2 = SetPinFrequencySafe(fosc2, frequency2);
    //if the pin frequency was set successfully, turn pin 13 on
    if(success1 && success2){
    pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);
    pwmWrite(fosc1, 128);
    pwmWrite(fosc2, 128);

void loop()


May 22, 2019, 05:49 am Last Edit: May 22, 2019, 05:50 am by J-M-L
Resolution is basically how close you can come to the frequency you are looking at delivering.
How close you can get depends on the clock speed of your arduino, how high the target frequency is (you play with the prescaler) and which timer you use (8 bit or 16 bit)

Go back and Read the first page of this discussion, especially post #7

Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Go Up