PWM frequency library

Hi,

Thanks for your reply. Sorry, I'm new Arduino user (-:
I assumed that it's the PWM library since the PWM function are not working while running the program
Could it be connect to the Java version on my PC?

The message that i get is:

[/
http://downloads.arduino.cc/packages/package_index.json file signature verification failed. File ignored.
java.lang.NullPointerException
java.lang.RuntimeException: java.lang.NullPointerException
	at cc.arduino.contributions.libraries.ui.LibraryManagerUI.lambda$onUpdatePressed$0(LibraryManagerUI.java:212)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
	at cc.arduino.contributions.libraries.LibrariesIndexer.rescanLibraries(LibrariesIndexer.java:114)
	at cc.arduino.contributions.libraries.LibrariesIndexer.setLibrariesFolders(LibrariesIndexer.java:106)
	at processing.app.BaseNoGui.onBoardOrPortChange(BaseNoGui.java:665)
	at processing.app.Base.onBoardOrPortChange(Base.java:1295)
	at processing.app.Base$12.actionPerformed(Base.java:1522)
	at processing.app.Base.filterVisibilityOfSubsequentBoardMenus(Base.java:1561)
	at processing.app.Base.access$000(Base.java:82)
	at processing.app.Base$11.actionPerformed(Base.java:1494)
	at processing.app.Base.rebuildBoardsMenu(Base.java:1472)
	at processing.app.Base$8.onIndexesUpdated(Base.java:1327)
	at cc.arduino.contributions.libraries.ui.LibraryManagerUI.lambda$onUpdatePressed$0(LibraryManagerUI.java:210)
	... 1 more
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at cc.arduino.contributions.libraries.LibrariesIndexer.rescanLibraries(LibrariesIndexer.java:114)
	at cc.arduino.contributions.libraries.LibrariesIndexer.setLibrariesFolders(LibrariesIndexer.java:106)
	at processing.app.BaseNoGui.onBoardOrPortChange(BaseNoGui.java:665)
	at processing.app.Base.onBoardOrPortChange(Base.java:1295)
	at processing.app.Base.openLibraryManager(Base.java:1346)
	at processing.app.Base.lambda$rebuildImportMenu$2(Base.java:1060)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
	at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
	at java.awt.Component.processMouseEvent(Component.java:6533)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
	at java.awt.Component.processEvent(Component.java:6298)
	at java.awt.Container.processEvent(Container.java:2236)
	at java.awt.Component.dispatchEventImpl(Component.java:4889)
	at java.awt.Container.dispatchEventImpl(Container.java:2294)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
	at java.awt.Container.dispatchEventImpl(Container.java:2280)
	at java.awt.Window.dispatchEventImpl(Window.java:2746)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
	at java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:709)
	at java.awt.EventQueue$3.run(EventQueue.java:703)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.awt.EventQueue$4.run(EventQueue.java:731)
	at java.awt.EventQueue$4.run(EventQueue.java:729)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
]

Thanks,
Lior

Thanks, that's much better to be able to read the full error output in text. That is quite strange. I don't think it's related to your Java version but I'm not sure of the cause. Do you get that error only when you do Sketch > Include library > Manage libraries or do you also get it when you're trying to compile or upload your sketch?

Hi,

it was while Sketch > Include library > Manage libraries.
the message that i got while compiling is too long to be send here but part of it is:

