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

Arduino UNO WiFi-Controlled Stepper Motor with Limit Switches and LED Indicators

Image of Arduino UNO WiFi-Controlled Stepper Motor with Limit Switches and LED Indicators

Circuit Documentation

Summary

This circuit is designed to control a bipolar stepper motor using an Arduino UNO R4 WiFi and an Arduino Motor Shield (Rev3). The circuit includes pushbuttons and limit switches for user input, as well as red, green, and blue LEDs to provide visual feedback. The stepper motor's movement can be controlled in terms of direction and steps, with the limit switches providing boundary conditions. The circuit is powered by a 12V power supply, and the LEDs are protected by 220 Ohm resistors.

Component List

Microcontroller

  • Arduino UNO R4 WiFi: A microcontroller board based on the ATmega328, with integrated WiFi capabilities.

Motor Driver

  • Arduino Motor Shield (Rev3): A shield for Arduino boards that can control servo motors, DC motors, and stepper motors.

Actuators

  • Stepper Motor (Bipolar): A bipolar stepper motor that can be precisely controlled for a set number of steps.

Power Supply

  • 12V Power Supply: Provides the necessary voltage to power the motor and the motor shield.

User Input

  • Pushbuttons: Three pushbuttons are used to control the LEDs and motor functions.
  • Limit Switches: Two limit switches are used to provide boundary conditions for the stepper motor's movement.

Indicators

  • LEDs: Three LEDs (red, green, blue) are used to provide visual feedback based on user input.

Protection

  • Resistors: Three 220 Ohm resistors are used to limit the current through the LEDs, protecting them from damage.

Wiring Details

Arduino UNO R4 WiFi

  • A0: Connected to a 220 Ohm resistor leading to the red LED.
  • A1: Connected to a 220 Ohm resistor leading to the green LED.
  • A2: Connected to a 220 Ohm resistor leading to the blue LED.
  • D2: Connected to a pushbutton.
  • D4: Connected to a limit switch (backward limit).
  • D5: Connected to a limit switch (forward limit).
  • D6: Connected to a pushbutton.
  • D10: Connected to a pushbutton.

Arduino Motor Shield (Rev3)

  • GND: Connected to the common ground of pushbuttons, limit switches, and LEDs.
  • 5V: Not connected in this circuit.
  • B-: Connected to one lead of the stepper motor.
  • B+: Connected to one lead of the stepper motor.
  • A-: Connected to one lead of the stepper motor.
  • A+: Connected to one lead of the stepper motor.
  • VMOT: Connected to the positive terminal of the 12V power supply.
  • GND: Connected to the negative terminal of the 12V power supply.

Stepper Motor (Bipolar)

  • D, C, B, A: Connected to the motor shield to control the stepper motor.

12V Power Supply

  • +: Connected to the VMOT pin of the motor shield.
  • -: Connected to the GND pin of the motor shield.

Pushbuttons

  • Pin 1: Connected to the digital pins on the Arduino for input.
  • Pin 2: Connected to the common ground.

Limit Switches

  • C: Connected to the common ground.
  • NO: Connected to the digital pins on the Arduino for input.

LEDs

  • Cathode: Connected to the common ground.
  • Anode: Connected to the digital pins on the Arduino through a 220 Ohm resistor.

Resistors

  • Pin 1: Connected to the Arduino analog pins.
  • Pin 2: Connected to the anode of the LEDs.

Documented Code

#include "Stepper.h"

// Define number of steps per revolution:
const int stepsPerRevolution = 200;

// Define button pins:
const int redButtonPin = 2;
const int greenButtonPin = 10;
const int blueButtonPin = 6;

// Define limit switch pins:
const int forwardLimitSwitchPin = 5;
const int backwardLimitSwitchPin = 4;

// Define LED pins:
const int redLEDPin = A0;
const int greenLEDPin = A1;
const int blueLEDPin = A2;

// Give the motor control pins names:
#define pwmA 3
#define pwmB 11
#define brakeA 9
#define brakeB 8
#define dirA 12
#define dirB 13

// Initialize the stepper library on the motor shield:
Stepper myStepper = Stepper(stepsPerRevolution, dirA, dirB);

// Variables to keep track of the motor state:
bool motorDirection = true; // true for forward (clockwise), false for backward (counter-clockwise)

