Skip to content

Instantly share code, notes, and snippets.

@errkk
Last active April 19, 2016 21:53
Show Gist options
  • Save errkk/5360d5129864bc83bf0bbcdc6a28c53d to your computer and use it in GitHub Desktop.
Save errkk/5360d5129864bc83bf0bbcdc6a28c53d to your computer and use it in GitHub Desktop.
Controls a 4 phase 28BYJ-48 stepper motor, via some digital pins an an LM2003 driver
// Control a 4 phase stepper motor with an Arduino
// Controls a 28BYJ-48 stepper motor, via some digital pins an an LM2003 driver
// Uses direct port manipulation for fast concurrent pin state changes
// https://en.wikipedia.org/wiki/Stepper_motor#/media/File:Drive.png
int motorSpeed = 1000;
int count = 0;
int countsperrev = 32*16;
// can do > 1100 uS
int lookup[8] = {B1000,
B1000,
B0100,
B0110,
B0010,
B0011,
B0001,
B1001};
// can do > 800 uS
// abcd
int wave[12] = {B1000,
B1000,
B1000,
B0100,
B0100,
B0100,
B0010,
B0010,
B0010,
B0001,
B0001,
B0001};
// abcd
int fullStep[12] = {B0110,
B0011,
B0011,
B1001,
B1001,
B1100,
B0110,
B0110,
B0111,
B0011,
B1001,
B1001};
// abcd
int halfStep[9] = { B0001,
B1001,
B1000,
B1100,
B0100,
B0110,
B0010,
B0011,
B0001};
void setup() {
// 76543210
DDRD = DDRD | B00111100; // Set pins 2-5 as outputs
}
void loop(){
if(count < countsperrev) {
clockwise();
count++;
} else {
delay(700);
count = 0;
}
}
void clockwise()
{
for(int i = 8; i >= 0; i--)
{
// Set the pin states for this step
PORTD = (halfStep[i] << 2);
delayMicroseconds(motorSpeed);
}
// Set all pins low
PORTD = (B0000 << 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment