PWM frequency library

I don't know why, but i feel this library is deprecated somehow. The github repository do no allow to send messages / issue to the owner.

Possibly the library work with an older arduino IDE?

Has anyone been able to compile this for the Atmega1284? I'm using the MightyCore bootloader and I get the following message every time I try and compile:

Arduino: 1.8.4 (Mac OS X), Board: "ATmega1284, Standard, 1284, 2.7v, Disabled (default), 20 MHz external"

Build options changed, rebuilding all
sketch/PWM_lib_example.ino.cpp.o: In function setup': /Users/BradJackson/Google Drive/Jackson Ampworks/Arduino/PWM_lib_example/PWM_lib_example.ino:24: undefined reference to InitTimersSafe()'
/Users/BradJackson/Google Drive/Jackson Ampworks/Arduino/PWM_lib_example/PWM_lib_example.ino:27: undefined reference to SetPinFrequencySafe(signed char, unsigned long)' sketch/PWM_lib_example.ino.cpp.o: In function loop':
/Users/BradJackson/Google Drive/Jackson Ampworks/Arduino/PWM_lib_example/PWM_lib_example.ino:39: undefined reference to `pwmWrite(unsigned char, unsigned char)'
collect2: error: ld returned 1 exit status
exit status 1
Error compiling for board ATmega1284.

Hello

I am using arduino nano can i generate 7 mhz pwm signal with 50%duty cycle ?
if yes then guide me How Can i generate ?

Best Regards
seggi.

This Library didn't work. The following message was appeared:

The library "Arduino PWM Frequency Library v_05" cannot be used.
Library names must contain only basic letters and numbers.
(ASCII only and no spaces, and it cannot start with a number)

Please, I need it!

Hi, could anyone help me?
I have noticed that there's no PWM.cpp file. The library wasn't installed.

You extracted the filename of the zip file directly into your library directory as "Arduino PWM Frequency Library v_05"
You should simply make a directory under libraries and call it PWM for example.
C:\Program Files\arduino-1.8.1\libraries\PWM

Then copy all the stuff in under the PWM directory into this new library PWM directory, and copy PWM_lib_example and PWM_lib_resolution_example into there as well.

It should look like this when your done ...

Directory of C:\Program Files\arduino-1.8.1\libraries\PWM

11-May-2018 16:49 .
11-May-2018 16:49 ..
16-Aug-2012 16:55 2,208 keywords.txt
24-Sep-2012 15:24 4,873 PWM.h
11-May-2018 14:38 PWM_lib_example
11-May-2018 14:38 PWM_lib_resolution_example
11-May-2018 16:49 0 showDir.txt
11-May-2018 14:38 utility
3 File(s) 7,081 bytes
5 Dir(s) 113,831,374,848 bytes free

Hello
I like to using this pwm.h librari variable phase shifting arduino between two pwm chanel.
I like the shifing variable 0-180 degrees.

Somebody doing same?

thanks!

Cannot compile the code for Atmega8. While I tried compiling for Atmega168, the code uses 2720 bytes of space and compiles successfully and works fine. Anyone please help me.

post your code if you want help

Hi

I am trying to lower the Arduino Uno PWM frequencies to drive 4 x solenoid valves. Ideally, pins 5 & 6 are untouched to retain timer0 function. However, understand that creating custom frequencies would sacrifice 2 pins on Arduino, which I have found out to be pins 6 and 11.

In the original post by runnerup:
"If you don't want to sacrifice any 8bit PWM pins, don't call the initialize function for that timer, try changing the prescaler to manipulate frequency instead. "

So if I would like to leave pins 5 and 6 untouched and change PWM frequency for pins 3, 9, 10 AND 11 to 31Hz? Do I simply use this setup to change the timer prescaler and the PWM frequencies for the 4 pins would be lowered to 31Hz?

void setup()
{
Timer1_SetPrescaler(ps_1024);
Timer2_SetPrescaler(psalt_1024);
}

I tried doing that and alternatively using this function PwmFrequency to also change the prescaler. But neither works.

So is this a limitation such pins 6 and 11 cannot be used?

Appreciate some clarification/ advice, thanks!

Great Library

Some problems i am facing:
1- Cannot modify the frequency of pin 11, the bool doesn't return success
2- Can't get the resolution on pin 3 and 11 although the timer gives me resolution

runnerup:
The Leonardo has a different CPU architecture. That means different timers with different properties that I would have to wrap.
So far, Arduino microcontrollers use three main architectures 'groups'. Arduino Mega and ADK are of the AVR mega 1280/1281/2560/2561 group, which are all identical except for the amount of memory they hold. The Leonardo uses ATmega32u4. And pretty much everything else uses 88/168/328 variants.
I have an Uno and a Mega, which means I was able to build and test for every architecture except the Leonardo. If there is enough interest in this project, I would be willing to try to get it working on the Leonardo. But I have no reliable way too test it in real life to make sure it works.

I am using Atmega 64/128/256 with MegaCore , your library is very necessary for me for one project. But it does not support this processor. Can you help me? Thanks.

I've tried implementing the library and i get an error, i thought this was supposed to work for all Arduinos?

@Ayman1993

Not ALL libraries are meant for ALL Arduinos.
Which Library the one in question already or a different one ?

What board are you trying to use your lib with ?

Ok less questions and more directions.
Please read the sticky posts at the top of forum sections and add the details they recommend.

Hello
This library is very useful to me.
I have a question.

Is there a way to select a frequency as a float rather than an integer?

I need a frequency of 10.5 Hz.

Thank you.

Is there a way to select a frequency as a float rather than an integer?

Not in the library API

I need a frequency of 10.5 Hz.

This frequency is low so this is achievable (at least approchable to a correct resolution) so you could manually play with the prescaler, Timer etc -> see this article for details and the very beginning of this current discussion of the PWM library

This would be my first attempt at using a library like this.
Can someone explain the limitations of creating a specific frequency when using this library?
I keep seeing the phrase"resolution" tossed around but I cannot really grasp what that means relative to a clock signal.
I am trying to generate two clocks of 104 khz and 165khz on a UNO.
When I run the following code I get close but there is some deviation.
Tweaking the two frequency variables slightly does nothing or it makes it jump past my target freq.
Per my oscilloscope I am getting 105.290 khz & 166.710 khz. This is within 1.25% of my target and would probably be fine for my application but I really want to know what I would have to do if my tolerances needed to be tighter.
Is this expected behavior or is my programming a hot mess?

#include <PWM.h>
#include <FastLED.h>

#define LED_PIN     4
#define NUM_LEDS    300
#define BRIGHTNESS  64
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 300


//------

int fosc1 = 9;                // the pin that the timer1 is attached to
int fosc2 = 3;                // the pin that the timer2 is attached to
int32_t frequency1 = 165000;   //frequency (in Hz)
int32_t frequency2 = 104000;   //frequency (in Hz) 

//-------

CRGBPalette16 currentPalette;
TBlendType    currentBlending;

extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;


void setup() {
    delay( 3000 );     // power-up safety delay
    FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
    FastLED.setBrightness(  BRIGHTNESS );
    
    currentPalette = RainbowColors_p;
    currentBlending = LINEARBLEND;

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

    //sets the frequency for the specified pin
    bool success1 = SetPinFrequencySafe(fosc1, frequency1);
    bool success2 = SetPinFrequencySafe(fosc2, frequency2);
  
    //if the pin frequency was set successfully, turn pin 13 on
    if(success1 && success2){
    pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);
  }
    
    pwmWrite(fosc1, 128);
    pwmWrite(fosc2, 128);
    
}


void loop()

Resolution is basically how close you can come to the frequency you are looking at delivering.
How close you can get depends on the clock speed of your arduino, how high the target frequency is (you play with the prescaler) and which timer you use (8 bit or 16 bit)

Go back and Read the first page of this discussion, especially post #7

Hi everyone,

I want to use the library on my Mega 2560 to generate a PWM of 20kHz. I have been able to do so on my Uno but my waveform is getting distorted in Mega. I will really appreciate some help in understanding where I am going wrong.

The code I used is as follows:

#include <PWM.h>


void setup()
{
  // put your setup code here, to run once:
  pinMode(3, OUTPUT);
  InitTimersSafe();
  bool success = SetPinFrequency(3,20000);
}

void loop() 
{
  // put your main code here, to run repeatedly:
  pwmWrite(3, 127);
  
}

Hi,
I am using this library to change frequency using pin9 on an arduino mini pro and is working fine but I want to ask if is possible to change pwm resolution using this library?

I mean now I can set values between 1 to 255 for duty cycle, that means 8 bits. Can this be somehow changed to have 10 bits (1024 values) or even more? Is there any way doing that with this library?

Thank you