-
Notifications
You must be signed in to change notification settings - Fork 0
Home
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.
- 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
- Download or clone this repository.
- Place the
Operatefolder into yourArduino/librariesdirectory. - Restart the Arduino IDE.
- Alternatively, use Sketch > Include Library > Add .ZIP Library... in the Arduino IDE.
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);
}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.
-
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 bystepssteps.
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.
This library is released under the MIT License.
Happy Coding!
Operate Library for Arduino by Malhar Ashtaputre