Arduino Stepper Motor Speed Control with Potentiometer

0
557

 In this Arduino electronics tutorial on stepper motor, it is shown how to control the speed of stepper motor using Potentiometer. This can be useful in many speed control application such as CNC machine, Robotics, Industrial manufacturing machines and others. For this AccelStepper stepper motor library is used. The stepper motor is Nema 17 and L298N IC is used as stepper motor controller. The Nema 17 used here has four wires with two phases and the 4 wire stepper motor Arduino code for acceleration control is provided. Video demonstration along with schematic drawing is also provided.

 

Interfacing & Circuit Schematic 

The potentiometer is connected to the analog input pin A0 of the Arduino. By varying the potentiometer, the ADC converts the voltage input to range of value between 0 to 1023. This ADC value is mapped to motor speed which is between maximum and minimum speed. The maximum and minimum speed is prior defined first in terms of RPM(Revolution Per Minute) and then converted to steps per seconds as required by the AccelStepper library. So as the potentiometer is rotated, the speed of the stepper motor is set. Once speed is set, we calculate the time taken for revolution and the RPM. Then at the end the revolution time in seconds, RPM and speed in steps per seconds are displayed on the serial monitor as outputs.

 Following is the picture of interfacing of Arduino with Stepper motor(Nema 17), L298 stepper motor controller, potentiometer and diodes on breadboard.

s2b252822529-2388178

 The following is the circuit schematic drawing of the interfacing of Arduino, Nema 17 stepper motor, L298N motor driver, potentiometer and diodes.

2-6111764

Arduino Code for Stepper Motor Control with Potentiometer

The following is the Arduino sketch for stepper motor speed control with potentiometer.


//Program for acceleration control for Stepper Motor using potentiometer, L298N & Arduino
//Source: https://ee-diary.blogspot.com

#include <AccelStepper.h>

//define pins for stepper motor
int en = 10;
int in1 = 9;
int in2 = 8;
int in3 = 7;
int in4 = 6;
//define pin for potentiometer
int potPin = A0;

int fullstep = 4; //number of stages in full drive
int halfstep = 8; //number of stages in half drive
int stepdrive = fullstep; //select step drive mode

//define stepper motor with step mode and inputs
AccelStepper stepper(stepdrive, in1, in2, in3, in4);

int steps = (stepdrive/4)*200; //number of steps per revolution
unsigned long revTime = 0;
//Set minimum and maximum speed in rpm
float rpmMin = 10.0;
float rpmMax = 50.0;
//Calculate minimum and maximum speed in steps/s
float speedMin = rpmMin*steps/60.0;
float speedMax = rpmMax*steps/60.0;
//variables
float rpm;
int anaValue, s;

void setup(){
Serial.begin(9600); //set Serial output baud rate
pinMode(en, OUTPUT); //set enable
digitalWrite(en, HIGH);
stepper.setMaxSpeed((stepdrive/4)*800); //max speed 800 or 1600 steps/s
stepper.setAcceleration(500); //set acceleration rate (steps/s^2)
//For output format
Serial.println("Outputs:");
Serial.println("Revol. Time(secs) RPM Speed(steps/sec)");
Serial.println("-------------------------------------------------------------n");
}

void loop() {
anaValue = analogRead(potPin); // potentiometer voltage
// map voltage to speed (step/s)
s = map(anaValue, 0, 1023, speedMin, speedMax);

stepper.move(100); //move the internal motor 100 steps
stepper.setSpeed(s); //set the internal motor speed
stepper.runSpeed(); //run the stepper motor
//For each complete revolution
if((stepper.currentPosition() % steps)==0) {
revTime = millis()-revTime; // time (ms) for one revolution
rpm = stepper.speed()*60.0/steps; // stepper motor rpm

//Outputs
//Print Revolution Time
Serial.print(String(revTime)+"s ttt");
//Print RPM
Serial.print(String(rpm,2)+"rpmtt");
//Print Speed
Serial.println(String(stepper.speed(),0)+"steps/sec");
delay(25); //delay 25ms to prevent any duplicates

revTime=millis(); //update revolution start time
}

}

In this Arduino stepper motor tutorial speed control using potentiometer was illustrated. Accelstepper library supports many other functionality such as acceleration rate control position control get RPM and speed and position information and others. To learn about acceleration control see the tutorial Stepper Motor Acceleration & Speed Control with Arduino. To learn about speed and position control see the tutorail Nema 17 Stepper Motor Speed and Direction Control with Arduino. The library also supports full drive stepping and half drive stepping. To learn about full drive mode see Arduino Stepper Motor Control using L298N and to learn about half drive mode see Half Drive Stepper Motor Control using Arduino.