Stepper Motor + Arduino Uno Hurdle

Hello, I have a simple NEMA 17 stepper motor hooked up with an arduino and also 5V going in to Analog Input A0. With the code I have, I have stepsPerRevolution = 2 and myStepper.step as 1 or -1 depending on the voltage it reads. The serial monitor reads the voltage fine, but the stepper motor doesn't move 180 degrees when the voltage is either above 2.6V or below 2.4V I thought that by having 2 steps per revolution and only taking 1 step at a time, it would move 180 degrees. Where is my problem at?

int sensorPin = 0;

#include <Stepper.h>

const int stepsPerRevolution = 2;

int dirPin = 8;

int stepPin = 9;

Stepper myStepper(stepsPerRevolution, dirPin, stepPin);

void setup()
{
  
  myStepper.setSpeed(10);
  
  pinMode(dirPin, OUTPUT);
  
  pinMode(stepPin, OUTPUT);
  
  Serial.begin(9600);
}


void loop() 
{
  
  int sensorValue = analogRead(sensorPin);
  
  float voltage = sensorValue * (5.0 / 1023.0);
  
  Serial.println(voltage);
  
  if (voltage > 2.60)
  {
    myStepper.step(1);
    
    delay(300);
  }
  else if (voltage < 2.40)
  {
    myStepper.step(-1);
    
    delay(300);  
  } 
    
}

NEMA 17 is a standard for the mounting portion of the motor. It tells us nothing about the current or voltage requirements of the motor. It is VERY unlikely that the motor has 2 steps per revolution. It is VERY unlikely that you can drive it directly from the Arduino.

I forgot to mention that I have an EasyDriver for the NEMA 17 as well. Is there a way or a better code to use to make it move 180 degrees? The step angle of the NEMA 17 is 1.8 degrees so it can do 200 steps per revolution. I apply 12V power to the EasyDriver to run the stepper motor.

Is there a way or a better code to use to make it move 180 degrees?

If you set the stepsPerRevolution correctly (200, not 2), and step 100 times, not 1, then the motor (forget about the NEMA 17, it means nothing) should turn 180 degrees per call.

PaulS:

If you set the stepsPerRevolution correctly (200, not 2), and step 100 times, not 1, then the motor (forget about the NEMA 17, it means nothing) should turn 180 degrees per call.

PaulS you have been great help and I greatly appreciate it. I changed my steps per revolution to 200 and the step to 1. My new code is:

int sensorPin = 0;

#include <Stepper.h>

const int stepsPerRevolution = 200;

int dirPin = 8;

int stepPin = 9;

Stepper myStepper(stepsPerRevolution, dirPin, stepPin);

void setup()
{
 
  myStepper.setSpeed(10);
 
  pinMode(dirPin, OUTPUT);
 
  pinMode(stepPin, OUTPUT);
 
  Serial.begin(9600);
}

void loop()
{
 
  int sensorValue = analogRead(sensorPin);
 
  float voltage = sensorValue * (5.0 / 1023.0);
 
  Serial.println(voltage);
 
  if (voltage > 2.60)
  {
    myStepper.step(100);
   
  }
  else if (voltage < 2.40)
  {
    myStepper.step(-100);
   
  }
   
}




I got rid of the 300ms delay because it still was not going 180 degrees per call. Right now, with the code uploaded from above, the stepper motor takes 3 steps, and then there is very small delay, and then it steps 3 times again and repeats. How can I get it where it can be a smooth 180 degree turn with a motor speed of 10 RPM? I am new to arduino coding and I apologize for my ignorance with the subject.

Right now, with the code uploaded from above, the stepper motor takes 3 steps, and then there is very small delay, and then it steps 3 times again and repeats. How can I get it where it can be a smooth 180 degree turn with a motor speed of 10 RPM?

How are you powering the stepper motor?

You might try getting it to work at much faster speeds, and then try slowing it down. The slower the motor turns, the more time is spend in hold between steps which draw a lot of current. If your power supply can't keep up the the demands, then you will have problems.

Add some Serial.print() statements, in setup() and loop(). Make sure that the Arduino is not resetting due to low voltage because the motors are hogging it all.