Using a switch to control two stepper motors independently

Hi forum!

I’m relatively new to Arduino projects, but I’ve muddled through most of the tutorials and I have a pretty good grasp of what’s going on at the simple level I’m working on.

My project involves two stepper motors running simultaneously. They run at different rates. I am using one of these motor shields, and I can get the motors to run just fine using a modified version of AFMotor_MultiStepper (from the AccelStepper library). The last piece of my project is being able to control each of these motors independently with two switches (one for each motor). I cannot figure out how to wire them. Is it possible to do this? I feel like I have to insert the switch between the Uno and the motor shield somehow, but I’m not sure how to do that.

Thanks in advance!

You need to post your code - otherwise I have no idea what to advise you.
And please use the code button </> so your code looks like this and is easy to copy to a text editor

…R

Hi,

When you say control each motor with a switch. Do you mean turn them ON and OFF with a switch each. OR control the SPEED of each motor with their own switches.

If its speed, you would use a potentiometer for each motor.

Tom... :)

Robin2:
You need to post your code - otherwise I have no idea what to advise you.

Thanks for your reply. My question isn’t so much about how to write the code; it’s more of a “how can I actually wire this setup” sort of question. Here’s the code that came with the tutorial I mentioned.

#include <AccelStepper.h>
#include <AFMotor.h>

// two stepper motors one on each port
AF_Stepper motor1(200, 1);
AF_Stepper motor2(200, 2);

// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
// wrappers for the first motor!
void forwardstep1() {  
  motor1.onestep(FORWARD, SINGLE);
}
void backwardstep1() {  
  motor1.onestep(BACKWARD, SINGLE);
}
// wrappers for the second motor!
void forwardstep2() {  
  motor2.onestep(FORWARD, SINGLE);
}
void backwardstep2() {  
  motor2.onestep(BACKWARD, SINGLE);
}

// Motor shield has two motor ports, now we'll wrap them in an AccelStepper object
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);

void setup()
{  
    stepper1.setMaxSpeed(20.0);
    stepper1.setAcceleration(100.0);
    stepper1.moveTo(400);
    
    stepper2.setMaxSpeed(10.0);
    stepper2.setAcceleration(100.0);
    stepper2.moveTo(400);
    
}

void loop()
{
    // Change direction at the limits
    if (stepper1.distanceToGo() == 0)
	stepper1.moveTo(-stepper1.currentPosition());
    stepper1.run();
    stepper2.run();
}

TomGeorge: Hi,

When you say control each motor with a switch. Do you mean turn them ON and OFF with a switch each. OR control the SPEED of each motor with their own switches.

If its speed, you would use a potentiometer for each motor.

Tom... :)

Thanks for your reply, Tom. All I want to do is turn each motor on or off with a switch each, but I can't figure out how to wire it using the motor shield, and I haven't been able to find a tutorial.

Edited to say that I don't know how to wire one individual motor to a simple switch. It's not the "two motors two switches" thing that's making it difficult. One motor one switch is difficult. Because there are four leads for each motor, I can't just put one switch in series with the leads. Do you know what I mean?

Hi, Check this tutorial, the board looks the same as yours.

https://learn.adafruit.com/downloads/pdf/adafruit-motor-shield.pdf

Tom... :)

TomGeorge:
Hi,
Check this tutorial, the board looks the same as yours.

https://learn.adafruit.com/downloads/pdf/adafruit-motor-shield.pdf

Tom… :slight_smile:

Thanks for the link, Tom. I have been using that tutorial to help set up my motors, and most of what I have done so far came from the guide. However, I don’t see anything in there about setting up a switch. Maybe you can answer this question: it it possible, given an arduino and the motor shield I linked, to attach one stepper motor and have that stepper motor controlled by an on/off switch that does NOT also simply control the power to the arduino itself? If so, and you know of a guide, that’s what I think I need to read. My gut says “yes” but I’m not sure how to do it, and I’m also not sure whether the switch should control the motor electrically (simple switch) or through a piece of code. Thanks again!

chicagopianotuner: it it possible, given an arduino and the motor shield I linked, to attach one stepper motor and have that stepper motor controlled by an on/off switch that does NOT also simply control the power to the arduino itself?

To make the motors work the Arduino needs to generate a series of step pulses for each motor.

It would be perfectly feasible to have the Arduino check the status of two switches and only produce the necessary pulses when the switch is ON.

The code in the second example in this Simple Stepper Code works like that.

...R

Hi Robin, thanks for your reply. The code you wrote in the link appears to be very close to what I need, but it doesn’t seem to use a motor shield. I need (or at least I think I need!) the motor shield to operate my motors as the current requirement might exceed the limit of the Uno. Is it possible to use pins of the Arduino to toggle a switch while the motor shield is in use? Or, maybe more generally, it is it possible to receive input values in any way while the motor shield is in use?

Thanks again! :slight_smile:

chicagopianotuner:
but it doesn’t seem to use a motor shield.

You are correct. It was written for specialized stepper drivers such as the Pololu A4988 - which make using a stepper much easier.

My idea is giving you the link was just to give the general idea. You should be able to write a singleStep() function that works with your motor shield and use that in place of the one in my code.

…R
Stepper Motor Basics

Thanks a lot for your help everyone. In the end, my issue ended up being quite simple: I didn’t realize that some of the pins were still accessible and unused despite attaching the motor shield. Once I figured that out, I used some examples and a bit of Robin’s code to help run my motors.

Like I said, I’m very new at this and I appreciate the time everyone took helping me out.

chicagopianotuner: I didn't realize that some of the pins were still accessible and unused despite attaching the motor shield.

I did not realize that was what was troubling you. It is a common problem (with written or spoken language) for two people to understand each other perfecly when actually they both have very different understandings of the same issue. Diplomats rely on this and find words that both parties can live with. But it does not work with engineering.

May I suggest that if you had said "I have a motor shield which covers up all the Arduion pins and I don't know how to connect two switches" ...

...R