Elegoo V4 motor movement

I am trying to get the following sequence forward, left,right,backward but it's moving first to the right and then to the left, I create the sequence of event by hand and I think that I have the right configuration, if there is somebody that knows what I've been failing please let me know thank you,
PD: I checked the connections and everything is good.

// Define motor control pins
#define PWMA 5  // Motor A Speed (PWM)
#define AIN1 7  // Motor A Direction
#define PWMB 6  // Motor B Speed (PWM)
#define BIN1 8  // Motor B Direction
#define STBY 3  // Standby Pin 

// Function to move forward
void moveForward(int speed) {
  digitalWrite(AIN1, HIGH); // Motor A Forward
  digitalWrite(BIN1, HIGH); // Motor B Forward
  analogWrite(PWMA, speed); // Set speed (0-255)
  analogWrite(PWMB, speed);
}

// Function to move backward
void moveBackward(int speed) {
  digitalWrite(AIN1, LOW); // Motor A Reverse
  digitalWrite(BIN1, LOW); // Motor B Reverse
  analogWrite(PWMA, speed);
  analogWrite(PWMB, speed);
}

// Function to turn left
void turnLeft(int speed) {
  digitalWrite(AIN1, LOW);  // Motor A Reverse
  digitalWrite(BIN1, HIGH); // Motor B Forward
  analogWrite(PWMA, speed);
  analogWrite(PWMB, speed);
}

// Function to turn right
void turnRight(int speed) {
  digitalWrite(AIN1, HIGH); // Motor A Forward
  digitalWrite(BIN1, LOW);  // Motor B Reverse
  analogWrite(PWMA, speed);
  analogWrite(PWMB, speed);
}

// Function to stop the motors
void stopMotors() {
  analogWrite(PWMA, 0);
  analogWrite(PWMB, 0);
  delay(200);
  digitalWrite(STBY, LOW);
}

void setup() {
  pinMode(PWMA, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(PWMB, OUTPUT);
  pinMode(BIN1, OUTPUT);
  pinMode(STBY, OUTPUT); // Enable TB6612
  digitalWrite(STBY, HIGH); // Activate motor driver
}

void loop() {
  moveForward(200); // Move forward at speed 200
  delay(2000);

  turnLeft(200); // Turn Left at speed 200
  delay(1000);

  turnRight(200); // Turn Right at speed 200
  delay(1000);

  moveBackward(200); // Move backward at speed 200
  delay(2000);

  stopMotors(); // Stop the motors
  delay(1000);
}

I expect if you posted an annotated schematic showing exactly how you wired this will get you some good answers. If you go that path be sure to show all parts, connections, power, ground, power sources etc.

thank you !

Motor A is not moving at all?

yes, is turning to the right in the meantime that motor A goes backward and B forward but the line code said that has to do the opposite:

void turnLeft(int speed) {
  digitalWrite(AIN1, LOW);  // Motor A Reverse
  digitalWrite(BIN1, HIGH); // Motor B Forward
  analogWrite(PWMA, speed);
  analogWrite(PWMB, speed);
}

and happened the same with turnRight

Your post is misleading. I thought it wasn't going forwards and backwards at all..

The code doesn't know which side the motor is located.
Just rename the functions opposite, or swap them in the pin definition.

#define PWMA 6  // Motor A Speed (PWM)
#define AIN1 8  // Motor A Direction
#define PWMB 5  // Motor B Speed (PWM)
#define BIN1 7  // Motor B Direction
#define STBY 3  // Standby Pin

It works thank you!

You are welcome!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.