Change frequency Arduino UNO

There’s a lot of forum posts about this, but they are mostly 5 years+

How do I change output frequency pwm on Arduino UNO to 1k, and 2k HZ?

this might help

https://www.arduino.cc/en/pmwiki.php?n=Tutorial/SecretsOfArduinoPWM

and this discussion too (and library)

1 Like

This makes it more confusing. I just need to know the code, or what library etc. to use to get 2k HZ out of my digital pins.

then you just need to read the second link and play with the PWM frequency library

#include <PWM.h>
const byte freqPin = 11;

void setup() {
  InitTimersSafe();
  SetPinFrequencySafe(freqPin, 1000); // 1000 ==> 1 Khz
  pwmWrite(freqPin, 128); // 50% duty
}

void loop() {}
1 Like

Okay so I have been troubleshooting and testing a lot. I think the problem was I did both measuring and outputting frequency on the same Arduino. I borrowed one extra and did frequencychanging on one, and measuring on the other.

This is the code i used and it worked:

#include <PWM.h>

int ut = 9;
int32_t frequency = 1500;

void setup() {
InitTimersSafe ();
SetPinFrequencySafe (ut, frequency);

}

void loop() {
pwmWrite (ut, 127); //går fra 0-255

}

great

(side note: you don’t need the pwmWrite() in the loop, just run it once in the setup() is enough)

1 Like

ok, thank you:)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.