Flashing code onto Arduino Uno connected to power source and L298n motor

My code works if I run it with my laptop connected to my Arduino, the Arduino connected to the motor, and the motor powered by the battery pack. Now, I want the sketch to run with the Arduino disconnected from my computer. I have the battery connected to both the arduino and motor in series (they're both lighting up). But now when I plug in the arduino into my laptop, the port doesn't appear. It appears if I sever the Arduino-motor connection, but the code doesn't work if I reconnect the circuit after I flash the sketch (wheels don't turn).

Here's a diagram of the circuit

Here's the code as well, although I don't know if it's relevant

I am not going to download your code. More members will see your code if posted properly. Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code. If the code is too large to post it can be attached.

Your diagram link does not work for me. It would be better if you post the image so that we do not have to download it either. See How to post images.

Huh that’s weird, the link works for me before I attach it to text:

I tried posting my modified question but it didn’t show up, so I’ll comment here (sorry if you get a duplicate later on).

//WIRING: in1 - D5, in2 - D6, in3 - D9, in4 - D10
//analog joystick values (with pin definitions on left): left to right - x-axis 0 to 1000, up to down - y-axis 0 to 1000

// ---------------------------------------------------------------------------
// This Arduino sketch accompanies the OpenBot Android application.
// By Matthias Mueller, Intelligent Systems Lab, 2020
//
// The sketch has the following functinonalities:
//  - receive control commands from Android application (USB serial)
//. - produce low-level controls (PWM) for the vehicle
//  - toggle left and right indicator signals
//  - wheel odometry based on optical speed sensors
//  - estimate battery voltage via voltage divider
//  - estimate distance based on sonar sensor
//  - send sensor readings to Android application (USB serial)
//  - display vehicle status on OLED
//
// Dependencies: Install via "Tools --> Manage Libraries" (type library name in the search field)
//  - Interrupts: PinChangeInterrupt by Nico Hood (read speed sensors and sonar)
//  - OLED: Adafruit_SSD1306 & Adafruit_GFX (display vehicle status)
// Contributors:
//  - October 2020: OLED display support by Ingmar Stapel
// ---------------------------------------------------------------------------

// PIN_PWM_L1,PIN_PWM_L2,PIN_PWM_R1,PIN_PWM_R2  Low-level control of left DC motors via PWM
// PIN_SPEED_L, PIN_SPEED_R                     Measure left and right wheel speed
// PIN_VIN                                      Measure battery voltage via voltage divider
// PIN_TRIGGER                                  Arduino pin tied to trigger pin on ultrasonic sensor.
// PIN_ECHO                                     Arduino pin tied to echo pin on ultrasonic sensor.
// PIN_LED_LB, PIN_LED_RB                       Toggle left and right rear LEDs (indicator signals)

//------------------------------------------------------//
// DEFINITIONS
//------------------------------------------------------//

// DO NOT CHANGE!
#define DIY 0
#define PCB_V1 1
#define PCB_V2 2

//------------------------------------------------------//
//SETTINGS
//------------------------------------------------------//

// Setup the OpenBot version (DIY,PCB_V1,PCB_V2)
#define OPENBOT DIY
// Enable/Disable no phone mode (1,0)
// In no phone mode:
// - the motors will turn at 75% speed
// - the speed will be reduced if an obstacle is detected by the sonar sensor
// - the car will turn, if an obstacle is detected within STOP_THRESHOLD
// WARNING: If the sonar sensor is not setup, the car will go full speed forward!
#define NO_PHONE_MODE 1

//------------------------------------------------------//
// PINOUT
//------------------------------------------------------//
//Setup the pin definitions

#define PIN_PWM_L1 5
#define PIN_PWM_L2 6
#define PIN_PWM_R1 9
#define PIN_PWM_R2 10
#define X_pin 0
#define Y_pin 1

//------------------------------------------------------//
// INITIALIZATION
//------------------------------------------------------//

#include <limits.h>
const unsigned int STOP_THRESHOLD = 32; //cm

