Hi Everyone,
I'm a beginner, first post.I read the "how to use this forum" a couple times, and i'll try to stick to the rules.
Please let me know if there is something i'm not doing right, (i think you will!) and i'll make the corrections.
I also spent days looking for a solution on this forum and elsewhere (the AccelStepper Google group for example)
================
setup
Arduino uno + easy drivers + Nema17 (specs/datasheets below)
Problem
The unexpected behavior with the Arduino example "stepper_oneRevolution":
>>> The motor doesn't go backward.
After setting the step per revolution to 1600, it goes counter-clock wise half a revolution, then again counter-clock wise 1 full revolution.
================
To start with and match the wiring (Easy Driver Hook-up Guide - SparkFun Learn), I changed the pins number from this:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
to this:
Stepper myStepper(stepsPerRevolution, 2, 3, 4, 5);
I also tried to swap 2 and 3 and a few other combinations with no success. I got it going backward at some point (2,4,3,5 if i remember) but the Forward and backward moves weren't symmetric.
I looked at the Stepper.h file but couldn't find anything obvious. (not much coding knowledge here)
================
Questions:
Since the demo Sparkfun code seems to work (as well as other codes from this forum, as long as they don't use the Stepper.h or AccelStepper.h:
- Is it a wiring issue that wouldn't show up with simpler codes (pins not used ...)?
- Do i need to tweak the stepper.h library?
- Is it something else i'm not seeing/aware of?
Thanks in advance, any help will be greatly appreciated.
Please let me know if i need to provide more or different informations in order to be helped.
================
More infos, specs ...
Attached is a Fritzing Breadboard wiring diagram.
Stepper motor (Nema17) : https://images-na.ssl-images-amazon.com/images/I/91nlDJYWTQL.pdf
Stepper Driver: EasyDriver: https://www.amazon.com/gp/product/B004G4XR60/ref=oh_aui_detailpage_o05_s00?ie=UTF8&psc=1
================
All seems to work as expected using different simple codes (from easydriver or from this forum), as long as they don't use another library like the Stepper.h or the AccelStepper.h
For example, this works well:
================
For quicker looks at code:
- Copy/paste from Arduino IDE, these are the codes i actually upload to the Arduino Uno.
- I did not change the comments so we can see the changes (Pin numbers and StepPerRevolution)
What (seems to) works:
SparkFun Easy Driver Basic Demo
Toni Klopfenstein @ SparkFun Electronics
March 2015
https://github.com/sparkfun/Easy_Driver
Simple demo sketch to demonstrate how 5 digital pins can drive a bipolar stepper motor,
using the Easy Driver (https://www.sparkfun.com/products/12779). Also shows the ability to change
microstep size, and direction of motor movement.
Development environment specifics:
Written in Arduino 1.6.0
This code is beerware; if you see me (or any other SparkFun employee) at the local, and you've found our code helpful, please buy us a round!
Distributed as-is; no warranty is given.
Example based off of demos by Brian 4
Schmalz (designer of the Easy Driver).
http://www.schmalzhaus.com/EasyDriver/Examples/EasyDriverExamples.html
******************************************************************************/
//Declare pin functions on Redboard
#define stp 2
#define dir 3
#define MS1 4
#define MS2 5
#define EN 6
//Declare variables for functions
char user_input;
int x;
int y;
int state;
void setup() {
pinMode(stp, OUTPUT);
pinMode(dir, OUTPUT);
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(EN, OUTPUT);
resetEDPins(); //Set step, direction, microstep and enable pins to default states
Serial.begin(9600); //Open Serial connection for debugging
Serial.println("Begin motor control");
Serial.println();
//Print function list for user selection
Serial.println("Enter number for control option:");
Serial.println("1. Turn at default microstep mode.");
Serial.println("2. Reverse direction at default microstep mode.");
Serial.println("3. Turn at 1/8th microstep mode.");
Serial.println("4. Step forward and reverse directions.");
Serial.println();
}
//Main loop
void loop() {
while(Serial.available()){
user_input = Serial.read(); //Read user input and trigger appropriate function
digitalWrite(EN, LOW); //Pull enable pin low to allow motor control
if (user_input =='1')
{
StepForwardDefault();
}
else if(user_input =='2')
{
ReverseStepDefault();
}
else if(user_input =='3')
{
SmallStepMode();
}
else if(user_input =='4')
{
ForwardBackwardStep();
}
else
{
Serial.println("Invalid option entered.");
}
resetEDPins();
}
}
//Reset Easy Driver pins to default states
void resetEDPins()
{
digitalWrite(stp, LOW);
digitalWrite(dir, LOW);
digitalWrite(MS1, LOW);
digitalWrite(MS2, LOW);
digitalWrite(EN, HIGH);
}
//Default microstep mode function
void StepForwardDefault()
{
Serial.println("Moving forward at default step mode.");
digitalWrite(dir, LOW); //Pull direction pin low to move "forward"
for(x= 1; x<200; x++) //Loop the forward stepping enough times for motion to be visible
{
digitalWrite(stp,HIGH); //Trigger one step forward
delay(2);
digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
delay(2);
}
Serial.println("Enter new option");
Serial.println();
}
//Reverse default microstep mode function
void ReverseStepDefault()
{
Serial.println("Moving in reverse at default step mode.");
digitalWrite(dir, HIGH); //Pull direction pin high to move in "reverse"
for(x= 1; x<200; x++) //Loop the stepping enough times for motion to be visible
{
digitalWrite(stp,HIGH); //Trigger one step
delay(2);
digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
delay(2);
}
Serial.println("Enter new option");
Serial.println();
}
// 1/8th microstep foward mode function
void SmallStepMode()
{
Serial.println("Stepping at 1/8th microstep mode.");
digitalWrite(dir, LOW); //Pull direction pin low to move "forward"
digitalWrite(MS1, HIGH); //Pull MS1, and MS2 high to set logic to 1/8th microstep resolution
digitalWrite(MS2, HIGH);
for(x= 1; x<1600; x++) //Loop the forward stepping enough times for motion to be visible
{
digitalWrite(stp,HIGH); //Trigger one step forward
delay(2);
digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
delay(2);
}
Serial.println("Enter new option");
Serial.println();
}
//Forward/reverse stepping function
void ForwardBackwardStep()
{
Serial.println("Alternate between stepping forward and reverse.");
for(x= 1; x<5; x++) //Loop the forward stepping enough times for motion to be visible
{
//Read direction pin state and change it
state=digitalRead(dir);
if(state == HIGH)
{
digitalWrite(dir, LOW);
}
else if(state ==LOW)
{
digitalWrite(dir,HIGH);
}
for(y=1; y<200; y++)
{
digitalWrite(stp,HIGH); //Trigger one step
delay(2);
digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
delay(2);
}
}
Serial.println("Enter new option:");
Serial.println();
}
And what doesn't seem to work:
/*
Stepper Motor Control - one revolution
This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 - 11 of the Arduino.
The motor should revolve one revolution in one direction, then
one revolution in the other direction.
Created 11 Mar. 2007
Modified 30 Nov. 2009
by Tom Igoe
*/
#include <Stepper.h>
const int stepsPerRevolution = 1600; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 2, 3, 4, 5);
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}
Thanks again.
Stefan