4

I'm using an arduino connected to 2 DC motors through L298N. the arduino is powered by a 9V battery, and the L298N is powered by 4 AA batteries. there's also a switch that starts the movement.

I've added print statements to debug, one in the setup, one in the start of the loop, and two before and after running so i know when the motors start and how long they run. I also print the state of the switch. this is what i get:

1                          // switch state. 1  means off.
loop                       // start of loop statement
1
loop
0                          // switch state changed to on
0                          // added another switch check after the if
Forward for: 5 seconds     // motors on
setup                      // reset for some reason
loop                       // start of loop
0                          // switch still on
setup                      // another reset
loop                      
0
0
Forward for: 5 seconds     // motor starts again
setup                      // another reset
loop
0
0
Forward for: 5 seconds     // stuck with the motors on, no more resets

its not always this bad, sometimes it runs once or twice correctly (for five seconds) and then off. some times it gets stuck on without reseting. also, sometimes it starts reading the switch value as 1 after a few seconds of reading the correct value, even though the switch wasen't moved. also, when I remove one of the motor batteries so they don't get power everything works great. What causes the reset and the freezing? How can I fix that?

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;
int baseSpeed = 230;
int Stime;
int Etime;

void go(int d) { // d = 1 will move, d = 0 will stop
  analogWrite(RightSpeedPin, (d == 1 ? 230 : 0));
  analogWrite(LeftSpeedPin, (d == 1 ? 255 : 0));
}

void forward(int t) {
  Serial.print("Forward for: ");
  Serial.print(t / 1000);
  Serial.println(" seconds");
  digitalWrite(Rdir1, HIGH);
  digitalWrite(Ldir1, HIGH);
  digitalWrite(Rdir2, LOW);
  digitalWrite(Ldir2, LOW);
  digitalWrite(led, HIGH);
  Stime = millis();
  while (millis() < Stime + t) {
    go(1);
  }
  go(0);
  Etime = millis();
  Serial.print("ran for ");
  Serial.println(Etime - Stime);
  digitalWrite(led, LOW);
}

void spin() {
  digitalWrite(Rdir1, LOW);
  digitalWrite(Rdir2, HIGH);
  digitalWrite(Ldir1, HIGH);
  digitalWrite(Ldir2, LOW);
  Stime = millis();
  while (millis() < Stime + 100) {
    go(1);
  }
  go(0);
}

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_PULLUP);
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  Serial.println("setup");
}

void loop() {  
  Serial.println("loop");
  ReadVal = digitalRead(SwitchPin);
  Serial.println(ReadVal);
  if (!ReadVal) {
    delay(500);
    Serial.println(ReadVal);
  // starting situation- both motors heading forward
    // running for 5 secs
    forward(5000);
    delay(1000);
    spin();
  }
}

and this is my scheme:

circuit scheme

7
  • Does it also freeze if you remove the motors (not their batteries)?
    – PMF
    May 14, 2023 at 13:32
  • didn't try since it doesn't freeze when i remove the battery. i didn't think removing them completely might have a different result.
    – young marx
    May 14, 2023 at 14:53
  • 1
    Micro processor pull up resistors can vary greatly. If pulling the motors improves the expected behavior, consider adding a 1K (1K/3V = 3mA) pull up resistor to the enable / disable switch. If that improves things, try increasing it to 10K (10K/3V = 0.3mA). As you want to draw as small a current as possible to extend battery life. Also, avoid routing 9V and 6V battery wires in parallel as much as possible.
    – st2000
    May 14, 2023 at 15:23
  • @st2000 the problem is that the Arduino is resetting and freezing mid run when the motors are connected, why do you think the pull up resistor is the cause here? I don't get any problems when the motors aren't powered, and the resistor is still in use. the 9V is connected to the Arduino barrel jack, the 6V is connected to the L298N and the negative rail. there's no crossing.
    – young marx
    May 14, 2023 at 15:43
  • 1
    Brush DC motors (assumed what is being used) are making and breaking contact with inductive loads with each revolution. That is a lot of noise! Most small motors come with small capacitors across their input terminals to mitigate problems. The schematic looks correct. And the code has activated the internal pull up resistor on the enable input. But if the internal pull up resistor was large, noise could induce a high enough voltage to unexpectedly disable the motors. Or, perhaps, with enough noise, the processor may see enough supply voltage variation that it unexpectedly resets.
    – st2000
    May 14, 2023 at 18:53

2 Answers 2

2

The L298 has a pair of darlington bipolar transistors in each motor lead. This will cause a voltage drop of at least 1.4V on each lead or 2.8V to the motor, starving it of power. This voltage drop is burnt up as heat which your battery is supplying. A 9V smoke alarm battery is a bad choice for powering an Arduino even though many place show using it as a power source. Take a look at this link, it has some nice graphs showing just how much the voltage drop will be. https://www.powerstream.com/9V-Alkaline-tests.htm The maximum current draw of the Arduino R3 is in the range of 200mA. Try connecting several 9V batteries in parallel and if the problem disappears should the 9V battery.

1

When brushed motors are involved I usually think of arc suppression caps across the motor terminals, 100 mF or larger cap across the motor power supply, solid common (GND) connections. Sparking motor commutators can cause all sorts of problems.

1
  • I've tried with a 4700uF 16V cap across the motor driver and the common ground, its the highest F i could get and it did nothing. should i order a stronger cap online?
    – young marx
    Jun 12, 2023 at 19:05

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.