analogWrite and digitalWrite on same pin?

paynterf

Well-known member
I'm using analogWrite(MOTOR_PIN) to control motor speed using a DRV8871 motor driver. When I stop the motor with analogWrite(0), the motor stops but I still hear a residual (and very annoying) whine. I tried using digitalWrite(HIGH) which is supposed to rapidly stop the motor, and digitalWrite(LOW) which is supposed to all the motor to 'coast' to a stop, but neither of these commands had any effect on motor speed. Grok tells me this is a result of a Teensy 4.1 'FlexPWM hardware' quirk. Any thoughts on how I get rid of the whine?

This works, but leaves one of the DRV8871's 'whining'
Code:
void StopBothMotors()
{
  analogWrite(LEFT_IN1, 0);
  analogWrite(LEFT_IN2, 0);
  analogWrite(RIGHT_IN1, 0);
  analogWrite(RIGHT_IN2, 0);
}

This (and replacing HIGH with LOW) does nothing
Code:
void StopBothMotors()
{
  digitalWrite(LEFT_IN1, HIGH);
  digitalWrite(LEFT_IN2, HIGH);
  digitalWrite(RIGHT_IN1, HIGH);
  digitalWrite(RIGHT_IN2, HIGH);
}
 
Back
Top