[/avrdude: Version 6.3, compiled on Dec 16 2016 at 13:33:19
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"

         Using Port                    : COM9
         Using Programmer              : wiring
         Overriding Baud Rate          : 115200
         AVR Part                      : ATmega2560
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    10     8    0 no       4096    8      0  9000  9000 0x00 0x00
           flash         65    10   256    0 yes    262144  256   1024  4500  4500 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : Wiring
         Description     : Wiring
         Programmer Model: AVRISP
         Hardware Version: 15
         Firmware Version Master : 2.10
         Vtarget         : 0.0 V
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e9801 (probably m2560)
avrdude: reading input file "C:\Users\KAYKOV~1.AUT\AppData\Local\Temp\arduino_build_56302/Testing_the_motor_driver_Using_Timer1_for_Generate_CLK.ino.hex"
avrdude: writing flash (4662 bytes):

Writing | ################################################## | 100% 0.78s

avrdude: 4662 bytes of flash written
avrdude: verifying flash memory against C:\Users\KAYKOV~1.AUT\AppData\Local\Temp\arduino_build_56302/Testing_the_motor_driver_Using_Timer1_for_Generate_CLK.ino.hex:
avrdude: load data flash data from input file C:\Users\KAYKOV~1.AUT\AppData\Local\Temp\arduino_build_56302/Testing_the_motor_driver_Using_Timer1_for_Generate_CLK.ino.hex:
avrdude: input file C:\Users\KAYKOV~1.AUT\AppData\Local\Temp\arduino_build_56302/Testing_the_motor_driver_Using_Timer1_for_Generate_CLK.ino.hex contains 4662 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.62s

avrdude: verifying ...
avrdude: 4662 bytes of flash verified

avrdude done.  Thank you.

http://downloads.arduino.cc/packages/package_index.json file signature verification failed. File ignored.]

The output shows that the sketch has successfully compiled and uploaded. The error you're seeing:

http://downloads.arduino.cc/packages/package_index.json file signature verification failed. File ignored.]

will not cause any problems for compiling or uploading the sketch. If the code isn't working correctly that has nothing to do with that error. That error does indicate a problem with Boards Manager and possibly Library Manager this may cause you to be unable to use those features to get updatable boards/libraries notifications or install boards/libraries. Often it's possible to do those processes manually if needed so you may be able to just ignore that. I suspect it's some sort of a network issue. It's trying to verify the downloaded package index file and the verification is failing.

Edit:

I found out there is an example how to widen resolution and use 16 bit register fully , with pwmWriteHR ....

sorry for OT

junktronix:
I'm curious what you fixed. I've noticed that occasionally the PWM doesn't start immediately. The PWM output will be high or low for 5-10mS before the pulses start.

I am using an Arduino Uno with this library. Does anyone know what causes these errors or how to fix them? Attached is an image showing the significant error periodically occurring at the beginning of the PWM signal of an output when viewed on a logic analyzer.

PWM Error.jpg

Hi,

I have to make a project where PWM frequency should be around 8-9kHz on one pin and also servo library is involved. To test how PWM frequency library works with servo library I whipped together simple sketch with a few lines borrowed from examples. I used Arduino Nano.

When servo is attached to the pin 9 and SetPinFrequencySafe to pins 10 or 11, there is no PWM on these pins; servo works OK
When servo is attached to the pin 9 and SetPinFrequencySafe to pin 3, there is 9kHz PWM on pin 3, but when I turn the pot to change the duty cycle, the actual change happens with delay of 3-4 seconds, very weird indeed; servo works OK

So the question is, can PWM frequency library and servo library work reliably together and what pins should I use. I need 1 pin for PWM and 1 pin for servo.

Thank you beforehand,
Fred

Here is the code I used for testing

#include <PWM.h>
#include <Servo.h>  

int32_t frequency = 9000; //frequency (in Hz)

int servoPin = 9;
Servo servo; 
int angle = 0; // servo position in degrees 

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

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

  
  servo.attach(servoPin); 
  
}

void loop()
{
  
  int sensorValue = analogRead(A0);
  pwmWrite (10, sensorValue / 4);
  
  delay(30);   
  
// scan from 0 to 180 degrees
  for(angle = 0; angle < 180; angle++)  
  {                                  
    servo.write(angle);               
    delay(15);                   
  } 
  // now scan back from 180 to 0 degrees
  for(angle = 180; angle > 0; angle--)    
  {                                
    servo.write(angle);           
    delay(15);       
  } 

  
}

