// Sketch by R. Jordan Kreindler, written September 2016, to rotate // a stepper motor, pause 3 seconds each 90 degrees of turn, and reverse // direction after each full revolution int aPin = 2; //IN1: first end of the blue and yellow stator coil int bPin = 3; //IN2: first end of the pink 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 pink and orange stator coil int direction = 0; // We do not connect IN5, IN6, or IN7 int delay1 = 2; // The delay between each step int delay2 = 3000; // The delay after each 90 degree turn int count = 0; // The number of steps int numberOfRotations = 0; // The number of times the rotor has // turned 360 degrees void setup() { Serial.begin(9600); Serial.print("Motor started \n"); // Set all pins as output to send output signals from the Arduino // UNO to the coil windings pinMode(aPin, OUTPUT); pinMode(bPin, OUTPUT); pinMode(aPrimePin, OUTPUT); pinMode(bPrimePin, OUTPUT); // Start with all coils off digitalWrite(aPin, LOW); digitalWrite(bPin, LOW); digitalWrite(aPrimePin, LOW); digitalWrite(bPrimePin, LOW); } void loop() { // Send current to // 1. The aPin, and the bPin // 2. Then to the bPin and the aPrimePin // 3. Then to the aPrimePin and the bPrime Pin // 4. Then the bPrimePin and the aPin. // Thus producing steps with two coils activated at the same time // to increase torque. // 1. Energize the blue yellow, and pink orange stator coils digitalWrite(aPin, HIGH); 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 // 2. Energize the pink orange, and in reverse current direction // the blue yellow stator coil digitalWrite(aPin, LOW); digitalWrite(bPin, HIGH); 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 in reverse current directions the blue yellow and // pink orange coils digitalWrite(aPin, LOW); digitalWrite(bPin, LOW); digitalWrite(aPrimePin, HIGH); digitalWrite(bPrimePin, HIGH); // Allow some delay between energizing the coils to allow // the stepper rotor time to respond. delay(delay1); // So, delay1 microseconds // 4. Energize the blue yellow, and in reverse current direction // the pink orange coils digitalWrite(aPin, HIGH); 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; Serial.print("Number of steps = "); Serial.print(count); Serial.print(" Angle Rotated = "); Serial.print((count / 2048.00) * 360); Serial.println(" Degrees"); if (count%512 == 0 ) delay(delay2); if (count%2048 == 0 ) { direction = numberOfRotations%2; numberOfRotations = ++numberOfRotations; Serial.print("numberOfRotations = "); Serial.println(numberOfRotations); Serial.println("\n"); count = 0; Serial.print("\nDirection = "); if(direction == 0) { Serial.println("clockwise"); 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 { Serial.println("counter-clockwise"); 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 } } }