// Variables to store previous states
int prevRedButtonState = LOW; // Inverted logic
int prevGreenButtonState = LOW; // Inverted logic
int prevBlueButtonState = LOW; // Inverted logic
int prevForwardLimitSwitchState = HIGH;
int prevBackwardLimitSwitchState = HIGH;

void setup() {
  Serial.begin(9600); // Initialize serial communication for debugging

  // Set the PWM and brake pins so that the direction pins can be used to control the motor:
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  pinMode(brakeA, OUTPUT);
  pinMode(brakeB, OUTPUT);

  digitalWrite(pwmA, HIGH);
  digitalWrite(pwmB, HIGH);
  digitalWrite(brakeA, LOW);
  digitalWrite(brakeB, LOW);

  // Set the motor speed (RPMs):
  myStepper.setSpeed(80);

  // Set button pins as inputs with internal pull-up resistors:
  pinMode(redButtonPin, INPUT_PULLUP);
  pinMode(greenButtonPin, INPUT_PULLUP);
  pinMode(blueButtonPin, INPUT_PULLUP);

  // Set limit switch pins as inputs with internal pull-up resistors:
  pinMode(forwardLimitSwitchPin, INPUT_PULLUP);
  pinMode(backwardLimitSwitchPin, INPUT_PULLUP);

  // Set LED pins as outputs:
  pinMode(redLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);

  // Initialize LEDs to be off:
  digitalWrite(redLEDPin, LOW);
  digitalWrite(greenLEDPin, LOW);
  digitalWrite(blueLEDPin, LOW);
}

void loop() {
  // Read the current state of the buttons and limit switches
  int currentRedButtonState = !digitalRead(redButtonPin); // Inverted logic
  int currentGreenButtonState = !digitalRead(greenButtonPin); // Inverted logic
  int currentBlueButtonState = !digitalRead(blueButtonPin); // Inverted logic
  int currentForwardLimitSwitchState = digitalRead(forwardLimitSwitchPin);
  int currentBackwardLimitSwitchState = digitalRead(backwardLimitSwitchPin);

  // Debugging: Print the state of the buttons
  Serial.print("Red Button State: ");
  Serial.println(currentRedButtonState);
  Serial.print("Green Button State: ");
  Serial.println(currentGreenButtonState);
  Serial.print("Blue Button State: ");
  Serial.println(currentBlueButtonState);

  // Control LEDs based on button states:
  digitalWrite(redLEDPin, currentRedButtonState == HIGH ? HIGH : LOW); // Inverted logic
  digitalWrite(greenLEDPin, currentGreenButtonState == HIGH ? HIGH : LOW); // Inverted logic
  digitalWrite(blueLEDPin, currentBlueButtonState == HIGH ? HIGH : LOW); // Inverted logic

  // Check the state of the red button to control direction:
  if (currentRedButtonState == HIGH && prevRedButtonState == LOW) { // Inverted logic
    motorDirection = !motorDirection; // Toggle motor direction
    Serial.println("Red button pressed, toggling motor direction");
    if (motorDirection) {
      Serial.println("Motor direction: Forward (Clockwise)");
    } else {
      Serial.println("Motor direction: Backward (Counter-Clockwise)");
    }
    delay(100); // Increase debounce delay to 100ms
  }

  // Check the state of the green button to enable/disable the motor:
  if (currentGreenButtonState == HIGH && prevGreenButtonState == LOW) { // Inverted logic
    // Single press detected, turn the motor one revolution
    if (motorDirection && currentForwardLimitSwitchState == HIGH) {
      myStepper.step(stepsPerRevolution); // Step one revolution forward (clockwise)
    } else if (!motorDirection && currentBackwardLimitSwitchState == HIGH) {
      myStepper.step(-stepsPerRevolution); // Step one revolution backward (counter-clockwise)
    }
    delay(100); // Increase debounce delay to 100ms
  }

  // Check if the green button is pressed while the blue button is being held
  if (currentGreenButtonState == HIGH && currentBlueButtonState == HIGH) { // Inverted logic
    // Continue turning the motor until the corresponding limit switch is triggered
    while (digitalRead(greenButtonPin) == LOW && digitalRead(blueButtonPin) == LOW) {
      if (motorDirection && digitalRead(forwardLimitSwitchPin) == HIGH) {
        myStepper.step(1); // Step one step forward (clockwise)
      } else if (!