Programming For Stepper Motor

Hello! I am having trouble programming a stepper motor (Kysan NEMA 17 from Arduino.com) to respond to an input. It is being used in an auto-regulating flow system. I have been learning to program in Arduino as I go, so my knowledge is still somewhat limited.

How it SHOULD work: Arduino Uno counts/stores pulses from a flow sensor (pin 6), calculates "flowrate" for display on an LCD, then a stepper motor opens or closes a valve if that value is outside a predetermined limit. Currently, the stepper is not responding to the code at all, but everything else is functional. The stepper operation has been verified with sample code.

Any insight or guesses as to why it might not be working would be GREATLY appreciated! Thank you so much!

//**These are pre-loaded libraries I want to include in my code
#include <LiquidCrystal.h>
#include <Stepper.h>
#include <MotorDriver.h>

//This is the basic setup of the code, where I define variables
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define FLOWSENSORPIN 6
#define MOTOR_CLOCKWISE      0
#define MOTOR_ANTICLOCKWISE  1
/******Motor Shield Pins definitions*************/
#define MOTORSHIELD_IN1	8
#define MOTORSHIELD_IN2	11
#define MOTORSHIELD_IN3	12
#define MOTORSHIELD_IN4	13
#define CTRLPIN_A	9
#define CTRLPIN_B	10
#define stepper_ctrl[]={0x27,0x36,0x1e,0x0f};
MotorStruct stepperMotor;


void initialize()
{
	pinMode(MOTORSHIELD_IN1,OUTPUT);
	pinMode(MOTORSHIELD_IN2,OUTPUT);
	pinMode(MOTORSHIELD_IN3,OUTPUT);
	pinMode(MOTORSHIELD_IN4,OUTPUT);
	pinMode(CTRLPIN_A,OUTPUT);
	pinMode(CTRLPIN_B,OUTPUT);
}

void setup() 
{
      pinMode(FLOWSENSORPIN, LOW);
      int pin = 6;
      unsigned long duration;
      lcd.begin(16,2);
}

void loop() 
{

    int flowpinstate = 0;
    int flowrate = 0; 
    int lastflowpinstate;
    int pulses = 0;
    long duration = 0;


    flowpinstate = digitalRead(FLOWSENSORPIN);
  
   if (flowpinstate != lastflowpinstate) //This compares the flow pin input to the previous to determine if there was a flow.
  
  { duration = pulseIn(FLOWSENSORPIN, HIGH)/1000; } //Divides the pulse time by 1000, giving a more easily interpreted number  
   {
      if (flowpinstate == HIGH) // If there was a high on the flow sensor, increments the pulse counter
     { (pulses++); } 
   
 }
   {flowrate = duration/pulses; }
    lastflowpinstate = flowpinstate; // Saves the current flowpin state for comparison in the next run of the loop 

    if (duration >= 100) 
      {duration = 0;
       pulses = 0;} 
    if (flowrate > 50)
      { stepperMotor.direction = MOTOR_ANTICLOCKWISE;}
    if (flowrate < 50)
      { stepperMotor.direction = MOTOR_CLOCKWISE;}
    
        
  
lcd.setCursor (0,0);
lcd.print ("FLOWRATE: "); lcd.print (flowrate);

delay(500);

 }

I have no experience with stepper motors, so please pardon the question, but what part of the code causes the stepper motor to move and by how much ?

Not a problem - I have limited experience with programming! It should be this part right here:

    if (flowrate > 50)
      { stepperMotor.direction = MOTOR_ANTICLOCKWISE;}
    if (flowrate < 50)
      { stepperMotor.direction = MOTOR_CLOCKWISE;}
      int pin = 6;
      unsigned long duration;

What are these for?

It should be this part right here:

That code defines the direction that the motor should step, if you ever actually told it to step.

You have WAY too many curly braces in your code, and the indenting is awful. The Tools + Auto Format menu item will stop you from embarrassing yourself again.

That code defines the direction that the motor should step, if you ever actually told it to step.

It's not just me that can't find the stepper movement code then.

Thank you! I'm actually not even remotely embarrassed - as stated, I'm extremely new to this, and it's a learning process!

