Pulse Width Modulation frequency control

From Interaction Station Wiki
Jump to navigation Jump to search

PWM frequency control

Pulse Width Modulation can produce hum in your motor, because it turn on and off your motor at a specific frequency. If you don't like the sound of your motor, you can change the frequency by placing one of the lines of code underneath in front of your Arduino code. When you change the PWM frequency of Pin 5 and 6, it changes how the delay and millis(); function works, so if you can try to evade changing those pins to another frequency.

Copy paste the only the lines for the pins you want to change in your void setup() and to which frequency you would like to change them.

//Frequencies for Pin 3 & 11

TCCR2B = TCCR2B & B11111000 | B00000001; // for PWM frequency of 31372.55 Hz

TCCR2B = TCCR2B & B11111000 | B00000010; //  3921.16 Hz

TCCR2B = TCCR2B & B11111000 | B00000011; // 980.39 Hz

TCCR2B = TCCR2B & B11111000 | B00000100; //  (default)

TCCR2B = TCCR2B & B11111000 | B00000101; //  245.10 Hz

TCCR2B = TCCR2B & B11111000 | B00000110; //  122.55 Hz

TCCR2B = TCCR2B & B11111000 | B00000111; //  30.64 Hz

//Frequencies for Pin 5 & 6

TCCR0B = TCCR0B & B11111000 | B00000001; // for PWM frequency of 62500.00 Hz

TCCR0B = TCCR0B & B11111000 | B00000010; //  7812.50 Hz

TCCR0B = TCCR0B & B11111000 | B00000011; //  (default)

TCCR0B = TCCR0B & B11111000 | B00000100; //  244.14 Hz

TCCR0B = TCCR0B & B11111000 | B00000101; //  61.04 Hz

//Frequencies for Pin 9 & 10

TCCR1B = TCCR1B & B11111000 | B00000001; // set timer 1 divisor to 1 for PWM frequency of 31372.55 Hz

TCCR1B = TCCR1B & B11111000 | B00000010; //  3921.16 Hz

TCCR1B = TCCR1B & B11111000 | B00000011; // 490.20 Hz (default)

TCCR1B = TCCR1B & B11111000 | B00000100; //  122.55 Hz

TCCR1B = TCCR1B & B11111000 | B00000101; //  30.64 Hz

More precise control PWM on the ESP32

Do you want to pick a specific frequency for your PWM? The ESP32 has a function that allows you do do this. This is called the ledcWrite function. Here is an example of a motor speeding up and down in a loop using the ledcWrite function. When you hook up your ESP32 to your motor control circuit, make sure you're using one of the appropriate pins. Some of the pins are not available or used for internal processes. Check here if you're not sure:

https://randomnerdtutorials.com/esp32-pinout-reference-gpios/

const int ledPin = 4;

int speed = 0;  // the speed of the motor
int acceleration = 5;  // how many points to accelerate the motor by

int freq = 20000; // Set the Pulse Width Modulation Frequency to 20kHz. This is above human hearing. 
int resolution = 8;

void setup() {
  // put your setup code here, to run once:
  pinMode(motorPin, OUTPUT);
  ledcAttach(motorPin, freq, resolution);  // Initialize PWM on LED pin
  ledcWrite(motorPin, 0);                  // Initialize LED to 0
}
void loop() {
  // set the speed of motorPin:
  ledcWrite(led, speed);

  // change the speed for next time through the loop:
  speed = speed + acceleration;

  // reverse the direction of the acceleration at the ends of the acceleration:
  if (speed <= 0 || speed >= 255) {
    acceleration= -acceleration;
  }
  // wait for 30 milliseconds to see the acceleration effect
  delay(30);
}