Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Controlled Stepper Motor with Direction Toggle

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Stepper Motor with Direction Toggle

Circuit Documentation

Summary

This circuit is designed to control a 28BYJ-48 Stepper Motor using an Arduino UNO microcontroller and a ULN2003A driver. The motor's rotation direction can be changed with a toggle switch, and a pushbutton is used to initiate a control sequence. A 5V Power Supply Unit (PSU) provides power to the system.

Component List

Arduino UNO

Toggle Switch

28BYJ-48 Stepper Motor

ULN2003A

5V PSU

Pushbutton

Wiring Details

Arduino UNO

Toggle Switch

28BYJ-48 Stepper Motor

ULN2003A

5V PSU

Pushbutton

Documented Code

// Pin definitions
const int switchPin = 2; // Pin connected to the switch
const int motorPins[] = {8, 9, 10, 11}; // Pins connected to the ULN2003A inputs

// Variables
bool direction = true; // true for one direction, false for the other

void setup() {
  // Initialize motor pins as outputs
  for (int i = 0; i < 4; i++) {
    pinMode(motorPins[i], OUTPUT);
  }
  
  // Initialize switch pin as input
  pinMode(switchPin, INPUT_PULLUP); // Use internal pull-up resistor
}

void loop() {
  // Read the switch state
  int switchState = digitalRead(switchPin);
  
  // Determine the direction based on the switch state
  if (switchState == LOW) {
    direction = !direction; // Change direction if switch is closed
  }
  
  // Rotate the stepper motor
  if (direction) {
    stepMotorForward();
  } else {
    stepMotorBackward();
  }
  
  delay(100); // Adjust delay as needed
}

void stepMotorForward() {
  // Sequence for forward rotation
  digitalWrite(motorPins[0], HIGH);
  digitalWrite(motorPins[1], LOW);
  digitalWrite(motorPins[2], LOW);
  digitalWrite(motorPins[3], LOW);
  delay(10);
  
  digitalWrite(motorPins[0], LOW);
  digitalWrite(motorPins[1], HIGH);
  digitalWrite(motorPins[2], LOW);
  digitalWrite(motorPins[3], LOW);
  delay(10);
  
  digitalWrite(motorPins[0], LOW);
  digitalWrite(motorPins[1], LOW);
  digitalWrite(motorPins[2], HIGH);
  digitalWrite(motorPins[3], LOW);
  delay(10);
  
  digitalWrite(motorPins[0], LOW);
  digitalWrite(motorPins[1], LOW);
  digitalWrite(motorPins[2], LOW);
  digitalWrite(motorPins[3], HIGH);
  delay(10);
}

void stepMotorBackward() {
  // Sequence for backward rotation
  digitalWrite(motorPins[0], LOW);
  digitalWrite(motorPins[1], LOW);
  digitalWrite(motorPins[2], LOW);
  digitalWrite(motorPins[3], HIGH);
  delay(10);
  
  digitalWrite(motorPins[0], LOW);
  digitalWrite(motorPins[1], LOW);
  digitalWrite(motorPins[2], HIGH);
  digitalWrite(motorPins[3], LOW);
  delay(10);
  
  digitalWrite(motorPins[0], LOW);
  digitalWrite(motorPins[1], HIGH);
  digitalWrite(motorPins[2], LOW);
  digitalWrite(motorPins[3], LOW);
  delay(10);
  
  digitalWrite(motorPins[0], HIGH);
  digitalWrite(motorPins[1], LOW);
  digitalWrite(motorPins[2], LOW);
  digitalWrite(motorPins[3], LOW);
  delay(10);
}

This code is responsible for controlling the stepper motor's direction and steps. The switchPin is used to read the state of the toggle switch, which determines the direction of the motor's rotation. The motorPins array defines the connections to the ULN2003A driver inputs. The stepMotorForward and stepMotorBackward functions define the sequence of energizing the motor coils to rotate the motor in either direction. The loop function continuously checks the switch state and commands the motor to step accordingly.