L298N motor controller question...

i wasn't sure if I should post this here or in the motors forum, so if anyone thinks its in the wrong spot, please let me know :slight_smile:

I've been researching the L298N dual H bridge motor controller as I am trying to incorporate it into a project I'm working on and I was hoping somebody could clear up a couple of questions for me. There seem to be a lot of posts and articles and information out there, but some of it appears to be contradictory to me (Extremely new and not trained in any of this)... specifically in this tutorial

tutorial

the question i was searching to answer is asked by someone which was;

"Why do you connect Pi's 5V to the 5V on the board??

This seems weird. Everywhere else I read that the board will supply 5V when running off 7-12V supply. But they never say you should put 5V from the Pi in it???!

He's asking about the Pi, but I had the same question about the mega...

I also found it strange that the L298 controller was supplying the arduino with power and wondered if it was incorrect as the arduino would have it's own power supply via wall-wart and it seemed to me that two supplies could cause a problem...

The author's response was;

This step is not necessary, I like to tie together rails of equal v-value for stability of my projects (some components are heavier draw than others and v-droop is annoying). What is necessary is to make sure your projects components are always grounded together.

To which another person responded;

That's not a good idea. In the best case it won't do a thing, in the worst (most likely) case, you are shorting 2 voltage sources that are not identical, hence generating a voltage delta across a very small resistor.

so I guess I'm hoping someone here can set me straight... if the arduino board has it's own power supply, should I Not connect the 5v terminal from the L298 to the 5v circuit from the arduino?

Also, I was never really able to find a good layman's explanation for what the jumper pins on the ENA and ENB pins are for, or alternatively, how you connect the driver without removing them... again, very green here, very not well versed in electronics... just trying to make some sense of things. As it is now, i seem to have it working, but it's been kind of trial and error and i'm not sure that it won't explode as I've only ever powered it up in short spurts to test the code.

I've tried inserting an image above of the controller, but i'm not sure it will work... I've never had to try to use a URL before to insert an image into a post on a forum... if not, the first link to the tutorial previously mentioned displays clearly the version i have, I don't have an official schematic, they all seem to have some slight differences and I'm not really sure about which of those differences reflect my device.

Anyways, any clarification or insight into this controller and the proper way to connect it (5v L298 to 5v arduino? can, should, would you control a motor without removing the jumper? ) would be very much appreciated.

Thanks,
Joe

. if the arduino board has it's own power supply, should I Not connect the 5v terminal from the L298 to the 5v circuit from the arduino?

Given that question, I don't think you should connect anything just yet.
First or all, the L298 board has it's own onboard 5V regulator.
The arduino has it's own 5V regulator.
The L298 has a motor voltage input.
The arduino has an external dc voltage barreljack.
Since you stated you have little electronics experience, allow me to clarify this issue for you.
If you have TWO 5 V regulators, it is best to use them both. Run the L298 from the dc motor Vin and run the arduino from the external dc voltage input. The arduino and L298 need a common GND for the arduino digital pins to control the L298 InX pins. The ENA and ENB pins are if you don't plan to use PWM and want to hard wire the enable pins for the two motors. If you want to PWM the motors, you need to remove those jumpers because you need to connect those pins to your arduino PWM pins (9 & 10)
If you actually read the "tutorial" then you didn't understand everything if you are asking these questions.

Look at the connection diagram in the "tutorial" and then look at the code:
L298 PWM PIN CONNECTIONS.jpg
Do you see the RED lines indicating the PWM pin connections to the ENA & ENB pins ?

How could you possibly control those pins with your pwm if you left the jumpers on ?
Did you ask yourself that question ? (obviously them must be removed)

 //Code by Reichenstein7 (thejamerson.com)

//Keyboard Controls:
//
// 1 -Motor 1 Left
// 2 -Motor 1 Stop
// 3 -Motor 1 Right
//
// 4 -Motor 2 Left
// 5 -Motor 2 Stop
// 6 -Motor 2 Right

