Controlling motors with Arduino Mega

Hello,

I’m trying to make a robot that I can control using my smartphone. I’m using an Arduino Mega+Wifi which is a Mega with an integrated Esp8266 module. I made a simple program to test the motors (controlled by an Osoyoo Model-X controller), but it doesn’t move. When I take the robot in my hand I feel a small shake but it’s weird because the code only tells the motors to change direction twice, and the shake happens every second without stopping.
The robot carries two 9V batteries connected in parallel. The set of batteries is connected in parallel to the Arduino and the motors board (+ on Vin and 12V, - on GND of both boards).

I also tried the same code (adapted for UNO pins) on an Arduino UNO and it works, but I need the ESP module so I can’t use the UNO.

I also tried to connect the set of batteries to the motors controller and the Arduino to the computer (not connected to the batteries) to make sure the motors had enough power, and it still doesn’t work (and the shake disappears).

I tested the motors individually with 9V, they work.

I am desperate at this point I need help x)

Here is the code:

#define IN1  23    // K1,K2 motor direction
#define IN2  22    // K1,K2 motor direction
#define IN3  25    // K3,K4 motor direction
#define IN4  24   // K3,K4 motor direction
#define ENA  5    // Needs to be a PWM pin to be able to control motor speed ENA
#define ENB  6    // Needs to be a PWM pin to be able to control motor speed ENA

void clamp(int* x, int min, int max) {
  if(*x < min) *x = min;
  if(*x > max) *x = max;
}

//Sets the speed of each motor (an integer between -255 and 255)
void setSpeed(int leftSpeed, int rightSpeed) {

  //The speed shoud always be between -255 and 255
  clamp(&leftSpeed, -255, 255);
  clamp(&rightSpeed, -255, 255);

  //Sets the speed for each motor
  analogWrite(ENA, abs(leftSpeed));
  analogWrite(ENB, abs(rightSpeed));

  //Sets the direction of rotation for each motor
  digitalWrite(IN1, leftSpeed > 0);
  digitalWrite(IN2, leftSpeed < 0);
  digitalWrite(IN3, rightSpeed > 0);
  digitalWrite(IN4, rightSpeed < 0);
}

void setup() {
  
  pinMode(IN1, OUTPUT); 
  pinMode(IN2, OUTPUT); 
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT); 
  pinMode(ENA, OUTPUT);  
  pinMode(ENB, OUTPUT);
  
  setSpeed(0,0);
  delay(1000);
  setSpeed(-255, -255);
  delay(1000);
  setSpeed(0,0);
  delay(1000);
  setSpeed(255, 255);
  delay(1000);
  setSpeed(0,0);
}

void loop() {
}

I am unfamiliar with the Mega/ESP combo board. What voltage is supplied to the logic supply input of the L298 (pin 9 or Vss)? The L298 data sheet specifies the minimum logic supply at 4.5V.

What is the stall current of the motor? What is the rated voltage of the motor?

9V smoke alarm batteries are not a good choice for powering motors as they do not supply much current. Combine that with the 2 to 4 volts dropped by the L298 the motors may not be getting the power that they need.

The L298 is ancient and inefficient technology. There are much better modern motor drivers available. Pololu, for instance, has a wide range of motor drivers.

The L298 receives 9V, from the same power supply as the Arduino Mega.
I don't know much about the motors but I know they work if they are connected directly to the 9V battery.

After some more research I finally came to the same conclusion as you, the current is not sufficient. So I bought a set of 1.5V batteries, and I will try to replace my two 9V batteries in parallel by eight 1.5V batteries in serial. I will update this thread if it works (and if I don't forget)