Stepper motor control with a potentiometer can be useful. Typically the stepper motor movement are controlled with programs and usually are programmed to do repeated predefined movement for automation. On the other hand, stepper motor control with potentiometer is manual. This type of stepper motor control is also important in many application. Earlier it was shown how to control a servo motor with potentiometer. Here it is illustrated how one can control a Nema 17 stepper motor using pontentiometer. The example is part of my bluetooth controlled arduino car which I wanted to modify into a crane.
Program code for Stepper Motor Control with Potentiometer
#include <AccelStepper.h>
// Stepper motor pin definitions
#define X_STEP_PIN 54
#define X_DIR_PIN 55
#define X_ENABLE_PIN 38
#define X_CS_PIN 53
#define T2 14 // Analog Input
// Initialize the stepper motor
AccelStepper stepper(AccelStepper::DRIVER, X_STEP_PIN, X_DIR_PIN);
void setup() {
Serial.begin(9600); // Initialize serial port for debugging.
pinMode(T2, INPUT); // configure to control stepper motor
// Initialize stepper motor
pinMode(X_ENABLE_PIN, OUTPUT);
digitalWrite(X_ENABLE_PIN, LOW); // Enable the stepper driver
stepper.setMaxSpeed(1000); // Set maximum speed for the stepper motor
stepper.setAcceleration(500); // Set acceleration for the stepper motor
// Test stepper motor by moving 180 degrees
int stepsPerRevolution = 200; // Number of steps per revolution for your stepper motor
int stepsFor180Degrees = stepsPerRevolution / 2; // Calculate steps for 180 degrees
Serial.println("Testing Stepper Motor");
stepper.moveTo(stepsFor180Degrees); // Move 180 degrees
while (stepper.distanceToGo() != 0) {
stepper.run();
}
delay(1000); // Wait for a moment
stepper.moveTo(0); // Move back to the original position
while (stepper.distanceToGo() != 0) {
stepper.run();
}
}
void loop() {
int potValue = analogRead(T2); // Read potentiometer value
int stepperPosition = map(potValue, 0, 1023, 0, 200); // Map potentiometer value to stepper position range (0 to 200)
stepper.moveTo(stepperPosition);
// Step the motor
stepper.run();
delay(100); // Delay for a short period before the next reading
}
1. Include AccelStepper Library
#include <AccelStepper.h>
The code starts by including the AccelStepper library, which simplifies controlling stepper motors with Arduino. This library provides advanced features for controlling stepper motors, such as acceleration and deceleration control.
2. Stepper Motor Pin Definitions
cpp// Stepper motor pin definitions
#define X_STEP_PIN 54
#define X_DIR_PIN 55
#define X_ENABLE_PIN 38
#define X_CS_PIN 53
#define T2 14 // Analog Input
- X_STEP_PIN (Pin 54): Controls the steps of the stepper motor.
- X_DIR_PIN (Pin 55): Controls the direction of the stepper motor.
- X_ENABLE_PIN (Pin 38): Enables the stepper motor driver.
- X_CS_PIN (Pin 53): Typically used for chip select in certain applications; not used in this example.
- T2 (Pin 14): Analog input pin connected to a potentiometer for reading its value.
3. Initialize the Stepper Motor
AccelStepper stepper(AccelStepper::DRIVER, X_STEP_PIN, X_DIR_PIN);
This line initializes the stepper motor object using the AccelStepper library. The DRIVER option indicates that the stepper motor is being controlled via a driver that requires step and direction inputs.
4. Setup Function
void setup() {
Serial.begin(9600); // Initialize serial port for debugging.
pinMode(T2, INPUT); // Configure to control stepper motorSerial.begin(9600): Initializes the serial communication at 9600 baud rate, which is useful for debugging and monitoring values in the Serial Monitor. pinMode(T2, INPUT): Sets the pin connected to the potentiometer as an input to read analog values.
5. Initialize Stepper Motor
// Initialize stepper motor
pinMode(X_ENABLE_PIN, OUTPUT);
digitalWrite(X_ENABLE_PIN, LOW); // Enable the stepper driver
stepper.setMaxSpeed(1000); // Set maximum speed for the stepper motor
stepper.setAcceleration(500); // Set acceleration for the stepper motorpinMode(X_ENABLE_PIN, OUTPUT): Configures the enable pin for the stepper motor driver as an output. digitalWrite(X_ENABLE_PIN, LOW): Activates the stepper motor driver by setting the enable pin to LOW (active low configuration). stepper.setMaxSpeed(1000): Sets the maximum speed for the stepper motor in steps per second. stepper.setAcceleration(500): Sets the acceleration for the stepper motor in steps per second squared.
6. Test Stepper Motor by Moving 180 Degrees
// Test stepper motor by moving 180 degrees
int stepsPerRevolution = 200; // Number of steps per revolution for your stepper motor
int stepsFor180Degrees = stepsPerRevolution / 2; // Calculate steps for 180 degrees
Serial.println("Testing Stepper Motor");
stepper.moveTo(stepsFor180Degrees); // Move 180 degrees
while (stepper.distanceToGo() != 0) {
stepper.run();
}
delay(1000); // Wait for a moment
stepper.moveTo(0); // Move back to the original position
while (stepper.distanceToGo() != 0) {
stepper.run();
}
}
- stepper.run(); Executes the necessary steps to move the motor. It needs to be called repeatedly in the loop for continuous motion.
stepper.run()
.7. Loop Function
void loop() {
int potValue = analogRead(T2); // Read potentiometer value
int stepperPosition = map(potValue, 0, 1023, 0, 200); // Map potentiometer value to stepper position range (0 to 200)
stepper.moveTo(stepperPosition);
// Step the motor
stepper.run();
delay(100); // Delay for a short period before the next reading
}
The loop()
function continuously reads the potentiometer's value and adjusts the stepper motor's position based on the input.
- int potValue = analogRead(T2); Reads the analog value from the potentiometer (range 0 to 1023) and stores it in
potValue
. - int stepperPosition = map(potValue, 0, 1023, 0, 200); Maps the potentiometer value to a stepper position. The
map()
function scales the potentiometer's 0-1023 range to a 0-200 range, corresponding to the stepper motor's position. - stepper.moveTo(stepperPosition); Moves the stepper motor to the calculated position based on the potentiometer reading.
- stepper.run(); Continues stepping the motor towards the target position.
- delay(100); Introduces a short delay (100 milliseconds) before the next potentiometer reading to prevent rapid changes.
Summary of the Program
- Initialization: The code initializes the stepper motor using the AccelStepper library, setting the stepper motor's speed and acceleration.
- Testing: In the
setup()
function, the stepper motor is tested by moving 180 degrees forward and back to ensure it is functioning correctly. - Control Loop: The
loop()
function reads the potentiometer's value to determine the desired position of the stepper motor, allowing manual control of the motor's position. The motor's position is updated based on the potentiometer's input, providing a basic but effective way to control the stepper motor.
This code provides a practical example of controlling a stepper motor's position using an analog input, making it suitable for projects requiring precise positioning based on user input.
Summary
Manual control via a potentiometer allows for real-time, user-driven adjustments. This method is beneficial in applications like camera sliders, where the operator can smoothly adjust the speed and position, or in prototyping and testing environments, where flexibility and control are necessary. It is also useful in educational settings, where learners can interact directly with the hardware to understand stepper motor dynamics better.
Earlier, I demonstrated how to use RAMPS shield with potentiometer to control a servo motor. Here in this example, I showed how you can control a Nema 17 stepper motor with a potentiometer. This setup is part of my Bluetooth-controlled Arduino car project, which I plan to modify into a crane for greater versatility.