Changing stepper motor move direction to opposite direction

Hello guys, I'm trying to use stepper motor 28BJY with ULN2003 motor driver. I want the stepper motor to turn 90 degrees to a direction, wait, then goes back to 0 degrees (but turning to opposite direction). I also used temperature sensor (TMP36) and photoresistor as an input for when the stepper motor will move.

The problem is that after turning the first time for 90 degrees. I can't seems to go back to 0 degrees (with opposite direction). If I use stepper.runToNewPosition(), it does go to 0 degrees, but in the same direction. I also tried moveTo() and move() but still doesn't work.

#include <AccelStepper.h>
#include <LiquidCrystal_I2C.h>
#include <Chrono.h>

#define STEP_PER_REVOLUTION 2048

AccelStepper stepper(AccelStepper::FULL4WIRE, 6, 9, 10, 11);

LiquidCrystal_I2C lcd(0x27, 16, 2);

Chrono timer;
Chrono duration_timer;

const int photoresistor = A0;
const int tmp_sensor = A1;
const int red_led = 2;
const int green_led = 4;
const int buzzer = 3;
unsigned long duration = 5000;

int tmp = 0;
int light_val = 0;

int light_threshold = 200;
int target_revolution = 512;  // Turn the step motor 90 degree
bool start_water = false;
bool is_water_finished = false;
int state = 0;

void setup() 
{
  Serial.begin(19200);  // Open port communication between arduino and pc

  // Assign I/O pin for components
  pinMode(red_led, OUTPUT);
  pinMode(green_led, OUTPUT);
  pinMode(buzzer, OUTPUT);

  // Initizialize stepper motor
  stepper.setMaxSpeed(1000.0);
  stepper.setAcceleration(50.0);
  stepper.setSpeed(200);
  stepper.setCurrentPosition(0);
  stepper.moveTo(STEP_PER_REVOLUTION);
  stepper.enableOutputs();
  // Initizialize LCD I2C
  lcd.init();
  lcd.clear();
  lcd.backlight();

  lcd.setCursor(0, 0);
  lcd.print("Initializing...");
  delay(2000);
  lcd.clear();
}

void loop() 
{
  light_val = analogRead(photoresistor);
  tmp = analogRead(tmp_sensor);
  // Serial.print(light_val);
  // Serial.print("  ");
  // Serial.print(tmp);
  // Serial.println(state);
  // Serial.println(start_water);
  // Serial.println(is_water_finished);
  if (light_val < light_threshold)
  {
    // If the value is below the threshold, start or reset the timer.
    if (!duration_timer.hasPassed(1000))
    {
      duration_timer.restart();
      Serial.print(duration_timer.isRunning());
    }
  }
  else
  {
    // If the value is above the threshold, stop the timer.
    duration_timer.stop();
  }

  if (duration_timer.hasPassed(duration) && !start_water) {
    // Set the flag to indicate that the check is completed.
    start_water = true;
  }

  if(light_val < light_threshold && start_water && !is_water_finished)
  {
    switch (state) {
      case 0: // Move to 90 degrees
        if (stepper.currentPosition() < STEP_PER_REVOLUTION / 4) {
          Serial.println("TURNING TURNING");
          stepper.runToNewPosition(STEP_PER_REVOLUTION / 4);
          Serial.println(stepper.currentPosition());
        }
        else
        {
          state = 1; // Switch to the pause state
          Serial.println("DONE TURNING");
          Serial.println(stepper.currentPosition());
          timer.restart(); // Start the pause timer
        }
      break;

      case 1: // Pause for 20 seconds
        if (timer.hasPassed(duration)) {
          state = 2; // Switch to the move back state
          Serial.println(stepper.currentPosition());
          Serial.println("WAITING WAITING WAITING");
        }
      break;

      case 2: // Move back to 0 degrees
        stepper.runToNewPosition(0);  // I'm confused here
        start_water = false;
        is_water_finished = true;
        Serial.println(stepper.currentPosition());
        Serial.println("RETURNING RETURNING");
      break;
    }
  }
}

Try a move to -90 degrees.

Hello, thanks for the reply! I tried using stepper.moveTo(-512) after i stepper.setCurrentPosition(0). It does move but not in opposite direction.

case 2: // Move back to 0 degrees
       stepper.moveTo(-512);  // I'm confused here
       stepper.setSpeed(100);
       stepper.run();
       lcd.setCursor(0, 0);
       lcd.print(stepper.isRunning());
       start_water = false;
       is_water_finished = false;
       // Serial.println(stepper.currentPosition());
       // Serial.println("RETURNING RETURNING");

@hii_kari_ri
Is you motor wired this way?

IN1 --> pin6
IN2 --> pin10
IN3 --> pin9
IN4 --> pin11

I find the MobaTools stepper library easier to use. That library has a function to turn the stepper to a specified angle (write()). Of course the steps per revolution must be accurately specified for it to work.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.