Jump to content

Experiment 7: Stepper Motor Control via Microcontroller

From cognifront
Revision as of 08:46, 18 August 2025 by Samrat277 (talk | contribs)

Experiment 7: Stepper Motor Control via Microcontroller

Objective

To control the rotation and direction of a stepper motor using Arduino and a ULN2003 driver.

Task

  1. Connect stepper motor and driver to Arduino.
  2. Write code to rotate motor clockwise and counter-clockwise.
  3. Vary delay to change motor speed.

Circuit Diagram

Circuit Diagram

Board Connections

Board Connections

Connections

Component Arduino Pin Description
Stepper Motor IN1 D8 Control input 1
Stepper Motor IN2 D10 Control input 2
Stepper Motor IN3 D9 Control input 3
Stepper Motor IN4 D11 Control input 4
ULN2003 VCC 5V Power supply for driver
ULN2003 GND GND Common ground

Code

// © 2025, Cognifront. Member of The IoT Kits Family.

//Experiment 7: Stepper Motor Control via Microcontroller

// Include the Stepper motor library
#include <Stepper.h>

// Define the number of steps per revolution for the stepper motor
// For a 28BYJ-48 stepper, it's typically 2048 steps per revolution (with gear reduction)
const int stepsPerRevolution = 2048;  

// Create a Stepper object named myStepper
// Connected to digital pins 8, 10, 9, 11 (sequence matters!)
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

void setup() {
  // Start serial communication for debugging and monitoring
  Serial.begin(9600);
  Serial.println("Stepper Motor Speed Test"); // Initial message
}

void loop() {
  // Rotate clockwise (CW) at increasing speeds
  for (int spd = 5; spd <= 20; spd += 5) {
    Serial.print("Rotating CW at ");
    Serial.print(spd);
    Serial.println(" RPM");

    myStepper.setSpeed(spd);                        // Set stepper speed in RPM
    myStepper.step(stepsPerRevolution / 2);         // Rotate 1/2 revolution in CW direction
    delay(500);                                     // Short pause between speed changes
  }

  // Rotate counterclockwise (CCW) at decreasing speeds
  for (int spd = 20; spd >= 5; spd -= 5) {
    Serial.print("Rotating CCW at ");
    Serial.print(spd);
    Serial.println(" RPM");

    myStepper.setSpeed(spd);                        // Set stepper speed in RPM
    myStepper.step(-stepsPerRevolution / 2);        // Rotate 1/2 revolution in CCW direction
    delay(500);                                     // Short pause between speed changes
  }
}

Observation

  • The stepper motor rotates in both clockwise and counterclockwise directions.
  • The motor speed changes as per the programmed RPM values.
  • Smooth operation is observed with correct wiring sequence.

Conclusion

The experiment successfully demonstrates control of a stepper motor’s speed and direction using Arduino and a ULN2003 driver. This forms the basis for applications like robotic motion control, CNC systems, and precise positioning mechanisms.