Pages: 1 2 3 [4] 5 6   Go Down
Author Topic: PWM frequency library  (Read 69927 times)
0 Members and 1 Guest are viewing this topic.
Toledo, OH
Offline Offline
God Member
*****
Karma: 36
Posts: 514
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
There are technically libraries out there that allow you to set the PWM frequency.  They are, however, typically labeled as audio "tone" libraries.  It doesn't matter if the library is designed for audio, it's still just a PWM library.  I've written some of these libraries, some of which will output PWM signals at up to the gigahertz range.

This interests me.  Particularly the part about the gigahertz range.  I haven't heard of this before.  Could you point me to somewhere that explains how this is done?

Sorry, must have been drunk, I meant MEGAhertz, not gigahertz.

One of my tone libraries (toneAC) does PWM control of two pins alternating at up to 2.66 MHz with an ATmega running at 16MHz.

Official site
Arduino Playground
Support forum

Sorry about the confusion.

Tim
« Last Edit: April 29, 2013, 12:24:14 pm by teckel » Logged

Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

CR
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!!
I just want to say, thanks for the library!!!, it seems to be very helpful, because, as you said, you did found a necessity, and well, you are now giving us a solution... Well, then, I'm an Arduino begginer smiley-kitty, and I'm developing some project that involves the use of Ardu...and also the PWM management, thus, its frequency.
I did download your library, but, when I compile the first example, in the IDE message area says:
"PWM_lib_example.cpp: In function ‘void setup()’:
PWM_lib_example:23: error: ‘InitTimersSafe’ was not declared in this scope
PWM_lib_example:26: error: ‘SetPinFrequencySafe’ was not declared in this scope
PWM_lib_example.cpp: In function ‘void loop()’:
PWM_lib_example:38: error: ‘pwmWrite’ was not declared in this scope"


Also I was reading some comments, that it actually works perfectly smiley-cry...what you think could be the problem?? It seems like the IDE does not detect the #include <PWM.h>...
Thanks!!
Logged

Offline Offline
Newbie
*
Karma: 7
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!!
I just want to say, thanks for the library!!!, it seems to be very helpful, because, as you said, you did found a necessity, and well, you are now giving us a solution... Well, then, I'm an Arduino begginer smiley-kitty, and I'm developing some project that involves the use of Ardu...and also the PWM management, thus, its frequency.
I did download your library, but, when I compile the first example, in the IDE message area says:
"PWM_lib_example.cpp: In function ‘void setup()’:
PWM_lib_example:23: error: ‘InitTimersSafe’ was not declared in this scope
PWM_lib_example:26: error: ‘SetPinFrequencySafe’ was not declared in this scope
PWM_lib_example.cpp: In function ‘void loop()’:
PWM_lib_example:38: error: ‘pwmWrite’ was not declared in this scope"


Also I was reading some comments, that it actually works perfectly smiley-cry...what you think could be the problem?? It seems like the IDE does not detect the #include <PWM.h>...
Thanks!!

The compiler is telling you that it can't find the functions you are calling.
Assuming your includes are correct (which is the case if you are using an example), then you didn't add the library to the 'libraries' folder.

If you don't understand how libraries work in the Arduino environment try looking at this: http://www.arduino.cc/en/Hacking/Libraries
Logged

CR
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, thank you so much for your help, I already solve the problem....now, I'm traying to manipulate the "frequency" value from the Serial monitor...but I can't make it work, here's the code:
PD: Any help, THANKS!!!


/************************** TEC -2013 **************************
   
Este sketch es una variacion del PWM_lib_example de la biblioteca PWM,
donde se controla la frecuencia desde el monitor serial del IDE,
dando con ello un poco mas de practicidad, y mayor manejo sobre la
frecuencia deseada.

********************************************************************
*/

#include <PWM.h>

int brightness = 0;     //how bright the LED is.
int fadeAmount = 5;     //how many points to fade the LED by

