Translate

Stepper Motor Control Step Wise with Arduino

 

1. Introduction

Precision is the heart of robotics. While standard DC motors spin continuously, Stepper Motors move in fixed, incremental "steps." This allows for incredible accuracy without needing a feedback sensor, making them the go-to choice for 3D printers, CNC machines, and camera sliders. In this tutorial, we’ll learn how to wire and code a stepper motor to move one step at a time.

2. Components Needed

To build this project, gather the following parts:

  • Arduino Uno (or any compatible board)

  • Stepper Motor (e.g., NEMA 17 for power or 28BYJ-48 for small projects)

  • Motor Driver (L298N for NEMA 17 or ULN2003 for 28BYJ-48)

  • External Power Supply (9V or 12V battery/adapter)

  • Jumper Wires and a Breadboard

3. Circuit and Connection

Stepper Motor Step Control Diagram



We will use 4 digital pins to control the motor phases. no need for potentiometer related connections (A0)

Arduino PinDriver InputFunction
Digital 8IN1Coil 1 Start
Digital 9IN2Coil 1 End
Digital 10IN3Coil 2 Start
Digital 11IN4Coil 2 End
GNDGNDCommon Ground
VinVCCExternal Power (+)

4. Circuit Explanation

  • The Driver Board: Arduino pins can't handle the high current a motor needs. The driver acts as a "power switch" that the Arduino triggers.

  • Common Ground: You must connect the ground (-) of your external battery to the Arduino's GND pin so they speak the same "electrical language."


5. The Code

Here is the complete code. You can copy and paste this directly into your Arduino IDE.

C++
/*
 * Arduino Stepper Motor - One Step at a Time
 * This code moves the motor slowly and logs the step count to the Serial Monitor.
 */

#include <Stepper.h>

// Step Resolution: 200 for NEMA 17, 2048 for 28BYJ-48
const int stepsPerRevolution = 200; 

// Initialize library on pins 8, 9, 10, 11
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0;         

void setup() {
  // Set the rotation speed (RPM)
  myStepper.setSpeed(60);
  
  // Initialize communication with computer
  Serial.begin(9600);
  Serial.println("Stepper Motor Test Initialized...");
}

void loop() {
  // Move exactly one step forward
  myStepper.step(1);
  
  // Print status to Serial Monitor
  Serial.print("Steps taken: ");
  Serial.println(stepCount);
  
  stepCount++;
  
  // Wait 500ms (0.5 seconds) before the next step
  delay(500);
}

6. Working of the Code

  1. #include <Stepper.h>: This brings in the standard library that handles the pulse sequencing for the motor coils.

  2. Stepper myStepper: This line creates a "motor object" and maps it to your chosen pins.

  3. myStepper.step(1): This command tells the motor to move forward by exactly one step. Changing the number to -1 would make it go backward.

  4. Serial.println: This sends the current step count back to your computer so you can see it in the Serial Monitor (Ctrl+Shift+M).


7. Pro Tips

  • Vibrating but not turning? Swap the middle two pins in your code: Stepper myStepper(200, 8, 10, 9, 11);.

  • Power Source: Don't use the Arduino’s 5V pin for the motor. It will likely cause the Arduino to crash or reset. Use a separate battery.

  • Heat: It’s normal for stepper motors to feel warm. They use current to "hold" their position even when not spinning.

8. Conclusion

Interfacing a stepper motor is the first step toward building complex machines like 3D printers or robotic arms. Now that you have the motor moving, try changing the delay(500) to something smaller to see how fast your motor can go!

തുടക്കക്കാർക്കായി ഇലക്ട്രോണിക്സ് ലളിതമായി പഠിക്കാം.

Empowering students in Kerala with hands-on technical skills.