DRV8838 Speed Control?

Is there any sample arduino code or library for the drv8838 motor driver? Most of the code I have seen only drives the enable pin high or low. I want to drive the enable pin with PWM to control motor speed but I’m not sure how to do it.

PS-I currently have it running correctly with this code; just need to figure out how to control speed.

int enablePin = 3;
int phasePin = 4;

void setup()
{
pinMode(enablePin, OUTPUT);
pinMode(phasePin, OUTPUT);
}
void loop()
{

digitalWrite(enablePin, HIGH);
digitalWrite(phasePin, HIGH);
delay(100);
digitalWrite(enablePin, LOW);
digitalWrite(phasePin, LOW);
delay(500);
digitalWrite(enablePin, HIGH);
digitalWrite(phasePin, LOW);
delay(100);
digitalWrite(enablePin, LOW);
digitalWrite(phasePin, LOW);
delay(500);
}

Take a look at this: https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/

Hello.

Unfortunately, we do not have a library for the DRV8838. The easiest way to get speed control will be to use analogWrite() as jlo suggested.

By the way, if you want to write your own library, you might check out our Romi 32U4 Control Board library since that board uses the DRV8838.

- Patrick

Thank you all for the input. I have it working with this code:

/*

  • Test code for Pololu drv8838 motor driver. Drives motor forward,
  • then backward at low speed for 3 seconds, then repeats at high speed.
  • analogWrite value (0-255) changes speed, and phasePin (LOW/HIGH) changes
  • motor direction.
    */

int enablePin = 3;
int phasePin = 4;

void setup()
{
pinMode(enablePin, OUTPUT);
pinMode(phasePin, OUTPUT);
}
void loop()
{

analogWrite(enablePin, 50);
digitalWrite(phasePin, HIGH);
delay(3000);
analogWrite(enablePin, 255);
digitalWrite(phasePin, HIGH);
delay(3000);
analogWrite(enablePin, 50);
digitalWrite(phasePin, LOW);
delay(3000);
analogWrite(enablePin, 250);
digitalWrite(phasePin, LOW);
delay(3000);
}

1 Like