B-328p Motor Driver and High Pitch Whine

How do I change the PWM Freq in my project to eliminate the motor whine? I’m building in Arduino, and using the “OrangutanMotors.h” library.

Thanks a million for the help!

#include <OrangutanLEDs.h>
#include <OrangutanAnalog.h>
#include <OrangutanMotors.h>
  
OrangutanAnalog analog;
OrangutanLEDs leds;
OrangutanMotors motors;
 
void setup() {}
 
void loop() {          
  // note that the following line could also be accomplished with:
  // int pot = analog.read(7);
  int pot = analog.readTrimpot();    // determine the trimpot position
  int motorSpeed = pot/2-256;  // turn pot reading into number between -256 and 255
  if(motorSpeed == -256)
    motorSpeed = -255; // 256 is out of range
  motors.setSpeeds(motorSpeed, motorSpeed);
   
  int ledDelay = motorSpeed;
  if(ledDelay < 0)
    ledDelay = -ledDelay;  // make the delay a non-negative number
  ledDelay = 256-ledDelay; // the delay should be short when the speed is high
 
  leds.red(HIGH);       // turn red LED on
  delay(ledDelay);
 
  leds.red(LOW);       // turn red LED off
  delay(ledDelay);
}

There is an option in the library for Arduino. For anyone else looking for this later

File Name: OrangutanMotors.cpp

#ifndef ARDUINO	
    // use the system clock/8 (=2.5 MHz) as the timer clock,
	// which will produce a PWM frequency of 10 kHz
	// Arduino uses Timer0 for timing functions like micros() and delay() so we can't change it
    //TCCR0B = TCCR2B = 0x02;  //<-- Comment Out
#endif

	// use the system clock (=20 MHz) as the timer clock,
	// which will produce a PWM frequency of 78 kHz.  The Baby Orangutan B
	// and 3Pi can support PWM frequencies this high.  The
	// Orangutan LV-168 cannot support frequencies above 10 kHz.
    TCCR0B = TCCR2B = 0x01; //<-- Uncomment

Hello.

I am glad you were able to find the answer to your question. Thank you for taking the time to post what it was.

-Derrill