Can I operate two stepping motors with one Arduino?

#include <Stepper.h>

int stepsPerRev = 2048;

Stepper stepper (stepsPerRev, 11,9,10,8); // ( IN4,IN2,IN3,IN1)

int btn1 = 7;
int btn2 = 6;

void setup() {
 stepper.setSpeed(15);
 
  pinMode(btn1, INPUT_PULLUP);
  pinMode(btn2, INPUT_PULLUP);
}

void loop() {
 
  if (digitalRead(btn1)==LOW){
    stepper.step(1);
    }
  if (digitalRead(btn2)==LOW){
    stepper.step(-1);
    }

}

Currently, I'm operating one stepping motor with this code.

How can I control the two stamping motors separately by adding two stamping motors and two new buttons?

I'm not good at Arduino :joy:
I would appreciate it if you could write the code and let me know. :sleepy:

Use two Stepper instances?

You probably should look at the StateChangeDetection example to see how to properly use a button so that each press gives one step, not millions of steps continuously until you release the button as your code currently tries to.

1 Like
#include <Stepper.h>

int stepsPerRev = 2048;

Stepper stepper1 (stepsPerRev, 11, 9, 10, 8); // ( IN4,IN2,IN3,IN1)
Stepper stepper2 (stepsPerRev, 7, 6, 5, 4); // ( IN4,IN2,IN3,IN1)

int btn1 = 7;
int btn2 = 6;

void setup() {
  stepper1.setSpeed(15);
  stepper2.setSpeed(15);

  pinMode(btn1, INPUT_PULLUP);
  pinMode(btn2, INPUT_PULLUP);
}

void loop() {

  if (digitalRead(btn1) == LOW) {
    stepper1.step(1);
  }
  if (digitalRead(btn2) == LOW) {
    stepper1.step(-1);
  }

}

The Arduino forum is not a free code writing service!

If you want to offer payment to someone to write the code for you, your topic can be moved to the appropriate forum section for you to make that offer.

If not, then you can get as much help as you need to write the code, for free, in this forum section, but you must show that you are willing to attempt the task and to learn how to write code. If they see you doing that, forum members will be very pleased to help you.

1 Like

Thank you. I'll try it! :grinning: :joy:

I'm sorry :pensive:
I've tried a lot before and I've been having a hard time because I failed.

I'll be careful next time.

oops, button pins collide with second stepper pins. change them accordingly to your setup.

1 Like

The forum can help you so that you do not have such a hard time, but the forum will not do everything for you to give you an easy time. You would not learn very much if we made everything too easy for you.

Always remind yourself that we are not in the room with you, we cannot see the circuit in front of you, or what you see on your screen, or what is in your head. You have to share those things with the forum, all the details, so that we can help you. Read the forum guide in the sticky post for guidance on what you need to share with the forum and how to do each thing.

1 Like

duplicate the pieces of code you have for a 2nd set of buttons and motor

1 Like

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