Hi guys,
I am using a stepper motor with an A4988 driver. I am using a potentiometer to change the speed using accel stepper library. However, as you can see, at low speed the stepper misses steps.
Any ideas?
Code:
#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER,5,4);
// This defines the analog input pin for reading the control voltage
// Tested with a 10k linear pot between 5v and GND
#define ANALOG_IN A0
double lastTime=0;
double thisTime=0;
void setup()
{
stepper.setMaxSpeed(200);
}
void loop()
{ int analog_in = analogRead(ANALOG_IN);
stepper.setSpeed(analog_in);
stepper.runSpeed();
}
Think about how many times per second you are doing the analog read. Is this really necessary? The data sheet for the Arduino processor states the processor is STOPPED for two instruction cycles while the analog read is taking place. How many times will this take place while running the stepper at low speed?
Paul
AKA intrtia. Things at rest want to stay at rest. Your motor does rest between steps. The length of the rest time is what determines the rotation time.
Paul
If you change the code, post the new version so that we can keep up.
Read the forum guidelines to see how to properly post code.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
To start with, post a wiring diagram, giving the voltage and current capabilities of the stepper motor power supply, and state the value to which you set the current limit on the A4988 driver. Post a link to the motor data sheet.
Provide details on the mounting arrangement of the stepper. Is a gearbox involved?
How much does the robot weigh? Have you done the power/torque calculations on what is required to move it?
The power supply for motors is 12V, 3.2A
Datasheet: https://www.datasheet4u.com/datasheet-pdf/MotionKing/17HS4401/pdf.php?id=928661
Model: 17HS4401
I bought the wheels which can be mounted directly on the motor shaft.
Motors run on full-step mode
I am building a self-balancing robot which weights 1.2kg approx.
I am testing the motor program using a potentiometer and later I will hook it up with a PID program.
Revised code
#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 5, 4);
// This defines the analog input pin for reading the control voltage
// Tested with a 10k linear pot between 5v and GND
#define ANALOG_IN A0
double lastTime = 0;
double thisTime = 0;
void setup()
{
stepper.setMaxSpeed(200);
}
void loop()
{ thisTime = millis();
if ((thisTime - lastTime) > 100) {
lastTime = thisTime;
int analog_in = analogRead(ANALOG_IN);
stepper.setSpeed(analog_in);
}
stepper.runSpeed();
}