Help with H-Bridge L298N

Hey everyone,

I am trying to use the L298N H-Bridge, but I have a few problems with it..

Whenever I connect a battery (either 11.1V LiPo, 9V, or any other power source, and share ground with the Arduino) the PWM voltage produced by the H Bridge is always too high.
Doesn’t matter whether it has load (and doesn’t matter what load), or with no load at all.

I check the output voltage using a multimeter, between the two output terminals.I also tried to connect several loads (for example a motor, and to control its speed) to make sure it is not just false-readings - and the readings are correct.

This is the code I am using, nothing special:

const int PWM = 6, A = 4, B = 5;

void setup() {
  pinMode(PWM, OUTPUT);
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
}

void loop() {
  digitalWrite(A, LOW);
  digitalWrite(B, HIGH);
  analogWrite(PWM, 1);
}

When I write analogWrite(255), it gives the battery voltage (minus H Bridge voltage drop, understandable) and when I write analogWrite(0) - 0 volts of course.
But anything in between, makes absolutely no sense.

For example, using a fully charged 9V battery and analogWrite(1) I get a reading of 5V, and for analogWrite(50) I get around 6V. It is not proportional to anything. Same behaviour with the 11.1V LiPo battery, and any other power-source I’ve tried.

At first I thought it is a ground-sharing problem, but I do share the ground between the H-Bridge and the Arduino. (and I know it works, because I get different readings without it)

I tried several H-Bridge and I always got the same result.
I also compared my setup with this tutorial, and everything is as it should be.

The H-Bridge module I am using is the exact one like in the tutorial.

However, when I “trigger” PWM pulses manually like the following code (and short the enable pin on the H-Bridge of course), it does work:

const int A = 4, B = 5;

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
}

int pwm = 100;

void loop() {
  digitalWrite(A, LOW);
  digitalWrite(B, HIGH);
  delayMicroseconds(pwm);
  digitalWrite(A, LOW);
  digitalWrite(B, LOW);
  delayMicroseconds(1000 - pwm);
}

This works perfectly fine, but the above code, using analogWrite doesn’t work whatsoever.
I also tried to power the H-Bridge using the 5V (taking out the voltage regulator jumper, and using 5V to power the H-Bridge) - same result.

Example setup schematic:

I can’t figure out what I’m missing..
Any ideas? What else can I try to check?
Thanks in advance!

(p.s. I asked about it a couple of weeks ago but I didn’t explain myself well and the topic shifted to different things, so I’m asking it again, and simplified)

Forget about the H-bridge and connect a LED and resistor directly to some PWM output pin. Control the PWM duty cycle using a pot and you have built a miraculous dimmer.

Conclusion: PWM controls current, not voltage.

Post a schematic. I'm not going to watch a video.
Try connecting the motor

If I would want to control a LED, I would do it like that. But I want to control the voltage, and PWM controls voltage, not current

Post a schematic. I'm not going to watch a video.

Which video? No one asked you to watch a video.
And how will a schematic help you in this case? Yes schematics can often help (a lot) but in this case it is point less - there is nothing special you need to see.

I said:

I check the output voltage using a multimeter, between the two output terminals.I also tried to connect several loads (for example a motor, and to control its speed) to make sure it is not just false-readings - and the readings are correct.

But just because you asked, here:

  • The battery is either 9V or 11.1V LiPo

The motor's speed matches what I see on the multimeter - analogWrite(1) gives a reading of 5V - and the motor spins accordingly.
I can't get it below 5V (except for 0, off, of course)

Is it better now? If there is anything else you'd like to know about my setup, just ask.
I hope my schematic is good enough.

Is PWM = 6 ?

Then use a RC low pass filter.

The pin number is 6, yes, the pin I am using to send PWM to the H bridge

But there is H bridge, why not to use it?
Of course, I could also use transistors, but the point is to use H bridge
The H bridge should do the job, why should I use something else? There are always alternatives, but to give up and use a different way isn't a good solution, never.. In fact, it is not a solution

The LED demonstrates that the PWM pin works as it should, in software and in hardware. Afterwards you can add the H-bridge and verify that it works as well.

What you describe is no pwm, hence the need for led test

You haven't said what A and B are
Labels are unreadable in fritzing

When I connect a LED to a PWM pin it does work, the PWM pin on the Arduino works perfectly fine, no issues with that.
I am asking about the H bridge, not the Arduino PWM.
The minimum voltage the H bridge output is a bit above 5V, and it seems like it is not proportional to anything. 128 isn't half of the voltage I get with 255.

I can work with it even if it is not proportional to anything (at least not known), but the more "interesting" issue is that it does not output less than 5V, even when the duty cycle is really low (analogWrite(1))

How come what I described is not PWM? It is PWN, no doubt.
See my recent comment (reply to DrDiettrich) regarding the LED.

A and B are, well, the H bridge pins.. You don't need to read the schematic labels in order to understand that.. Sorry to ask, but.. have you ever even used this type of H bridge before?

Post a photos of the wire connections to the module and the arduino. Looks like pin 4 is not a pwm pin

Something's miswired
Nobody has ever reported these symptoms
before which suggests human error.

READ THIS https://www.instructables.com/Tutorial-for-MD-L298-Motor-Driver-Module/?amp_page=true

For two H-bridge reasons:

  1. Voltage is remaining on the Darlington transistors.
  2. Current only flows in one direction so that a load is required for a somewhat linear voltage.

Only if you want current flow in both directions as happens on the Arduino PWM pins. A light bulb or resistor then will get the same AC energy regardless of the duty cycle.

For driving motors or other loads the current should only flow in one direction, forward or backward, so that the direction and brake is controlled by the IN pins.

Thanks, I just finished reading the user manual
to refresh my memory.

YOU SHOULD NOT BE USING B INPUT IF YOU
ONLY HAVE ONE MOTOR !
'B' should NOT appear in anywhere in your code.

FYJ8TUCJ9OWJHGE

FYI, according to your schematic you are using pins 3,4, & 5 but your code is using pin 6 for
pwm but the schematic shows the ENA pwm input of the L298 connected to pin 3 of the arduino.

Not surprised it doesn't work.

I don´t lnow what is the problem of posting your actual wiring...

  1. In your "Example setup schematic", the enable pin is connected to Arduino ~D3, not ~D6 as is in your first code. @DaveX gave you this clue

  2. In your second code, when you pretend to trigger PWM manually, you´re actually not using PWM at all (as @raschemmel said). The correct code would be:

void loop() {
  digitalWrite(A, LOW);
  analogWrite(B, 100); // or desired speed
  delay(500);
  digitalWrite(A, LOW);
  digitalWrite(B, LOW);
  delay(500);
}

I submit that ithe analogWrite command should be A NOT B because the OP's motor is motor 1