1

I'm using an arduino uno with two DC motors. I'm also using the built in LED to tell me when the motors should be on as a debug tool. The code is a simple while loop that turns both the motors and the LED on for 10 seconds, and off for 5, and repeat, while a witch is on. This works fine when the motors are not connected to power (when the switch is on, the LED turns on for 10 secs, off for 5 and repeat), but when the motor driver is connected to the power the timing is changed. The motors and the LED are on for less than a second, and off for 5. the "on" period changes a bit, once it even reached the intended 10 seconds.

Why is that and how do I fix that? I tried changing batteries (though i don't know how that might change things, as the led is also working incorrectly and it works of a seperate power source).

the Arduino is connected to a 9V battery and the motors are connected to 4 AA batteries through a L298N motor driver.

this is my code:

#include <Arduino.h>
int RightSpeedPin = 5;
int Rdir2 = 6;
int Rdir1 = 7;
int led = 13;
int LeftSpeedPin = 10;
int Ldir2 = 8;
int Ldir1 = 9;
int SwitchPin = 4;
int ReadVal;
void setup() {
  // put your setup code here, to run once: 
  pinMode(Rdir1, OUTPUT);
  pinMode(Rdir2, OUTPUT);
  pinMode(RightSpeedPin, OUTPUT);
  pinMode(LeftSpeedPin, OUTPUT);
  pinMode(Ldir1, OUTPUT);
  pinMode(Ldir2, OUTPUT);
  pinMode(SwitchPin, INPUT);
  pinMode(led, OUTPUT);
}

void loop() {  
  ReadVal = digitalRead(SwitchPin);
  while (ReadVal == 1)
  {
    digitalWrite(led, HIGH);
  // starting situation- both motors heading forward
    // running for 10 secs
    digitalWrite(Rdir1, HIGH);
    digitalWrite(Ldir1, HIGH);
    digitalWrite(Rdir2, LOW);
    digitalWrite(Ldir2, LOW);
    analogWrite(RightSpeedPin, 255);
    analogWrite(LeftSpeedPin, 255);
    delay(10000);
    // off for 5
    analogWrite(RightSpeedPin, 0);
    analogWrite(LeftSpeedPin, 0);
    digitalWrite(led, LOW);
    ReadVal = digitalRead(SwitchPin);
    delay(5000);
  }
}

link to the schematics. its rough, I'm not very experienced at drawing schematics. Note that all the ground wires are connected to a breadboard ground rail other that the 9V that powers the arduino. its connected with a round plug, so i don't have individual wires. EDIT: fixed the schematic circuit

6
  • 1
    A schematic of your setup would help. Maybe a picture if all wires can be clearly seen. Also your sentence: "...and it works of a seperate power source)." sounds important but is confusing. I'd write an ANSWER, but I am only guessing w/o a schematic... did you connected the 5V power between the Arduino and L298N motor board?
    – st2000
    May 6, 2023 at 15:00
  • my first guess is that the state of the switch is not being detected correctly ... use a pullup resistor ... second guess is that the arduino is resetting ... you have no code to tell you when setup() executes ... use Serial.print() for debugging messages ... or blink the led twice inside setup()
    – jsotola
    May 6, 2023 at 16:27
  • added schematics. what would blinking inside of the setup tell me?
    – young marx
    May 6, 2023 at 19:20
  • 1
    think about when setup() executes ... read all of my comment again
    – jsotola
    May 6, 2023 at 19:28
  • Please embed the schematic in your question, do not use external resources, as they can disappear. May 7, 2023 at 11:47

1 Answer 1

0

After debugging by printing:

sTime = millis();
analogWrite(RightSpeedPin, 255);
analogWrite(LeftSpeedPin, 255);
delay(5000);
analogWrite(RightSpeedPin, 0);
analogWrite(LeftSpeedPin, 0);
Serial.print(millis() - stime);

I got times that are less than 300 ms, and no reset (added print to setup). I still have no idea why. after that, I just replaced delay with while (millis() < Stime + t) { which solved my problem.

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.