PWM frequency library

Hello sir,
1st of all thanx for the library and effort. but how can i use all of the pwm pins on mega 2560 beacuse i am making a 3 phase inverter and i require all of them operational and currently i am only getting only 11 and 12 operational. looking for a reply from you.

Hi,

with Arduino IDE 1.05 this Library works really nice, but not with IDE 1.6.5.
Any Idea why ?

1 Like

Hi All,

I'm new to the Arduino family and my first project is PWM related. My research has led me to this library but i am unable to install it into IDE1.6 as I get an error that it is not a valid library file. When comparing it to the ones already installed I noticed that the folder didn't include libraray.properties or readme.adoc files. Could this be the problem?

Thanks

I managed to get the PWM library working with IDE 1.6 by doing the following:

  1. Download and unzip Arduino PWM Frequency Library v-05, here you will find three folders, "PWM", "PWM_lib_example" & "PWM_lib_resolution_example". Move the the two example folders into the PWM folder.

  2. The go to C:\Program Files (x86)\Arduino\libraries and copy the "PWM" folder here.

  3. When complete your libraries folder should look like the attached picture.

  4. Restart the ide and PWM should now be available in your list of libraries.

JcanReefMan:
I managed to get the PWM library working with IDE 1.6 by doing the following:

  1. Download and unzip Arduino PWM Frequency Library v-05, here you will find three folders, "PWM", "PWM_lib_example" & "PWM_lib_resolution_example". Move the the two example folders into the PWM folder.

  2. The go to C:\Program Files (x86)\Arduino\libraries and copy the "PWM" folder here.

  3. When complete your libraries folder should look like the attached picture.

  4. Restart the ide and PWM should now be available in your list of libraries.

Thanks! This was a big help.

You're welcome

hello,
I am trying to use this library to set frequency for my pins, but this is also presetting some duty cycle as well. I want to change duty cycle through labview interface, It works well normally, but when I set the pin frequency through this library I don't have control over duty cycle. Any help?

There's no github with this library exported? Google Code is closed! Thanks

Not getting this to work with the NANO. The LED example only turns it on and off, it's acting like a binary 0 and 1, not fading just on and off blinking (255=on,0=off), not pwm'ing.

I was doing something wrong. IT works flawlessly. Just got to be sure of the pins you are using, that they are capable of the frequency you are requesting (8bit limitation on some pins)

RichMo:
I made a 1Hz to 2MHz Adjustable frequency PWM library for Arduino. Maybe this will help you.
http://www.rmcybernetics.com/projects/code/index.htm

Hi, I have seen your PWM library.
What I don't fully onderstand is how to use the pulsewidth.
If I have a frequency of 75Hz, what should the pulsewidth be for 50% dutycycle?
75Hz is 13,33 msec, so 50% is 6,67 msec, so pulsewidth should be about 6666667 nsec?

If I have a frequency of 75Hz, what should the pulsewidth be for 50% dutycycle?

To create a signal with 50/50 duty cycle, timer in CTC mode is more effective than timer in PWM mode.

but yes, calculation are correct. We can assume the width is of the HIGH signal, so a 25/75 duty is ~3200000ns

Good work runnerup! This library seems very useful!

I am having difficulties to make it compile in my Arduino 1.6.6 environment. I read the entire thread and searched the Arduino site on library implementation. I have used other libraries before and even written a few of my own, but i cant get this one properly installed. I have the folders and files arranged like this:

../libraries/PWM/examples/PWM_lib_example/PWM_lib_example.ino
../libraries/PWM/examples/PWM_lib_resolution_example/PWM_lib_resolution_example.ino

../libraries/PWM/utility/ATimerDefs.cpp
../libraries/PWM/utility/ATimerDefs.h
../libraries/PWM/utility/BTimerDefs.cpp
../libraries/PWM/utility/BTimerDefs.y

../libraries/PWM/keywords.txt
../libraries/PWM/PWM.h

What am I doing wrong? I'm sure it is right there in front of me and that I should see it, but I don't. Any help is much appreciated.

Never mind this post. My Arduino installation was corrupt. It compiles after I reinstalled the inviroment. Thank you for this awesome library!

Kind regards,
Simen

Good morning,

Is it possible, that there is an issue within this library?

I have tried to set the Frequency at PIN 2 form my Mega 2560 at 1kHz.