// Declare L298N Dual H-Bridge Motor Controller directly since there is not a library to load.

// Motor 1
int dir1PinA = 2;
int dir2PinA = 3;
int speedPinA = 9; // Needs to be a PWM pin to be able to control motor speed

// Motor 2
int dir1PinB = 4;
int dir2PinB = 5;
int speedPinB = 10; // Needs to be a PWM pin to be able to control motor speed

void setup() {  // Setup runs once per reset
// initialize serial communication @ 9600 baud:
Serial.begin(9600);

//Define L298N Dual H-Bridge Motor Controller Pins

pinMode(dir1PinA,OUTPUT);
pinMode(dir2PinA,OUTPUT);
pinMode(speedPinA,OUTPUT);
pinMode(dir1PinB,OUTPUT);
pinMode(dir2PinB,OUTPUT);
pinMode(speedPinB,OUTPUT);

}

void loop() {

// Initialize the Serial interface:

if (Serial.available() > 0) {
int inByte = Serial.read();
int speed; // Local variable

switch (inByte) {

//______________Motor 1______________

case '1': // Motor 1 Forward
analogWrite(speedPinA, 255);//Sets speed variable via PWM 
digitalWrite(dir1PinA, LOW);
digitalWrite(dir2PinA, HIGH);
Serial.println("Motor 1 Forward"); // Prints out “Motor 1 Forward” on the serial monitor
Serial.println("   "); // Creates a blank line printed on the serial monitor
break;

case '2': // Motor 1 Stop (Freespin)
analogWrite(speedPinA, 0);
digitalWrite(dir1PinA, LOW);
digitalWrite(dir2PinA, HIGH);
Serial.println("Motor 1 Stop");
Serial.println("   ");
break;

case '3': // Motor 1 Reverse
analogWrite(speedPinA, 255);
digitalWrite(dir1PinA, HIGH);
digitalWrite(dir2PinA, LOW);
Serial.println("Motor 1 Reverse");
Serial.println("   ");
break;

//______________Motor 2______________

case '4': // Motor 2 Forward
analogWrite(speedPinB, 255);
digitalWrite(dir1PinB, LOW);
digitalWrite(dir2PinB, HIGH);
Serial.println("Motor 2 Forward");
Serial.println("   ");
break;

case '5': // Motor 1 Stop (Freespin)
analogWrite(speedPinB, 0);
digitalWrite(dir1PinB, LOW);
digitalWrite(dir2PinB, HIGH);
Serial.println("Motor 2 Stop");
Serial.println("   ");
break;

case '6': // Motor 2 Reverse
analogWrite(speedPinB, 255);
digitalWrite(dir1PinB, HIGH);
digitalWrite(dir2PinB, LOW);
Serial.println("Motor 2 Reverse");
Serial.println("   ");
break;

default:
// turn all the connections off if an unmapped key is pressed:
for (int thisPin = 2; thisPin < 11; thisPin++) {
digitalWrite(thisPin, LOW);
}
  }
    }
      }

Also, I was never really able to find a good layman's explanation for what the jumper pins on the ENA and ENB pins are for, or alternatively, how you connect the driver without removing them... again, very green here, very not well versed in electronics... just trying to make some sense of things. As it is now, i seem to have it working, but it's been kind of trial and error and i'm not sure that it won't explode as I've only ever powered it up in short spurts to test the code.

Doesn't the "tutorial" qualify as a "good laymen's description of the ENA & ENB pins ?