I will look into the awful indenting and excessive curly braces, although if by curly braces you mean these: {}, I found while writing the code that I had to put these around any little function I wanted it to do in order to get it to compile correctly.

Do you possibly have any feedback regarding how I should instruct my stepper to turn? Thank you so much! I appreciate it!

Do you possibly have any feedback regarding how I should instruct my stepper to turn?

The stepper operation has been verified with sample code.

The sample code might be a good place to start.

Have a look at stepper motor basics and this simple stepper code.

You have not said what stepper driver board you are using but that simple code should allow you to test your motor with any board that requires step and direction signals.

Please also post a link to the datasheet for your stepper motor.
And details of the motor power supply - volts and amps.

...R

I found while writing the code that I had to put these around any little function I wanted it to do in order to get it to compile correctly.

{I'm} {quite} {certain} {that}{you} {didn't}.

PaulS:
{I'm} {quite} {certain} {that}{you} {didn't}.

Well that's just not necessary! I am sorry if I have offended you with my limited knowledge. I DID try the auto-format you suggested, but the environment stated that no changes were necessary. I could have done that wrong as well, I suppose.

As for everyone else, thank you! I am reading up on them right now and will update when I have some progress to report. I appreciate your time!

Robin2:
Please also post a link to the datasheet for your stepper motor.
And details of the motor power supply - volts and amps.

Here are the specifications for the stepper:

Specifications:
-Model number: 1124090
-Holding Torque: 5.5Kg.cm
-Rated Voltage: 4.2V
-NO.of Phase: 2
-Step Angle: 1.8° ± 5%
-Resistance Per Phase: 2.8Ω± 10%
-Inductance Per Phase: 4.8mH± 20%
-Current Per Phase: 1.5A
-Shaft: 5mm diameter w/ one flat
-Insulation Class: Class B
-Dielectric Strength: 100MOhm
-Operation Temp Range: -20 ~ +40° C
-Lead Wire: 22AWG / 750mm
-4 pin 2.54mm connector

Also, I am using an AC/DC power adapter, 12V/2A output

Robin2:
Have a look at stepper motor basics and this simple stepper code.

WOW! Thank you so much! That is all a LOT of good info, and I'm back to working on my code. I see now that (as was stated) I never actually told my motor to move - now THAT is embarrassing! :wink:

I have a quick question (for anyone) about defining the direction and step pins. Is it relevant which pins I use for these? If so, where do I find which pins control the individual functions?

My MotorShield uses pins 8, 11, 12, and 13 for the motor interface, and the sample code it came with listed pins 9 and 10 as control pins A and B, respectively, but does not indicate if each of these controls a specific function. (At least not to my untrained eyes).

byte directionPin = 9;
byte stepPin = 8;
[code]

This is so exciting! I've been roaming around unaided for quite sometime, and it's been dismal. Thank you so much!

justduckyjenn:
My MotorShield uses pins 8, 11, 12, and 13 for the motor interface, and the sample code it came with listed pins 9 and 10 as control pins A and B, respectively, but does not indicate if each of these controls a specific function. (At least not to my untrained eyes).

If you are referring to my simple stepper code, it is not intended to work with a motor shield. A motor shield is a poor choice for controlling a stepper. If you want to use a motor shield you would be well advised to use either the stepper or Accelstepper libraries. Personally I would recommend using an A4988 stepper driver in preference to the motor shield.

If you are referring to some other code please post the code.

...R

I was referring to your simple stepper code, the second version of it you posted specifically.

I would eventually like to look into a superior method of interfacing a stepper, but at this point, is it possible to accomplish my goal using the motor shield? I wasn't previously aware of the disadvantages, and the Motor Shield is what I have.

I would eventually like to look into a superior method of interfacing a stepper, but at this point, is it possible to accomplish my goal using the motor shield? I wasn't previously aware of the disadvantages, and the Motor Shield is what I have.

The Motor Shield won't work well, or at all, with that stepper motor. The winding resistance is too low.

Do yourself a great favor and buy one of the Pololu stepper drivers, like this one. Be sure to follow all the directions on the product page.

