Pages: 1 [2] 3 4 ... 10   Go Down
Author Topic: 28BYJ-48 5-Volt Stepper  (Read 63421 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 2
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I haven't tried either of your sketches yet but you have been busy. Given your comments about heat and 12-volt operation apparently you are putting a lot of stress testing with your stepper(s).
Logged

Offline Offline
Edison Member
*
Karma: 6
Posts: leet
If you're not living on the Edge, you're taking up too much space!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is the expected life of a stepper?  I have discovered that 8v 6xNiMh or 12v is fine so long as you turn off the power when it's not moving.  Or between steps when it's moving slowly.  The 64:1 gear ratio keeps most external torque from changing the current phase in the sequence when the power is off.  My functions prove this concept.  Notice my parameters in the function calls look like this:  4500 is 45 degrees.  4525 is 45.25 degrees.  3050 is 30.50 RPM.  The slow functions take RPHour.  Just multiply RPM*60 if you prefer to use that instead.  You can even step by 2.00 degress for example without microstepping.  1/2 stepping?  1/8 stepping?  The 8-step size is not a whole interval of 2.00, but you can step 180 times and it will be 360 degrees of rotation anyway.  degrpm() supports ramping.  Or just use degrpm8() if 1/11th of a degree is good enough for you.  The only thing to add is speed ramping to achieve the highest RPM in degrpm8.  I figure it's not important because we're only moving a short distance anyway. 

Does anyone have code to translate an arbitrary angle to X and Y motors?  In other words 45 degrees would mean moving each motor 1 microstep at a time alternating evenly between them.  0 degrees is all X.  90 degrees all Y.  I don't want to move one motor 30 steps and the other 40 that would look jaggy.  Arctan gives you the ratio, but I need more than that.
Logged

If you fall... I'll be there for you!
-Floor

Skype Brighteyes3333
(262) 696-9619

Offline Offline
Edison Member
*
Karma: 6
Posts: leet
If you're not living on the Edge, you're taking up too much space!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have added the ability to measure a torque load and stop when it reaches the end of pulling a string for example.
Also Serial.printing turns or degrees as it is running.
Increased resolution to +-1/22 degrees in degrpm8().
Logged

If you fall... I'll be there for you!
-Floor

Skype Brighteyes3333
(262) 696-9619

Offline Offline
Jr. Member
**
Karma: 2
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sbright33 - your sketch posted on January 13, 2012, 06:51:43 PM is missing declarations for cw() and ccw().

Also, has your testing with microsecond delays determined an optimal delay in microseconds after doing the stepper write?
Logged

Offline Offline
Jr. Member
**
Karma: 2
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I rewrote my original stepper test to utilize direct port manipulation. I hesitated in doing this until I was positive that writes to PORTB were not going to mess up the crystal on PB6 & PB7 - it does not. DO NOT TRY THIS SKETCH unless your setup is identical to the sketch. The PORTB writes only write to Digital pins 8-to-13. If you use different pins then you MUST rewrite the sketch. Read the other warning in the sketch. This sketch only been tested on an Arduino NANO.

Code:
// WARNING: USE AT YOUR OWN RISK!!!
// Do not use this script if your Arduino doesn't use a Atmega328 or Atmega168
// It is only been tested on an Arduino NANO
// This Arduino example demonstrates bidirectional operation of a
// 28BYJ-48, which is readily available on eBay, using a ULN2003
// interface board to drive the stepper.
//////////////////////////////
// The Atmega328p chips used on the Arduino board have three ports.
// We are interested in port B:
// We are using Arduino Digital bits 8-11, which map to Atmega328p PB0-PB3
// B (digital pin 8 to 13)
// NOTE: as tested by anescient, there's no harm in writing to PORTB as long as DDRB[6:7] are 0
// Read: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1261361330
////////////////////////////////////////////////
// The speed and direction of the stepper motor is determined
// by adjusting a 1k-ohm potentiometer connected to Arduino pin A2.
// When the potentiometer is rotated fully counterclockwise, the motor
// will rotate at full counterclockwise speed. As the potentiometer is
// rotated clockwise, the motor will continue to slow down until is
// reaches its minimum speed at the the potentiometer's midpoint value .
// Once the potentiometer crosses its midpoint, the motor will reverse
// direction. As the potentiometer is rotated further clockwise, the speed  
// of the motor will increase until it reaches its full clockwise rotation
// speed when the potentiometer has been rotated fully clockwise.
////////////////////////////////////////////////

//declare variables for the motor pins
int motorPin1 = 8; // Blue   - 28BYJ48 pin 1
int motorPin2 = 9; // Pink   - 28BYJ48 pin 2
int motorPin3 = 10; // Yellow - 28BYJ48 pin 3
int motorPin4 = 11; // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)

int motorSpeed = 0;     //variable to set stepper speed
int potPin = 2; //potentiometer connected to A2
int potValue = 0; //variable to read A0 input


//////////////////////////////////////////////////////////////////////////////
void setup() {
  //declare the motor pins as outputs
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  Serial.begin(9600);
}

//////////////////////////////////////////////////////////////////////////////
void loop(){

  potValue = analogRead(potPin);     // read the value of the potentiometer
  Serial.println(potValue);          // View full range from 0 - 1024 in Serial Monitor
  if (potValue < 535){               // if potentiometer reads 0 to 535 do this
    motorSpeed = (potValue/15 + 5);  //scale potValue to be useful for motor
    clockwise();                     //go to the ccw rotation function
  }
  else {                             //value of the potentiometer is 512 - 1024
    motorSpeed = ((1024-potValue)/15 + 5); //scale potValue for motor speed
    counterclockwise(); //go the the cw rotation function
  }
}

//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 1 to 4
//delay "motorSpeed" between each pin setting (to determine speed)

void counterclockwise (){
  // 1
  PORTB = 0b0001;
  delay(motorSpeed);
  // 2
  PORTB = 0b0011;
  delay(motorSpeed);
  // 3
  PORTB = 0b0010;
  delay(motorSpeed);
  // 4
  PORTB = 0b0110;
  delay(motorSpeed);
  // 5
  PORTB = 0b0100;
  delay(motorSpeed);
  // 6
  PORTB = 0b1100;
  delay(motorSpeed);
  // 7
  PORTB = 0b1000;
  delay(motorSpeed);
  // 8
  PORTB = 0b1001;
  delay(motorSpeed);
}

//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 4 to 1
//delay "motorSpeed" between each pin setting (to determine speed)

void clockwise(){
   // 1
   PORTB = 0b1000;
  delay(motorSpeed);
  // 2
  PORTB = 0b1100;
  delay(motorSpeed);
  // 3
  PORTB = 0b0100;
  delay(motorSpeed);
  // 4
  PORTB = 0b0110;
  delay(motorSpeed);
  // 5
  PORTB = 0b0010;
  delay(motorSpeed);
  // 6
  PORTB = 0b0011;
  delay(motorSpeed);
  // 7
  PORTB = 0b0001;
  delay(motorSpeed);
  // 8
  PORTB = 0b1001;
  delay(motorSpeed);
}

Now if I can determine the optimal "motorspeed" value then I can optimize the stepper's functionality.
« Last Edit: January 14, 2012, 05:19:41 pm by celem » Logged

Offline Offline
Edison Member
*
Karma: 6
Posts: leet
If you're not living on the Edge, you're taking up too much space!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I couldn't fit cw and ccw() in the 10k code box limits of the forum.  It is included in the previous version, as well as the original version.  400us works if you ramp up the speed first.  Or just call degrpm(35rpm) it does that for you.  All the relevant speeds are listed in the comments.  I don't see the advantage of PORTB?  DigitalWrite is only microseconds apart which is not significant compared to 400 or 1200us.  Can you go faster than me?  My sketch is compatible with most Arduinos having the relevant ports which you specify at the top.
Logged

If you fall... I'll be there for you!
-Floor

Skype Brighteyes3333
(262) 696-9619

Offline Offline
Jr. Member
**
Karma: 2
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PORTB's advantage is probably moot, since we are throwing in delays anyway. The only advantage is to eliminate those microseconds of delay when setting two ports simultaneously. However, the difference is not trivial - is about 95 times faster, according to John Boxall's blog: http://tronixstuff.wordpress.com/tag/portb/

However, using PORTx commands introduce a lot of risk, reduces portability and readability and should be avoided unless truly needed.

I just wanted to see if I could get it working and I will NOT be using PORTx for a stepper application.
Logged

Offline Offline
God Member
*****
Karma: 0
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My point being that sequential turn off of motor windings is the same as a movement command, ergo there is a probability of either a backstep or a forward step as you release control.
Logged

Offline Offline
Edison Member
*
Karma: 6
Posts: leet
If you're not living on the Edge, you're taking up too much space!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As you move from 1 step to the next you are only changing 1 winding at a time.  So if it begins to move before the 4th winding is not changed, all the better!  Then there is a delay of 1000x.  When you come to an immediate stop, there is no changes to worry about.  When you turn off the power in my code, the order could be important.  I have not handled this case yet.  You can choose which order to do this in.  If 1-4 then it could in theory move backward.  If 4-1 then maybe forward.  It could not move forward if there is some torque against it.  With the gear ratio of 64:1 nor will it move backwards.  Even if it did move a step 1/11th degree, you're coming to a stop anyway.  When you start up again it will be as if it didn't slip backwards.  It should not effect torque or top speed.  Agree?
Logged

If you fall... I'll be there for you!
-Floor

Skype Brighteyes3333
(262) 696-9619

Offline Offline
Jr. Member
**
Karma: 2
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For those of you interested in greater technical detail on controlling a stepper motor, take a look at Atmel Corp's Application Note  titled "AVR446: Linear speed control of stepper motor". Lots of math there for you number crunchers.   it is an PDF located at:
http://www.atmel.com/dyn/resources/prod_documents/doc8017.pdf
« Last Edit: January 15, 2012, 02:06:10 pm by celem » Logged

Offline Offline
Jr. Member
**
Karma: 2
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was able to get the Arduino stepper library to perform better for the 28BYJ-48, but, of course, the  Arduino stepper library is limited and does not support acceleration/deceleration.  Here is working Arduino stepper library code for the 28BYJ-48:

Code:
/*
  Derived from YourDuino.com Example Software Sketch
   Small Stepper Motor and Driver, by:
   terry@yourduino.com
*/

#include <Stepper.h>
//declare variables for the motor pins
int motorPin1 = 8; // Blue   - 28BYJ48 pin 1
int motorPin2 = 9; // Pink   - 28BYJ48 pin 2
int motorPin3 = 10; // Yellow - 28BYJ48 pin 3
int motorPin4 = 11; // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)

#define STEPS  64   //Number of steps per revolution

//The pin connections need to be 4 pins connected
// to Motor Driver In1, In2, In3, In4  and then the pins entered
// here in the sequence 1-3-2-4 for proper sequencing of 28BYJ48
Stepper small_stepper(STEPS, motorPin1, motorPin3, motorPin2, motorPin4);

int  Steps2Take;

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  small_stepper.setSpeed(200);
}/*--(end setup )---*/