#if NO_PHONE_MODE
int turn_direction = 0; // right
const unsigned long TURN_DIRECTION_INTERVAL = 2000; // How frequently to change turn direction (ms).
unsigned long turn_direction_timeout = 0;   // After timeout (ms), random turn direction is updated.
#endif

//Vehicle Control
int ctrl_left = 0;
int ctrl_right = 0;
//Voltage measurement
const unsigned int VIN_ARR_SZ = 10;
unsigned int vin_counter = 0;
unsigned int vin_array[VIN_ARR_SZ] = {0};

//Speed sensor
const unsigned int DISK_HOLES = 20;
volatile int counter_left = 0;
volatile int counter_right = 0;

//Indicator Signal
const unsigned long INDICATOR_INTERVAL = 500; //Blinking rate of the indicator signal (ms).
unsigned long indicator_timeout = 0;
int indicator_val = 0;

//Serial communication
const unsigned long SEND_INTERVAL = 1000; // How frequently vehicle data is sent (ms).
unsigned long send_timeout = 0;
String inString = "";

//------------------------------------------------------//
// SETUP
//------------------------------------------------------//

void setup()
{
  //Outputs
  pinMode(PIN_PWM_L1, OUTPUT);
  pinMode(PIN_PWM_L2, OUTPUT);
  pinMode(PIN_PWM_R1, OUTPUT);
  pinMode(PIN_PWM_R2, OUTPUT);

  //Inputs
  Serial.begin(115200, SERIAL_8N1); //8 data bits, no parity, 1 stop bit
  send_timeout = millis() + SEND_INTERVAL; //wait for one interval to get readings

#if HAS_SPEED_SENSORS
  attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(PIN_SPEED_L), update_speed_left, FALLING);
  attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(PIN_SPEED_R), update_speed_right, FALLING);
#endif
}

//------------------------------------------------------//
// MAIN LOOP
//------------------------------------------------------//

void loop() {

  float joy_x = analogRead(X_pin);
  float joy_y = analogRead(Y_pin);
  Serial.println("X-axis: ");
  Serial.println(joy_x);
  Serial.println("Y_axis: ");
  Serial.println(joy_y);
  Serial.println("ctrl left: ");
  Serial.println(ctrl_left);
  Serial.println("ctrl_right: ");
  Serial.println(ctrl_right);
  // Send vehicle measurments to serial every SEND_INTERVAL
  if (millis() >= send_timeout) {
    send_vehicle_data();
    send_timeout = millis() + SEND_INTERVAL;
  }
  // drive forward
  if (joy_x > 450 && joy_x < 530) //neutral x is 504
  {
    if (joy_y > 500 && joy_y < 550)
    {
      analogWrite(PIN_PWM_L1, 0);
      analogWrite(PIN_PWM_L2, 0);
      analogWrite(PIN_PWM_R1, 0);
      analogWrite(PIN_PWM_R2, 0);
      ctrl_left = 0;
      ctrl_right = 0;
      Serial.println("not moving");
    }
    if (joy_y < 500)
    {
      ctrl_left = 5;
      ctrl_right = 5;
      Serial.println("going forwards");
    } else if (joy_y > 550) //neutral y is 523
    {
      ctrl_left = -5;
      ctrl_right = -5;
      Serial.println("going backwards");
    }
  } else if (joy_x < 450)
  {
    ctrl_left = 2;
    ctrl_right = 5;
    Serial.println("turn left");
  } else if (joy_x > 530)
  {
    ctrl_left = 5;
    ctrl_right = 2;
    Serial.println("turn right");
  }
  if (ctrl_left > 0)
  {
    ctrl_left = max(64, min(ctrl_left, 192));
  } else if (ctrl_left < 0)
  {
    ctrl_left = min(-64, max(ctrl_left, -192));
  } else
  {
    ctrl_left = 0;
  }
  if (ctrl_right > 0)
  {
    ctrl_right = max(64, min(ctrl_right, 192));
  } else if (ctrl_right < 0)
  {
    ctrl_right = min(-64, max(ctrl_right, -192));
  } else
  {
    ctrl_right = 0;
  }

  update_left_motors();
  update_right_motors();
}
//------------------------------------------------------//
// FUNCTIONS
//------------------------------------------------------//
void update_left_motors() {
  if (ctrl_left < 0) {
    analogWrite(PIN_PWM_L1, -ctrl_left);
    analogWrite(PIN_PWM_L2, 0);
  }
  else if (ctrl_left > 0) {
    analogWrite(PIN_PWM_L1, 0);
    analogWrite(PIN_PWM_L2, ctrl_left);
  }
  else { //Motor brake
    analogWrite(PIN_PWM_L1, 255);
    analogWrite(PIN_PWM_L2, 255);
  }
}

