SPDT Switch, Stepper Motor, Limit Switches and Arduino Uno

Hello,

I am trying to build an elevator for a dollhouse using a stepper motor and limit switches. At this point in my project, I’m thinking that I need a DPDT switch instead of the single pole that I currently have. I have the single pole switch turning the motor off, but ideally, I would like to have one of either two things happen:

  • Push a DPDT switch up and the elevator goes up to the upper limit switch. Push the DPDT switch down and the elevator goes down to the lower limit switch.
  • Use the SPDT switch and somehow store the motor polarity. The switch would just be used as an on/off button (?). You would turn the elevator on, it would go up to the upper limit switch where the motor would stop and the polarity would be switched. Turn the switch back on and the elevator would go down to the lower limit switch and the polarity would switch again.

My code that I have created so far is below.

#include <Stepper.h>
 
// change this to the number of steps on your motor
#define STEPS 200
 
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 4, 5, 6, 7);
 
 
void setup()
{
  Serial.begin(9600);
  Serial.println("Stepper test!");
  // set the speed of the motor to 30 RPMs
  stepper.setSpeed(60);
}
 
void loop()
{
  stepper.step(3200);
  delay(500);
}

If there are just two positions for the elevator then a simple push-button switch will work fine.

You need a variable to record whether the elevator is at the top, the bottom or movingUp or movingDown ('T' 'B' 'U' and 'D')

then the button control logic would be something like this

void (checkButton) {
  buttonPressed = digitalRead(buttonPin);
  if (buttonPressed == LOW) { // assumes LOW when pressed
    if (motorState == 'T') {
       motorState = 'D';
    }
    if (motorState == 'B') {
      motorState = 'U'
    }
  }
}

and the motor move logic would be something like

void motorMove() {
  if (motorState == 'U') {
    // code to move up one step
    topLimitState = digitalRead(topLimitPin);
    if (topLimitState == LOW) {
       motorState = 'T';
    }
  }
  // similarly for going down
}

...R

@Robin2, thank you for your response. I've read through your code and the direction that you are taking makes sense to me: "check if the elevator is at the top. If so, move down." and vice versa.

Just so I am clear, you're stating that I do not need limit switches or the double throw switches and that I can accomplish what I am trying to do via the stepper motor (# of steps/distance moved) and a momentary switch?

Sorry for the delayed response. I didn't have notifications turned on (they are now).

noahwass:
Just so I am clear, you're stating that I do not need limit switches

You do need at least one limit switch so the stepper motor can find the zero position and I would put a limit switch at the top and bottom.

If you have a limit switch at the top and bottom a simple DC motor with low gearing would work, and would probably be better. If it uses a worm drive the elevator won't move when the motor is unpowered, Stepper motors draw full power all of the time, even when stationary.

...R
Stepper Motor Basics
Simple Stepper Code

also look up the AccelStepper library