Please Help! I got the code below to work. I sent the PWM signal into Motor Driver BTS7960 43A. The motor speeds up and slows down as expected. But I want to control the DUTY CYCLE to change speed NOT the frequency. PWM Noise must not be heard so I have set the PWM frequency to 21,000hz.

Thanks

Mimics the fade example but with an extra parameter for frequency. It should dim but with a flicker
because the frequency has been set low enough for the human eye to detect. This flicker is easiest to see when
the LED is moving with respect to the eye and when it is between about 20% - 60% brighness. The library
allows for a frequency range from 1Hz - 2MHz on 16 bit timers and 31Hz - 2 MHz on 8 bit timers. When
SetPinFrequency()/SetPinFrequencySafe() is called, a bool is returned which can be tested to verify the
frequency was actually changed.

This example runs on mega and uno.
*/

#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);
}

hi,
I have read every post of every page but can not able to compile library with ide 1.8
getting, 'InitTimersSafe' was not declared in this scope
can someone run it in latest ides ?

Hi,
I'm seeing strange behavior of the pwmWriteHR() function on a Mega (not an official Arduino one). I'm not 100% what the board model is. It has an ATmega1280 processor though.

With attached code I see that pin 3 is getting the PWM set for pin 2 and pin 8 gets the output set for pin 7. Though I have to set a PWM value for 3 and 8 or no output will be given. So setting a value for a pin enables output but it gets the value set for the previous pin.

When I use analogWrite() I'm seeing normal behavior.

Arduino IDE 1.8.1 on Win10.

Is it my code or a library bug?

#include <PWM.h>

int led = 11;

void setup() {

 Serial.begin(115200);

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

  //SetPinFrequency(led, 15625);  //setting the frequency
  SetPinFrequency(2, 31250);
  SetPinFrequency(3, 31250);
  SetPinFrequency(5, 31250);
  SetPinFrequency(6, 31250);
  SetPinFrequency(7, 31250);
  SetPinFrequency(8, 31250);
  
  Serial.println("TIMER1");
  uint16_t frequency = Timer1_GetFrequency();
  uint16_t decimalResolution = Timer1_GetTop() + 1;
  uint16_t binaryResolution = GetPinResolution(led); //this number will be inaccurately low because the float is being truncated to a int

  char strOut[75];
  sprintf(strOut, "Frequency: %u Hz\r\n Number of Possible Duties: %u\r\n Resolution: %u bit\r\n", frequency, decimalResolution, binaryResolution );

  Serial.println(strOut);

  Serial.println("TIMER2");
   frequency = Timer2_GetFrequency();
   decimalResolution = Timer2_GetTop() + 1;
   binaryResolution = GetPinResolution(led); //this number will be inaccurately low because the float is being truncated to a int

   strOut[75];
  sprintf(strOut, "Frequency: %u Hz\r\n Number of Possible Duties: %u\r\n Resolution: %u bit\r\n", frequency, decimalResolution, binaryResolution );

  Serial.println(strOut);

  Serial.println("TIMER3");
   frequency = Timer3_GetFrequency();
   decimalResolution = Timer3_GetTop() + 1;
   binaryResolution = GetPinResolution(led); //this number will be inaccurately low because the float is being truncated to a int

   strOut[75];
  sprintf(strOut, "Frequency: %u Hz\r\n Number of Possible Duties: %u\r\n Resolution: %u bit\r\n", frequency, decimalResolution, binaryResolution );

  Serial.println(strOut);
  
  Serial.println("TIMER4");
   frequency = Timer4_GetFrequency();
   decimalResolution = Timer4_GetTop() + 1;
   binaryResolution = GetPinResolution(led); //this number will be inaccurately low because the float is being truncated to a int

   strOut[75];
  sprintf(strOut, "Frequency: %u Hz\r\n Number of Possible Duties: %u\r\n Resolution: %u bit\r\n", frequency, decimalResolution, binaryResolution );

  Serial.println(strOut);

 
}


