Step motor resets arduino

Whenever I hold the stepper motor in place with a handle, the Arduino seems to reset, which is something I want to prevent. I'm using the following code:

#include <Encoder.h>
#include <Stepper.h>

// Stepper motor configuration
const int stepsPerRevolution = 2048;  
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);  

// Encoder configuration
Encoder myEnc(2, 3);  

const int centerValue = 0;              // Center position value for the encoder
const int inactivityTimeout = 5000;     // Time in milliseconds to return to center
const int deadband = 2;                  // Deadband for preventing rapid switching
const int returnSteps = 5;               // Steps to return towards center

unsigned long lastMovementTime = 0;     // Timestamp for last movement
long position = 0;                       // Current encoder position

void setup() {
    Serial.begin(9600);
    myStepper.setSpeed(10);  // Set the speed of the stepper motor
    Serial.println("Tiller Control Initialized:");
}

void loop() {
    // Read encoder value
    long newPos = myEnc.read();
    
    if (newPos != position) {
        position = newPos;
        Serial.print("Tiller Position: ");
        Serial.println(position);
        lastMovementTime = millis();  // Update last movement time
    }

    // Calculate deviation from the center value
    int deviation = position - centerValue;

    // Apply deadband to prevent rapid switching
    if (abs(deviation) > deadband) {
        // Move the stepper motor based on deviation
        if (deviation > 0) {
            myStepper.step(-1);  // Gradually turn counterclockwise
        } else {
            myStepper.step(1);   // Gradually turn clockwise
        }
    } else {
        // Stop the motor if within the deadband
        myStepper.step(0);  // No movement
    }

    // Check for inactivity
    if (millis() - lastMovementTime > inactivityTimeout) {
        // Gradually bring back to center after inactivity timeout
        if (abs(position) > deadband) {  // Move only if position is significant
            if (position > 0) {
                myStepper.step(-returnSteps);  // Move back to center counterclockwise
            } else {
                myStepper.step(returnSteps);   // Move back to center clockwise
            }
        } else {
            // Stop the motor if close to center
            myStepper.step(0);  // No movement
        }
    }
}

How can I prevent the Arduino from resetting when the stepper motor is held in place?

(the step motor has a cog attached to a rotary encoder which also has a cog, that way the step motor can print out positional data)

Also the entire board is powered from a USB port on my PC

A schematic would show us all how your stepper is both powered and connected to the Arduino.

i tried generating a schematic but the website didn't have my exact parts, I could send a picture though

a handdrawn schematic is really sufficient.
Here is a short tutorial how to make a hand-drawn schematic.

Additionally you can post pictures. There are some rules for such pictures too:

take picture from vertically above. If you take the picture from an angle you have an optical error called parallaxis which means numbers on the pcb are very hard to relate with the jumper-wire

all wires must be visible from end to end

use standard-colors for standard things:
black or blue wires for GND
red wire for +Vcc

other colors for signals

I think I need a 9v battery to go with the USB

image

Two questions:

  1. How much current does your stepper motor require?
  2. How much current can your USB port supply?

If the answer to question #1 is greater than the answer to question #2, there's your answer as to why the Arduino resets. Beyond the modest current requirements of some discrete LEDs, a few sensors and maybe a display, the Arduino is not usable as a power supply.

If you mean this kind of battery

This is not sufficient either.
Such 9V batteries are unable to deliver a higher current.
Their capacity is 200 mAh.

If your stepper-motor draws 200 mA. The battery will be empty after one hour.
Even if your stepper-motor is just standing still because stepper-motor draw the same amount of current when standing still as when they rotate

You should post a picture and datasheet of

  • your stepper-motor
  • and the stepperdriver

Those can't make much current either. Use 6 AA or AAA batteries and it will work better.

i have the 28byj48 stepper-motor and the uln2003 driver board, the driver board says 5-12V, the motor says 5V DC, the usb port though i'm not sure, one small detail though, when using a USB extension cord, the motor works but resets, when connecting directly to the pc it does not even move

That is the voltage. What you need to find is how much current.

If the motor says 5V, supplying it with 9V won't end happily.

You need a separate, relatively high current capable, 5V supply. Don't forget to connect the grounds to complete the circuit.

the current is (i think) 200mA

So it is this stepper-motor?

the specs say 100 mA per coil.
The stepper-motor has two coils which means 200 mA.

You should not exceed 5V. If you supply this stepper-motor with more than 5V
the stepper-motor will get too warm and will burn through.

You need a power-supply of 5V minimum 500 mA.
One of these smartphone wall-plug-chargers will do.
Most of them have 5V 1A or 5V 2A

You have to connect the GND of the extra-power-supply with GND of the microcontroller-board

For more detailed explanations post the datasheet of your steppermotor-driver or a picture of your steppermotor-driver

So THAT’S why it was heating up, but I wired it to the 5V pin though

That is a too unprecise description to understand what you really did.
Use much more words or a schematic to show what

is meaning.

Do you have a digital multimeter for measuring voltages and current?

Do you know how to measure voltages?

Measuring currents is done totally different than measuring voltages !
Do you know how to measure current?

I think my brother has a multimeter somewhere but I have no idea how to measure the current, and about what I did, I kind of just wired it in a way that it would turn on in the first place as I’ve never used a step motor before

How about posting a smartphone taken picture?

Sure, let me just change my positive and negative cable colors.

I just wired up a Nano to a ULN2003 driver board hooked up to a 28BYJ-48 motor, and powered the lot from a 5V bench power supply. 370mA. You might get away with that, so long as your USB cable didn't have too much internal resistance. But as you've already found out, you might end up with one that had a higher internal resistance and that wouldn't do the job. The more current the motor tried to draw, the hotter the cable got, the higher the resistance got, the lower the voltage got at the far end, and the Uno browned out.

Use a proper 5V power supply for the driver board. One that can deliver at least 500mA; 1A or higher would be better.