Created Sun, 14 Jun 2015 23:19:15 +0000 by djgardn2
Sun, 14 Jun 2015 23:19:15 +0000
Hello,
This is a simple starter tutorial with code attached below, for the 28BYJ-48 stepper motors as seen in the pictures below which may come with different looking PCB’s, but function the same. The stepper motor is what I used in my previous project found here [url]http://chipkit.net/forum/viewtopic.php?f=15&t=3269[/url]
Materials used:
One Microcontroller (PIC32MX250F128B)
One 28BYJ-48 stepper motor with ULN2003 PCB
Four jumper wires (male to female)
Two jumper wires (female to female)
Power source (10,000 mAh power bank battery pack 5volts)
The power bank is optional and can be switched out with any 5 volt power source and the jumper wire connection style may vary depending on the connectors available on your microcontroller board to make the necessary connections. You will want to be sure that the power source can provide well over 200mA or 0.2amps since it will be required for the stepper motor and your microcontroller current requirements.
For the code examples attached I used the digital pins 13,12,11,10. You can use any digital pin, just make sure you change it accordingly in the code example attached if you do. This was put together using MPIDE for PIC32 microcontrollers, but the code will work with Arduino or similar microcontrollers as well.
Connections between the microcontroller pins to the stepper motor PCB are as follows:
Stepper motor PCB <----to----> PIC32 Microcontroller Pin
IN1 <--------------wire------------> 13
IN2 <--------------wire------------> 12
IN3 <--------------wire------------> 11
IN4 <--------------wire------------> 10
The plus sign on the stepper PCB will be connected to 5 volts (red wire in front on photos) and the minus sign will be connected to ground (black wire in front on photos). Also, depending on the stepper motor PCB the plus sign and minus sign might be located behind or in front of the pin connectors.
On the stepper motor PCB make sure that there is a jumper on the two pins closest to the resistors as seen in the photo near the 5 volt input connection. Sometimes depending who you purchase the stepper motor from, the jumper might be missing, the jumper is needed in order for it to function (the black jumper/connector seen in photo).
[attachment=0]StepperMotorsImage.jpg[/attachment]
Video of the stepper motor in action found here: [url]https://www.youtube.com/watch?v=EOM6aipshaI[/url]
Three different code examples attached below.
Stepper_Motor_Tutorial_Simple
/* You can change the stepperPins */
/* to any digital pins that you would like */
/* wire connection order {in1,in2,in3,in4} */
uint8_t stepperPins [4] = {13,12,11,10};
/*These are to state movement direction*/
uint8_t STOP = 0;
uint8_t FORWARD = 1;
uint8_t BACKWARD = 2;
/*This is used to keep track of the current step state*/
uint8_t stepper_Tracker = 0;
void setup() {
pinMode(stepperPins[0], OUTPUT);
pinMode(stepperPins[1], OUTPUT);
pinMode(stepperPins[2], OUTPUT);
pinMode(stepperPins[3], OUTPUT);
set_Motor_Pins_Low();
delay(1000);
}
void loop() {
one_Step(FORWARD);
delay(5);/*make larger to slow down spin speed, should not be 0 */
}
/*Ignore below, unless you want to know how it works*/
void one_Step(uint8_t stepper_direction)
{
if(stepper_direction == FORWARD)
{
if(stepper_Tracker == 3)
{
stepper_Tracker = 0;
}
else
{
stepper_Tracker = stepper_Tracker+1;
}
set_Motor_Pin(stepper_direction);
}
else if(stepper_direction == BACKWARD)
{
if(stepper_Tracker == 0)
{
stepper_Tracker =3;
}
else
{
stepper_Tracker = stepper_Tracker-1;
}
set_Motor_Pin(stepper_direction);
}
}
void set_Motor_Pin(uint8_t moving_direction)
{
if (moving_direction == FORWARD)
{
if(stepper_Tracker == 0)
{
digitalWrite(stepperPins[3], LOW);
digitalWrite(stepperPins[0], HIGH);
}
else
{
digitalWrite(stepperPins[stepper_Tracker-1], LOW);
digitalWrite(stepperPins[stepper_Tracker], HIGH);
}
}
else if(moving_direction == BACKWARD)
{
if(stepper_Tracker ==3)
{
digitalWrite(stepperPins[0], LOW);
digitalWrite(stepperPins[3], HIGH);
}
else
{
digitalWrite(stepperPins[stepper_Tracker+1], LOW);
digitalWrite(stepperPins[stepper_Tracker], HIGH);
}
}
else if(moving_direction == STOP)
{
set_Motor_Pins_Low();
}
}
void set_Motor_Pins_Low()
{
digitalWrite(stepperPins[0], LOW);
digitalWrite(stepperPins[1], LOW);
digitalWrite(stepperPins[2], LOW);
digitalWrite(stepperPins[3], LOW);
}
Stepper_Motor_Tutorial_Forward_then_Backward
/* You can change the stepperPins */
/* to any digital pins that you would like */
/* wire connection order {in1,in2,in3,in4} */
uint8_t stepperPins [4] = {13,12,11,10};
/*These are to state movement direction*/
uint8_t STOP = 0;
uint8_t FORWARD = 1;
uint8_t BACKWARD = 2;
/*This is used to keep track of the current step state*/
uint8_t stepper_Tracker = 0;
void setup() {
pinMode(stepperPins[0], OUTPUT);
pinMode(stepperPins[1], OUTPUT);
pinMode(stepperPins[2], OUTPUT);
pinMode(stepperPins[3], OUTPUT);
set_Motor_Pins_Low();
delay(1000);
}
void loop() {
unsigned long tempStartTime = millis();
unsigned long currentStepperTime = millis();
while ((currentStepperTime-tempStartTime) < 10000)/*loop 10 seconds forward*/
{
one_Step(FORWARD);
delay(5);/*make larger to slow down spin speed, should not be 0 */
currentStepperTime = millis();
}
tempStartTime = millis();
currentStepperTime = millis();
while ((currentStepperTime-tempStartTime) < 10000)/*loop 10 seconds backward*/
{
one_Step(BACKWARD);
delay(5);/*make larger to slow down spin speed, should not be 0 */
currentStepperTime = millis();
}
}
/*Ignore below, unless you want to know how it works*/
void one_Step(uint8_t stepper_direction)
{
if(stepper_direction == FORWARD)
{
if(stepper_Tracker == 3)
{
stepper_Tracker = 0;
}
else
{
stepper_Tracker = stepper_Tracker+1;
}
set_Motor_Pin(stepper_direction);
}
else if(stepper_direction == BACKWARD)
{
if(stepper_Tracker == 0)
{
stepper_Tracker =3;
}
else
{
stepper_Tracker = stepper_Tracker-1;
}
set_Motor_Pin(stepper_direction);
}
}
void set_Motor_Pin(uint8_t moving_direction)
{
if (moving_direction == FORWARD)
{
if(stepper_Tracker == 0)
{
digitalWrite(stepperPins[3], LOW);
digitalWrite(stepperPins[0], HIGH);
}
else
{
digitalWrite(stepperPins[stepper_Tracker-1], LOW);
digitalWrite(stepperPins[stepper_Tracker], HIGH);
}
}
else if(moving_direction == BACKWARD)
{
if(stepper_Tracker ==3)
{
digitalWrite(stepperPins[0], LOW);
digitalWrite(stepperPins[3], HIGH);
}
else
{
digitalWrite(stepperPins[stepper_Tracker+1], LOW);
digitalWrite(stepperPins[stepper_Tracker], HIGH);
}
}
else if(moving_direction == STOP)
{
set_Motor_Pins_Low();
}
}
void set_Motor_Pins_Low()
{
digitalWrite(stepperPins[0], LOW);
digitalWrite(stepperPins[1], LOW);
digitalWrite(stepperPins[2], LOW);
digitalWrite(stepperPins[3], LOW);
}
Stepper_Motor_Tutorial_No_Delay
/* You can change the stepperPins */
/* to any digital pins that you would like */
/* wire connection order {in1,in2,in3,in4} */
uint8_t stepperPins [4] = {13,12,11,10};
/*These are to state movement direction*/
uint8_t STOP = 0;
uint8_t FORWARD = 1;
uint8_t BACKWARD = 2;
unsigned long tempStartTime = 0;
unsigned long currentStepperTime = 0;
/*This is used to keep track of the current step state*/
uint8_t stepper_Tracker = 0;
void setup() {
pinMode(stepperPins[0], OUTPUT);
pinMode(stepperPins[1], OUTPUT);
pinMode(stepperPins[2], OUTPUT);
pinMode(stepperPins[3], OUTPUT);
set_Motor_Pins_Low();
delay(1000);
}
void loop() {
tempStartTime = millis();
if((tempStartTime-currentStepperTime) > 5) /* 5 milliseconds delay, make larger to slow down spin*/
{
one_Step(FORWARD);
currentStepperTime = millis();
}
}
/*Ignore below, unless you want to know how it works*/
void one_Step(uint8_t stepper_direction)
{
if(stepper_direction == FORWARD)
{
if(stepper_Tracker == 3)
{
stepper_Tracker = 0;
}
else
{
stepper_Tracker = stepper_Tracker+1;
}
set_Motor_Pin(stepper_direction);
}
else if(stepper_direction == BACKWARD)
{
if(stepper_Tracker == 0)
{
stepper_Tracker =3;
}
else
{
stepper_Tracker = stepper_Tracker-1;
}
set_Motor_Pin(stepper_direction);
}
}
void set_Motor_Pin(uint8_t moving_direction)
{
if (moving_direction == FORWARD)
{
if(stepper_Tracker == 0)
{
digitalWrite(stepperPins[3], LOW);
digitalWrite(stepperPins[0], HIGH);
}
else
{
digitalWrite(stepperPins[stepper_Tracker-1], LOW);
digitalWrite(stepperPins[stepper_Tracker], HIGH);
}
}
else if(moving_direction == BACKWARD)
{
if(stepper_Tracker ==3)
{
digitalWrite(stepperPins[0], LOW);
digitalWrite(stepperPins[3], HIGH);
}
else
{
digitalWrite(stepperPins[stepper_Tracker+1], LOW);
digitalWrite(stepperPins[stepper_Tracker], HIGH);
}
}
else if(moving_direction == STOP)
{
set_Motor_Pins_Low();
}
}
void set_Motor_Pins_Low()
{
digitalWrite(stepperPins[0], LOW);
digitalWrite(stepperPins[1], LOW);
digitalWrite(stepperPins[2], LOW);
digitalWrite(stepperPins[3], LOW);
}
The code example files can also be downloaded off my website too if preferred.
If you’re interested in a detailed video explanation of how stepper motors work, I recommend this one: [url]http://bit.ly/1MeFP7N[/url]
Do you have a favorite library to use with stepper motors? Post below with suggestions if you do.