Skip to content
Vihaan Parlikar edited this page Jul 13, 2025 · 1 revision

Operate-Lib

Operate-Lib is a lightweight Arduino library for easy and flexible control of stepper motors. It provides a simple class interface for common stepper operations, including setting speed, direction, homing with a limit switch, and position limits. This library is suitable for DIY CNC, robotics, pick-and-place, and other projects requiring robust stepper motor control.


Features

  • Simple API for stepper motor movement
  • Homing functionality using a limit switch
  • Position limiting to prevent out-of-range movement
  • Speed adjustment with microsecond delay
  • No hardware timer usage—works on any Arduino digital pins

Installation

  1. Download or clone this repository.
  2. Place the Operate folder into your Arduino/libraries directory.
  3. Restart the Arduino IDE.
  4. Alternatively, use Sketch > Include Library > Add .ZIP Library... in the Arduino IDE.

Usage Examples

Basic Example

Move a stepper motor forward for 2 seconds, then backward for 2 seconds:

#include <Arduino.h>
#include <operate.h>

#define STEP_PIN 2
#define DIR_PIN 5

operate motor;

void setup() {
  pinMode(STEP_PIN, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
  motor.setPin(STEP_PIN, DIR_PIN);
  motor.setSpeed(300); // microseconds between steps
}

void loop() {
  // Move forward for 2 seconds
  unsigned long startTime = millis();
  while (millis() - startTime < 2000) {
    motor.move(1, 1);
  }
  delay(500);
  // Move backward for 2 seconds
  startTime = millis();
  while (millis() - startTime < 2000) {
    motor.move(1, 0);
  }
  delay(2000);
}

Advanced Example: Speed Control

Gradually increase and decrease the speed of the stepper motor:

#include <Arduino.h>
#include <operate.h>

#define STEP_PIN 2
#define DIR_PIN 5

operate motor;

void setup() {
  Serial.begin(9600);
  pinMode(STEP_PIN, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
  motor.setPin(STEP_PIN, DIR_PIN);
}

void loop() {
  // Increase speed (decrease delay)
  for (int delayMicros = 800; delayMicros >= 100; delayMicros -= 100) {
    motor.setSpeed(delayMicros);
    motor.move(50, 1);
    delay(200);
  }
  delay(1000);
  // Decrease speed (increase delay)
  for (int delayMicros = 100; delayMicros <= 800; delayMicros += 100) {
    motor.setSpeed(delayMicros);
    motor.move(50, 0);
    delay(200);
  }
  delay(2000);
}

You can find more usage examples in the examples/ directory.


API Reference

Class: operate

Methods

  • void setPin(int stepPin, int dirPin);
    Set the step and direction pins.

  • void setSpeed(int microseconds);
    Set the delay between steps (lower value is faster).

  • void move(int steps, int dir);
    Move the motor by steps steps.
    dir: 1 = forward, 0 = backward.

  • int home();
    Home the motor using the configured limit switch.

  • int getCurrentPos();
    Get the current step position.

  • int setHoming(int enabled, int speed, int timeout, int switchPin);
    Configure homing:

    • enabled: Enable (1) or disable (0) homing.
    • speed: Step delay for homing (microseconds).
    • timeout: Timeout value for the homing operation.
    • switchPin: Digital pin number for the limit switch.
  • int setPosition(long pos);
    Move to a specific position, respecting limits.

  • int setPositionLimits(long max, long min);
    Set upper and lower bounds for movement.


License

This library is released under the MIT License.


Happy Coding!