Motor issues while using motor driver

I have a circuit I have modeled on TinkerCad using two DC motors and an L293D motor driver, when I change the max speed of my motors in my code, motor2 changes its RPMs accordingly but motor1 either does not change or is just 0 RPMs depending on the value. Why is this happening?(I can't upload the schematics because I'm a new user :frowning:

Here is my code:


#include <Servo.h>

// Motor control pins for L293D
const int Motor1Enable = 7;  // Enable pin for Motor 1
const int Motor1Pin1 = 9;    // Input 1 for Motor 1
const int Motor1Pin2 = 8;    // Input 2 for Motor 1
const int Motor2Enable = 5;   // Enable pin for Motor 2
const int Motor2Pin1 = 3;     // Input 1 for Motor 2
const int Motor2Pin2 = 4;     // Input 2 for Motor 2

const int SlideSwitch = 6;    // Pin for the slide switch
const int ServoPin = 11;       // Pin for Servo

const int MaxMotorSpeed = 25; // PWM value to limit speed to ~2200 RPM

unsigned long motorsStartTime = 0;
unsigned long servoMoveTime = 0;
bool sequenceRunning = false;
bool lastSwitchState = HIGH;   // Assume switch starts in OFF position

Servo myServo;  // Create a servo object

void setup() {
  Serial.begin(9600);
  pinMode(SlideSwitch, INPUT_PULLUP);  // Slide switch as input with internal pull-up resistor
  
  // Motor pin setup
  pinMode(Motor1Enable, OUTPUT);
  pinMode(Motor1Pin1, OUTPUT);
  pinMode(Motor1Pin2, OUTPUT);
  pinMode(Motor2Enable, OUTPUT);
  pinMode(Motor2Pin1, OUTPUT);
  pinMode(Motor2Pin2, OUTPUT);
  
  myServo.attach(ServoPin);  // Attach the servo to its pin

  // Initialize motors to OFF and servo to 0 degrees
  stopMotors();
  myServo.write(0);
}

void loop() {
  int switchState = digitalRead(SlideSwitch);  // Read the state of the slide switch

  // Detect when switch is turned ON (LOW) and sequence is not already running
  if (switchState == LOW && lastSwitchState == HIGH && !sequenceRunning) {
    sequenceRunning = true;
    runMotorSequence();
  }

  lastSwitchState = switchState;  // Update last switch state
}

void runMotorSequence() {
  // Start Motor1 and Motor2 at limited speed
  Serial.println("Motor1 and Motor2 starting at limited speed");
  startMotors();
  motorsStartTime = millis();

  // Run Motor1 and Motor2 for 5 seconds
  while (millis() - motorsStartTime < 5000) {
    // Wait
  }

  // Move Servo to 90 degrees
  Serial.println("Servo moving to 90 degrees (Motor1 and Motor2 stay on)");
  myServo.write(90);
  servoMoveTime = millis();

  // Wait for 3 seconds
  while (millis() - servoMoveTime < 3000) {
    // Wait
  }

  // Move Servo back to 0 degrees
  Serial.println("Servo moving back to 0 degrees (Motor1 and Motor2 stay on)");
  myServo.write(0);

  // Run Motor1 and Motor2 for 3 more seconds
  motorsStartTime = millis();
  while (millis() - motorsStartTime < 3000) {
    // Wait
  }

  // Turn off Motor1 and Motor2
  Serial.println("Motor1 and Motor2 stopped");
  stopMotors();

  Serial.println("Sequence complete. Both motors are off and Servo is at 0 degrees.");
  sequenceRunning = false;
}

void startMotors() {
  // Set direction and enable Motor 1
  digitalWrite(Motor1Pin1, HIGH);
  digitalWrite(Motor1Pin2, LOW);
  analogWrite(Motor1Enable, MaxMotorSpeed);
  
  // Set direction and enable Motor 2
  digitalWrite(Motor2Pin1, HIGH);
  digitalWrite(Motor2Pin2, LOW);
  analogWrite(Motor2Enable, MaxMotorSpeed);
}

void stopMotors() {
  // Stop Motor 1
  digitalWrite(Motor1Pin1, LOW);
  digitalWrite(Motor1Pin2, LOW);
  analogWrite(Motor1Enable, 0);
  
  // Stop Motor 2
  digitalWrite(Motor2Pin1, LOW);
  digitalWrite(Motor2Pin2, LOW);
  analogWrite(Motor2Enable, 0);
}

Your topic has been moved. Please do not post in "Uncategorized"; see the sticky topics in Uncategorized - Arduino Forum.

  • Where did you find this code ?
  • Is this on an UNO ?
const int Motor1Enable = 7;  // Enable pin for Motor 1
. . .
 analogWrite(Motor1Enable, MaxMotorSpeed);

PWM pins:
UNO (R3 and earlier), Nano, Mini 3, 5, 6, 9, 10, 11

  • Do you see the problem ?
1 Like

Yes, but now motor1 RPMs are being limited much more than motor2 RPMs, I changed motor1 enable pin to a PWM pin.

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