Stepper motors are used when a high degree of control and accuracy is needed and speed is not. 3D printers, CNC machines,surveillance devices, medical controls, cameras, andmany other items are operated, in part, by a stepper motor. Stepper motors could also be used to control a small vehicle if you wanted its motion to be very precise! Check out
This lesson will show you the basics of how to drive a stepper motor using an Arduino, a stepper motor driver, and a stepper motor.
Parts:
1 – Arduino Uno
1 – 28BYJ – 48 Stepper Motor
1 – ULN2003 Stepper Motor Driver Board
Assorted Wiring
Wiring:
Code:
/*-----( Import needed libraries )-----*/
#include <Stepper.h>
/*-----( Declare Constants, Pin Numbers )-----*/
//---( Number of steps per revolution of INTERNAL motor in 4-step mode )---
#define STEPS_PER_MOTOR_REVOLUTION 32
//---( Steps per OUTPUT SHAFT of gear reduction )---
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 64 //2048
/*-----( Declare objects )-----*/
// create an instance of the stepper class, specifying
// the number ofsteps of the motor and the pins it's
// attached to
//The pin connections need to be 4 pins connected
// to Motor Driver In1, In2, In3, In4 and then the pins entered
// here in the sequence 1-3-2-4 for proper sequencing
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 8, 10, 9, 11);
/*-----( Declare Variables )-----*/
int Steps2Take;
void setup() /*----( SETUP: RUNS ONCE )----*/
{
// Nothing (Stepper Library sets pins as outputs)
}
void loop()
{
// In slow motion, this is the 4 step sequence that
//is required to make the motor take one step
small_stepper.setSpeed(1);
Steps2Take = 4; // Rotate CW
small_stepper.step(Steps2Take);
delay(2000);
// now we will make the motor rotate half a revolution at a moderate speed
Steps2Take = STEPS_PER_OUTPUT_REVOLUTION / 2;
small_stepper.setSpeed(100);
small_stepper.step(Steps2Take);
delay(1000);
//now we will have it rotate half a revolution backwards at a quicker pace
Steps2Take = - STEPS_PER_OUTPUT_REVOLUTION / 2; //
small_stepper.setSpeed(700);
small_stepper.step(Steps2Take);
delay(2000);
}