I have tried to manage this on several ways, by setting the "pinfrequency" and later the "timerfrequency".

I need to set the frequency to 1kHz at 11 Pins. Pin 13 to 11 are working well with this library.
But i have no idea, how to manage the Frequency at the other Pins.

#include <PWM.h>


void setup() {
  // put your setup code here, to run once:
InitTimers();
SetPinFrequency(2,500);
pinMode(2,OUTPUT);
}

void loop() {

//pwmWriteHR(testpin,20000);
pwmWrite(2,255);
  // put your main code here, to run repeatedly:

}

Can someone help me?

Here is the code:

Thank you very much!

Mikey3110:
Is it possible, that there is an issue within this library?

I have tried to set the Frequency at PIN 2 form my Mega 2560 at 1kHz.

I have tried to manage this on several ways, by setting the "pinfrequency" and later the "timerfrequency".

But what is going wrong? Have you tried to write 127 instead of 255 for testing?

How did you implement the library? I am having trouble getting it to compile.

Hey guys,

I have one quick simple silly question.
I'm thinking about using this library for controlling SSR relay (heating water in coffee maker).
Is there any benefit for using PWM library instead digitalWrite for my purpose?

Thank you and I'm really sorry for this kind of question

kalu1: digitalWrite will work fine for your task. Consult the datasheet of your SSRelay, should list a maximum frequency response of sorts. PWM'ing it may break it. Hope this helps. Usually that type of heater has almost two minutes pulse width. Generally though a Keurig uses a temp sensor to tell when the water is hot enough.

#include<PWM.h>
int32_t frequency =100000;
void setup()
{
InitTimersSafe() ;
bool success = SetPinFrequencySafe(9, frequency);
if(success) {
pinMode(13, OUTPUT);
digitalWrite(13,HIGH);
}
}

void loop() {
int sensorValue= analogRead(A0);
pwmWrite(9, sensorValue /4);
delay(30);

}
i am applying freq of 50hz from signal gen through a diode at a0 and i am getting pwm for +VE cycle( as i hv used diode),so i need to save this and then send it during -ve cycle in order to get total pwm
anyone could help with the code

Does anyone know if this library broke on arduino 1.6?

I've used this library for outputting 4 PWM signals for my custom aquarium LEDs, it worked fine for over a year.
(running on an arduino mega2560 with a WizNet 5500 ethernet board on top)
Now I wanted to make some changes, recompiled the code using the latest arduino libraries on Arch linux and it simply doesn't work anymore. No errors, but only pin 11 seems to work (but not properly), pins 5 / 6 and 44 simply give no signal as far as I can tell.
Using analogWrite instead of pwmWriteHR turns on the LEDs without problems.

Does anyone have any clue as to what the cause might be?

Wouter

hello all,

I plan to use an Arduino Uno as a PWM generator for testing external electronics at work --> need optimum signal quality. Therefore I have measured the PWM accuracy for both TIM2 (8-bit) and TIM1 (16-bit). Attached please find the results for frequency and duty cycle errors. Summary:

  • 8-bit TIM2 (at pin D3):

  • frequency error varies between -1.5%/+0.65%, depending (only) on frequency

  • duty cycle error varies between -1.6%/+0%, depending on frequency and duty cycle

  • 16-bit TIM1 (at pin D9 or D10):

  • frequency error of -0.15% is due to a clock mismatch is easily compensated in SW. The remaining "jitter" is +/-0.02% :slight_smile:

  • duty cycle error is below my measurement resolution of 0.1% :slight_smile:

Overall a very nice result for the 16-bit timer and a very easy to use library --> thanks a lot for your great work!! :slight_smile:

Just a few comments on the 8-bit timer:

  • it is not very surprising to see resolution errors with the 8-bit timer. However, from the above results I conclude that the lib uses only "phase correct" mode, not "fast PWM" mode...? As a result the next lower prescaler is available around ~64INC in worst case --> duty cycle error around 1/64~1.5% (which is what I measure)
  • attached please find a very simple software which uses both fast PWM and phase correct modes. This allows (slightly) smoother prescaler selection and thus lower DC error. Maybe you want to consider something similar for the PWM lib...?

Thanks again for your great library. Hope I could return something by the attached measurements.

Greetings from Germany,
Georg

calibration_Arduino.pdf (323 KB)

calibrate_PWM.zip (3.84 KB)