STPLPB-01 Control Speed of Stepper Motor with L298N module with Arduino

Project Video
Project Details

This is code to control speed of Bipolar 4 Wire Stepper Motor with L298N Module and 3 push button switches.

  • Push button: Increase Speed
  • Push button: Decrease Speed
  • Push button: Start/Stop motor

Project Code

/*
 * 
 * Original code from Arduino IDE:  Stepper Motor Control - one revolution
 * Written by Ahmad Shamshiri on April 30, 2020 in Ajax, Ontario, Canada
 * 
 * This Arduino code allows you to control speed of Stepper Motor using L298N Module
 * with 3 push buttons to slow it down, speed it up, start or stop the motor
 * Direction of rotation is set in code
 * Video code: STLPB-01 on YouTube
 * 
 * Watch video for details: https://youtu.be/csf_G1pi0ps
 * 
 
***** Free Arduino Course worth $200****
Arduino Step by Step Course  (over 150 lectures) Starts here http://robojax.com/L/?id=338


If you found this tutorial helpful, please support me so I can continue creating 
 make donation using PayPal http://robojax.com/L/?id=64

* 
 * Code is available at http://robojax.com/learn/arduino

 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. 

 */

#include <Stepper.h>

//Robojax.com L298N Stepper Speed STLPB-01
const int speedPBInc  =2;//define input pin 
const int stopPB=3;//define input pin for Stop push button
const int speedPBDec =4;//define input pin 
const int motorPin[] ={8, 9, 10, 11};
const int direction =1;//0 for CW, 1 for CCW;
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
int speedStep = 5;
int stepMinimum = 5;
int stepMaximum = 100;
int stopType =0;//0=fully stopped , 1=hold (consumes energy) 

int currentSpeed=60;
int currentSPR=stepsPerRevolution;

#define START 1 //
#define STOP 0
#define CW 1
#define CCW -1
int motorStopState=STOP;//change if you need to
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, motorPin[0], motorPin[1], motorPin[2], motorPin[3]);

void setup() {
  //Robojax.com L298N Stepper Speed STLPB-01
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
  pinMode(speedPBInc, INPUT_PULLUP);
  pinMode(stopPB,INPUT_PULLUP);
  pinMode(speedPBDec,INPUT_PULLUP); 
  //attachInterrupt(digitalPinToInterrupt(stopPB), stopMotor, FALLING);  
}

void loop() {
  //Robojax.com L298N Stepper Speed STLPB-01
  updateState();

if(!motorStopState)
{
  currentSPR =0;
}else{
  currentSPR =stepsPerRevolution;  
}
  
  myStepper.setSpeed(currentSpeed);
  if(direction ==1)
  {
    myStepper.step(-currentSPR);
  }else{
    myStepper.step(currentSPR);    
  }
 

}//loop


/*
 * updateState()
 * @brief reads push buttons and updates values
 * @param none
 * @return no return
 * Written by Ahmad Shamshiri for robojax.com
 * on Nov 01, 2019 at 18:10 in Ajax, Ontario, Canada
 */
void updateState()
{

 //Robojax.com L298N Stepper Speed STLPB-01
  if(digitalRead(stopPB) ==LOW)
  {
   motorStopState =1-motorStopState;// stop the motor
    if(motorStopState  ==STOP)
    {
      stopMotor();
    }
   delay(500);
 
  }

  if(digitalRead(speedPBInc) ==LOW)
  {
  motorStopState = START;
  currentSpeed += speedStep;
   if( currentSpeed >=stepMaximum )  currentSpeed =stepMaximum ;
  }

  if(digitalRead(speedPBDec) ==LOW)
  {
  motorStopState = START;    
   currentSpeed -= speedStep;
   if( currentSpeed <stepMinimum )  currentSpeed =stepMinimum ;
  }  
//Robojax.com L298N Stepper Speed STLPB-01  
}//updateState end


/*
 * stopMotor()
 * @brief turns themotor OFF
 * @param none
 * @return no return
 * Written by Ahmad Shamshiri for robojax.com
 * on May 02, 2020 in Ajax, Ontario, Canada
 */
void stopMotor()
{
  //Robojax.com L298N Stepper Speed STLPB-01
  if(stopType ==0)
  {
    for(int i=0; i<4; i++)
    {
      digitalWrite(motorPin[i], LOW);
    }
  }
  //Robojax.com L298N Stepper Speed STLPB-01
}//stopMotor() end