Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino-Controlled Stepper Motor with Rotary Encoder Feedback

Image of Arduino-Controlled Stepper Motor with Rotary Encoder Feedback

Circuit Documentation

Summary

This circuit is designed to control a bipolar stepper motor using an A4988 stepper motor driver, with an Arduino UNO as the microcontroller. The Arduino UNO reads inputs from a rotary encoder to determine the direction and steps for the stepper motor. A 9V battery provides power to the system.

Component List

A4988 Stepper Motor Driver (Red)

  • Description: A module to drive a bipolar stepper motor.
  • Pins: ENABLE, MS1, MS2, MS3, RESET, SLEEP, STEP, DIR, VMOT, GND, 2B, 2A, 1A, 1B, VDD

Arduino UNO

  • Description: A microcontroller board based on the ATmega328P.
  • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0

Rotary Encoder

  • Description: An input device that provides rotational position feedback.
  • Pins: GND, +, SW, DT, CLK

Battery 9V

  • Description: A standard 9V battery to provide power.
  • Pins: VCC, GND

Stepper Motor (Bipolar)

  • Description: A motor that converts digital pulses into mechanical shaft rotation.
  • Pins: D, B, C, A

Wiring Details

A4988 Stepper Motor Driver (Red)

  • STEP connected to Arduino UNO D3
  • DIR connected to Arduino UNO D2
  • VMOT connected to Battery 9V VCC
  • GND connected to Battery 9V GND and Arduino UNO GND
  • 2B connected to Stepper Motor A
  • 2A connected to Stepper Motor B
  • 1A connected to Stepper Motor C
  • 1B connected to Stepper Motor D
  • VDD connected to Arduino UNO 5V

Arduino UNO

  • D3 connected to A4988 Stepper Motor Driver STEP
  • D2 connected to A4988 Stepper Motor Driver DIR
  • 5V connected to A4988 Stepper Motor Driver VDD
  • GND connected to A4988 Stepper Motor Driver GND
  • D5 connected to Rotary Encoder CLK
  • D4 connected to Rotary Encoder DT

Rotary Encoder

  • GND connected to Arduino UNO GND
    • connected to Arduino UNO 5V
  • CLK connected to Arduino UNO D5
  • DT connected to Arduino UNO D4

Battery 9V

  • VCC connected to A4988 Stepper Motor Driver VMOT
  • GND connected to A4988 Stepper Motor Driver GND

Stepper Motor (Bipolar)

  • A connected to A4988 Stepper Motor Driver 2B
  • B connected to A4988 Stepper Motor Driver 2A
  • C connected to A4988 Stepper Motor Driver 1A
  • D connected to A4988 Stepper Motor Driver 1B

Documented Code

#define STEP_PIN 3
#define DIR_PIN 2
#define CLK_PIN 5
#define DT_PIN 4

volatile int encoderValue = 0;
volatile int lastEncoded = 0;

void setup() {
  pinMode(STEP_PIN, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
  pinMode(CLK_PIN, INPUT);
  pinMode(DT_PIN, INPUT);

  attachInterrupt(digitalPinToInterrupt(CLK_PIN), updateEncoder, CHANGE);
  attachInterrupt(digitalPinToInterrupt(DT_PIN), updateEncoder, CHANGE);
}

void loop() {
  // Example: Rotate stepper motor based on encoder value
  if (encoderValue > 0) {
    digitalWrite(DIR_PIN, HIGH); // Set direction
    for (int i = 0; i < encoderValue; i++) {
      digitalWrite(STEP_PIN, HIGH);
      delayMicroseconds(1000); // Adjust delay for speed control
      digitalWrite(STEP_PIN, LOW);
      delayMicroseconds(1000);
    }
    encoderValue = 0; // Reset encoder value after moving
  } else if (encoderValue < 0) {
    digitalWrite(DIR_PIN, LOW); // Set direction
    for (int i = 0; i < abs(encoderValue); i++) {
      digitalWrite(STEP_PIN, HIGH);
      delayMicroseconds(1000); // Adjust delay for speed control
      digitalWrite(STEP_PIN, LOW);
      delayMicroseconds(1000);
    }
    encoderValue = 0; // Reset encoder value after moving
  }
}

void updateEncoder() {
  int MSB = digitalRead(CLK_PIN); // MSB = most significant bit
  int LSB = digitalRead(DT_PIN);  // LSB = least significant bit

  int encoded = (MSB << 1) | LSB; // Converting the 2 pin value to single number
  int sum = (lastEncoded << 2) | encoded; // Adding it to the previous encoded value

  if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue++;
  if (sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue--;

  lastEncoded = encoded; // Store this value for next time
}

This code is responsible for controlling the stepper motor based on the input from the rotary encoder. The updateEncoder function is an interrupt service routine that updates the encoderValue based on the rotation of the encoder. The loop function then uses this value to control the direction and number of steps the stepper motor takes.