Stepper Motor Forward Reverse and speed control using Potentiometer and button in Arduino mega 2560 -.Control speed in real-time stepper operation as your required speed with sensor and button to direction changes...-

arduino mega to control stepper direction and speed

download our free Arduino stepper control code and read the code carefully, then read all pins where to connect in our case steps pin is 8, direction pin is 9 and enables pin is 10 and potentiometer input pin is A0 (variable voltage signal) respectively.

how its work.

. first, upload the code into Arduino or Arduino mega then need to connect all connections than on power supply and increase speed first using a potentiometer to start operation then toggle button to change direction .you also used sensor instead of a button.

Stepper Motor Forward Revrse and speed control using Potentiometer and button .

// this file is created by yawot.com //any type of automation required please contact my website www.yawot.com

#include // defines pins numbers

const int stepPin = 8;

const int dirPin = 9;

const int enPin = 10;

const int LimitSwitch_LEFT_Pin = 11;

const int LimitSwitch_RIGHT_Pin = 12;

const int stepsPerRevolution = 3200; // change this to fit the number of steps per revolution // for your motor

// initialize the stepper library on pins 8 through 11:

Stepper myStepper(stepsPerRevolution, 5, 5, 2, 2);

int stepCount = 0; // number of steps the motor has taken

void setup() { // nothing to do inside the setup

pinMode(LimitSwitch_LEFT_Pin , INPUT);

pinMode(LimitSwitch_RIGHT_Pin , INPUT); // Sets the two pins as Outputs

pinMode(stepPin,OUTPUT);

pinMode(dirPin,OUTPUT);

pinMode(enPin,OUTPUT);

digitalWrite(enPin,LOW);

// Set Dir to Home switch

digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction }

void loop() {

// read the sensor value:

int sensorReading = analogRead(A0);

// map it to a range from 0 to 100:

int motorSpeed = map(sensorReading, 0,1023,1,100);

// set the motor speed:

if (motorSpeed > 0) {

myStepper.setSpeed(motorSpeed);

// step 1/100 of a revolution:

myStepper.step(stepsPerRevolution /400);

}

int leftSw = digitalRead( LimitSwitch_LEFT_Pin);

int rightSw = digitalRead( LimitSwitch_RIGHT_Pin);

if( (leftSw == HIGH && (digitalRead(dirPin) == HIGH)) ||

(rightSw == HIGH && (digitalRead(dirPin) == LOW)) ){

motorStep(1);

}

else if( leftSw == LOW && (digitalRead(dirPin) == HIGH) ){

digitalWrite(dirPin,LOW);

}

else if( rightSw == LOW && (digitalRead(dirPin) == LOW ) ){

digitalWrite(dirPin,HIGH);

///

}

}

void motorStep( int MAX){

for(int x = 0; x < MAX; x++){

digitalWrite(stepPin,HIGH);

delayMicroseconds(20);

digitalWrite(stepPin,LOW);

delayMicroseconds(20);

}

} www.yawot.com