Arduino - Potentiometer Control Stepper Motor
plusdo
1 min read
Table of contents
Arduino - Potentiometer Control Stepper Motor
use potentiometer to control stepper motor turning speed
Highlight: A potentiometer is able to output a very stable signal with a range of 0 to 100.
#include <Stepper.h>
const int stepsPerRevolution = 2048;
const int rpm = 12;
Stepper stepper1 = Stepper(stepsPerRevolution, 8, 10, 9, 11);
int rawValue;
int oldValue;
byte potPercentage;
byte oldPercentage;
void setup() {
Serial.begin(115200); // set serial monitor to this baud rate
}
void loop() {
// read input twice
rawValue = analogRead(A0);
rawValue = analogRead(A0); // double read
// ignore bad hop-on region of a pot by removing 8 values at both extremes
rawValue = constrain(rawValue, 8, 1015);
// add some deadband
if (rawValue < (oldValue - 4) || rawValue > (oldValue + 4)) {
oldValue = rawValue;
// convert to percentage
potPercentage = map(oldValue, 8, 1008, 0, 100);
// Only print if %value changes
if (oldPercentage != potPercentage) {
Serial.print("Pot percentage is: ");
Serial.print(potPercentage);
Serial.println(" %");
oldPercentage = potPercentage;
}
}
//stepper motor speed control
stepper1.setSpeed(rpm * potPercentage / 100);
stepper1.step(stepsPerRevolution);
delay(10);
}
0
Subscribe to my newsletter
Read articles from plusdo directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by