Go Down

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

strager_smz

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? 

q2dg

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

donperry

#107
Nov 28, 2015, 08:18 pm Last Edit: Nov 29, 2015, 07:30 pm by donperry
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)

sjimmie

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?

68tjs

Quote
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.

lestofante

#110
Dec 03, 2015, 10:02 pm Last Edit: Dec 03, 2015, 10:03 pm by lesto
but yes, calculation are correct. We can assume the width is of the HIGH signal, so a 25/75 duty is ~3200000ns
Guida per principianti http://playground.arduino.cc/Italiano/newbie
Unoffical Telegram group https://t.me/genuino

SimenC

#111
Dec 17, 2015, 10:09 am Last Edit: Jan 11, 2016, 03:06 pm by SimenC Reason: Question no longer apply
Good work runnerup! This library seems very useful!

Quote
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

Mikey3110

#112
Jan 02, 2016, 12:49 pm Last Edit: Jan 02, 2016, 01:44 pm by Mikey3110
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.

Code: [Select]

#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!

SimenC

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.

kalu1

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

rinkrides

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.
Electronics Engineering, "EE=MC^(OMG x WTF?!)"
Professional Auto Collision/Custom tech

Harshaks

#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

benv666

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

gicking

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% :)
    • duty cycle error is below my measurement resolution of 0.1%  :)


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

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

atmelino

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
I got it to work following the instructions from post #102:

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.



Go Up