| A stepper motor (28BYJ-48) and controller board (based on chip ULN2003APG). |
|
This seems to be the most common stepper motor included as part of an Arduino kit. I spent quite a while tinkering with different libraries and techniques (some details at bottom of page), and it turned out that straightforward code was the best answer for this piece of kit.
|
Wired up and working. IN1 - Arduino Pin 8 IN2 - Arduino Pin 9 IN3 - Arduino Pin 10 IN4 - Arduino Pin 11 |
|
This straightforward code works perfectly for me; no additional libraries.
// This Arduino example demonstrates bidirectional operation of a
// 28BYJ-48, using a ULN2003 interface board to drive the stepper.
// The 28BYJ-48 motor is a 4-phase, 8-beat motor, geared down by
// a factor of 68. One bipolar winding is on motor pins 1 & 3 and
// the other on motor pins 2 & 4. The step angle is 5.625/64 and the
// operating Frequency is 100pps. Current draw is 92mA.
////////////////////////////////////////////////
//declare variables for the motor pins
int motorPin1 = 8; // IN 1
int motorPin2 = 9; // IN 2
int motorPin3 = 10; // In 3
int motorPin4 = 11; // IN 4
int motorSpeed = 1000; //variable to set stepper speed
// Experiment with this; too small will not work.
int count = 0; // count of steps made
int countsperrev = 512; // number of steps per revolution for this motor
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
void setup()
{
//declare the motor pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
Serial.begin(9600);
}
void loop()
{
int i;
for (i = 0; i < countsperrev; i++)
{
clockwise();
}
for (i = 0; i < countsperrev; i++)
{
anticlockwise();
}
}
/////
//set pins to ULN2003 high in sequence from 1 to 4
//delay "motorSpeed" between each pin setting (to determine speed)
/////
void anticlockwise()
{
for(int i = 0; i < 8; i++)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}
void clockwise()
{
for(int i = 7; i >= 0; i--)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}
void setOutput(int out)
{
digitalWrite(motorPin1, bitRead(lookup[out], 0));
digitalWrite(motorPin2, bitRead(lookup[out], 1));
digitalWrite(motorPin3, bitRead(lookup[out], 2));
digitalWrite(motorPin4, bitRead(lookup[out], 3));
}
|
I found, and experimented with, a few other libraries that you may want to investigate;
AccelStepper is a curious one, and
AFmotor from adafruit is a good one. Both need in-depth
examination to see if they're suitable for your own needs.
This webpage by 4tronix contains an excellent wiring diagram,
and this webpage I
also found useful information on.
I have always found major problems finding a suitable gear for a motor. There are so many varieties and sizes of gear shaft. Here, I have bought a small pack of plastic gears, used an electric drill bit to slightly enlarge the hole, and shoved it on. This one is working well.
| Plastic gear with resized hole, shoved on to the motor shaft. Try a google search for "5 Plastic Tooth Cogs Gears For 4mm & 2mm Motor Shaft" |
|