Components and supplies
rubber pads
Arduino UNO
Wooden base 15cm x 15cm
Pololu Universal Aluminum Mounting Hub for 5mm Shaft
X113647 Stepper Motor Driver
28BYJ48 stepper motor
Tools and machines
fretsaw
drill with 6mm bit for wood
Project description
Code
The full sketch in Arduino
arduino
A full description of the software with comments
1/* 2 3Stepper-based rotating stand controlled by serial dialogue 4 5Circuit and comments: 6See http://www.cesarebrizio.it/Arduino/Turntable.html 7Circuit is as illustrated here: 8https://www.tdegypt.com/wp-content/uploads/2017/08/stepper-motor-wiring.png 9the only exception being that the sketch uses digital outputs 4 - 5 - 6 - 7 10while the Fritzing diagram 11 12 created 23 Dec 2017 13 modified ---- 14 by Cesare Brizio 15 16This example code is in the public domain. 17 18This sketch controls a rotating platform based on a stepper motor. 19Stepper model is 28BYJ48, controlled via the X113647 Stepper Motor 20Driver (based on the ULN2003A IC) normally purchased with the stepper. 21 22The platform can rotate in both directions and is controlled via Serial Monitor 23or Tera Term. 24The following commands are implemented: 25 26R = Rotate Clockwise continuously (meaning: for several turns) 27r = Rotate for 360deg; clockwise 28L = Rotate Counter-clockwise continuously (meaning: for several turns) 29l = Rotate for 360deg; counter-clockwise 30S = Stop rotating 31 32Sources of information: 33Small stepper control: http://arduino-info.wikispaces.com/SmallSteppers 34 35*/ 36 37/*-----( Import needed libraries )-----*/ 38#include <AccelStepper.h> 39 40/*-----( Declare Constants and Pin Numbers )-----*/ 41/* NEVER PUT ; AFTER A #define statement!!!! */ 42// motor pins 43#define motorPin1 4 // Blue - 28BYJ-48 pin 1 44#define motorPin2 5 // Pink - 28BYJ-48 pin 2 45#define motorPin3 6 // Yellow - 28BYJ-48 pin 3 46#define motorPin4 7 // Orange - 28BYJ-48 pin 4 47 // Red - 28BYJ-48 pin 5 (VCC) 48 // Blue - 28BYJ-48 pin GND 49#define STEPS_PER_TURN 2048 // number of steps in 360deg; 50 51int motorSpeed = 500; // High speeds (800 and above) may cause erratic behavior in 28BYJ-48 52int motorAccel = 400; // As above: better avoiding extreme accelerations 53int myPos = 0; // will be used to define a starting point for 360deg; rotations 54int LeftTurnUp = 0; // Couple of flags to determine rotation direction 55int RightTurnDown = 0; // Couple of flags to determine rotation direction 56int Continuous = 0; // used below to discriminate single rotation commands 57int incomingByte = 0; // for incoming serial data 58 59/*-----( Objects for stepper control )-----*/ 60// Set up the stepper as 4 wire bipolar on pin 4,5,6,7 61// NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48 62AccelStepper stepper(4,motorPin1,motorPin3,motorPin2,motorPin4); 63 64void setup() 65{ 66 Serial.begin(9600); 67 stepper.setMinPulseWidth(20); // Advisable setting to avoid that pulses from Arduino 68 // are too quick to be decoded 69 stepper.setMaxSpeed(motorSpeed); 70 stepper.setSpeed(motorSpeed); 71 stepper.setAcceleration(motorAccel); 72 // the following two lines reset "step zero" to the current position 73 stepper.setCurrentPosition(stepper.currentPosition()); 74 stepper.runToPosition(); 75 Serial.println("Available commands:"); 76 Serial.println("R = continuous clockwise rotation"); 77 Serial.println("r = 360deg; clockwise rotation"); 78 Serial.println("L = continuous counter-clockwise rotation"); 79 Serial.println("l = 360deg; counter-clockwise rotation"); 80 Serial.println("S = stop rotation"); 81} 82 83void loop() 84{ 85 86 if (Serial.available() > 0) 87 { 88 incomingByte = Serial.read(); 89 { 90 if (incomingByte == 'R') 91 { 92 Serial.println("received «R» - activating continuous clockwise rotation"); 93 // The two lines that follow allow to send commands in any sequence: 94 // before execution, a quick stop is performed 95 stepper.stop(); // Stop as fast as possible: sets new target 96 stepper.runToPosition(); // Now stopped after quickstop 97 // The following couple of flags determines rotation direction 98 LeftTurnUp = 1; 99 RightTurnDown = 0; 100 Continuous = 1; // used below to discriminate single rotation commands 101 stepper.setCurrentPosition(stepper.currentPosition()); // Set step 0 "here" 102 stepper.setSpeed(motorSpeed); // Previous commands have reset the speed 103 } 104 105 if (incomingByte == 'L') 106 { 107 Serial.println("received «L» - activating continuous counter-clockwise rotation"); 108 // The two lines that follow allow to send commands in any sequence: 109 // before execution, a quick stop is performed 110 stepper.stop(); // Stop as fast as possible: sets new target 111 stepper.runToPosition(); // Now stopped after quickstop 112 // The following couple of flags determines rotation direction 113 RightTurnDown = 1; 114 LeftTurnUp = 0; 115 Continuous = 1; // used below to discriminate single rotation commands 116 stepper.setCurrentPosition(stepper.currentPosition()); // Set step 0 "here" 117 stepper.setSpeed(motorSpeed); // Previous commands have reset the speed 118 } 119 120 if (incomingByte == 'r') 121 { 122 Serial.println("received «r» - activating single clockwise rotation"); 123 // The two lines that follow allow to send commands in any sequence: 124 // before execution, a quick stop is performed 125 stepper.stop(); // Stop as fast as possible: sets new target 126 stepper.runToPosition(); // Now stopped after quickstop 127 // The following couple of flags determines rotation direction 128 LeftTurnUp = 1; 129 RightTurnDown = 0; 130 Continuous = 0; // used below to discriminate single rotation commands 131 stepper.setCurrentPosition(stepper.currentPosition()); // Set step 0 "here" 132 stepper.setSpeed(motorSpeed); // Previous commands have reset the speed 133 // Since I will have to step forward 2047 steps, I store my current 134 // position as starting point of the rotation 135 myPos=stepper.currentPosition(); 136 } 137 138 if (incomingByte == 'l') 139 { 140 Serial.println("received «l» - activating single counter-clockwise rotation"); 141 // The two lines that follow allow to send commands in any sequence: 142 // before execution, a quick stop is performed 143 stepper.stop(); // Stop as fast as possible: sets new target 144 stepper.runToPosition(); // Now stopped after quickstop 145 // The following couple of flags determines rotation direction 146 RightTurnDown = 1; 147 LeftTurnUp = 0; 148 Continuous = 0; // used below to discriminate single rotation commands 149 stepper.setCurrentPosition(stepper.currentPosition()); // Set step 0 "here" 150 stepper.setSpeed(motorSpeed); // Previous commands have reset the speed 151 // Since I will have to step backwards 2047 steps, I store my current 152 // position as starting point of the rotation 153 myPos=stepper.currentPosition(); 154 } 155 if (incomingByte == 'S') 156 { 157 Serial.println("received «S» - stopping rotation"); 158 // Reset the flags that determine rotation direction 159 LeftTurnUp = 0; 160 RightTurnDown = 0; 161 stepper.stop(); // Stop as fast as possible: sets new target 162 stepper.runToPosition(); // Now stopped after quickstop 163 } 164 } 165 } 166 167 if (Continuous == 1) // continuous rotation 168 { 169 if (LeftTurnUp == 1) //left turn 170 { 171 stepper.moveTo(10000); //move many steps - more then mechanically needed 172 } 173 174 if (RightTurnDown == 1) //right turn 175 { 176 stepper.moveTo(-10000); //move many steps - more then mechanically needed 177 } 178 stepper.run(); 179 } 180 if (Continuous == 0) // continuous rotation 181 { 182 if (LeftTurnUp == 1) //left turn 183 { 184 stepper.moveTo(myPos+STEPS_PER_TURN); // 1 turn = 2048 step 185 } 186 187 if (RightTurnDown == 1) //right turn 188 { 189 stepper.moveTo(myPos-STEPS_PER_TURN); // 1 turn = 2048 step 190 } 191 } 192 stepper.run(); 193}
The full sketch in Arduino
arduino
A full description of the software with comments
1/* 2 3Stepper-based rotating stand controlled by serial dialogue 4 5Circuit 6 and comments: 7See http://www.cesarebrizio.it/Arduino/Turntable.html 8Circuit 9 is as illustrated here: 10https://www.tdegypt.com/wp-content/uploads/2017/08/stepper-motor-wiring.png 11the 12 only exception being that the sketch uses digital outputs 4 - 5 - 6 - 7 13while 14 the Fritzing diagram 15 16 created 23 Dec 2017 17 modified ---- 18 by Cesare 19 Brizio 20 21This example code is in the public domain. 22 23This sketch controls 24 a rotating platform based on a stepper motor. 25Stepper model is 28BYJ48, controlled 26 via the X113647 Stepper Motor 27Driver (based on the ULN2003A IC) normally purchased 28 with the stepper. 29 30The platform can rotate in both directions and is controlled 31 via Serial Monitor 32or Tera Term. 33The following commands are implemented: 34 35R 36 = Rotate Clockwise continuously (meaning: for several turns) 37r = Rotate for 360deg; 38 clockwise 39L = Rotate Counter-clockwise continuously (meaning: for several turns) 40l 41 = Rotate for 360deg; counter-clockwise 42S = Stop rotating 43 44Sources of information: 45Small 46 stepper control: http://arduino-info.wikispaces.com/SmallSteppers 47 48*/ 49 50/*-----( 51 Import needed libraries )-----*/ 52#include <AccelStepper.h> 53 54/*-----( Declare 55 Constants and Pin Numbers )-----*/ 56/* NEVER PUT ; AFTER A #define statement!!!! 57 */ 58// motor pins 59#define motorPin1 4 // Blue - 28BYJ-48 pin 1 60#define 61 motorPin2 5 // Pink - 28BYJ-48 pin 2 62#define motorPin3 6 // Yellow 63 - 28BYJ-48 pin 3 64#define motorPin4 7 // Orange - 28BYJ-48 pin 4 65 // 66 Red - 28BYJ-48 pin 5 (VCC) 67 // Blue - 28BYJ-48 pin 68 GND 69#define STEPS_PER_TURN 2048 // number of steps in 360deg; 70 71int motorSpeed 72 = 500; // High speeds (800 and above) may cause erratic behavior in 28BYJ-48 73int 74 motorAccel = 400; // As above: better avoiding extreme accelerations 75int myPos 76 = 0; // will be used to define a starting point for 360deg; rotations 77int LeftTurnUp 78 = 0; // Couple of flags to determine rotation direction 79int RightTurnDown = 0; 80 // Couple of flags to determine rotation direction 81int Continuous = 0; // used 82 below to discriminate single rotation commands 83int incomingByte = 0; // for incoming 84 serial data 85 86/*-----( Objects for stepper control )-----*/ 87// Set up the 88 stepper as 4 wire bipolar on pin 4,5,6,7 89// NOTE: The sequence 1-3-2-4 is required 90 for proper sequencing of 28BYJ48 91AccelStepper stepper(4,motorPin1,motorPin3,motorPin2,motorPin4); 92 93 94void setup() 95{ 96 Serial.begin(9600); 97 stepper.setMinPulseWidth(20); 98 // Advisable setting to avoid that pulses from Arduino 99 // 100 are too quick to be decoded 101 stepper.setMaxSpeed(motorSpeed); 102 stepper.setSpeed(motorSpeed); 103 104 stepper.setAcceleration(motorAccel); 105 // the following two lines reset "step 106 zero" to the current position 107 stepper.setCurrentPosition(stepper.currentPosition()); 108 109 stepper.runToPosition(); 110 Serial.println("Available commands:"); 111 Serial.println("R 112 = continuous clockwise rotation"); 113 Serial.println("r = 360deg; clockwise 114 rotation"); 115 Serial.println("L = continuous counter-clockwise rotation"); 116 117 Serial.println("l = 360deg; counter-clockwise rotation"); 118 Serial.println("S 119 = stop rotation"); 120} 121 122void loop() 123{ 124 125 if (Serial.available() 126 > 0) 127 { 128 incomingByte = Serial.read(); 129 { 130 if (incomingByte 131 == 'R') 132 { 133 Serial.println("received «R» - activating continuous 134 clockwise rotation"); 135 // The two lines that follow allow to send commands 136 in any sequence: 137 // before execution, a quick stop is performed 138 stepper.stop(); 139 // Stop as fast as possible: sets new target 140 stepper.runToPosition(); 141 // Now stopped after quickstop 142 // The following couple of flags determines 143 rotation direction 144 LeftTurnUp = 1; 145 RightTurnDown = 0; 146 147 Continuous = 1; // used below to discriminate single rotation commands 148 149 stepper.setCurrentPosition(stepper.currentPosition()); // Set step 0 "here" 150 151 stepper.setSpeed(motorSpeed); // Previous commands have reset the speed 152 153 } 154 155 if (incomingByte == 'L') 156 { 157 Serial.println("received 158 «L» - activating continuous counter-clockwise rotation"); 159 // The two 160 lines that follow allow to send commands in any sequence: 161 // before execution, 162 a quick stop is performed 163 stepper.stop(); // Stop as fast as possible: 164 sets new target 165 stepper.runToPosition(); // Now stopped after quickstop 166 167 // The following couple of flags determines rotation direction 168 RightTurnDown 169 = 1; 170 LeftTurnUp = 0; 171 Continuous = 1; // used below to discriminate 172 single rotation commands 173 stepper.setCurrentPosition(stepper.currentPosition()); 174 // Set step 0 "here" 175 stepper.setSpeed(motorSpeed); // Previous commands 176 have reset the speed 177 } 178 179 if (incomingByte == 'r') 180 { 181 182 Serial.println("received «r» - activating single clockwise rotation"); 183 184 // The two lines that follow allow to send commands in any sequence: 185 186 // before execution, a quick stop is performed 187 stepper.stop(); 188 // Stop as fast as possible: sets new target 189 stepper.runToPosition(); 190 // Now stopped after quickstop 191 // The following couple of flags determines 192 rotation direction 193 LeftTurnUp = 1; 194 RightTurnDown = 0; 195 196 Continuous = 0; // used below to discriminate single rotation commands 197 198 stepper.setCurrentPosition(stepper.currentPosition()); // Set step 0 "here" 199 200 stepper.setSpeed(motorSpeed); // Previous commands have reset the speed 201 202 // Since I will have to step forward 2047 steps, I store my current 203 204 // position as starting point of the rotation 205 myPos=stepper.currentPosition(); 206 207 } 208 209 if (incomingByte == 'l') 210 { 211 Serial.println("received 212 «l» - activating single counter-clockwise rotation"); 213 // The two lines 214 that follow allow to send commands in any sequence: 215 // before execution, 216 a quick stop is performed 217 stepper.stop(); // Stop as fast as possible: 218 sets new target 219 stepper.runToPosition(); // Now stopped after quickstop 220 221 // The following couple of flags determines rotation direction 222 RightTurnDown 223 = 1; 224 LeftTurnUp = 0; 225 Continuous = 0; // used below to discriminate 226 single rotation commands 227 stepper.setCurrentPosition(stepper.currentPosition()); 228 // Set step 0 "here" 229 stepper.setSpeed(motorSpeed); // Previous commands 230 have reset the speed 231 // Since I will have to step backwards 2047 steps, 232 I store my current 233 // position as starting point of the rotation 234 235 myPos=stepper.currentPosition(); 236 } 237 if (incomingByte == 238 'S') 239 { 240 Serial.println("received «S» - stopping rotation"); 241 242 // Reset the flags that determine rotation direction 243 LeftTurnUp 244 = 0; 245 RightTurnDown = 0; 246 stepper.stop(); // Stop as fast as 247 possible: sets new target 248 stepper.runToPosition(); // Now stopped after 249 quickstop 250 } 251 } 252 } 253 254 if (Continuous == 1) // continuous 255 rotation 256 { 257 if (LeftTurnUp == 1) //left turn 258 { 259 stepper.moveTo(10000); 260 //move many steps - more then mechanically needed 261 } 262 263 if (RightTurnDown 264 == 1) //right turn 265 { 266 stepper.moveTo(-10000); //move many steps 267 - more then mechanically needed 268 } 269 stepper.run(); 270 } 271 if (Continuous 272 == 0) // continuous rotation 273 { 274 if (LeftTurnUp == 1) //left turn 275 276 { 277 stepper.moveTo(myPos+STEPS_PER_TURN); // 1 turn = 2048 step 278 279 } 280 281 if (RightTurnDown == 1) //right turn 282 { 283 stepper.moveTo(myPos-STEPS_PER_TURN); 284 // 1 turn = 2048 step 285 } 286 } 287 stepper.run(); 288}
Downloadable files
Fritzing Schematics
The full circuit made in Fritzing
Fritzing Schematics
Comments
Only logged in users can leave comments
CesareBrizio
0 Followers
•0 Projects
Table of contents
Intro
13
0