void loop()
{
  // sweep 1 turn each way
  small_stepper.setSpeed(200);  
  Steps2Take  =  2048;  // Rotate CW
  small_stepper.step(Steps2Take);
  delay(2000);
  
  small_stepper.setSpeed(200);  // 200 a good max speed??
  Steps2Take  =  -2048;  // Rotate CCW
  small_stepper.step(Steps2Take);
  delay(2000);

}

Most recently, I have been experimenting with the powerful accelstepper library (http://www.open.com.au/mikem/arduino/AccelStepper). It supports acceleration/deceleration and much more. I posted a short demo video of a 28BYJ-48 being driven by the accelstepper library in full-step mode. It actually is more impressive in half-step mode but I didn't make a video of that. The video is at: http://youtu.be/1F3240hCHE4

The sketch used in the video is shown below:
Code:
// accellsteppertest.ino
// Runs one stepper forwards and backwards, accelerating and decelerating
// at the limits. Derived from example code by Mike McCauley
// Set for 28BYJ-48 stepper

#include <AccelStepper.h>

#define FULLSTEP 4
#define HALFSTEP 8

//declare variables for the motor pins
int motorPin1 = 8; // Blue   - 28BYJ48 pin 1
int motorPin2 = 9; // Pink   - 28BYJ48 pin 2
int motorPin3 = 10; // Yellow - 28BYJ48 pin 3
int motorPin4 = 11; // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)

// The sequence 1-3-2-4 required for proper sequencing of 28BYJ48
AccelStepper stepper2(FULLSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

void setup()
{    
  stepper2.setMaxSpeed(1000.0);
  stepper2.setAcceleration(50.0);
  stepper2.setSpeed(200);
  stepper2.moveTo(2048);
}

void loop()
{
  //Change direction at the limits
  if (stepper2.distanceToGo() == 0) {
    stepper2.moveTo(-stepper2.currentPosition());
  }
  stepper2.run();
}

The accelstepper library has many useful features. Check it out!
Logged

Offline Offline
Edison Member
*
Karma: 6
Posts: leet
If you're not living on the Edge, you're taking up too much space!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I find that library to be difficult to understand for a beginner.  Maybe I'm biased because I wrote mine.  I've added some of the useful features mine was missing.  I can detect the torque load or change!
Logged

If you fall... I'll be there for you!
-Floor

Skype Brighteyes3333
(262) 696-9619

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I just bought the 28BYJ48 stepper motor and will try these codes.
Just so that I will not burn my Arduino Nano, how is the wiring done?
I have also ULN2003 driver.

Thanks
Logged

Offline Offline
Jr. Member
**
Karma: 2
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The wiring is listed in the sketch. I also used a nano as well as a mini-pro. The power pins on ULN2003 driver board, if yours is the same as mine, are poorly marked. Ground goes to the pin marked with a very litttle "-" and 5-volts Vcc is connected to the pin marked "+". For the ULN2003 driver board's LEDs to light the jumper must be in place - it is unmarked and is adjacent to the power pins.

From the sketch
...
int motorPin1 = 8;   // Blue   - 28BYJ48 pin 1
int motorPin2 = 9;   // Pink   - 28BYJ48 pin 2
int motorPin3 = 10;   // Yellow - 28BYJ48 pin 3
int motorPin4 = 11;   // Orange - 28BYJ48 pin 4
                        // Red    - 28BYJ48 pin 5 (VCC)

#define STEPS  64   //Number of steps per revolution

//The pin connections need to be 4 pins connected
// to Motor Driver In1, In2, In3, In4 ...
...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you selem,

did you take the +5V from Nano or separate powersupply?
Logged

Pages: 1 [2] 3 4 ... 10   Go Up
Jump to: