You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于BasicStepperDriver库用Arduino Uno同时控制两台步进电机?

How to Drive Two Stepper Motors with Arduino Uno Using BasicStepperDriver

Hey there! Great job getting one stepper motor up and running—adding a second one is really simple once you know the trick. Since each stepper needs its own pair of DIR and STEP pins, we just need to create separate instances of the BasicStepperDriver class for each motor. Let's walk through this with a modified version of your original code.

Step 1: Define Pins for Both Motors

First, pick 4 unused digital pins on your Arduino Uno (avoid pins 0/1 since those are for serial communication). For example:

  • Motor 1: DIR1 on pin 4, STEP1 on pin 3
  • Motor 2: DIR2 on pin 5, STEP2 on pin 6

Step 2: Create Two Driver Instances

Instead of one stepper object, we'll make two: stepper1 for the first motor, stepper2 for the second. Each gets its own DIR and STEP pin values.

Step 3: Initialize Both Motors in setup()

Call begin() for each stepper to set their RPM and microstepping mode, just like you did for the single motor.

Step 4: Control Each Motor Independently in loop()

You can now command each motor to move separately—rotate, move steps, or even sync their actions.

Full Modified Code

/* 
 * Dual stepper motor demo for Arduino Uno
 * Uses BasicStepperDriver library
 * 
 * Connect each motor's DIR/STEP pins to the defined Arduino pins
 * Copyright (C)2015-2017 Laurentiu Badea 
 * This file may be redistributed under the terms of the MIT license. 
 */ 
#include <Arduino.h> 
#include "BasicStepperDriver.h" 

// Motor steps per revolution (most steppers are 200 steps/1.8° per step)
#define MOTOR_STEPS 200 
#define RPM 120 
#define MICROSTEPS 1 

// Pin definitions for Motor 1
#define DIR1 4 
#define STEP1 3 

// Pin definitions for Motor 2
#define DIR2 5 
#define STEP2 6 

// Create driver instances for each motor
BasicStepperDriver stepper1(MOTOR_STEPS, DIR1, STEP1);
BasicStepperDriver stepper2(MOTOR_STEPS, DIR2, STEP2);

void setup() { 
  // Initialize both motors with RPM and microstepping
  stepper1.begin(RPM, MICROSTEPS);
  stepper2.begin(RPM, MICROSTEPS);
} 

void loop() { 
  // Example 1: Move both motors forward 1 full revolution at the same time
  stepper1.rotate(360);
  stepper2.rotate(360);

  delay(1000); // Pause for 1 second

  // Example 2: Move Motor 1 backward 1 revolution, Motor 2 forward half revolution
  stepper1.move(-MOTOR_STEPS * MICROSTEPS);
  stepper2.rotate(180);

  delay(5000); // Longer pause before repeating
}

Key Notes for Beginners

  • Each BasicStepperDriver instance controls one motor, so you can call methods like rotate(), move(), enable(), or disable() on either stepper1 or stepper2 independently.
  • If you want to use the SLEEP/ENABLE pin for either motor, just add that pin to the constructor (e.g., BasicStepperDriver stepper1(MOTOR_STEPS, DIR1, STEP1, SLEEP1);) and uncomment the relevant setup lines, just like you did for the single motor.
  • Make sure your power supply can handle both steppers—steppers draw more current than regular LEDs, so a USB port alone might not be enough for two motors.

内容的提问来源于stack exchange,提问作者Stam Echad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:56:59