unsigned int value = 0;
 int value2 = 0;
 int value3 = 0;
int dir = 0;


void loop() {


  pwmWriteHR(2, 32000); // ?
  pwmWriteHR(3, 0); // ?

  pwmWriteHR(7, 5000);
  pwmWriteHR(8, value);

  value = (value + 10) % 65536;
  value2 = (value2 + 5) % 65536;

//delay(1);

}

My problem posted just above is solved by texmit's fix earlier in this thread: PWM frequency library - #136 by texmit - Libraries - Arduino Forum

That fix was posted almost a year ago. Too bad the library owner seems to have orphaned it. It's a mighty useful one.

(deleted)

Dear Brothers, firstly sorry for bad english, my arduino is Arduino MEGA 2560, i want to learn how to change frequency and search the library for it, and find the google code link below :

https://code.google.com/p/arduino-pwm-frequency-library/downloads/list

and downloaded the 5th Version of it >> unzip it in the arduino.ide library on my computer, im sure that i follow this step of adding library :

btw my arduino.ide version is 1.8.2

why i cannot compile or even upload the example program ?
it said :

Arduino: 1.8.2 (Windows Store 1.8.3.0) (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users*****\Arduino\sketch_jun07a\sketch_jun07a.ino:1:17: fatal error: PWM.h: No such file or directory

#include <PWM.h>

^

compilation terminated.

exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

my teacher said that i must search the library for arduino mega instead of arduino uno (?), is that right ? is there any solution ?

Your teacher is wrong i'm afraid as both the UNO and MEGA use similar AVR architecture.

There is a good comparison link HERE

Clearly The mega has more pins due to the difference in processor but the lib should still be capable of handling that aspect.

More likely the library has been put in the wrong location.
Using <PWM.h> requires the files to be in the /libraries folder.
Using "PWM.h" requires the files to be in same folder as the sketch.

Hi Everyone, I'm new here ;]

Just wanted to say that this is an excellent PWM library and you guys are an awesome community!

I was wondering if anyone has tried using interrupts with this library? Let's say to just count the number of positive-edge pulses, then if a threshold is reached, stop the pwm?

Perhaps something along the lines of ISR(Timer1_compa_vect)?

Thanks,
Oyakodon

Hi there,

Do you guys think that's possible to make a Frequency sweep with this librarie?

I was trying to build one, but I'm not sure of what I am missing, or if it is possible indeed.

please, If you want to take a look in my code bellow:

     FPWM = lgf[lx]; // This is the frequency of the PWM that im obtainning from another function.

     Init: InitTimersSafe();
   //sets the frequency for the specified pin
     success = SetPinFrequencySafe(SPWM, FPWM);
   //initialize all timers except for 0, to save time keeping functions
   
   if (success)   //here its where I would change my PWM frequency, if success.
   {
     cdelay = 1;
     VL = 0;
     sumvL = 0;
     if (readFlag == 1)
     {
       readFlag = 0;
     }
     while (VL <= cdelay)
     {
       //use this functions instead of analogWrite on 'initialized' pins
       pwmWrite(SPWM, LPWM);  // more precisely here.
       if (readFlag == 1)
       {
         sumvL = sumvL + analogVal;
         VL++;
         readFlag = 0;
       }
     }
     Vs[lx] = sumvL / VL;
   }
   else 
   goto Init; //If success is not ok I would go back to InitTimersSafe() line.

Thank you for the attention and sorry for bad english.

@ hugoabreu

sudo code ?

Hi.

This code I've put its inside the for loop. I didn't put everything couse, its too long and it doesn't matter.

My main question is if you think that we can change frequencies of PWM inside a for loop.

Ty

@hugoabreu

All code matters.
Without it nobody can tell if its good or bad or could be improved in any way or even broken into better segments etc.

And yes it should be possible to call a change inside a loop.
However you should realise that in doing so timers and interrupts could also be changed with unforeseen results unless you account for them.