Skip to content

Instantly share code, notes, and snippets.

@mechapro
Last active August 29, 2015 14:00
Show Gist options
  • Save mechapro/a91410797cd9295e8eb9 to your computer and use it in GitHub Desktop.
Save mechapro/a91410797cd9295e8eb9 to your computer and use it in GitHub Desktop.
Example of driving a stepper motor using an Adruino, the AccelStepper library for ramping and the tinystep II stepper motor controller available at mechapro.de
// Example of driving a stepper motor using
// an Adruino and the AccelStepper library
// via the tinystep II stepper motor controller
// available from mechapro.de
#include <AccelStepper.h>
#include <stdint.h>
// pin assignment
const uint8_t stepPin = 2;
const uint8_t errorPin = 3;
const uint8_t dirPin = 4;
const uint8_t disablePin = 5;
const uint8_t sleepPin = 6;
// actuate stepper motor
void forwardstep() {
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, LOW);
delayMicroseconds(1);
digitalWrite(stepPin, HIGH);
}
void backwardstep() {
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, LOW);
delayMicroseconds(1);
digitalWrite(stepPin, HIGH);
}
// setting up the AccelStepper library with custom actuator functions
AccelStepper stepper(forwardstep, backwardstep); // use functions to step
void setup()
{
// configuration of gpio interface
pinMode(stepPin, OUTPUT);
pinMode(errorPin, INPUT_PULLUP);
pinMode(dirPin, OUTPUT);
pinMode(disablePin, OUTPUT);
pinMode(sleepPin, OUTPUT);
digitalWrite(disablePin, HIGH);
// ramp parameters
stepper.setMaxSpeed(12000);
stepper.setAcceleration(2000);
}
int dir = 1;
void loop()
{
if (stepper.distanceToGo() == 0)
{
// Random change to speed, position and acceleration
// Make sure we dont get 0 speed or accelerations
delay(100);
stepper.move(dir * 50000);
dir = dir * -1;
}
stepper.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment