Stepper motor using L298N driver, to add code to stop the revolution and to get it back to initial position

I would like to add a Code to the Stop the stepper motor when switch2 is pressed (push button), then I would like the stepper motor to go to initial position when a switch is turned off (ON&OFF switch) when the motor is running with the code, Please help me and if it needs additional IC please let me know.

#include <Stepper.h>

// Number of steps per output rotation
const int stepsPerRevolution = 200;

// Create Instance of Stepper library
Stepper myStepper(stepsPerRevolution, 2, 3, 4, 5);


void setup()
{
  // set the speed at 50 rpm:
  myStepper.setSpeed(50);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() 
{
  // step one revolution in one direction:
  Serial.println("clockwise");
  myStepper.step(1210);
  delay(500);

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-1210);
  delay(500);
}

I don't see any switches in your code. What have you tried? There are many examples in the IDE that show you how to use a button/swtich.

I am yet to add switches and switch is not added to code as well, I added a picture to give a general idea on why the stepper motor should return to initial state when switch is turned off, I need a code where the motor returns back to initial state, I want to attach a device to convoy belt, the device is supposed to up and down it should not pass over the top pully which is possible when the switch off command is given to the arduino, it stops suddenly and when it is turned on the code is run from beginning,
To put it way too simple, I want the arduino to finish the clockwise or counterclockwise depending on which direction it is running so that it returns back to the initial state

Does this setup run? What about the temperature of the stepper? Warm is okey but hot is wrong.
The L298 is usually not good for stepper motors. They usually call for current control.

Yes, it runs Up and Down perfectly(clockwise &anti-clockwise). I used L298N since it had a shield and temperature is perfectly normal

It sounds like you need limit switches on your setup. You can also check out the AccelStepper library which ramps motors up/down rather than abrupt stopping. This forum is not the place for others to write code for you. There are tons of examples. Google them, study them, learn from them. If/When you get stuck, post your best effort (using code tags) along with any errors and a good description of what you expect vs. what is actually happening and people with help.

That's good. Can You post a link to the stepper motor? Just for helpers to be sure. Looking at Your picture I don't feel safe.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.