Stepper motor turning randomly

I am new to electronics and even programming. I bought an Arduino Nano and a starter kit. I decided to play around with some code I found on the internet. I was wanting one button to drive the motor clockwise and the other to drive it counter. However, when I use the code the motor moves around randomly. I was wondering if it was my wiring or the program. Any help would be appreciated. I have attached a photo of the wiring and the code file to this. Any help would be appreciated.

#include <Stepper.h> //including stepper motor library

//defining pins section

int stepIN1Pin = 4;         
int stepIN2Pin = 5;
int stepIN3Pin = 6;
int stepIN4Pin = 7;
int stepsPerRevolution = 64; // amount of steps per revolution

const int button1Pin = 2;  // pushbutton 1 pin for clockwise rotation
const int button2Pin = 3;  // pushbutton 2 pin for counter clockwise rotation

Stepper myStepper(stepsPerRevolution, stepIN1Pin, stepIN3Pin, stepIN2Pin, stepIN4Pin);

void setup() {
  // Set up the pushbutton pins to be an input:
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  myStepper.setSpeed(64);
}

void loop() {
  // A == B means "EQUIVALENT". This is true if both sides are the same.
  // A && B means "AND". This is true if both sides are true.
  // A || B means "OR". This is true if either side is true.
  // !A means "NOT". This makes anything after it the opposite (true or false).

  //This section layouts the button scheme one button is clockwise rotation, the other counter clockwise 
  rotation  
int button1State, button2State;
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);
 if (((button1State == LOW) && !(button2State == LOW)))  // if we're pushing button 1 OR button 2
   myStepper.step(stepsPerRevolution);
if (((button2State == LOW) && !(button1State == LOW)))  // if we're pushing button 1 OR button 2
   myStepper.step(-stepsPerRevolution);
}

Do you have a PULL-UP résistance in your button?

=========

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

This is your image
78a90981448d9c4ed9314d8ecee7a8824aeed4f6.jpg

I don’t see pull-up… use

pinMode(button1Pin, INPUT_PULLUP);
 pinMode(button2Pin, INPUT_PULLUP);

Your buttons will bounce which will be bad as well…

Seems you are powering your motor from the arduino USB - that’s not great, need it’s separate power

Thank you guys,

I used 2 1k resistors and adjusted the code. That seemed to fix the issue.

But I am having a hard time understanding as to why that fixed the issue, anyone mind explain?

What would be a way to adjust the speed of the motor?

Without PULLUP, your button entry pin is floating and so you will read garbage on it. You don’t need to add resistors. They are built in your arduino and you activate them with the code above

Your motor seems to be the common BYJ48 STEPPER MOTOR which comes with a Breakout using ULN2003 As a Motor driver chip usually and to Power you Motor it is Recommanded to use an external Power Supply with 5V-500mA at least , Don't power it directly from arduino 5V pin.

There are thousands of tutorials on line for this setup, there is a built in library for steppers or the accelstepper library. Read about those too

Just as a courtesy - when you post a picture, please don't use a pizza box as the background.
It makes the detail of the picture harder to see, and also makes us hungry!

lastchancename:
Just as a courtesy - when you post a picture, please don't use a pizza box as the background.
It makes the detail of the picture harder to see, and also makes us hungry!

To be fair to OP - this is his background

So OP worling hard and not eating pizzas :slight_smile:

You guys have been a big help. Thank you very much!