Also, you will need a motor power supply capable of providing at least 2 amperes, at any voltage between 9 and about 40 V.

If I were to purchase one, would I be able to easily interface with my current set-up? Meaning, does it connect as easily to my Arduino as the motor shield, and would I have to alter my code at all?

Thank you for the info - looking into it now.

The code would be different. But if you use the Stepper or Accelstepper libraries the only change would be to the line that defines the type of driver you use.

The connections would also be different as the A4988 drivers are not on shields - which does make them less convenient. On the other hand they don't block any of the Arduino pins that they don't need. The simplest way for testing is to plug the A4988 into a breadboard and connect wires from the breadboard to the Arduino and to the motor. Be careful to ensure the connections between the motor and the driver cannot accidentally come loose even for an instant while the device is powered up.

I have never driven a motor with a motor shield - only with A4988s. It is possible, but I am not competent to advise.

...R

Robin2:
The code would be different. But if you use the Stepper or Accelstepper libraries the only change would be to the line that defines the type of driver you use.

The simplest way for testing is to plug the A4988 into a breadboard and connect wires from the breadboard to the Arduino and to the motor.

Wonderful! I have ordered one and eagerly anticipate it's receipt.
Question: Is there somewhere on this site or another, to your knowledge, that provides instructions on what connections should be made to where?
Edit I found directions right here in these forums - just wanted to update!

Also, I assume your sample code was written with the A4988 in mind. Hopefully I will have more luck integrating the rest of my project now!

justduckyjenn:
Question: Is there somewhere on this site or another, to your knowledge, that provides instructions on what connections should be made to where?

I use the instructions on the Pololu A4988 web page.

...R

Robin2:
I use the instructions on the Pololu A4988 web page.

Ok! I received my A4988, connected it according to the instructions on the page, including the 100uF capacitor. I am using a 12VDC external power supply, and I connected my stepper as follows:

Coil 1 (Red, Green): 2B, 2A
Coil 2 (Yellow, Blue): 1A, 1B

The stepper isn't responding at all: not even a hum. Also, my "L" led on my Arduino is constantly lit. Perhaps I am still making an error in my programming? I attempted to glean what I need from Robin's sample code, and have included it.

NOTE: Even when I simply load Robin's basic code into my Arduino, I get no response. I do not have a pushbutton wired, so I used the original one to test. I think this likely speaks to hardware issues, but I hope not!

//**These are pre-loaded libraries I want to include in my code
#include <Stepper.h>
#include <MotorDriver.h>
#include <LiquidCrystal.h>


//This is the basic setup of the code, where I define variables
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define FLOWSENSORPIN 6
byte directionPin = 9;
byte stepPin = 8;

void setup()
{
  pinMode(FLOWSENSORPIN, LOW);
  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  unsigned long duration;
  lcd.begin(16, 2);
}

void loop()
{

  int flowpinstate = 0;
  int flowrate = 0;
  int lastflowpinstate;
  int pulses = 0;
  long duration = 0;


  flowpinstate = digitalRead(FLOWSENSORPIN);

  if (flowpinstate != lastflowpinstate) //This compares the flow pin input to the previous to determine if there was a flow.

  {
    duration = pulseIn(FLOWSENSORPIN, HIGH) / 1000; //Divides the pulse time by 1000, giving a more easily interpreted number
  }


  if (flowpinstate == HIGH) // If there was a high on the flow sensor, increments the pulse counter
  {
    (pulses++);
  }

  flowrate = duration / pulses;

  lastflowpinstate = flowpinstate; // Saves the current flowpin state for comparison in the next run of the loop

  if (flowrate < 50)
  { digitalWrite(directionPin, LOW);
    digitalWrite(stepPin, HIGH);
    digitalWrite(stepPin, LOW);
  }

  else if (flowrate > 50)
  {
    digitalWrite(directionPin, HIGH);
    digitalWrite(stepPin, HIGH);
    digitalWrite(stepPin, LOW);
  }

  if (flowrate >= 100)
  { duration = 0;
    pulses = 0;
  }

  lcd.setCursor (0, 0);
  lcd.print ("FLOWRATE: "); lcd.print (flowrate);

  delay(500);


}