Project only works when Ardunio is USB connected

Hi all. New to Arduino, hope I chose the correct category.

I've seen this issue on the forums, and tried solutions that seem applicable, but to no avail.

The problem is that the 12v motor connected using an L298N driver spins only when the Arduino is connected to a PC using USB.

Solutions I have read and tried:

  • ensure 5v power supplied is actually 5v. Voltage meter says yes.
  • try a USB charging adapter instead of PC-connected USB. In this configuration, motor does not spin.
  • Insert code to blink LED-BUILTIN to see if code is actually running. LED_BUILTIN does blink in all configurations; USB-to-PC, USB-to-USBCharger, 9V battery connected to Elegoo 9V-5V power converter (voltage meter confirms Arduino 5V pin is getting 5V).

I also saw one post recommending hooking Ardunio ground PIN to ground on L298N. Didn't make sense to me but I tried. Result was the motor runs as if directly connected to 12v. IE code does not control the motor. (Hoping I didn't burn anything out).

What am I missing here?

#define enA 9
#define in1 7
#define in2 6

#define increment 1
#define interval 1000
#define lowspeed  205
#define highspeed 255

int direction = 0;
int isincrease = true;
int speed = lowspeed;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);

  // Set initial rotation direction
  direction = 0;
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  analogWrite(enA,0);  //start with no spin
}

void loop() {
  // Start Spinning

  if (isincrease == true) {
    speed += increment;
  }
  else {
    speed -= increment;
  }

  if (speed <= lowspeed) {
    speed = lowspeed;   //reset to lowspeed
    Serial.println("-----Low Speed-----");
    analogWrite(enA,speed);
    delay(interval * 3);
    Serial.println("--Change Direction--");
    if (direction == 0) {
      direction = 1;    //change direction
      digitalWrite(in2,LOW); //other way
      digitalWrite(in1,HIGH);
    }
    else {
      direction = 0;    //change direction
      digitalWrite(in1,LOW);
      digitalWrite(in2,HIGH); //other way
    }
    isincrease = true;
  }

  if (speed >= highspeed) {
    speed = highspeed;   //reset to lowspeed
    isincrease = false;
    Serial.println("-----High Speed-----");
    analogWrite(enA,speed);
    delay(interval * 3);
  }   
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(500); 
  Serial.println(speed);
  analogWrite(enA,speed);
  delay(interval); 

}


You don't have a circuit: an unbroken path that returns to its starting point. Electrons in the signals emanating from the Uno have no return path to come back to the Uno on. Otherwise known as a common ground.

2 Likes

Understood. But when I connect an Arduino GND pin to GND on L298n, the motor spins at top speed. The code is running, but motor spins at 100%, IE the PWM modulation is lost. I haven't let it run like that long enough to see if spin direction changes.

Then you have a new problem to work on.

Thank you van_der_decken! I retried when Arduino GND is connected to L298N GND, changing the code to simply reverse direction. It did.
I readded the PWM code to change speed as well.

All is working when USB is removed from Arduino and replaced with 5V power supply.

1 Like

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