void update_right_motors() {
  if (ctrl_right < 0) {
    analogWrite(PIN_PWM_R1, -ctrl_right);
    analogWrite(PIN_PWM_R2, 0);
  }
  else if (ctrl_right > 0) {
    analogWrite(PIN_PWM_R1, 0);
    analogWrite(PIN_PWM_R2, ctrl_right);
  }
  else { //Motor brake
    analogWrite(PIN_PWM_R1, 255);
    analogWrite(PIN_PWM_R2, 255);
  }
}

void send_vehicle_data() {
  //  float voltage_value = get_voltage();
  int ticks_left = counter_left;
  counter_left = 0;
  int ticks_right = counter_right;
  counter_right = 0;

#if (NO_PHONE_MODE || HAS_OLED)
  float rpm_factor = 60.0 * (1000.0 / SEND_INTERVAL) / (DISK_HOLES);
  float rpm_left = ticks_left * rpm_factor;
  float rpm_right = ticks_right * rpm_factor;
#endif
#if (NO_PHONE_MODE)
  //  Serial.print("Voltage: "); Serial.println(voltage_value, 2);
  Serial.print("Left RPM: "); Serial.println(rpm_left, 0);
  Serial.print("Right RPM: "); Serial.println(rpm_right, 0);
  //    Serial.print("Distance: "); Serial.println(distance_estimate);
  Serial.println("------------------");
#else
  Serial.print(voltage_value);
  Serial.print(",");
  Serial.print(ticks_left);
  Serial.print(",");
  Serial.print(ticks_right);
  Serial.print(",");
  Serial.print(distance_estimate);
  Serial.println();
#endif
}

Where did you get the idea that the L298 would be wired like that? It is not right.

The L298 does not need to be connected to upload code.

Do a Google search for "L298 to Uno wiring" to see how to correctly wire it.

Your diagram, corrected.

forum 298.jpg

forum 298.jpg

Thanks! The circuit works. Can I attach another wire from the positive end of the battery to Vin of the arduino to power it?

Can I attach another wire from the positive end of the battery to Vin of the arduino to power it?

You can, but you must be careful. Powering through Vin or the power jack means that the Arduino and all peripherals that are on the 5V rail are powered by the onboard 5V regulator. The on board 5V regulator is not heat sinked so will supply limited current before it overheats and shuts down. The recommend max power dissipation for the regulator is 1 Watt. With 12V into the regulator the max current is about 140 mA (1W / (12V - 5V)). The Arduino uses around 50ma of that leaving less than 90mA (max) for everything else. I would use a buck converter to drop the 12V to 5V and connect that to the 5V on the Arduino, bypassing the, weak, 5V regulator. Then the rated current of the DC DC converter is available on the 5V line.

If I’m using the stm32 bluepill which doesn’t have a Vin pin, can I connect the positive end of the battery to 5v instead?

The old and mostly obsolete L298n for most real motor drivers chews up some of the voltage / current you supply it with so a better PSU would be an initial recommendation or a better driver (they are ok for the simplest learning projects)

I would not rely on the MCU board for it’s operation and you may find an alternate supply helps you overcome the issues you now face.

GroundFungus advice is spot on.