void setup()
{
  //Inicializa todos los timers del Arduino, excepto el 0, para mantener las funciones de tiempo del micro
  Serial.begin(9600);
  InitTimersSafe(); 
 
}

void loop()
{
  int32_t frequency;
  if (Serial.available())
  {   
   
    frequency = Serial.read();
    SetPinFrequency(led, frequency);
   
  }
  pwmWrite(led, brightness);

  brightness = brightness + fadeAmount;

  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }     
 
  delay(30);   
   
  }
Logged

Northants - UK
Offline Offline
Sr. Member
****
Karma: 1
Posts: 258
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have done the following program (as initial steps for a thermostat program)

Code:
#include <PWM.h>



int T;
int32_t frequency = 500; //initialize the pwm output at 100Hz

void setup(){
  InitTimersSafe();
  SetPinFrequencySafe(11, frequency);
}


void loop(){

T = analogRead(0);

pwmWrite(11, 50); // write the duty to the output

delay(2000);

}

Nothing happens, other than the output being full on. I can't use the standard PWM as it makes the fan (and powersupply transformer) hum. Mind you even the standard analogwrite has stopped working or does the library do that ?
« Last Edit: June 12, 2013, 06:58:39 am by sparkylabs » Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I only can said thanks runnerup!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, thank you so much for your help, I already solve the problem....now, I'm traying to manipulate the "frequency" value from the Serial monitor...but I can't make it work, here's the code:
PD: Any help, THANKS!!!


/************************** TEC -2013 **************************
   
Este sketch es una variacion del PWM_lib_example de la biblioteca PWM,
donde se controla la frecuencia desde el monitor serial del IDE,
dando con ello un poco mas de practicidad, y mayor manejo sobre la
frecuencia deseada.

********************************************************************
*/

#include <PWM.h>

int brightness = 0;     //how bright the LED is.
int fadeAmount = 5;     //how many points to fade the LED by

void setup()
{
  //Inicializa todos los timers del Arduino, excepto el 0, para mantener las funciones de tiempo del micro
  Serial.begin(9600);
  InitTimersSafe(); 
 
}

void loop()
{
  int32_t frequency;
  if (Serial.available())
  {   
   
    frequency = Serial.read();
    SetPinFrequency(led, frequency);
   
  }
  pwmWrite(led, brightness);

  brightness = brightness + fadeAmount;

  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }     
 
  delay(30);   
   
  }


Hi! I have the same prolem as you. What did you do to solve it?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, thank you so much for your help, I already solve the problem....now, I'm traying to manipulate the "frequency" value from the Serial monitor...but I can't make it work, here's the code:
PD: Any help, THANKS!!!


/************************** TEC -2013 **************************
   
Este sketch es una variacion del PWM_lib_example de la biblioteca PWM,
donde se controla la frecuencia desde el monitor serial del IDE,
dando con ello un poco mas de practicidad, y mayor manejo sobre la
frecuencia deseada.

********************************************************************
*/

#include <PWM.h>

int brightness = 0;     //how bright the LED is.
int fadeAmount = 5;     //how many points to fade the LED by

void setup()
{
  //Inicializa todos los timers del Arduino, excepto el 0, para mantener las funciones de tiempo del micro
  Serial.begin(9600);
  InitTimersSafe(); 
 
}

void loop()
{
  int32_t frequency;
  if (Serial.available())
  {   
   
    frequency = Serial.read();
    SetPinFrequency(led, frequency);
   
  }
  pwmWrite(led, brightness);

  brightness = brightness + fadeAmount;

  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }     
 
  delay(30);   
   
  }


Hi! I have the same prolem as you. What did you do to solve it?

I'm not sure how the other guy solved it. But this is how I solved it:
read http://arduino.cc/en/Guide/Libraries thoroughly
the library .h and/or .cpp files should be in their own folder and no more than one directory level deep. see the description on the bottom of the page i linked to.
once I rearranged my files, it worked.
Just make sure that your .h or .cpp file is in its own folder in the library folder, and not in subdirectories under its own folder
Logged

