I will show you how to turn such a complex Arduino multi-step motor circuit into a professional prototype in this video. You can easily turn your complex breadboard circuit into a great PCB. First, let's look at the printed circuit board part, and then we will see how to control the dual-step motor with three different Arduino codes.
Step 1: Printed Circuit Board and SchematicYou can build the circuit on a breadboard, but I prefer to design a printed circuit board for a more professional prototype. The design of a printed circuit board starts with creating and drawing a schematic. The components required in the circuit are added and the connection points are wired. Once the schematic is complete, it is converted to a PCB and the printed circuit board design process begins, followed by the generation of the design file. This design file is uploaded to the homepage of PCBWay, a manufacturer of high-quality printed circuit boards, and the PCB order is easily completed. My projects are open source and you can use them as you like.
The Gerber (Design) file of the PCB and more can be found here: https://www.pcbway.com/project/shareproject/Multiple_Stepper_Motor_Controller_Board_7d1d6a1e.html
Step 2: Soldering of the PCBNow let's take a closer look at the design. The board is based on two stepper motor drivers and a plug-in Arduino Nano. Analog and power pins, servo motor input, and digital pins in the circuit are included to make the board more useful. There is also a jumper pin that disconnects the Arduino Nano from the external power supply. In this section, I will place the required components on the board and start the soldering process. I chose easy-to-find components, I have attached the bill of materials below. If you prefer, you can obtain this printed circuit board in assembled form from PCBWay.
- Electrolytic Capacitors Kit (10uF 16V) - https://amzn.to/4e7wiDl
- Multilayer Ceramic Capacitor Kit (100nF) - https://amzn.to/456I0dB
- JST-XH Connector Kit (XH-5A) - https://amzn.to/3KuXAGw
- 2.54mm Male and Female Pin Header Kit - https://amzn.to/3yMxGva
- 5mm LED Light Emitting Diode - https://amzn.to/3VbMta2
- Carbon Film Resistors (220 ohm) - https://amzn.to/3Voxhb0
- Screw Terminal Block Connector (5mm 2 Pin) - https://amzn.to/3X7uwMr
- 28BYJ-48 ULN2003 5V Stepper Motor - https://amzn.to/3KuDNa2
- Soldering Iron Kit - https://amzn.to/4aPQRl3
Soldering is complete, let's attach the Arduino board and stepper motors to the circuit, then open the shared source code. In the first code, we’ll use the Arduino Stepper Library, which comes with the Arduino IDE. Here is a simple sketch that turns the motor slowly in one direction, then rapidly in the opposite direction.
The sketch starts by including the built-in stepper library.
//Includes the Arduino Stepper Library
#include <Stepper.h>
Next, a constant is defined, which contains the number of ‘steps’ the motor takes to complete one revolution. The motor has two different working modes, full-step mode and half-step mode. For now, we will use the full-step mode.
// Defines the number of steps per rotation
const int stepsPerRevolution = 4076;
Enter the step sequence of the stepper motor. This also defines the pins to which the stepper motor is connected. Make sure you enter the step sequence correctly; otherwise the motor will not run properly.
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
Stepper leftStepper = Stepper(stepsPerRevolution, 2, 4, 3, 5);
Stepper rightStepper = Stepper(stepsPerRevolution, 6, 8, 7, 9);
In the loop function, we use the Set Speed function to specify the speed at which the stepper motor should move and the Step function to specify how many steps to take.
Passing a negative number to the Step function causes the motor to spin in the opposite direction.
The first code snippet causes the motor to spin very slowly clockwise, while the second causes it to spin very quickly counter-clockwise.
void loop() {
// Rotate CW slowly at 5 RPM
leftStepper.setSpeed(5);
rightStepper.setSpeed(5);
leftStepper.step(stepsPerRevolution);
rightStepper.step(stepsPerRevolution);
delay(1000);
// Rotate CCW quickly at 10 RPM
leftStepper.setSpeed(5);
rightStepper.setSpeed(5);
leftStepper.step(-stepsPerRevolution);
rightStepper.step(-stepsPerRevolution);
delay(1000);
}
After uploading the code, we feed the board with external five volt power. Make sure you connect the positive and negative pole correctly, for now the Arduino is powered via USB and the board works and executes the code without any problem.
Step 4: Second Control With Accel Stepper LibraryFor our next code, we will use an advanced stepper motor library the Accel Stepper library. It outperforms the standard Arduino Stepper library. It supports acceleration and deceleration. It supports half-step driving and multiple steppers at once, with independent concurrent stepping on each stepper. This library is not included with the Arduino Editor, so you’ll need to install it first.
// Include the AccelStepper Library
#include <AccelStepper.h>
This is a simple sketch that accelerates the stepper motor in one direction and then decelerates to come to rest. After one revolution, the motor reverses its spinning direction and repeats the process. We will be driving a four-wire stepper motor in full-step mode, so we set the Motor Interface Type constant to four. If you want to run the motor in half-step mode, set the constant to eight.
// Define step constant
#define MotorInterfaceType 4
Next, we enter the step sequence of the stepper motor.
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
AccelStepper leftStepper(MotorInterfaceType, 2, 4, 3, 5);
AccelStepper rightStepper(MotorInterfaceType, 6, 8, 7, 9);
In the setup function, the maximum permitted speed of the motor is set to one thousand (the motor will accelerate up to this speed when we run it). The acceleration/deceleration rate is then set to add acceleration and deceleration to the stepper motor’s movements. The constant speed is set to two hundred.
void setup() {
// set the maximum speed, acceleration factor,
// initial speed and the target position
leftStepper.setMaxSpeed(1000.0);
rightStepper.setMaxSpeed(1000.0);
leftStepper.setAcceleration(50.0);
rightStepper.setAcceleration(50.0);
leftStepper.setSpeed(200);
rightStepper.setSpeed(200);
leftStepper.moveTo(2038);
rightStepper.moveTo(2038);
}
In the loop function, an if statement is used to determine how far the motor needs to travel before reaching the target position. When the Distance To Go reaches zero, the motor is rotated in the opposite direction by setting the Move To position to negative of its current position.
void loop() {
// Change direction once the motor reaches target position
if (leftStepper.distanceToGo() == 0)
leftStepper.moveTo(-leftStepper.currentPosition());
if (rightStepper.distanceToGo() == 0)
rightStepper.moveTo(-rightStepper.currentPosition());
// Move the motor one step
leftStepper.run();
rightStepper.run();
}
At the bottom of the loop, you’ll notice that the Run function is called. This is the most important function, as the stepper will not move if it is not executed.
leftStepper.run();
rightStepper.run();
The code containing the Accel Stepper library works fine, take some time and observe how the stepper motors move and then let's take a look at the next code.
Step 5: Third Control With Full-step and Half-step ModeLet's move on to the next code. Here’s a sketch that drives one motor in full-step mode and the other in half-step mode while accelerating and decelerating. After one revolution, their spinning direction changes. As one motor will be driven in full-step mode and the other in half-step mode the following two constants are defined.
// Define step constants
#define FULLSTEP 4
#define HALFSTEP 8
// Creates two instances
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
AccelStepper leftStepper(HALFSTEP, 2, 4, 3, 5);
AccelStepper rightStepper(FULLSTEP, 6, 8, 7, 9);
I explained the other definitions in the previous code, now let's upload it and see how it works.
Finally, let's plug in the power jumper to feed the Arduino from external power, so both the motors and the Arduino will run on external five volts. As you can see, our board is working smoothly. We have come to the end of another project, I hope this project was helpful for you.
Comments