EnA: Enables PWM signal for Motor A (Please see the "Arduino Sketch

The PWM signal itself is what "enables" the pwm. The jumper disables PWM because it hard wires it to 5V. I don't have a schematic of that board so I don't know if there are pullup resistors between the ENA & ENB pins and the 5V pin. Judging from the photo of the L298 board, it does not appear to have any pullup resistors so if you measure the resistance from the pin that the jumper connects to to the 5V pin, it will probably show 0.1 ohm or continuity, meaning you could not possibly pull it low with a pwm signal because it is hard wired HIGH, confirming it is a DISABLE PWM JUMPER .

While I can't answer your question about that specific motor driver, I can answer your question about connecting power supplies together.

Unless you have taken specific precautions to allow some parts of the project to be powered while others are not, you need to have the whole project get powered at the same time. Otherwise, for example, an output pin from a powered device will try to back-feed into the input pin of the unpowered device. Or perhaps a pin will be high impedance and allow spurious data onto a UART.

When connecting +5V together, in general the higher voltage supply will 'win' and take over the bus. For example, when you power your Arduino from the USB port, you will usually see about 4.5V on the +5V line. If you then plug the external power in, you will see about 5.0V. If you then supplied 5.2V directly to the +5V line from another source, you would see 5.2V.

Depending on the type of power supply, this can be OK or have unintended effects. In the USB vs. External, the Arduino has a diode from the USB to +5V which prevents current from flowing back into the computer. Likewise, the Arduino has a linear regulator on the board, so it will shut down when the +5V rises above its set point, and will not backfeed into the wall transformer as long as the transformer's voltage is above what is on the +5V line.

Switching power supplies are a different story. Most basic types (buck, boost) will prevent backflow as well. A synchronous bridge power supply will actually backfeed to its supply, no matter its voltage. It attempts to maintain its output voltage by either taking current from its input, or sending current from its output to its input. In that case, you definitely do NOT want to connect another power source.

One last item is about total system power. You can power each piece separately if it has its own regulator, and the total current is less than the rating of the external power supply AND each individual regulator is rated for more than its individual load. This rating gets reduced by the heat generated by the regulator, if it is a linear regulator. The one on the Arduino is good for maybe 700mA before it starts to overheat.

Hamsterman

Otherwise, for example, an output pin from a powered device will try to back-feed into the input pin of the unpowered device

This is a good point to mention. What he is saying is that you CANNOT turn OFF the arduino power if you have ANY connections from GPIO (or ANALOG) pins to any external voltage that has power. You MUST shut down the power for BOTH the L298 AND the arduino SIMULTANEOUSLY , OR, shut down the external device (L298) BEFORE you shut down the arduino power. If you do NOT understand what I just said, please reread it as many times as necessary until you do.

Have you already purchased a 298-based solution? If not, it will be worth your while looking at more modern chips which are more efficient and don't need heatsinks the size of a truck. A 298 drops 2V of your motor supply, minimum, and that loss becomes almost 5V at higher currents.... that heats up the atmosphere and doesn't move your robot.

Pololu, for instance, have a range of drivers to suit various voltage and current combinations; I've used the #2130.

hey guys, sorry about the delayed response, been crazy around here. Thanks for info and sorry for the stupid question, phrased it really poorly, think i've figured it out now though. So because they both have 5v regulators, I will connect them together. Good to know about not disconnecting the arduino first... hadn't read that anywhere else and had done that a couple of times, hopefully i got lucky and didn't damage the board!

In regard's to Jimbo's comments, yes I already have the L298, but it only needs to drive the motor for a few seconds a couple of times a day to raise and lower a door, so hopefully the heat and voltage drop aren't too much of a problem... if so, i guess I'll be looking at other options.

Thanks,
Joe

Connect them together ?
You mean their inputs , right ?

I was referring to the 5V and ground circuits :slight_smile:

Thanks,
joe

I was referring to the 5V and ground circuits

ABSOLUTELY NOT !

NEVER CONNECT THE OUTPUTS OF VOLTAGE REGULATORS TOGETHER.

This causes the two regulators to fight with each other.

I was trying to tell you to connect the INPUTS together and leave the OUTPUTS SEPARATE, with the exception of the COMMON ground which is unavoidable for obvious reasons.

There should be NO connection between the L298 5V (from the onboard regulator on the L298 Bd) and the arduino 5V. They should each have their own regulator and only a common ground between them.