How to Drive a Stepper Motor with an Arduino

Stepper motors are widely used in applications that require precise control of rotation, such as 3D printers, CNC machines, and robotics. Unlike regular DC motors, stepper motors move in discrete steps, allowing for precise control of angular movement. In this blog post, we’ll guide you through the process of driving a stepper motor with an Arduino, covering the basics, the required components, and the code to get started.

What is a Stepper Motor?

A stepper motor is a type of brushless DC motor that divides a full rotation into equal steps. The motor can be rotated in both directions with precision, making it ideal for applications that require exact positioning.

Key Features of a Stepper Motor:

  • Precise Positioning: Moves in fixed steps (e.g., 1.8° per step for a 200-step motor).
  • High Torque at Low Speeds: Ideal for applications requiring holding torque.
  • Controlled Rotation: Direction and speed can be precisely controlled.

Components Required

To drive a stepper motor with an Arduino, you’ll need the following components:

  1. Arduino Board: Any model like Arduino Uno, Nano, or Mega.
  2. Stepper Motor: A common choice is the 28BYJ-48 stepper motor.
  3. Stepper Motor Driver: ULN2003 driver board (for 28BYJ-48) or A4988 driver (for other motors).
  4. Power Supply: Depending on the motor, usually 5V or 12V.
  5. Jumper Wires: To connect everything together.

Wiring the Stepper Motor to the Arduino

The wiring setup depends on the type of stepper motor and driver you’re using. Below is an example using the 28BYJ-48 stepper motor with the ULN2003 driver board.

Step 1: Connect the Stepper Motor to the Driver Board

  • The 28BYJ-48 motor comes with a 5-wire connector that plugs directly into the ULN2003 driver board.

Step 2: Connect the Driver Board to the Arduino

  • IN1 to Pin 8 on the Arduino.
  • IN2 to Pin 9 on the Arduino.
  • IN3 to Pin 10 on the Arduino.
  • IN4 to Pin 11 on the Arduino.
  • GND on the driver board to GND on the Arduino.
  • VCC on the driver board to 5V on the Arduino (or an external 5V power supply).

Step 3: Power the Motor

  • If your stepper motor requires more current than the Arduino can supply, use an external power supply connected to the driver board.

Writing the Code

Now that your hardware is set up, let’s write the code to control the stepper motor. We’ll use the Stepper library, which comes pre-installed with the Arduino IDE.

Step 1: Include the Stepper Library

#include <Stepper.h>

Step 2: Define Motor Steps and Pins

// Define the number of steps per revolution for your motor (e.g., 2048 steps)
#define STEPS_PER_REV 2048

// Create an instance of the Stepper class
Stepper stepper(STEPS_PER_REV, 8, 10, 9, 11);

Step 3: Set Up the Motor in the setup() Function

void setup() {
// Set the motor speed (RPM)
stepper.setSpeed(15);
}

Step 4: Control the Motor in the loop() Function

void loop() {
// Rotate the motor one revolution clockwise
stepper.step(STEPS_PER_REV);
delay(1000); // Wait 1 second

// Rotate the motor one revolution counterclockwise
stepper.step(-STEPS_PER_REV);
delay(1000); // Wait 1 second
}

Code Explanation:

  • Stepper stepper(STEPS_PER_REV, 8, 10, 9, 11);: Initializes the stepper motor object with the number of steps per revolution and the pin connections.
  • stepper.setSpeed(15);: Sets the speed of the motor in RPM.
  • stepper.step(STEPS_PER_REV);: Moves the motor one full revolution in the specified direction. Positive values rotate clockwise, and negative values rotate counterclockwise.

Uploading the Code

  1. Connect your Arduino to your computer via USB.
  2. Open the Arduino IDE and select the appropriate board and port.
  3. Copy the code above into the IDE.
  4. Click the “Upload” button to upload the code to the Arduino.

Testing the Setup

After uploading the code, your stepper motor should rotate one full revolution in one direction, pause for a second, and then rotate back in the opposite direction. If the motor doesn’t move, double-check your wiring and power connections.

Adjusting Speed and Steps

  • Speed: The speed can be adjusted by changing the value in stepper.setSpeed(). A higher value results in faster rotation.
  • Steps: You can control how far the motor turns by adjusting the argument in stepper.step(). For example, stepper.step(STEPS_PER_REV / 2); will rotate the motor by half a revolution.

Conclusion

Driving a stepper motor with an Arduino is a straightforward process that opens up a world of possibilities for precise motor control in your projects. Whether you’re building a robot, a CNC machine, or any other device requiring accurate rotation, stepper motors provide the reliability and control you need. By following this guide, you’ll have a solid foundation to start experimenting with stepper motors and creating more complex projects.

Happy tinkering!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top