Hey guys,
I am making a machine which reaquires two stepper motors to be controlled by a joystick via a motor shield. Everything in terms of the code works fine, but I need the motor itself to become completely rigid so that after a motor is moved and then stopped in a position, the motor doesnt return to the original position due to the overbearing its carrying. the motors will be moving a painting canvas in all directions and after it is moved to the direction it needs to, i'd want it to stay in that position unless I use the joystick to move it back into place.
Here is my code:
#include <AFMotor.h>
AF_Stepper motorX(200, 1); // 200 steps per revolution, motor port 1
AF_Stepper motorY(200, 2); // 200 steps per revolution, motor port 2
const int xAxisPin = A0;
const int yAxisPin = A1;
const int joystickThreshold = 50; // Adjust the threshold as needed
void setup() {
Serial.begin(9600);
Serial.println("AFMotor Library - Joystick Control");
motorX.setSpeed(200);
motorY.setSpeed(200);
pinMode(xAxisPin, INPUT);
pinMode(yAxisPin, INPUT);
}
void loop() {
int xValue = analogRead(xAxisPin);
int yValue = analogRead(yAxisPin);
int motorSpeedX = map(xValue, 0, 1023, -255, 255);
int motorSpeedY = map(yValue, 0, 1023, -255, 255);
// Control motor X with the X-axis
controlStepper(&motorX, motorSpeedX);
// Control motor Y with the Y-axis
controlStepper(&motorY, motorSpeedY);
delay(10);
}
void controlStepper(AF_Stepper *stepper, int speed) {
if (abs(speed) > joystickThreshold) {
stepper->setSpeed(abs(speed)); // Set the speed based on joystick input magnitude
if (speed > 0) {
stepper->step(1, FORWARD, SINGLE); // 1 step, FORWARD direction
} else {
stepper->step(1, BACKWARD, SINGLE); // 1 step, BACKWARD direction
}
} else {
stepper->release(); // Release the motor if joystick input is below the threshold
}
}
The items for the circuit are:
Arduino UNO
Motor Shield
2x NEMA 17 Bipolar Stepper Motors
Arduino Joystick
Were you NOT also given a power supply for the stepper motors? If there is inadequate power, voltage and current, then the motor is acting just as you designed the system. The answer is more TORQUE from the motor so it can hold in position. To get that you need to increase the supply voltage to the maximum the controller board can handle.
A "motor shield" cannot be used with most modern stepper motors.
You need a current-limiting stepper driver, capable of handing the rated current of the stepper motor, and a 12V to 30V power supply capable of delivering at least twice the rated stepper current.
Post a link to the steppers and forum members can recommend a suitable stepper driver.
With a current limiting stepper driver the PSU must be able to deliver the power the stepper needs. That's something different from the rated coil current. E.g. the higher the voltage, the lower the current needed from the PSU.
Good engineering practices require adding a significant safety margin for power supplies, a factor of 2 not being uncommon, hence my suggestion to use the rated current, rather than the power divided by the supply voltage.
That raises a red flag.
As @jremington said, modern (low impedance) stepper motors and brushed DC motor shields won't go together. Brushed DC motor shields can only drive high impedance steppers, but they have reduced torque at higher speeds (if that's important). Post links to the hardware.
Leo..
That's true. But as a current controlling stepper driver works like a buck converter, it makes absolutely no sense to base this margin on the coil current. The needed power is the only value to estimate the PSU, so the safety margin should also based on the needed power.
Assume a stepper with 2A and 1Ohm coil resistance. This needs 4W per coil or 8W for both. Even with a safety margin of 3 this are only 24W.
With 24V and your assumption of 4A the PSU would be far oversized with 96W.