stepper motor control by Push Button

I want to control Stepper motor with Push Button. Push-button input taken in PIN2 and power supplied in Motor with external 5 v source. When i m uploading code in arduino… it is automatically running a motor without push-button…plz help

#include <Stepper.h>
// Define number of steps per rotation:
const int buttonPin = 2; // input button pin
const int stepsPerRevolution = 2048;
int ledPin = 13;
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);
int buttonState=0;
void setup() {
pinMode(ledPin, OUTPUT);
void loop() {
if (Serial.available() > 0)
digitalWrite(ledPin, HIGH);

//Step one revolution in the other direction:
else {

You are reading the state of the button pin then ignoring it. Why ?

What is connected to the Serial input ?

The ubiquitous pair (pinMode(buttonPin,INPUT); and if(buttonState==HIGH)) :slight_smile:
Do you have pullup or pulldown resistors on input pins to keep the pins from "floating" and causing false HIGHs or LOWs when the button is not pressed? Best way is like S3 in the diagram, no external resistor needed, logic is reversed though (pin is LOW when button is pressed).

Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks... Tom... :slight_smile: