// Sketch by R. Jordan Kreindler, written October 2016, to rotate // a stepper motor using the Half Step Method and reverse direction // after approximately a full revolution. A 48 basic step stepper // motor is used, yielding 96 steps for the half step method. // An LCD is used to show rotation direction, angle(s), and RPM. float RPM; unsigned long time; #include // LiquidCrystal (RS, E, d4, d5, d6, d7) LiquidCrystal lcd(13, 12, 11, 10, 9, 8); // A standalone, non-shield, LCD // was used. // Pin assignments int aPin = 4; //IN1: coil a one end int bPin = 3; //IN2: coil b one end int aPrimePin = 5; //IN3: coil aPrime other end of coil a int bPrimePin = 2; //IN4: coil bPrime other end of coil b // We do not connect IN5, IN6, or IN7 int one = aPin; int two = bPin; int three = aPrimePin; int four = bPrimePin; int degrees = 0; int delay1 = 20; // The delay between each step in milliseconds int delay2 = 50; // The delay after each full revolution, in milliseconds int count = 0; // The number of steps int numberOfRotations = 1; // The number of times the rotor has // turned 360 degrees. void setup() { // Set all pins as output to send output signals from the Arduino // UNO to the coil windings of the stator lcd.begin(16, 2); // Sets the size of the LCD in characters and lines lcd.clear(); // Clear the LCD screen of characters and symbols pinMode(aPin, OUTPUT); pinMode(bPin, OUTPUT); pinMode(aPrimePin, OUTPUT); pinMode(bPrimePin, OUTPUT); lcd.setCursor(0, 0); lcd.print(" Clockwise"); // 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 // 2. The aPin, and the bPin // 3. The bPin // 4. Then to the bPin and the aPrimePin // 5. Then to the aPrimePin // 6. Then to the aPrimePin and the bPrime Pin // 7. Then to the bPrimePin // 8. Then the bPrimePin and the aPin. // Thus producing steps using the half step method // 1. Set the aPin High 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 milliseconds // 2. Energize aPin and bPin to HIGH 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 milliseconds // 3. Set the bPin to High 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 milliseconds // 4. Set the bPin and the aPrimePin to HIGH 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 milliseconds // 5. Set the aPrime Pin to high 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 milliseconds // 6. Set the aPrimePin and the bPrime Pin to HIGH 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 milliseconds // 7. Set the bPrimePin to HIGH 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 milliseconds // 8. Set the bPrimePin and the aPin to HIGH 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 milliseconds count = count + 8; degrees = (360.0 * (count / 400.0)); if ((numberOfRotations % 2) == 1) { // Check if number of rotations is even lcd.setCursor(0, 0); // Move the cursor to 1th position on 1st line lcd.print(" Clockwise "); lcd.setCursor(0, 1); // Move the cursor to 1th position on 2nd line lcd.print(" "); // Print a space lcd.print(degrees); // Print angular position in degrees lcd.print((char)223); // Print degree symbol lcd.print(" "); // Print a space } else { // If numberOfRotations is odd number lcd.setCursor(0, 0); // Move the cursor to 1th position on 1st line lcd.print(" Anti-Clockwise "); degrees = 360 - degrees; lcd.setCursor(0, 1); // Move the cursor to 1th position on 2nd line lcd.print(" -"); // Print a minus sign lcd.print(degrees); // Print angular position in degrees lcd.print((char)223); // Print degree symbol lcd.print(" "); // Print a space } if (count == 96) { // A full revolution of the stepper numberOfRotations = ++numberOfRotations; time = millis(); RPM = time / numberOfRotations; // Average time of a rotation RPM = (60000.00 / RPM); // Number of rotations per minute if (numberOfRotations >= 10) { lcd.setCursor(7, 1); lcd.print("RPM:"); lcd.print(round(RPM)); //Print RPM as integer lcd.print(" "); } delay(delay2); // delay2/1000 second(s) after each full rotation count = 0; // Reset step counter to zero //Reversive direction after each turn if ((numberOfRotations) % 2 == 0) { // Check if number of rotations is even // if so reverse direction aPin = four; bPin = three; aPrimePin = two; bPrimePin = one; } else { // If number of rotations is an odd number aPin = one; bPin = two; aPrimePin = three; bPrimePin = four; } } }