PWM frequency library

Dear Brothers, firstly sorry for bad english, my arduino is Arduino MEGA 2560, i want to learn how to change frequency and search the library for it, and find the google code link below :

https://code.google.com/p/arduino-pwm-frequency-library/downloads/list

and downloaded the 5th Version of it >> unzip it in the arduino.ide library on my computer, im sure that i follow this step of adding library :

btw my arduino.ide version is 1.8.2

why i cannot compile or even upload the example program ?
it said :

Arduino: 1.8.2 (Windows Store 1.8.3.0) (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users*****\Arduino\sketch_jun07a\sketch_jun07a.ino:1:17: fatal error: PWM.h: No such file or directory

#include <PWM.h>

^

compilation terminated.

exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

my teacher said that i must search the library for arduino mega instead of arduino uno (?), is that right ? is there any solution ?

Your teacher is wrong i'm afraid as both the UNO and MEGA use similar AVR architecture.

There is a good comparison link HERE

Clearly The mega has more pins due to the difference in processor but the lib should still be capable of handling that aspect.

More likely the library has been put in the wrong location.
Using <PWM.h> requires the files to be in the /libraries folder.
Using "PWM.h" requires the files to be in same folder as the sketch.

Hi Everyone, I'm new here ;]

Just wanted to say that this is an excellent PWM library and you guys are an awesome community!

I was wondering if anyone has tried using interrupts with this library? Let's say to just count the number of positive-edge pulses, then if a threshold is reached, stop the pwm?

Perhaps something along the lines of ISR(Timer1_compa_vect)?

Thanks,
Oyakodon

Hi there,

Do you guys think that's possible to make a Frequency sweep with this librarie?

I was trying to build one, but I'm not sure of what I am missing, or if it is possible indeed.

please, If you want to take a look in my code bellow:

     FPWM = lgf[lx]; // This is the frequency of the PWM that im obtainning from another function.

     Init: InitTimersSafe();
   //sets the frequency for the specified pin
     success = SetPinFrequencySafe(SPWM, FPWM);
   //initialize all timers except for 0, to save time keeping functions
   
   if (success)   //here its where I would change my PWM frequency, if success.
   {
     cdelay = 1;
     VL = 0;
     sumvL = 0;
     if (readFlag == 1)
     {
       readFlag = 0;
     }
     while (VL <= cdelay)
     {
       //use this functions instead of analogWrite on 'initialized' pins
       pwmWrite(SPWM, LPWM);  // more precisely here.
       if (readFlag == 1)
       {
         sumvL = sumvL + analogVal;
         VL++;
         readFlag = 0;
       }
     }
     Vs[lx] = sumvL / VL;
   }
   else 
   goto Init; //If success is not ok I would go back to InitTimersSafe() line.

Thank you for the attention and sorry for bad english.

@ hugoabreu

sudo code ?

Hi.

This code I've put its inside the for loop. I didn't put everything couse, its too long and it doesn't matter.

My main question is if you think that we can change frequencies of PWM inside a for loop.

Ty

@hugoabreu

All code matters.
Without it nobody can tell if its good or bad or could be improved in any way or even broken into better segments etc.

And yes it should be possible to call a change inside a loop.
However you should realise that in doing so timers and interrupts could also be changed with unforeseen results unless you account for them.

Thanks a lot. This library is AWESOME.

I was able to get 100kHz PWM with 16 bit resolution!

I'm experiencing an issue between a UNO and MEGA - my problem is described in a new post (did not notice this thread was still active, assumed it was too old)

Hi all,
this is really a great library - very helpful :slight_smile:

There's just one thing, that's bugging me. Each time, i include the lib in any of my programs, i get some warnings during the compiling.

/Users/philipp/Documents/Arduino/libraries/PWM/utility/BTimerDefs.cpp:26:0: warning: "UINT16_MAX" redefined
 #define UINT16_MAX 65535

 ^
In file included from /Applications/Arduino.app/Contents/Java/hardware/tools/avr/lib/gcc/avr/4.9.2/include/stdint.h:9:0,
                 from /Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/inttypes.h:37,
                 from /Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/avr/sfr_defs.h:126,
                 from /Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/avr/io.h:99,
                 from /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/wiring_private.h:26,
                 from /Users/philipp/Documents/Arduino/libraries/PWM/utility/BTimerDefs.cpp:23:
/Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/stdint.h:346:0: note: this is the location of the previous definition
 #define UINT16_MAX (__CONCAT(INT16_MAX, U) * 2U + 1U)
 ^
/Users/philipp/Documents/Arduino/libraries/PWM/utility/BTimerDefs.cpp:27:0: warning: "UINT8_MAX" redefined
 #define UINT8_MAX 255

 ^
In file included from /Applications/Arduino.app/Contents/Java/hardware/tools/avr/lib/gcc/avr/4.9.2/include/stdint.h:9:0,
                 from /Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/inttypes.h:37,
                 from /Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/avr/sfr_defs.h:126,
                 from /Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/avr/io.h:99,
                 from /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/wiring_private.h:26,
                 from /Users/philipp/Documents/Arduino/libraries/PWM/utility/BTimerDefs.cpp:23:
/Applications/Arduino.app/Contents/Java/hardware/tools/avr/avr/include/stdint.h:331:0: note: this is the location of the previous definition
 #define UINT8_MAX (INT8_MAX * 2 + 1)
 ^
Sketch uses 6508 bytes (20%) of program storage space. Maximum is 32256 bytes.
Global variables use 258 bytes (12%) of dynamic memory, leaving 1790 bytes for local variables. Maximum is 2048 bytes.

Nevertheless the code compiles and works fine.

Is someone else having the same problems and how can i solve this?

Thank you all!

Philipp

looks like two developers got the same name idea for a #define... the second one reinventing the wheel from [url=https://github.com/f32c/arduino/blob/master/hardware/fpga/f32c/system/include/stdint.h]stdint.h[/url] which has been here for more than 18 years and is better written....(it's a header file in the C standard library introduced in the C99 standard library section 7.18 to allow programmers to write more portable code)

you could go modify the library and put a test to see if it's already defined, something like

#ifndef UINT8_MAX
#define UINT8_MAX 255
#endif

but you could also simply remove them (possibly #include <stdint.h> but I think is added automatically hence the compiler barking at the redefinition - to be tested)

Thank you!
I just altered ATimerDefs.cpp and BTimerDefs.cpp.

Changed

#define UINT16_MAX 65535
#define UINT8_MAX 255

to

#ifndef UINT16_MAX
#define UINT16_MAX 65535
#endif
#ifndef UINT8_MAX
#define UINT8_MAX 255
#endif

in both files.

Works and the compiler is not barking anymore :slight_smile:

Very nice library indeed!

Regarding the frequency sweep, hugoabreu mentioned... I'm actually doing that for a project, using the Arduino Pro Mini (sweeping from 1 Hz to 500 Hz, with a 1 Hz step size, transitioning steps every 1000 - 2000 msec). Simply set the frequency and duty cycle for each frequency step:

if (SetPinFrequencySafe(led, frequency)) 
{
       pwmWrite(led, brightness);
}

Only one problem... The frequency steps sometimes do not transition smoothly. Meaning, the output will glitch and stick in the high or low state, for up to a second, before stabilizing at the new frequency/duty cycle. In my use case, the duty cycle remains constant throughout the sweep.

Unfortunately, for me, this is not good. Sweeping with these glitches is causing problems with connected device(s). Has anyone else encountered this problem? Is there a fix for it? Any assistance is appreciated.

I noticed the same issue that Uup115 reported. The frequency does not transition smoothly. Also the frequency is not exact (off by ~0.5%). This is due to the ceramic osc. used on the Uno and Mega. The Leonardo has a crystal osc. with much higher accuracy, but was not supported by this library.

So I forked the library and improved it with the following.

You can find the updated code here: GitHub - phatpaul/arduino-pwm-frequency-library

Hope it helps,
Paul

It would seem that you changed things so that InitTimers() and InitTimersSafe() now require a minimum frequency argument, breaking everyone's existing code. Could you explain that? I'm just getting started reading through the timer section of the ATmega data sheet.

Does this library work with the Arduino Zero, which has a SAMD processor?

If not, can someone make the necessary changes?

Aloha very happy to see that people are still using the library successfully! I once tested it out and all worked great, but now years later different IDE version (latest on Linux arduino-1.8.5-linux64.tar ) I have a problem getting the library to install, I am using V_05 from: Google Code Archive - Long-term storage for Google Code Project Hosting.
Regardless of how I put it the IDE complains about the package 'does not contain a valid library'...
It seems nobody has worked on the code since the end of 2012, what could cause this error in my IDE? Any suggestions or workarounds are highly appreciated!

PS: I think I just found the solution in post #126 of this thread (tab 9) a user posted a fixed PWM.zip library, which installed fine and I am onto troubleshooting the rest of my adventure...

meninas92:
Hi i have tried to contact the supporter of the library "runnerup". i have some minor fixes to the library i have created a zip compatible with newer versions of arduino. does anyone knows how to contact runnerup?

Would be great if you could upload it at a Git site or so, if the license permits?
And we are all very curious what it was that caused the library to fail to install in those newer IDEs?

Briney_Eye:
It would seem that you changed things so that InitTimers() and InitTimersSafe() now require a minimum frequency argument, breaking everyone's existing code. Could you explain that? I'm just getting started reading through the timer section of the ATmega data sheet.

+1
Yes that is wierd...