CesareBrizio
Published © GPL3+

Stepper-Based Rotating Stand Controlled by Serial Dialogue

A rotating platform based on a stepper motor 28BYJ48, controlled via the X113647 stepper motor driver by serial dialogue.

IntermediateFull instructions provided17,630
Stepper-Based Rotating Stand Controlled by Serial Dialogue

Things used in this project

Hardware components

28BYJ48 stepper motor
×1
X113647 Stepper Motor Driver
×1
Pololu Universal Aluminum Mounting Hub for 5mm Shaft
×1
Arduino UNO
Arduino UNO
×1
Wooden base 15cm x 15cm
×1
rubber pads
×4

Hand tools and fabrication machines

fretsaw
drill with 6mm bit for wood

Story

Read more

Schematics

Fritzing Schematics

The full circuit made in Fritzing

Code

The full sketch in Arduino

Arduino
A full description of the software with comments
/*
 
Stepper-based rotating stand controlled by serial dialogue

Circuit and comments: 
See http://www.cesarebrizio.it/Arduino/Turntable.html
Circuit is as illustrated here:
https://www.tdegypt.com/wp-content/uploads/2017/08/stepper-motor-wiring.png
the only exception being that the sketch uses digital outputs 4 - 5 - 6 - 7
while the Fritzing diagram 
 
 created 23 Dec 2017
 modified ----
 by Cesare Brizio
 
This example code is in the public domain.

This sketch controls a rotating platform based on a stepper motor.
Stepper model is 28BYJ48, controlled via the X113647 Stepper Motor 
Driver (based on the ULN2003A IC) normally purchased with the stepper.  

The platform can rotate in both directions and is controlled via Serial Monitor 
or Tera Term.
The following commands are implemented:

R = Rotate Clockwise continuously (meaning: for several turns)
r = Rotate for 360deg; clockwise
L = Rotate Counter-clockwise continuously (meaning: for several turns)
l = Rotate for 360deg; counter-clockwise
S = Stop rotating
 
Sources of information:
Small stepper control: http://arduino-info.wikispaces.com/SmallSteppers

*/

/*-----( Import needed libraries )-----*/
#include <AccelStepper.h>

/*-----( Declare Constants and Pin Numbers )-----*/
/* NEVER PUT ; AFTER A #define statement!!!! */
// motor pins
#define motorPin1  4     // Blue   - 28BYJ-48 pin 1
#define motorPin2  5     // Pink   - 28BYJ-48 pin 2
#define motorPin3  6     // Yellow - 28BYJ-48 pin 3
#define motorPin4  7     // Orange - 28BYJ-48 pin 4
                        // Red    - 28BYJ-48 pin 5 (VCC)
                        // Blue   - 28BYJ-48 pin GND  
#define STEPS_PER_TURN 2048 // number of steps in 360deg;

int motorSpeed = 500; // High speeds (800 and above) may cause erratic behavior in 28BYJ-48
int motorAccel = 400; // As above: better avoiding extreme accelerations
int myPos = 0; // will be used to define a starting point for 360deg; rotations
int LeftTurnUp = 0; // Couple of flags to determine rotation direction
int RightTurnDown = 0; // Couple of flags to determine rotation direction
int Continuous = 0; // used below to discriminate single rotation commands
int incomingByte = 0; // for incoming serial data

/*-----( Objects for stepper control )-----*/
// Set up the stepper as 4 wire bipolar on pin 4,5,6,7
// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48
AccelStepper stepper(4,motorPin1,motorPin3,motorPin2,motorPin4); 

void setup()
{
  Serial.begin(9600);
  stepper.setMinPulseWidth(20); // Advisable setting to avoid that pulses from Arduino
                                // are too quick to be decoded
  stepper.setMaxSpeed(motorSpeed);
  stepper.setSpeed(motorSpeed);
  stepper.setAcceleration(motorAccel);
  // the following two lines reset "step zero" to the current position
  stepper.setCurrentPosition(stepper.currentPosition());
  stepper.runToPosition();
  Serial.println("Available commands:");
  Serial.println("R = continuous clockwise rotation");
  Serial.println("r = 360deg; clockwise rotation");
  Serial.println("L = continuous counter-clockwise rotation");
  Serial.println("l = 360deg; counter-clockwise rotation");
  Serial.println("S = stop rotation");
}

void loop()
{

  if (Serial.available() > 0) 
  {
    incomingByte = Serial.read();
    {
      if (incomingByte == 'R')
      {
        Serial.println("received «R» - activating continuous clockwise rotation");
        // The two lines that follow allow to send commands in any sequence:
        // before execution, a quick stop is performed
        stepper.stop(); // Stop as fast as possible: sets new target
        stepper.runToPosition(); // Now stopped after quickstop
        // The following couple of flags determines rotation direction
        LeftTurnUp = 1;
        RightTurnDown = 0;
        Continuous = 1; // used below to discriminate single rotation commands
        stepper.setCurrentPosition(stepper.currentPosition()); // Set step 0 "here"
        stepper.setSpeed(motorSpeed); // Previous commands have reset the speed
      } 
      
      if (incomingByte == 'L')
      {
        Serial.println("received «L» - activating continuous counter-clockwise rotation");
        // The two lines that follow allow to send commands in any sequence:
        // before execution, a quick stop is performed
        stepper.stop(); // Stop as fast as possible: sets new target
        stepper.runToPosition(); // Now stopped after quickstop
        // The following couple of flags determines rotation direction
        RightTurnDown = 1;
        LeftTurnUp = 0;
        Continuous = 1; // used below to discriminate single rotation commands
        stepper.setCurrentPosition(stepper.currentPosition()); // Set step 0 "here"
        stepper.setSpeed(motorSpeed); // Previous commands have reset the speed
      }

      if (incomingByte == 'r')
      {
        Serial.println("received «r» - activating single clockwise rotation");
        // The two lines that follow allow to send commands in any sequence:
        // before execution, a quick stop is performed
        stepper.stop(); // Stop as fast as possible: sets new target
        stepper.runToPosition(); // Now stopped after quickstop
        // The following couple of flags determines rotation direction
        LeftTurnUp = 1;
        RightTurnDown = 0;
        Continuous = 0; // used below to discriminate single rotation commands
        stepper.setCurrentPosition(stepper.currentPosition()); // Set step 0 "here"
        stepper.setSpeed(motorSpeed); // Previous commands have reset the speed
        // Since I will have to step forward 2047 steps, I store my current 
        // position as starting point of the rotation
        myPos=stepper.currentPosition();
      } 
      
      if (incomingByte == 'l')
      {
        Serial.println("received «l» - activating single counter-clockwise rotation");
        // The two lines that follow allow to send commands in any sequence:
        // before execution, a quick stop is performed
        stepper.stop(); // Stop as fast as possible: sets new target
        stepper.runToPosition(); // Now stopped after quickstop
        // The following couple of flags determines rotation direction
        RightTurnDown = 1;
        LeftTurnUp = 0;
        Continuous = 0; // used below to discriminate single rotation commands
        stepper.setCurrentPosition(stepper.currentPosition()); // Set step 0 "here"
        stepper.setSpeed(motorSpeed); // Previous commands have reset the speed
        // Since I will have to step backwards 2047 steps, I store my current 
        // position as starting point of the rotation
        myPos=stepper.currentPosition();
      }
      if (incomingByte == 'S')
      {
        Serial.println("received «S» - stopping rotation");
        // Reset the flags that determine rotation direction
        LeftTurnUp = 0;
        RightTurnDown = 0;
        stepper.stop(); // Stop as fast as possible: sets new target
        stepper.runToPosition(); // Now stopped after quickstop
      } 
    }
  }

  if (Continuous == 1) // continuous rotation 
    {
    if (LeftTurnUp == 1)  //left turn
      {
        stepper.moveTo(10000); //move many steps - more then mechanically needed
    }

    if (RightTurnDown == 1)  //right turn
      {
       stepper.moveTo(-10000); //move many steps - more then mechanically needed
    }
    stepper.run();
  }
  if (Continuous == 0) // continuous rotation 
    {
    if (LeftTurnUp == 1)  //left turn
      {
        stepper.moveTo(myPos+STEPS_PER_TURN); // 1 turn = 2048 step
    }

    if (RightTurnDown == 1)  //right turn
      {
       stepper.moveTo(myPos-STEPS_PER_TURN); // 1 turn = 2048 step
    }
  }
  stepper.run();
} 

Credits

CesareBrizio
1 project • 12 followers
Retired, formerly software developer and database administrator
Contact

Comments

Please log in or sign up to comment.