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

Arduino-Controlled Stepper Motor with Speed and Direction Control

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

Circuit Documentation

Summary

This circuit is designed to control a bipolar stepper motor using an Arduino Nano microcontroller. The system's operation is influenced by user inputs from a potentiometer, a toggle switch, and two pushbuttons. The potentiometer adjusts the motor speed, the toggle switch enables or disables the system, and the pushbuttons determine the motor's rotation direction. A stepper driver interfaces the Arduino with the stepper motor, receiving power from a 12V power supply through a step-down buck converter. The circuit also includes a micro USB to cable connector for powering the Arduino Nano.

Component List

STEPPER DRIVER

Stepper Motor (Bipolar)

Arduino Nano

12V Power Supply

Step Down Buck Converter

Micro USB to Cable (2 Pin)

Pushbutton (x2)

Potentiometer

Rocker Switch

Wiring Details

STEPPER DRIVER

Stepper Motor (Bipolar)

Arduino Nano

12V Power Supply

Step Down Buck Converter

Micro USB to Cable (2 Pin)

Pushbutton (x2)

Potentiometer

Rocker Switch

Documented Code

/*
 * This Arduino Sketch controls a stepper motor using a potentiometer, 
 * a toggle switch, and two push buttons. The potentiometer sets the speed, 
 * the toggle switch enables/disables the system, and the push buttons 
 * control the direction of the stepper motor.
 */

// Pin definitions
const int enaPin = 2;
const int dirPin = 3;
const int pulPin = 4;
const int moveLeftButtonPin = 5;
const int moveRightButtonPin = 6;
const int toggleSwitchPin = 7;
const int potPin = A0;

void setup() {
  // Initialize output pins
  pinMode(enaPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(pulPin, OUTPUT);

  // Initialize input pins with internal pull-up resistors
  pinMode(moveLeftButtonPin, INPUT_PULLUP);
  pinMode(moveRightButtonPin, INPUT_PULLUP);
  pinMode(toggleSwitchPin, INPUT_PULLUP);

  // Potentiometer pin remains as INPUT
  pinMode(potPin, INPUT);

  // Enable the stepper driver
  digitalWrite(enaPin, LOW);
}

// Debounce function for the toggle switch
bool debounceSwitch(int pin) {
  bool currentState;
  bool previousState = digitalRead(pin); // Initial state
  delay(50); // 50 ms debounce delay (adjust as needed)
  currentState = digitalRead(pin); // Read state again after delay
  return currentState == previousState; // Return true if stable
}

void loop() {
  // Debounced system enable/disable logic
  bool systemEnabled = debounceSwitch(toggleSwitchPin);
  
  if (systemEnabled) {
    // Read the potentiometer value and map it to a delay
    int potValue = analogRead(potPin);
    int delayTime = map(potValue, 0, 1023, 1000, 100); // Speed control

    // Check the move left button
    if (digitalRead(moveLeftButtonPin) == LOW) {
      digitalWrite(dirPin, LOW); // Set direction to left
      stepMotor(delayTime);
    }
    // Check the move right button
    else if (digitalRead(moveRightButtonPin) == LOW) {
      digitalWrite(dirPin, HIGH); // Set direction to right
      stepMotor(delayTime);
    }
  }
  
  delay(1); // Small delay to avoid rapid looping
}

void stepMotor(int delayTime) {
  digitalWrite(pulPin, HIGH);
  delayMicroseconds(delayTime);
  digitalWrite(pulPin, LOW);
  delayMicroseconds(delayTime);
}

This code is designed to run on the Arduino Nano and controls the stepper motor's operation based on the inputs from the potentiometer, toggle switch, and pushbuttons. It includes functions for debouncing the toggle switch and stepping the motor at a speed determined by the potentiometer's position. The motor's direction is controlled by the state of the pushbuttons.