// here in the sequence 1-3-2-4 for proper sequencing
Stepper small_stepper(STEPS_PER_MOTOR_REVOLUTION, 6, 8, 7, 9);
留意 順序 1-3-2-4 對應 接腳 6, 8, 7, 9
5V Stepper Motors
You will need a stepper library, but there is one built into Arduino, so no installation should be needed.
These steppers normally have 4096 steps per revolution, but the built in stepper library works in
4 step sequence vs the 8 step sequence that it would expect, so we need to consider it as 2048, though you might find this is not completely accurate either and you may need to tweak code.
The code expects this pinout - you may modify as necessary:
IN1 --> 6
IN2 --> 7
IN3 --> 8
IN4 --> 9
5-12V+ -->5V ----要連接
Gnd- --> Gnd----要連接
//========程式=============
//2 phase step motor control
/*-----( 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 * 128 //4096
/*-----( Declare objects )-----*/
// create an instance of the stepper class, specifying
// the number of steps 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, 6, 8, 7, 9);
/*-----( Declare Variables )-----*/
int Steps2Take;
void setup() /*----( SETUP: RUNS ONCE )----*/
{
// Nothing (Stepper Library sets pins as outputs)
}/*--(end setup )---*/
void loop() /*----( LOOP: RUNS CONSTANTLY )----*/
{
small_stepper.setSpeed(1); // SLOWLY Show the 4 step sequence
Steps2Take = 4; // Rotate CW
small_stepper.step(Steps2Take);
delay(2000);
Steps2Take = STEPS_PER_OUTPUT_REVOLUTION / 2; // Rotate CW 1/2 turn
small_stepper.setSpeed(100);
small_stepper.step(Steps2Take);
delay(1000);
Steps2Take = - STEPS_PER_OUTPUT_REVOLUTION / 2; // Rotate CCW 1/2 turn
small_stepper.setSpeed(700); // 700 a good max speed??
small_stepper.step(Steps2Take);
delay(2000);
}/* --(end main loop )-- */
/* ( THE END ) */