1

I am using unipolar 1.8 degree stepper motor. I have written code to rotate stepper motor i.e. when A0 pin becomes HIGH it should rotate in clockwise and when A1 pin becomes HIGH it should move anti clockwise but motor is rotating only once when I am making A0/A1 HIGH and then stops. After that it is not rotating even if i am making A0, A1 pin high. I am very new to this please tell how to solve this problem. I am using microstepping drive, nema 17 stepper motor and Arduino Uno board.

#include <Stepper.h>

#define STEPS_PER_MOTOR_REVOLUTION 32

#define STEPS_PER_OUTPUT_REVOLUTION 32 * 200 //2048  

Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 9, 8);
int  Steps2Take;
int buttonApin = A0;
int buttonBpin = A1;
int dirpin = 8;
int steppin = 9;
void setup()
{
  pinMode(buttonApin, INPUT);
  pinMode(buttonApin, INPUT);
  pinMode(dirpin, OUTPUT);
  pinMode(steppin, OUTPUT);
  small_stepper.setSpeed(1900);
}
void loop()
{
  int valA = digitalRead(buttonApin);

  if (valA == HIGH)
  { digitalWrite(dirpin, HIGH);
    for (int x = 0; x < 100; x++)
    { digitalWrite(steppin, HIGH);
      delayMicroseconds(1000);
      digitalWrite(steppin, LOW);
      delayMicroseconds(1000);
    }
  }
  int valB = digitalRead(buttonBpin);

  if (valB == HIGH)
  {
    digitalWrite(dirpin, LOW);
    for (int x = 0; x < 100; x++)
    {
      digitalWrite(steppin, LOW);
      delayMicroseconds(1000);
      digitalWrite(steppin, HIGH);
      delayMicroseconds(1000);
    }
  }
}
  • 1
    Split the problem up, check you button code is working by using them to light a LED. Then get your motor spinning. (Any reason why you are using the Analog Pins?) – Code Gorilla Feb 23 '17 at 13:11
  • I think yo need to read the buttons with analogRead() as you have connected them to A0 and A1 pins of arduino (A0 & A1 pins are analog ). – Sonali_B Dec 12 '17 at 10:34
  • how many wires does the motor use? – jsotola Dec 13 '17 at 0:22
  • your code looks like it is written to drive a regular motor – jsotola Dec 13 '17 at 0:23
2

I have mentioned in the comments that you should read the buttons with analogRead(). But you can do it other way by simply connecting the button pin to digital inputs on arduino(any from 0-13). Analog pins are mostly used for accessing the sensor data as far as I know. I am correcting your code as below:

#include <Stepper.h>

#define STEPS_PER_MOTOR_REVOLUTION 32

#define STEPS_PER_OUTPUT_REVOLUTION 32 * 200 //2048  

Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 9, 8);
int  Steps2Take;
int buttonApin = 4; //connect button to digital pin 4 on arduino
int buttonBpin = 5; //connect button to digital pin 5 on arduino
int dirpin = 8;
int steppin = 9;
void setup()
{
  pinMode(buttonApin, INPUT);
  pinMode(buttonApin, INPUT);
  pinMode(dirpin, OUTPUT);
  pinMode(steppin, OUTPUT);
  small_stepper.setSpeed(1900);
}
void loop()
{
  int valA = digitalRead(buttonApin);

  if (valA == HIGH)
  { digitalWrite(dirpin, HIGH);
    for (int x = 0; x < 100; x++)
    { digitalWrite(steppin, HIGH);
      delayMicroseconds(1000);
      digitalWrite(steppin, LOW);
      delayMicroseconds(1000);
    }
  }
  int valB = digitalRead(buttonBpin);

  if (valB == HIGH)
  {
    digitalWrite(dirpin, LOW);
    for (int x = 0; x < 100; x++)
    {
      digitalWrite(steppin, LOW);
      delayMicroseconds(1000);
      digitalWrite(steppin, HIGH);
      delayMicroseconds(1000);
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.