// 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. // An LCD is used to show rotation direction, angle(s) turned, and RPM. int RPM; unsigned long time; #include // LiquidCrystal (RS, E, d4, d5, d6, d7) LiquidCrystal lcd(13, 12, 11, 10, 9, 8); // For standalone, non-shield, LCD // Pin assignments int aPin = 2; //IN1: coil a one end int bPin = 3; //IN2: coil b one end int aPrimePin = 4; //IN3: coil aPrime other end of coil a int bPrimePin = 5; //IN4: coil bPrime other end of coil b // We do not connect IN5, IN6, or IN7 int degrees = 0; int delay1 = 2; // The delay between each step in milliseconds int delay2 = 200; // 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 //Reversive direction after each turn if ((numberOfRotations) % 2 == 0) { // Check if number of rotations is even // if so reverse direction aPin = 5; bPin = 4; aPrimePin = 3; bPrimePin = 2; } else { // If number of rotations is an odd number aPin = 2; bPin = 3; aPrimePin = 4; bPrimePin = 5; } // 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 / 4096.0)); if ((numberOfRotations % 2) == 1) { // Check if number of rotations is odd 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 two spaces, to provide room for a minus // when the stepper turns counter-clockwise lcd.print(degrees); // Print angular position in degrees lcd.print((char)223); // Print degree symbol lcd.print(" "); // Print a space } else { // If numberOfRotations is even 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 == 4096) { time = millis(); if ( time > 55000 && time < 65000) { // See how many rotations were made // in a minute (approximately). // Print the number of rotations on the LCD, adjusting for fractional // values not otherwise accounted for. RPM is only approximate. RPM = numberOfRotations + 1; lcd.setCursor(7, 1); lcd.print("RPM: "); lcd.print(RPM); } delay(delay2); // delay2/1000 second(s) after each full rotation count = 0; // Reset step counter to zero numberOfRotations = ++numberOfRotations; } }