Anaheim CA.
Offline Offline
Faraday Member
**
Karma: 47
Posts: 2927
...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

for 'local' h and cpp files the format is #include "myFile.h" instead of  #include <myFile.h>.. When I have library issues I move everything to the sketch folder and change the left and right 'arrows' to double quotes for the included files that are in the sketch folder... Works for me quite well, YMMV...

Doc
Logged

--> WA7EMS <--
“The solution of every problem is another problem.” -Johann Wolfgang von Goethe
I do answer technical questions PM'd to me with whatever is in my clipboard

Offline Offline
Jr. Member
**
Karma: 1
Posts: 59
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi..
I am getting an error   InitTimersSafe() was not declared in this scope while running this code .
Code:
#include "PWM.h"

//use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz
int led = 9;                // the pin that the LED is attached to
int brightness = 0;         // how bright the LED is
int fadeAmount = 5;         // how many points to fade the LED by
int32_t frequency = 35; //frequency (in Hz)

void setup()
{
  //initialize all timers except for 0, to save time keeping functions
  InitTimersSafe();

  //sets the frequency for the specified pin
  bool success = SetPinFrequencySafe(led, frequency);
  
  //if the pin frequency was set successfully, turn pin 13 on
  if(success) {
    pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);    
  }
}

void loop()
{
  //use this functions instead of analogWrite on 'initialized' pins
  pwmWrite(led, brightness);

  brightness = brightness + fadeAmount;

  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }    
  
  delay(30);      
}

I have already added the PWM frequency library still i am getting the error.
I am using Arduino 1.0.5 version
« Last Edit: October 09, 2013, 11:03:33 am by ADI89 » Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 59
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Some one please help me...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to use this library to create a variable frequency quadrature output. I have confirmed I can set two outputs with the same frequency and different duty cycles but is there a way to offset on output by 1/2 pulse width? I am an arduino/programming/microcontroller noob. I tried spacing pwmWrite()s out with a delay() but it did not work smiley-sad

Some one please help me...

Kind of an old post and you may have figured this out but I compiled your code just fine. Double check your library was added properly.
« Last Edit: November 19, 2013, 04:23:26 pm by krgoodwin » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys, i am trying to control a dc motor with the pwm library (Arduino mega 2560) but the frequency that is generated it's too low because the motor doesn't move. I tried with 20KHZ frequency and didn't work. Here is my code:

#include<PWM.h>
int motor = 11 , valor_adc , tension;
int32_t frequency = 20000;
void setup(){
InitTimersSafe();
bool success = SetPinFrequencySafe(motor,frequency);
}
void loop(){
valor_adc=0;
valor_adc=analogRead(A0);
tension=valor_adc/4;
pwmWrite(motor,tension);
delay(30);
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys,

I did post in another area of the forum, but in hindsight really should have posted here.

I'm not sure how I go about getting the thread moved, so thought I would post a very brief summary:

I have an Uno and need to output two squarewaves - one at 3040Hz and one at 2010Hz. I'm having problems doing it using this library and must be doing something really stupid!

Anyway, the post is here: http://forum.arduino.cc/index.php?topic=213416.0

Thanks Jay smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much for this library runnerup. I am making a project where I will need more than the normal 256 steps of PWM resolution, and this is exactly what I needed.

One question though, I'm a little confused by why the resolution has to decrease as frequency increases. Is this an actual limitation having to do with the clock speed of the chip and how quickly it runs through the timers?

You said earlier it had to do with the way the library is set up to allow variability of both the resolution and the frequency. Does this mean that if I manually set the registers in my program that I could achieve the full 16 bits of resolution that the 16 bit timers are capable of? I really don't care much about changing the frequency, just the resolution.

The more resolution the better for this project.
Logged

Pages: 1 2 3 [4] 5 6   Go Up
Jump to: