Control two DC Motors with same PIN in arduino

Hi everyone, I am a newbie in developing with Arduino. I have a project that using two Motors DC 12V to control car with Arduino, two Motors will be input same Voltage, same direction. I have one idea that is wiring motor 1 (ENA, IN1, IN2) and motor 2 (ENB, IN3, IN4) into same PIN in Arduino UNO R3 (PIN 2, 3, 4). When I turn on supply, two motors work correctly but when I stop, just one Motor stopped, the other motor does not stop and voltage increase about 7V (it is so large). Can someone help me to fix it or my idea is incorrect?
Code project:

#include <L298NX2.h>

//Motor 1
#define ENB_1 3
#define IN3_1 2
#define IN4_1 4
//Motor 2
#define ENA_2 3
#define IN1_2 2
#define IN2_2 4

L298NX2 motor1_2(ENB_1, IN3_1, IN4_1, ENA_2, IN1_2, IN2_2);

void setup() {
  Serial.begin(9600);
  delay(5000);
}
void loop() {
  motor1_2.setSpeed(15);
  motor1_2.forward();
  Serial.println("RUN.");
  delay(2000);
  motor1_2.stop();    // cả 2 motor ngừng
  Serial.println("STOP.");
  delay(2000);
}

Your idea is correct. Your wiring (breadboard?) is questionable.

i'd use modern (MOSFET) drivers instead of the L29x dinosaurs.

If you want your car move straight then you'll have to add motion feedback and control each motor individually. Or you use stepper motors...

Wiring here is the power cord connecting the motor circuit to Arduino (sorry for my English a bit poor) and I use XY-160D to control two motors DC 775.

Hi @dainoso ,

could you provide a (simple) drawing of your wiring?


This is my diagram to connect motor driver with arduino.

So why are you using a library for the L298

Please no Fritzing. Black boxes instead of module pictures. GND on bottom, signals usually flow left to right.

Fritzing is fine with me.

Connecting the motor control pins in parallel is unusual as it does not allow to control each motor separately.

The library L298NX2.h provides the same functions as L298N.h but they effect the separate motor control pins of both motors in parallel.

see here https://github.com/AndreaLombardo/L298N?tab=readme-ov-file#l298nx2-methods

But if you electrically connect ENA with ENB, IN1 with IN3 and IN2 with IN4 it does not make sense to use the library L298NX2. This is only required for a standard wiring.

[Edit:] I have replicated your setup with an Arduino Nano, a L298N board, two motors and a separate power supply for the motor driver. It worked (as expected). If the second motor does not stop, you might doublecheck your wiring....

Using the standard lib L298N.h for this "hardwired" solution shows the same behaviour. (However I had to change the speed to something above 100 to get both motors running.),

//#include <L298NX2.h>
#include <L298N.h>

//Motor 1
#define ENB_1 3
#define IN3_1 2
#define IN4_1 4
//Motor 2
#define ENA_2 3
#define IN1_2 2
#define IN2_2 4

//L298NX2 motor1_2(ENB_1, IN3_1, IN4_1, ENA_2, IN1_2, IN2_2);
L298N motor1_2(ENA_2, IN1_2, IN2_2);

void setup() {
  Serial.begin(115200);
  delay(1000);
}
void loop() {
  motor1_2.setSpeed(15);
  motor1_2.forward();
  Serial.println("RUN.");
  delay(2000);
  motor1_2.stop();    // cả 2 motor ngừng
  Serial.println("STOP.");
  delay(2000);
}

Because I saw that this library has the same function to run DC motors, and when I used it with my motors (without sharing the same pins), it worked very well. That’s why I decided to use it.

Sorry for this image, I’ve never drawn it before.

Thank you very much, I had to change my code and not using this library. I wrote function to run motor and it was working.

//Motor
#define EN_MOTOR 3
#define IN1_MOTOR 2
#define IN2_MOTOR 4

void setup() {
  Serial.begin(9600);
  pinMode(EN_MOTOR, OUTPUT);
  pinMode(IN1_MOTOR, OUTPUT);
  pinMode(IN2_MOTOR, OUTPUT);
}
void loop() {
  setMotor(1, 30, EN_MOTOR, IN1_MOTOR, IN2_MOTOR);
  Serial.print("Run.");
  delay(2000);
  setMotor(0, 0, EN_MOTOR, IN1_MOTOR, IN2_MOTOR);
  Serial.println("Stop.");
  delay(5000);
}

void setMotor(int dir, int pwmVal, int EN, int IN1, int IN2) {
  analogWrite(EN, pwmVal);
  if (dir == 1) {
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
  } else if (dir == -1) {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
  } else {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
  }
}

You can still use it but you need to the single motor version
#include <L298N.h>

Did you test your application with the code from my post 10? To get the same results you should change the speed in setSpeed() from 15 to 30.

As @jim-p also posted there's nothing wrong with using the library L298N.h. It internally applies the same functions that you are using in your sketch.

Have fun!
ec2021

It was worked for me. But I think that I need to create own function for it to know how it's work

Yes, you will learn much more that way.

An additional good way to learn "how things are working" is to look up the respective library on github!

For the L298N library created by Andrea Lombardo you find this

https://github.com/AndreaLombardo/L298N

just by searching for "L298N.h github".

Usually you'll find examples, (not always) further explanations but most interestingly: The source code of the library!

Well written libraries consist of

  • A header file (.h) that includes all information relevant for a use of the library in a sketch.
  • The main .cpp file that includes the source code for each of the classes, their methods and functions.

Of course it is of advantage to understand the principles of C++ classes but studying the code and structure of libraries can lead to a great step forward.

Just as an example see how the methods forward() and backward() are implemented in L298N.cpp:

void L298N::forward() {
  digitalWrite(_pinIN1, HIGH);
  digitalWrite(_pinIN2, LOW);

  analogWrite(_pinEnable, _pwmVal);

  _direction = FORWARD;
  _isMoving = true;
}

void L298N::backward() {
  digitalWrite(_pinIN1, LOW);
  digitalWrite(_pinIN2, HIGH);

  analogWrite(_pinEnable, _pwmVal);

  _direction = BACKWARD;
  _isMoving = true;
}

You'll find a lot in common with your latest sketch! Libraries are not witchcraft, just a structured use of (more or less) basic capabilities.

I wish you a lot of fun and success with Arduino!
ec2021

Thank you very much. I will gain this knowledge :sweat_smile:.