Grove stepper and motor driver not going in reverse

Hello!

I am using the grove stepper and motor from Arduino Project Hub.

I cant get the motor to go in reverse. I am passing a negative int to myStepper.step() but it still rotates the same direction.

Any ideas?

#include <Stepper.h>
int t =0;

const int stepsPerRevolution = 200;

Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int stepCount = 0; 

void setup() {
  Serial.begin(9600);
}

void loop() {
  int lightValue = analogRead(A0);
  lightValue = map(lightValue, 0, 646, 0, 100);
  
  Serial.println("Light Value:");
  Serial.println(lightValue);
  
  int motorSpeed = 50;
  myStepper.setSpeed(motorSpeed);
    
  if (lightValue > 54) {
    Serial.println("lightvalue > 54");
    myStepper.step(stepsPerRevolution);
  } else if(lightValue < 46) {
    Serial.println("lightvalue < 46");
    myStepper.step(stepsPerRevolution*-1);
  } else {
    Serial.println("motor is still");
  }
  delay(250);
}

There is no pause between forward & reverse, maybe it IS reversing but you can't see it, put a 300 mS delay between F & R, stick a piece of tape on the shaft for a flag and try it.

#include <Stepper.h>
int t =0;

const int stepsPerRevolution = 200;

Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int stepCount = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  int lightValue = analogRead(A0);
  lightValue = map(lightValue, 0, 646, 0, 100);
 
  Serial.println("Light Value:");
  Serial.println(lightValue);
 
  int motorSpeed = 50;
  myStepper.setSpeed(motorSpeed);
   
  if (lightValue > 54) {
    Serial.println("lightvalue > 54");
    myStepper.step(stepsPerRevolution);
  delay(300);
  } else if(lightValue < 46) {
    Serial.println("lightvalue < 46");
    myStepper.step(stepsPerRevolution*-1);
    delay(300);
  } else {
    Serial.println("motor is still");
  }
  delay(250);
}

There is delay(250); at the end of the if else statement.

Hi,
Did you try the " Stepper Motor Control - one revolution" in the Examples- Stepper in the IDE?
This is it, you may need to change a statement to match your wiring pins.

/*
 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 = 200;  // 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, 8, 9, 10, 11);

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);
}

Tom... :slight_smile:

Have you connected the wires in the right sequence? only a third of the possible wiring posibilities will
produce proper rotation for a 5-wire stepper. Don't assume the order is left-to-right.

@MarkT I changed the pin sequence from 8, 9, 10, 11 to 8, 10, 9, 11 and it works correctly.