Stepper motor is locking up

Hi all,

Working on a simple program with my Leonardo and Adafruit motor shield to drive a stepper when a momentary button is pushed. My program works but after a few presses of the button the board locks up and needs to be restarted. I'm guessing there's some kind of locking/race condition going on here but can't seem to figure it out.

Thanks in advance!

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

int buttonState = 0;        // initialize button to 0

void setup() {
  pinMode(2, INPUT_PULLUP);                 // input pin for button
  
  AFMS.begin();                             // intialize motor shield
  myMotor->setSpeed(100);                   // set RPM
  
  pinMode(LED_BUILTIN, OUTPUT);             // built-in LED
}

void loop() {
    buttonState = digitalRead(2);
    digitalWrite(LED_BUILTIN, LOW); 
    
    if(buttonState == LOW) //button pressed
    {
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    
    // turn motor
    myMotor->step (30, FORWARD, DOUBLE);
    }
}

Not enough information. It could be a hardware problem.

Please post a wiring diagram and links to the motor, motor power supply and motor control board.

The Adafruit Motor Shield is a brushed DC motor driver, not a current limiting stepper driver, and won't work for a majority of modern stepper motors. Pololu has a good collection of modern stepper drivers.

Attached are some photos.

Motor: Stepper motor - NEMA-17 size - 200 steps/rev, 12V 350mA : ID 324 : $14.00 : Adafruit Industries, Unique & fun DIY electronics and kits
Motor controller: Adafruit Motor/Stepper/Servo Shield for Arduino v2 Kit [v2.3] : ID 1438 : $19.95 : Adafruit Industries, Unique & fun DIY electronics and kits
Power supply: 12V 5A switching power supply : ID 352 : $24.95 : Adafruit Industries, Unique & fun DIY electronics and kits

Both the motor and controller are from Adafruit and supposed to work together (each controller supports up to 2 steppers).

Thanks in advance!

Yes, that high impedance motor should work with that motor controller. Many other steppers won't.

Please post a wiring diagram. The photos are uninformative.

Not sure what program is ideal to use for a wiring diagram. I have simplified things though. Basically just have the stepper connected to the shield per Adafruit webpage and switch to pin 2 and ground (using PULLUP). That's it.

Not sure what the blue light thing is but maybe try removing it in case it is drawing too much from the Arduino.

Also looks like there are jumper pads on the shield so make sure those are configured as per the specs from ADA.

Not sure what program is ideal to use for a wiring diagram.

Pen or pencil and paper have worked extremely well, for millennia.

Take a pic of your art work and post it.

Here is a pic of the setup and a pic of the diagram I made.

Simplified code

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <Servo.h> 


Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

int buttonState = 0;        // initialize button to 0

void setup() {
  Serial.begin(9600);                       // open serial port at 9600 bps;
  pinMode(2, INPUT_PULLUP);                 // input pin for button
  
  AFMS.begin();                             // intialize motor shield
  myMotor->setSpeed(100);                   // set RPM
  
  pinMode(LED_BUILTIN, OUTPUT);             // built-in LED
}

void loop() {
    digitalWrite(LED_BUILTIN, LOW);             // turn built-in LED off
    Serial.println(buttonState);
    
    while(digitalRead(2) == LOW) //button pressed
    {
      Serial.println(buttonState);
      digitalWrite(LED_BUILTIN, HIGH);   // turn the built-in LED on
    
    // turn motor
    myMotor->step (30, FORWARD, DOUBLE);
    }
    Serial.println(buttonState);
}

Always locks up but after a random number of button presses. I'm powering the Leonardo off of USB.

Ok so I just tried hooking things up to an Uno board and it seems to work fine there. I guess this Leonardo board is either defective or can't handle these kinds of programs? I'll have to get a replacement from the store.