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

young marx
  • 151
  • 3

2 Answers2

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.

Gil
  • 1,863
  • 9
  • 17
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.

Gene R
  • 11
  • 1