public

sbright33's Arduino stepper motor driver demo

  • Download Gist
sbright_arduino_28BYJ-48_stepper.ino
Arduino
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
// EDIT: ***THIS VERSION IS OBSOLETE!!***
// PLEASE SEE NEW VERSION AT https://gist.github.com/4178431
 
// cobbled together from http://arduino.cc/forum/index.php/topic,85335.15.html
// all work by sbright33, not me.
 
#define dw digitalWrite
#define dm delayMicroseconds
const int mp1 = 4; // Blue - 28BYJ48 pin 1
const int mp2 = 5; // Pink - 28BYJ48 pin 2
const int mp3 = 6; // Yellow - 28BYJ48 pin 3
const int mp4 = 7; // Orange - 28BYJ48 pin 4
// Red - 28BYJ48 pin 5 VCC
int motorSpeed = 3000; // set stepper speed
int stepnum = 0;
float err = 0;
void setup() {
pinMode(mp1, OUTPUT);
pinMode(mp2, OUTPUT);
pinMode(mp3, OUTPUT);
pinMode(mp4, OUTPUT);
//Serial.begin(115200);
}
void loop() {
//long m=millis();
//Serial.println(millis()-m);
//if(random(4)) degrpm8(random(2),9*(random(20)+2),3200); else delay(100);
//degrpmslowCool4(1,long(360)*100*5,10*60);
//for(int i=0;i<90;i++) {degrpm(1,400,800);delay(50);}
//for(int i=0;i<8;i++) degrpm8(0,4500,2400);
//revRestart(1,2,2000,4);
//does not work 5v for(int i=0;i<500;i++){move(1); off(); delay(1);}
//for(int i=0;i<500;i++){move(1); off(); delay(1);} //<5ms ok w 12v Cool4 better
degrpm(1, 180 * 100, 2000);
//ramp(1,2000);
off();
delay(500);
}
void ramp(boolean bcw, int rpm100) {
//past 1200 it ramps in cwss() also that's bad
for (int i = 50; i < 1200; i += 100) //1200 or rpm100 for <1200
degrpmEZ(bcw, 900, i); //<900deg ignores speed
revRestart(bcw, 2, rpm100, 1);
}
void revRestart(boolean bcw, long revo, int rpm100, int xinrev) {
//xinrev small only
//in case it gets stuck due to torque load spike
for (long i = 0; i < revo * xinrev; i++)
degrpm(bcw, 36000 / xinrev, rpm100);
}
void rev(boolean bcw, long revo) {
long step2 = revo * 64 * 8 - 12; //not exactly right?
int rpm100 = 500;
if (bcw) cwss(12, rpm100); //ramp up speed
else ccwss(12, rpm100);
for (long i = 0; i < step2; i++)
if (bcw) cw();
else ccw();
off();
}
void degrpmslowCool4(boolean bcw, long deg100, int rph100) {
//ccw only, more torque, less current, less heat, most efficient code for maH
//but 4x as jerky
motorSpeed = 1000;
int step2 = deg100 * 64 * 8 / 360 / 100 / 2; //rounded down
int d = long(351500) * 2 / rph100; //div 2? see stepper.xls was /2 now *2
for (int i = 0; i < step2; i++) {
ccw();
ccw();
off();
delay(d);
} //for
off();
}
void degrpmslowCool(boolean bcw, long deg100, int rph100) {
//ccw only, more torque, less current, less heat
motorSpeed = 1500; //why 1500 needed?
int step2 = deg100 * 64 * 8 / 360 / 100; //rounded down
int d = long(351500) / 2 / rph100; //div 2? see stepper.xls
for (int i = 0; i < step2; i++) {
ccw4st1();
off();
delay(d); //cools while off
ccw4st2();
off();
delay(d);
} //for
off();
}
void degrpmslowHot(boolean bcw, long deg100, int rph100) {
//ccw only, more torque, hot w 12v
motorSpeed = 1000;
int step2 = deg100 * 64 * 8 / 360 / 100; //rounded down
int d = long(351500) / 2 / rph100; //div 2? see stepper.xls
for (int i = 0; i < step2; i++) {
ccw4st1();
delay(d);
ccw4st2();
delay(d);
} //for
off();
}
void degrpmslow2(boolean bcw, long deg100, int rph100) {
//ccw only, more torque, less current, less heat, 50% duty cycle
//compromise Hot,Cool
motorSpeed = 1000; //12v
int step2 = deg100 * 64 * 8 / 360 / 100; //rounded down
int d = long(351500) / 2 / rph100; //div 2? see stepper.xls
for (int i = 0; i < step2; i++) {
ccw4st1();
off();
delay(d / 4);
st1();
delay(d / 2);
off();
delay(d / 4);
ccw4st2();
off();
delay(d / 4);
st2();
delay(d / 2);
off();
delay(d / 4);
} //for
off();
}
void degrpmEZ(boolean bcw, long deg100, int rpm100) {
//max 64 turns or 23,000 deg or 2,300,000 deg100 long is bigger
//max 3500 rpm100 with 12v
int step2 = deg100 * 64 * 8 / 360 / 100; //rounded down
if (rpm100 < 50) rpm100 = 50; //minimum should use degrpmslow()
rpm100 = long(1463600) / rpm100 - 20; //see stepper.xls
if (bcw) cwss(step2, rpm100);
else ccwss(step2, rpm100);
}
void degrpm(boolean bcw, long deg100, int rpm100) {
//max 64 turns or 23,000 deg or 2,300,000 deg100 long is bigger
//max 3500 rpm100 with 12v
int step2 = deg100 * 64 * 8 / 360 / 100; //rounded down
if (rpm100 < 50) rpm100 = 50; //minimum should use degrpmslow()
rpm100 = long(1463600) / rpm100 - 20; //see stepper.xls
if (bcw) cwss(step2, rpm100);
else ccwss(step2, rpm100);
//with this code you can step by 2.00 deg 180x will be 360+-1
//0.50 deg 720x works to 360
//even though step size is >0.50 or 0.72?
float movedeg = float(step2) * 360 / 64 / 8; //float library adds 2K size to sketch
//Serial.println(movedeg);
//Serial.println(movedeg-(float)deg100/100); //moved too little only?
//ccw4st1 can help this
err += (movedeg - (float) deg100 / 100);
if (err < -1) {
motorSpeed = 1200;
if (bcw) cw();
else ccw();
err += (float(360) / 64 / 8);
//Serial.print("err=");Serial.println(err);
} //if err
//soft stop moves further than it should
//if(bcw) cwss(15,2000);
//else ccwss(15,2000);
delay(10); //so it stops and holds before off in loop
//off();
}
void degrpm8(boolean bcw, long deg100, int rpm100) {
//max 8 turns
//max <<3500 rpm100 with 12v cuz no ramping ~17RPM
const int min2start = 1200; //700 12v, 1200 5v
int step2 = deg100 * 64 * 64 / 360 / 100; //rounded down
if (rpm100 < 50) rpm100 = 50; //minimum should use degrpmslow()
rpm100 = long(1463600) / rpm100 - 20; //see stepper.xls same SAME
motorSpeed = rpm100;
if (motorSpeed < min2start) motorSpeed = min2start;
for (int i = 0; i < step2; i++) {
if (bcw) stepnum++;
else stepnum --;
st07();
}
//was if(bcw)cwss(step2,rpm100); else ccwss(step2,rpm100);
//does not adjust for err like degrpm cuz step 0.088 or 1/11 deg
float movedeg = float(step2) * 360 / 64 / 64; //float library adds 2K size to sketch
//Serial.println(movedeg);
err += (movedeg - (float) deg100 / 100);
//Serial.println(err);
//if(err<-1) {motorSpeed=1200; if(bcw) cw(); else ccw(); err+=(float(360)/64/8);}
//off();
}
//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 1 to 4
void ccw () {
// 1
digitalWrite(mp1, HIGH);
digitalWrite(mp2, LOW);
digitalWrite(mp3, LOW);
digitalWrite(mp4, LOW);
delayMicroseconds(motorSpeed);
// 2
digitalWrite(mp1, HIGH);
digitalWrite(mp2, HIGH);
digitalWrite(mp3, LOW);
digitalWrite(mp4, LOW);
delayMicroseconds(motorSpeed);
// 3
digitalWrite(mp1, LOW);
digitalWrite(mp2, HIGH);
digitalWrite(mp3, LOW);
digitalWrite(mp4, LOW);
delayMicroseconds(motorSpeed);
// 4
digitalWrite(mp1, LOW);
digitalWrite(mp2, HIGH);
digitalWrite(mp3, HIGH);
digitalWrite(mp4, LOW);
delayMicroseconds(motorSpeed);
// 5
digitalWrite(mp1, LOW);
digitalWrite(mp2, LOW);
digitalWrite(mp3, HIGH);
digitalWrite(mp4, LOW);
delayMicroseconds(motorSpeed);
// 6
digitalWrite(mp1, LOW);
digitalWrite(mp2, LOW);
digitalWrite(mp3, HIGH);
digitalWrite(mp4, HIGH);
delayMicroseconds(motorSpeed);
// 7
digitalWrite(mp1, LOW);
digitalWrite(mp2, LOW);
digitalWrite(mp3, LOW);
digitalWrite(mp4, HIGH);
delayMicroseconds(motorSpeed);
// 8
digitalWrite(mp1, HIGH);
digitalWrite(mp2, LOW);
digitalWrite(mp3, LOW);
digitalWrite(mp4, HIGH);
delayMicroseconds(motorSpeed);
}
//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 4 to 1
void cw() {
// 1
digitalWrite(mp4, HIGH);
digitalWrite(mp3, LOW);
digitalWrite(mp2, LOW);
digitalWrite(mp1, LOW);
delayMicroseconds(motorSpeed);
// 2
digitalWrite(mp4, HIGH);
digitalWrite(mp3, HIGH);
digitalWrite(mp2, LOW);
digitalWrite(mp1, LOW);
delayMicroseconds(motorSpeed);
// 3
digitalWrite(mp4, LOW);
digitalWrite(mp3, HIGH);
digitalWrite(mp2, LOW);
digitalWrite(mp1, LOW);
delayMicroseconds(motorSpeed);
// 4
digitalWrite(mp4, LOW);
digitalWrite(mp3, HIGH);
digitalWrite(mp2, HIGH);
digitalWrite(mp1, LOW);
delayMicroseconds(motorSpeed);
// 5
digitalWrite(mp4, LOW);
digitalWrite(mp3, LOW);
digitalWrite(mp2, HIGH);
digitalWrite(mp1, LOW);
delayMicroseconds(motorSpeed);
// 6
digitalWrite(mp4, LOW);
digitalWrite(mp3, LOW);
digitalWrite(mp2, HIGH);
digitalWrite(mp1, HIGH);
delayMicroseconds(motorSpeed);
// 7
digitalWrite(mp4, LOW);
digitalWrite(mp3, LOW);
digitalWrite(mp2, LOW);
digitalWrite(mp1, HIGH);
delayMicroseconds(motorSpeed);
// 8
digitalWrite(mp4, HIGH);
digitalWrite(mp3, LOW);
digitalWrite(mp2, LOW);
digitalWrite(mp1, HIGH);
delayMicroseconds(motorSpeed);
}
void move(boolean bcw) {
motorSpeed = 1200;
if (bcw) stepnum++;
else stepnum --;
st07();
}
void ccwss(int steps, int speed) {
//900 self starting 5v motor 5v ps
//800 most of 100% torque
//700 less torque
//600 almost none
//5v motor 12v supply:
//700 self starting
//400 decent torque
//6.6v 20RPM
//8.3v load 8.5v float 6x NiMh
//20RPM strong 28RPM weak
//15RPM 100% torque
//does not overheat
//5V 10RPM strong
//5V 20RPM weak
//5V 24RPM no torque
//12V 20RPM self start 35 max speed
//if(speed<700) steps-=10;
//ramp up speed
motorSpeed = 1000; //<12 steps, 800 for 12v
if (steps >= 12) {
motorSpeed = 3000;
if (speed < 1200) {
ccw();
ccw();
steps -= 2;
}
motorSpeed = 1200;
if (speed < 1200) {
ccw();
ccw();
steps -= 2;
}
motorSpeed = 800;
if (speed < 800) {
ccw();
ccw();
steps -= 2;
}
motorSpeed = 700;
if (speed < 700) for (int i = 0; i < 4; i++) {
ccw();
steps --;
}
motorSpeed = speed;
} //if
for (int i = 0; i < steps; i++) ccw(); //64*8 is 1 rev
}
void cwss(int steps, int speed) {
motorSpeed = 1000; //<12 steps, 800 for 12v
if (steps >= 12) {
motorSpeed = 3000;
if (speed < 1200) {
cw();
cw();
steps -= 2;
}
motorSpeed = 1200;
if (speed < 1200) {
cw();
cw();
steps -= 2;
}
motorSpeed = 800;
if (speed < 800) {
cw();
cw();
steps -= 2;
}
motorSpeed = 700;
if (speed < 700) for (int i = 0; i < 4; i++) {
cw();
steps --;
}
motorSpeed = speed;
} //if
for (int i = 0; i < steps; i++) cw(); //64*8 is 1 rev
}
void ccw4st1() {
// 1
dw(mp1, HIGH);
dw(mp2, LOW);
dw(mp3, LOW);
dw(mp4, LOW);
dm(motorSpeed);
// 2
dw(mp1, HIGH);
dw(mp2, HIGH);
dw(mp3, LOW);
dw(mp4, LOW);
dm(motorSpeed);
// 3
dw(mp1, LOW);
dw(mp2, HIGH);
dw(mp3, LOW);
dw(mp4, LOW);
dm(motorSpeed);
// 4
dw(mp1, LOW);
dw(mp2, HIGH);
dw(mp3, HIGH);
dw(mp4, LOW);
dm(motorSpeed);
}
void st1() {
dw(mp1, LOW);
dw(mp2, HIGH);
dw(mp3, HIGH);
dw(mp4, LOW);
}
void ccw4st2() {
// 5
dw(mp1, LOW);
dw(mp2, LOW);
dw(mp3, HIGH);
dw(mp4, LOW);
dm(motorSpeed);
// 6
dw(mp1, LOW);
dw(mp2, LOW);
dw(mp3, HIGH);
dw(mp4, HIGH);
dm(motorSpeed);
// 7
dw(mp1, LOW);
dw(mp2, LOW);
dw(mp3, LOW);
dw(mp4, HIGH);
dm(motorSpeed);
// 8
dw(mp1, HIGH);
dw(mp2, LOW);
dw(mp3, LOW);
dw(mp4, HIGH);
dm(motorSpeed);
}
void st2() {
dw(mp1, HIGH);
dw(mp2, LOW);
dw(mp3, LOW);
dw(mp4, HIGH);
}
void off() {
dw(mp1, LOW);
dw(mp2, LOW);
dw(mp3, LOW);
dw(mp4, LOW);
}
void st07() {
if (stepnum == -1) stepnum = 7;
if (stepnum == 8) stepnum = 0;
switch (stepnum) {
case 0:
dw(mp4, HIGH);
dw(mp3, LOW);
dw(mp2, LOW);
dw(mp1, LOW);
dm(motorSpeed);
break;
case 1:
dw(mp4, HIGH);
dw(mp3, HIGH);
dw(mp2, LOW);
dw(mp1, LOW);
dm(motorSpeed);
break;
case 2:
dw(mp4, LOW);
dw(mp3, HIGH);
dw(mp2, LOW);
dw(mp1, LOW);
dm(motorSpeed);
break;
case 3:
dw(mp4, LOW);
dw(mp3, HIGH);
dw(mp2, HIGH);
dw(mp1, LOW);
dm(motorSpeed);
break;
case 4:
dw(mp4, LOW);
dw(mp3, LOW);
dw(mp2, HIGH);
dw(mp1, LOW);
dm(motorSpeed);
break;
case 5:
dw(mp4, LOW);
dw(mp3, LOW);
dw(mp2, HIGH);
dw(mp1, HIGH);
dm(motorSpeed);
break;
case 6:
dw(mp4, LOW);
dw(mp3, LOW);
dw(mp2, LOW);
dw(mp1, HIGH);
dm(motorSpeed);
break;
case 7:
dw(mp4, HIGH);
dw(mp3, LOW);
dw(mp2, LOW);
dw(mp1, HIGH);
dm(motorSpeed);
break;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.