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 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
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.
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
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
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
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.