How does using -pwm work on the drv8833 motor driver?

Hello everyone, how does using -PWM work on the DRV8833 motor driver? Doesn't a -PWM just output 0, because when I do this:

int pot;
  pot = 150;
  analogWrite(MO2_IN1, -pot);
  digitalWrite(MO1_IN2, HIGH);

I can observe a difference between 255 and 150, meaning it is not outputting 0, so what is it outputting?

You are measuring this with a multimeter? Multimeter can't measure the actual voltage because it is changing too rapidly. It may measure the average voltage, if you are lucky.

To see what's going on you need an oscilloscope.

Ah, sorry, I missed the detail of what you said there.

No. analogWrite() doesn't accept negative numbers. If you try to give one, the maths will turn it back into a positive number between 0 and 255.

-150 expressed as an unsigned byte would be about 105.

So then, how does this work? Theoretically, this shouldn't work, because it is like setting
both pins to HIGH (255 = HIGH I think)

pot = 255;
analogWrite(MO2_IN1, -pot);
digitalWrite(MO1_IN2, HIGH);

It's outputting 256-150 = 106
Similarly anything bigger than 255 roll back to 0 and increase from there. 260 would become 4.

-255 is 1

const int MO1_IN1 = 3;
const int MO1_IN2 = A0;
const int MO1_IN3 = 5;
const int MO1_IN4 = A1;

const int MO2_IN1 = 6;
const int MO2_IN2 = A2;
const int MO2_IN3 = 10;
const int MO2_IN4 = A3;

const int sleep = A4;
const int fault1 = 4;
const int fault2 = A5;
int pot;
void setup() {
  Serial.begin(9600);
  pinMode(MO1_IN1, OUTPUT);
  pinMode(MO1_IN2, OUTPUT);
  pinMode(MO1_IN3, OUTPUT);
  pinMode(MO1_IN4, OUTPUT);

  pinMode(MO2_IN1, OUTPUT);
  pinMode(MO2_IN2, OUTPUT);
  pinMode(MO2_IN3, OUTPUT);
  pinMode(MO2_IN4, OUTPUT);

  pinMode(sleep, OUTPUT);
  pinMode(fault1, INPUT_PULLUP);
  pinMode(fault2, INPUT_PULLUP);

  digitalWrite(MO1_IN1, LOW);
  digitalWrite(MO1_IN2, LOW);
  digitalWrite(MO1_IN3, LOW);
  digitalWrite(MO1_IN4, LOW);

  digitalWrite(MO2_IN1, LOW);
  digitalWrite(MO2_IN2, LOW);
  digitalWrite(MO2_IN3, LOW);
  digitalWrite(MO2_IN4, LOW);
}
void loop() {
  digitalWrite(sleep, HIGH);
  pot = 255;
  analogWrite(MO2_IN1, pot);
  digitalWrite(MO1_IN2, LOW);
  analogWrite(MO1_IN3, pot);
  digitalWrite(MO1_IN4, LOW);
  analogWrite(MO2_IN1, pot);
  digitalWrite(MO2_IN2, LOW);
  analogWrite(MO2_IN3, pot);
  digitalWrite(MO2_IN4, LOW);
  delay(1000);
  pot = 120;
  analogWrite(MO2_IN1, pot);
  digitalWrite(MO1_IN2, LOW);
  analogWrite(MO1_IN3, pot);
  digitalWrite(MO1_IN4, LOW);
  analogWrite(MO2_IN1, pot);
  digitalWrite(MO2_IN2, LOW);
  analogWrite(MO2_IN3, pot);
  digitalWrite(MO2_IN4, LOW);
  delay(1000);
  pot = 255;
  analogWrite(MO2_IN1, 255-pot);
  digitalWrite(MO1_IN2, HIGH);
  analogWrite(MO1_IN3, 255-pot);
  digitalWrite(MO1_IN4, HIGH);
  analogWrite(MO2_IN1, 255-pot);
  digitalWrite(MO2_IN2, HIGH);
  analogWrite(MO2_IN3, 255-pot);
  digitalWrite(MO2_IN4, HIGH);
  delay(1000);
  pot = 120;
  analogWrite(MO2_IN1, 255-pot);
  digitalWrite(MO1_IN2, HIGH);
  analogWrite(MO1_IN3, 255-pot);
  digitalWrite(MO1_IN4, HIGH);
  analogWrite(MO2_IN1, 255-pot);
  digitalWrite(MO2_IN2, HIGH);
  analogWrite(MO2_IN3, 255-pot);
  digitalWrite(MO2_IN4, HIGH);
  delay(1000);
}

In that code, the motor is connected to:

const int MO1_IN1 = 3;

const int MO1_IN2 = A0;

That motor is only working when it's going backwards, this part:

  pot = 255;
  analogWrite(MO2_IN1, 255-pot);
  digitalWrite(MO1_IN2, HIGH);
  analogWrite(MO1_IN3, 255-pot);
  digitalWrite(MO1_IN4, HIGH);
  analogWrite(MO2_IN1, 255-pot);
  digitalWrite(MO2_IN2, HIGH);
  analogWrite(MO2_IN3, 255-pot);
  digitalWrite(MO2_IN4, HIGH);
  delay(1000);
  pot = 120;
  analogWrite(MO2_IN1, 255-pot);
  digitalWrite(MO1_IN2, HIGH);
  analogWrite(MO1_IN3, 255-pot);
  digitalWrite(MO1_IN4, HIGH);
  analogWrite(MO2_IN1, 255-pot);
  digitalWrite(MO2_IN2, HIGH);
  analogWrite(MO2_IN3, 255-pot);
  digitalWrite(MO2_IN4, HIGH);
  delay(1000);

And when in backwards, its speed isn't changing while all the other motors are.
When in forward the motor doesn't move, or output any voltage (I measured it in my voltmeter). @PaulRB, @kmin

You never set M01_IN1.

1 Like

But what kind of Arduino? Are you sure that all the pins that you use analogWrite() with support PWM on your Arduino?

@memind your code sets MO2_IN1 twice each time.

May I suggest you to test your setup first without PWM. When everything works, move on.
Also be aware that

  analogWrite(MO2_IN1, 255-pot);
  digitalWrite(MO2_IN2, HIGH);

doesn't give same result (in opposite direction) than

  analogWrite(MO2_IN1, 255-pot);
  digitalWrite(MO2_IN2, LOW);

You would have much easier work if you only PWM "high" pin.

You obviously have errors in you code, maybe you should post your entire code, so we can see

Looks like a complete code in post #6

Hi, @memind
Welcome to the forum.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Yea that was the problem I accidentally wrote analogWrite(MO2_IN1, pot); instead of writing: analogWrite(MO1_IN1, pot);

See post #7

Yes, I'm sure, I'm using Arduino Nano. Anyways the problem was just that I accidentally used analogWrite(MO2_IN1, pot); instead of analogWrite(MO1_IN1, pot); (I used MO2 not MO1) @jim-p
Also, for example -pot just means 255 - pot. So from now on I will be using 255 - pot.