Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
guru-florida committed May 22, 2019
0 parents commit d7857ce
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# PWM Freak

This is very small library for configuring the PWM frequency for AVR based Arduinos. I didnt write it, It is taken from the playground.arduino.cc site and provided in this library for your convenience.

# Why Change the Default Frequency?

If you are controlling a motor, voice coil or any sort of inductive device you
are likely hearing a humming sound while using the default PWM frequency. This
is simply because the switching of the inductive device is within our hearing
range. By increasing the frequency to 20Khz (or higher like 31.2Khz) we'll put it
well above our hearing range...but dont expect the dogs to like it!

# Setting PWM base frequency

Simply add the library using the Arduino library manager and include the PWMFreak.h
include file.

```
#include <PWMFreak.h>
```

Then in your setup function call the function to set the frequency for the given
pin. The function will figure out what timer is backing that pin and set the PWM
frequency. Other PWM pins that rely on the same timer will also be affected!

```
void setup()
{
// change PWM for pin 6 to use divisor value 1
setPwmFrequency(6, 1);
}
```

The resulting frequency is equal to the base frequency divided by
the given divisor:
- Base frequencies:
o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
o The base frequency for pins 5 and 6 is 62500 Hz.
- Divisors:
o The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64,
256, and 1024.
o The divisors available on pins 3 and 11 are: 1, 8, 32, 64,
128, 256, and 1024.

PWM frequencies are tied together in pairs of pins. If one in a
pair is changed, the other is also changed to match:
* Pins 5 and 6 are paired on timer0
* Pins 9 and 10 are paired on timer1
* Pins 3 and 11 are paired on timer2

Note that this function will have side effects on anything else
that uses timers:
* Changes on pins 3, 5, 6, or 11 may cause the delay() and
millis() functions to stop working. Other timing-related
functions may also be affected.
* Changes on pins 9 or 10 will cause the Servo library to function
incorrectly.

Thanks to macegr of the Arduino forums for his documentation of the
PWM frequency divisors. His post can be viewed at:
https://forum.arduino.cc/index.php?topic=16612#msg121031


2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
setPwmFrequency KEYWORD2

10 changes: 10 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=PWMFreak
version=0.2
author=Colin MacKenzie
maintainer[email protected]
sentence=Configures the PWM frequency on a pin
paragraph=A very small library for configuring the PWM frequency for AVR based Arduinos. At least some of this code has been copied from the Internet, see PWMFreak.h for attribution.
category=Timing
url=https://github.com/flyingeinstein/PWMFreak
architectures=avr
includes=PWMFreak.h
71 changes: 71 additions & 0 deletions src/PWMFreak.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <Arduino.h>

// Taken from:
// https://playground.arduino.cc/Code/PwmFrequency/

/**
* Divides a given PWM pin frequency by a divisor.
*
* The resulting frequency is equal to the base frequency divided by
* the given divisor:
* - Base frequencies:
* o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
* o The base frequency for pins 5 and 6 is 62500 Hz.
* - Divisors:
* o The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64,
* 256, and 1024.
* o The divisors available on pins 3 and 11 are: 1, 8, 32, 64,
* 128, 256, and 1024.
*
* PWM frequencies are tied together in pairs of pins. If one in a
* pair is changed, the other is also changed to match:
* - Pins 5 and 6 are paired on timer0
* - Pins 9 and 10 are paired on timer1
* - Pins 3 and 11 are paired on timer2
*
* Note that this function will have side effects on anything else
* that uses timers:
* - Changes on pins 3, 5, 6, or 11 may cause the delay() and
* millis() functions to stop working. Other timing-related
* functions may also be affected.
* - Changes on pins 9 or 10 will cause the Servo library to function
* incorrectly.
*
* Thanks to macegr of the Arduino forums for his documentation of the
* PWM frequency divisors. His post can be viewed at:
* https://forum.arduino.cc/index.php?topic=16612#msg121031
*/

// sets the PWM frequency for a given pin
// pulled from the Internet
void setPwmFrequency(int pin, int divisor) {
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode;
} else {
TCCR1B = TCCR1B & 0b11111000 | mode;
}
} else if(pin == 3 || pin == 11) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 32: mode = 0x03; break;
case 64: mode = 0x04; break;
case 128: mode = 0x05; break;
case 256: mode = 0x06; break;
case 1024: mode = 0x07; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}

0 comments on commit d7857ce

Please sign in to comment.