I don’t see any obvious problems with using analogWrite() to do PWM with the DRV8835. Can you post some pictures of your setup and all of your connections? What kind of motors are you using? If you have the MODE pin connected, is it pulled LOW or HIGH? Are both motors behaving the same way?
It might be helpful to step back and verify that the pin works at full on and full off. If the MODE pin is pulled low or left disconnected, you should be able to run this code to turn both motors on for 6 seconds followed by a 6 second coast:
//Set up pins
void setup() {
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
}
//Turn pin D5 (IN1) and D6(IN2) off for 6 seconds and then on for 6 seconds
void loop() {
digitalWrite(5, LOW);
digitalWrite(6, LOW);
delay(6000);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
delay(6000);
}
If that works, then the hardware is probably wired correctly. You can try this to run through a sweep of different speeds:
//Set up pins
void setup() {
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
}
//Turn pin D5 (IN1) and D6(IN2) off for 6 seconds and slowly throttle to 100% duty cycle 6 seconds
void loop() {
analogWrite(5, 0);
analogWrite(6, 0);
delay(6000);
analogWrite(5, 31);
analogWrite(6, 31);
delay(1000);
analogWrite(5, 63);
analogWrite(6, 63);
delay(1000);
analogWrite(5, 127);
analogWrite(6, 127);
delay(1000);
analogWrite(5, 159);
analogWrite(6, 159);
delay(1000);
analogWrite(5, 191);
analogWrite(6, 191);
delay(1000);
analogWrite(5, 255);
analogWrite(6, 255);
delay(1000);
}