// Sketch by R. Jordan Kreindler, written September 2016, to rotate // a stepper motor, pause 1 seconds each 180 degrees of turn, reverse // direction after each full revolution. Turn on red LED if clockwise // rotation and green LED if counter-clockwise int aPin = 2; //IN1: first end of the blue and yellow stator coil int bPin = 3; //IN2: first end of the white and orange stator coil int aPrimePin = 4; //IN3: second end of the blue and yellow stator coil int bPrimePin = 5; //IN4: second end of the White and orange stator coil int redLED = 6; // redLED on if rotor turning clockwise int greenLED = 7; // greenLED on if rotor turning counter-clockwise int count = 0; // The number of turns int delay1 = 15; // Delay between steps int delay2 = 1000; // Pause at 180 degrees int direction = 2; // Direction flag: 0 = clockwise; else = counter-clockwise int numberOfRotations = 0; // We do not connect IN5, IN6, or IN7 void setup() { // Set all pins as output to send output signals from the Arduino // UNO to the coil windings of the stator pinMode(aPin, OUTPUT); pinMode(bPin, OUTPUT); pinMode(aPrimePin, OUTPUT); pinMode(bPrimePin, OUTPUT); // Set LED pins for output pinMode(redLED, OUTPUT); pinMode(greenLED, OUTPUT); // Start with all coils off digitalWrite(aPin, LOW); digitalWrite(bPin, LOW); digitalWrite(aPrimePin, LOW); digitalWrite(bPrimePin, LOW); } void loop() { direction = numberOfRotations % 2; if (direction == 1) { digitalWrite(redLED, LOW); digitalWrite(greenLED, HIGH); } else { digitalWrite(greenLED, LOW); digitalWrite(redLED, HIGH); } // Send current to the apin, and then the bpin // then reverse the current and send reversed current // to aPrimePin and then bPrimePin. // 1. Energize the blue yellow stator coil digitalWrite(aPin, HIGH); digitalWrite(bPin, LOW); digitalWrite(aPrimePin, LOW); digitalWrite(bPrimePin, LOW); // Allow some delay between energizing the coils to allow // the stepper rotor time to respond. delay(delay1); // So, delay1 microseconds // 2. Reverse current direction and energize the blue yellow stator coil digitalWrite(aPin, LOW); digitalWrite(bPin, LOW); digitalWrite(aPrimePin, HIGH); digitalWrite(bPrimePin, LOW); // Allow some delay between energizing the coils to allow // the stepper rotor time to respond. delay(delay1); // So, delay1 microseconds // 3. Energize the blue yellow stator coil digitalWrite(aPin, LOW); digitalWrite(bPin, HIGH); digitalWrite(aPrimePin, LOW); digitalWrite(bPrimePin, LOW); // Allow some delay between energizing the coils to allow // the stepper rotor time to respond. delay(delay1); // So, delay1 microseconds // 4. Reverse current direction and energize the white orange stator coil digitalWrite(aPin, LOW); digitalWrite(bPin, LOW); digitalWrite(aPrimePin, LOW); digitalWrite(bPrimePin, HIGH); // Allow some delay between energizing the coils to allow // the stepper rotor time to respond. delay(delay1); // So, delay1 microseconds count = count + 4; if (count == 100) delay(delay2); if (count == 200) { delay(delay2); numberOfRotations = ++numberOfRotations; count = 0; if (direction == 0) { aPin = 5; //IN1: first end of the blue and yellow stator coil bPin = 4; //IN2: first end of the pink and orange stator coil aPrimePin = 3; //IN3: second end of the blue and yellow stator coil bPrimePin = 2; //IN4: second end of the pink and orange stator coil } else { aPin = 2; //IN1: first end of the blue and yellow stator coil bPin = 3; //IN2: first end of the pink and orange stator coil aPrimePin = 4; //IN3: second end of the blue and yellow stator coil bPrimePin = 5; //IN4: second end of the pink and orange stator coil } } }