SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By Ozwurld
#170853
Hello all,

I found what is called the PWM library in the arduino playground. Could someone please plase check my code and see if it does what the comments say, i dont have an oscilloscope at the moment and theres no way i know of that i can use to see if the code really does output a PWM.
Code: Select all
/*

 Aurthor: Open Source :)
 Using the PWM library to generate a 25Khz PWM on pin 9
 
 */

#include <PWM.h>

int pwmPin = 9;
int32_t frequency = 25000; // desired frequency in Hertz

void setup()
{
  //initialize all timers except for 0, to save time keeping functions
  initTimerSafe();//why??? and what is time keeping??

  //sets the frequency for the specified pin i.e 25Khz
  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()
{
  //setting the duty to 50% with 8 bit pwm. 128 is 1/2 of 256
  pwmWrite(led, 128);//loop PWM forever??
}


thanks.
By trueRF
#170869
Well check at a slower frequency first. Set your frequency to 1Hz and check set the duty cycle as needed. Does the led turn off or on every second? Does it stay on or off for the appropriate amount of time? Once the duty cycle looks like it's working change the frequency to 2hz...5hz...10hz...etc. After 60Hz to 75Hz or so you probably wont be able to perceive the difference. If everything works up to that point odds are it'll work at 25KHz. At least for blinking an LED.

If you still aren't confident, you can rig up a small speaker to listen to the changes in frequency This is somewhat more complex, as you'll probably need some sort of amp and some Rs and Cs to modify the square wave from the arduino into something that can be played through a speaker without breaking something.

The code looks okay. I'm not entirely sure, but I don't think you need to include the pwnWrite in the loop. I think it's a fire and forget type thing. So if your goal is to power up and have the pwm start running at 50% then you can put that outside the loop. Once set it'll keep running until it is reset. Not that the current code wouldn't work it would just reset the duty cycle to 50% every time the loop cycles...like once every few microseconds.
By Ozwurld
#171069
Got it working, This is the code i am using, thanks everyone..

Note that i changed my frequency to 50KHz
Code: Select all
/*
50Khz PWM for the Buck Converter
 */

#include <PWM.h>

int led = 9;
int32_t frequency = 50000; // desired frequency in Hertz

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

  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()
{
  //setting 40% constant duty cycle (with 8 bit pwm. 102 is 0.4 of 255)
  pwmWrite(led, 102);
  delay(30);
}