Go Down

Topic: Stepper motor BYJ48 (Read 255 times) previous topic - next topic

Zyx7

Hello,
I'm currently working on the stepper motor. I want it to start from an initial position (very important) and it has to rotate an angle of 300 degree for example, but with the code I found, the initial position is not defined. Everytime I turn it on, it just rotates for 360 in one direction, wait, then the other, no matter the initial position. Does anyone have an idea of how I can do this?
Code: [Select]
/*
   BYJ48 Stepper motor code
   Connect :
   IN1 >> D8
   IN2 >> D9
   IN3 >> D10
   IN4 >> D11
   VCC ... 5V Prefer to use external 5V Source
   Gnd
   written By :Mohannad Rawashdeh
  https://www.instructables.com/member/Mohannad+Rawashdeh/
     28/9/2013
  */

#define IN1  4
#define IN2  5
#define IN3  6
#define IN4  7
int Steps = 0;
boolean Direction = true;// gre
unsigned long last_time;
unsigned long currentMillis ;
int steps_left=2000;
long time;
void setup()
{
Serial.begin(115200);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
// delay(1000);

}
void loop()
{
  while(steps_left>0){
  currentMillis = micros();
  if(currentMillis-last_time>=1000){
  stepper(1);
  time=time+micros()-last_time;
  last_time=micros();
  steps_left--;
  }
  }
   Serial.println(time);
  Serial.println("Wait...!");
  delay(2000);
  Direction=!Direction;
  steps_left=2000;
}

void stepper(int xw){
  for (int x=0;x<xw;x++){
switch(Steps){
   case 0:
     digitalWrite(IN1, LOW);
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, HIGH);
   break;
   case 1:
     digitalWrite(IN1, LOW);
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, HIGH);
   break;
   case 2:
     digitalWrite(IN1, LOW);
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, LOW);
   break;
   case 3:
     digitalWrite(IN1, LOW);
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, LOW);
   break;
   case 4:
     digitalWrite(IN1, LOW);
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break;
   case 5:
     digitalWrite(IN1, HIGH);
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break;
     case 6:
     digitalWrite(IN1, HIGH);
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break;
   case 7:
     digitalWrite(IN1, HIGH);
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, HIGH);
   break;
   default:
     digitalWrite(IN1, LOW);
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
     
   break;
}
SetDirection();
}
}
void SetDirection(){
if(Direction==1){ Steps++;}
if(Direction==0){ Steps--; }
if(Steps>7){Steps=0;}
if(Steps<0){Steps=7; }
}

outsider

#1
May 22, 2018, 11:38 am Last Edit: May 22, 2018, 12:03 pm by outsider
Quote
I want it to start from an initial position (very important)
Arduino has no way of knowing the position of the motor when it starts, you will either have to set the motor to the start position manually (by hand) or use a limit switch or other sensor and run the motor to the switch (home position) in your setup() routine.
Also, your motor has 2048 steps per shaft revolution, so you cannot get exactly 300 degrees, 1707 steps is close. (300.05859375 degrees).

MarkT

Different versions of that motor have different numbers of steps per revolution allegedly, and only approximately 2048, not exactly a power of two.

If you want absolute positioning you need a absolute encoder, a stepper motor is no use in itself.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

outsider

Adafruit sells a version of that motor that has a different gear train (around 1/4) of the standard that takes about 513.04 steps per rev, damn near drove me crazy, cause I didn't get the results expected, till I bought 2 of the "Real McCoy"  :)
https://www.adafruit.com/product/858

Go Up