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

Arduino UNO Controlled Stepper Motor Elevator with Pushbutton Floor Selection

View and Edit Project In Cirkit DesignerImage of Arduino UNO Controlled Stepper Motor Elevator with Pushbutton Floor Selection

Circuit Documentation

Summary

This circuit is designed to control a Nema 17 stepper motor using an Arduino UNO and an A4988 stepper motor driver. The circuit also includes three pushbuttons to select different floor positions for the stepper motor. The motor's position is controlled by the Arduino, which reads the state of the pushbuttons and sends appropriate signals to the A4988 driver to move the motor to the desired position.

Component List

  1. Arduino UNO

    • Description: 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
  2. Nema 17 42-STH48

    • Description: Bipolar stepper motor.
    • Pins: A2 (black), A1 Green, B2 Red, B1 Blue
  3. A4988 Stepper Motor Driver (Red)

    • Description: Stepper motor driver carrier.
    • Pins: ENABLE, MS1, MS2, MS3, RESET, SLEEP, STEP, DIR, VMOT, GND, 2B, 2A, 1A, 1B, VDD
  4. Pushbutton (x3)

    • Description: Momentary pushbutton switch.
    • Pins: Pin 2, Pin 1, Pin 3, Pin 4
  5. 12v Battery

    • Description: Power source.
    • Pins: -, +
  6. Electrolytic Capacitor

    • Description: Capacitor with a capacitance of 1µF.
    • Pins: -, +
    • Properties: Capacitance: 1µF

Wiring Details

Arduino UNO

Nema 17 42-STH48

A4988 Stepper Motor Driver (Red)

Pushbutton 1

Pushbutton 2

Pushbutton 3

12v Battery

Electrolytic Capacitor

Code Documentation

#include <AccelStepper.h>

// Define pin connections
const int dirPin = 2;
const int stepPin = 3;
const int buttonFloor1 = 4; // Button for floor 1
const int buttonFloor2 = 5; // Button for floor 2
const int buttonFloor3 = 6; // Button for floor 3

// Define motor interface type
#define motorInterfaceType 1

// Creates an instance of AccelStepper
AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);

// Define step positions for each floor (adjust these values for your setup)
const long stepsPerFloor[] = {0, 1500, 2800}; // Example: Floor 1 = 0, Floor 2 = 2000, Floor 3 = 2800
long currentFloor = 1;  // Start at floor 1
bool isMoving = false;  // Flag to check if the motor is moving

void setup() {
    // Set motor parameters for optimized speed and acceleration
    myStepper.setMaxSpeed(500);
    myStepper.setAcceleration(200);

    // Initialize pins
    pinMode(buttonFloor1, INPUT_PULLUP);
    pinMode(buttonFloor2, INPUT_PULLUP);
    pinMode(buttonFloor3, INPUT_PULLUP);
}

void loop() {
    // Read button states
    bool floor1Pressed = digitalRead(buttonFloor1) == LOW;
    bool floor2Pressed = digitalRead(buttonFloor2) == LOW;
    bool floor3Pressed = digitalRead(buttonFloor3) == LOW;

    // Handle button presses for each floor
    if (floor1Pressed && currentFloor != 1 && !isMoving) {
        moveToFloor(1);
    } else if (floor2Pressed && currentFloor != 2 && !isMoving) {
        moveToFloor(2);
    } else if (floor3Pressed && currentFloor != 3 && !isMoving) {
        moveToFloor(3);
    }

    // Run the motor to reach the target position
    if (myStepper.distanceToGo() != 0) {
        myStepper.run();
    } else if (isMoving) {
        isMoving = false;  // Reset the moving flag
    }
}

// Function to move to a specific floor
void moveToFloor(int targetFloor) {
    currentFloor = targetFloor;
    myStepper.moveTo(stepsPerFloor[currentFloor - 1]); // Use the specific step count for the target floor
    isMoving = true;  // Set the moving flag
    delay(300);       // Debounce delay
}

This code is designed to control the stepper motor to move to different floor positions based on the pushbutton inputs. The AccelStepper library is used to manage the motor's movements, and the motor's speed and acceleration are set for optimal performance. The moveToFloor function is called when a button is pressed to move the motor to the corresponding floor position.