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);
 }
 }
}
per1234
4,2782 gold badges23 silver badges43 bronze badges
asked Feb 23, 2017 at 10:31
4
  • 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?) Commented Feb 23, 2017 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 ). Commented Dec 12, 2017 at 10:34
  • how many wires does the motor use? Commented Dec 13, 2017 at 0:22
  • your code looks like it is written to drive a regular motor Commented Dec 13, 2017 at 0:23

1 Answer 1

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);
 }
 }
}
gre_gor
1,6824 gold badges18 silver badges28 bronze badges
answered Dec 12, 2017 at 10:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.