3

I am trying to operate two DC motors at once using a L293D chip. I have managed to control them however, one of the motors has a slight problem. Whenever I start the program (attached below), one of the motor (I tried switching them, it isn't a problem due to the motor itself) has a counterclockwise spin for a second before starting to spin clockwise as it is suppose to. When it is spinning counterclockwise, the other motor is not doing any movements.

Is something wrong in the code or in the circuit? (I've also attached a hyperlink to my circuit which I found online, as well as my code)

Thank you

#define E1 11  // Enable Pin for motor 1
#define E2 10  // Enable Pin for motor 2
#define I1 12  // Control pin 1 for motor 1
#define I2 13  // Control pin 2 for motor 1
#define I3 8   // Control pin 1 for motor 2
#define I4 9   // Control pin 2 for motor 2

void setup() {
  pinMode(E1, OUTPUT);
  pinMode(E2, OUTPUT);
  pinMode(I1, OUTPUT);
  pinMode(I2, OUTPUT);
  pinMode(I3, OUTPUT);
  pinMode(I4, OUTPUT);
}

void loop() {
  analogWrite(E1, 255); // Run at full speed
  analogWrite(E2, 255); // Run at full speed
  digitalWrite(I1, HIGH);
  digitalWrite(I2, LOW);
  digitalWrite(I3, HIGH);
  digitalWrite(I4, LOW);
  delay(4500);
  // change direction
  digitalWrite(E1, LOW);
  digitalWrite(E2, LOW);
  delay(2000000000000);
  analogWrite(E1, 255);  // Run at full speed
  analogWrite(E2, 255);  // Run at full speed
  digitalWrite(I1, LOW);
  digitalWrite(I2, HIGH);
  digitalWrite(I3, LOW);
  digitalWrite(I4, HIGH);
  delay(000);
}

http://hw.sudarmuthu.com/wp-content/uploads/sites/2/2013/06/basic-bot1.png

2 Answers 2

1

I'm having trouble compiling your code Max. It's failing on this line: delay(2000000000000);

The last line of code: delay(000); doesn't seem to be doing anything useful and can be removed.

I do not have a L293D IC to test the following code with so I need you to test it for me please.

#define E1 11  // Enable Pin for motor 1
#define E2 10  // Enable Pin for motor 2

#define I1 12  // Control pin 1 for motor 1
#define I2 7   // Control pin 2 for motor 1 (change pin 13 to pin 7)
#define I3 8   // Control pin 1 for motor 2
#define I4 9   // Control pin 2 for motor 2

void setup() {
  pinMode(E1, OUTPUT);
  pinMode(E2, OUTPUT);
  pinMode(I1, OUTPUT);
  pinMode(I2, OUTPUT);
  pinMode(I3, OUTPUT);
  pinMode(I4, OUTPUT);

  // Disable motors
  digitalWrite(E1, LOW);
  digitalWrite(E2, LOW);
}

void loop() {

    // Set direction first
    digitalWrite(I1, HIGH);
    digitalWrite(I2, LOW);
    digitalWrite(I3, HIGH);
    digitalWrite(I4, LOW);

    // Enable motors
    analogWrite(E1, 255);
    analogWrite(E2, 255);

    // Motors run for 4.5 seconds
    delay(4500);

    // change direction

    // Disable motors
    digitalWrite(E1, LOW);
    digitalWrite(E2, LOW);

    // Give the motors time to stop if required
    delay(500);

    // Change motor direction
    digitalWrite(I1, LOW);
    digitalWrite(I2, HIGH);
    digitalWrite(I3, LOW);
    digitalWrite(I4, HIGH);

    // Enable motors
    analogWrite(E1, 255);
    analogWrite(E2, 255);

    // Motors run for 4.5 seconds
    delay(4500);

    // Disable motors
    digitalWrite(E1, LOW);
    digitalWrite(E2, LOW);

    // Give the motors time to stop if required
    delay(500);
}
1
  • It works! I switch the cable from pin 13 to 7 and now there is no longer that problem! Thanks. Yeah the code is weird but it is because I edit the times (as in sometimes I do use the last line). Thank you! Do you know why putting it as pin 13 caused this problem?
    – Max
    Oct 6, 2017 at 20:15
2
#define I2 13  // Control pin 2 for motor 1

Pin 13 is the LED and is blinked by the bootloader. You will have to use a different pin for your motor.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.