Step motor shaking problem when Arduino starts

hi i am on a project that controls 2 step motors simultaneously. it simply goes up and down periodically. it is working fine except powering up Arduino. Whenever i power on system my motors stars shaking abruptly. İ solve this problem with placing an on-off button to motor driver's adapter. and i will power step motors after powering the Arduino. but it is very unconfortable.

is this a problem? why it happens? is there any convenient way to solve this?

maybe another solution is setting enable pin high when powering Arduino. but i cant figure out how to set eneble pin low while it connected directly 5v.

lastly i am open for any comment on my code. i try to improve myself and there is so much thing to hear from you.

i am using
arduino mega
a4998 motor driver
17HS3401 step motor

i took a video shows the problem.

problem video.zip (4.9 MB)

// Define pin connections & motor's steps per revolution
const int dirPin1 = 3;
const int stepPin1 = 2;

const int dirPin2 = 4;//
const int stepPin2 = 5;

//screen
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

//j1

#define j1sw A0
#define j1y A2
#define j1x A1
int j1swVal=0;
int j1yVal=0;
int j1xVal=0;

#define testButon 8
bool testVal;
#define initButon 9




//parameters
const int stepsPerRevolution = 200;
int stepdelay=4000;
int hiz=0;

int steps=200;
int mesafe=1;

int beklemeSuresi=3000;
float bekleme=3.0;

unsigned long counter=0;

void setup(){
  // Declare pins as Outputs
  Serial.begin(9600);
  // Declare pins as Outputs
  pinMode(stepPin1, OUTPUT);
  pinMode(dirPin1, OUTPUT);

  pinMode(stepPin2, OUTPUT);
  pinMode(dirPin2, OUTPUT);
  
  //j1
  pinMode(j1sw,INPUT);
  pinMode(j1x,INPUT);
  pinMode(j1y,INPUT);

  
  //screen
  lcd.init();                      // initialize the lcd 
  lcd.backlight();
  //welcome screen
  lcd.setCursor(2,0);
  lcd.print("Hucre Uyarim");
  lcd.setCursor(2,1);
  lcd.print("v2");
  delay(2000);

  //parameters
  initialize();
  
}


void loop(){
    // Set motor direction clockwise

  lcd.setCursor(0,1);lcd.print("dongu: ");lcd.print(counter);
  counter++;
  
  digitalWrite(dirPin1, HIGH);
  digitalWrite(dirPin2, LOW);
  
  for(int x = 0; x < steps; x++)
  {

    digitalWrite(stepPin1, HIGH);
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(stepdelay);
    digitalWrite(stepPin1, LOW);
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(stepdelay);
  }
  delay(beklemeSuresi); // Wait a second
  
  // Set motor direction counterclockwise
  digitalWrite(dirPin1, LOW);
  digitalWrite(dirPin2, HIGH);
  
  for(int x = 0; x < steps; x++)
  {
    digitalWrite(stepPin1, HIGH);
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(stepdelay);
    digitalWrite(stepPin1, LOW);
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(stepdelay);
  }
  delay(beklemeSuresi); // Wait a second
  bool initButonVal=0;
  initButonVal=digitalRead(initButon);
  if (initButonVal==1)initialize();
  Serial.println(initButon);
  
}
void test(){
  // Set motor direction clockwise
  digitalWrite(dirPin1, HIGH);
  digitalWrite(dirPin2, LOW);
  
  for(int x = 0; x < steps; x++)
  {

    digitalWrite(stepPin1, HIGH);
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(stepdelay);
    digitalWrite(stepPin1, LOW);
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(stepdelay);
  }
  delay(beklemeSuresi); // Wait a second
  
  // Set motor direction counterclockwise
  digitalWrite(dirPin1, LOW);
  digitalWrite(dirPin2, HIGH);
  
  for(int x = 0; x < steps; x++)
  {
    digitalWrite(stepPin1, HIGH);
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(stepdelay);
    digitalWrite(stepPin1, LOW);
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(stepdelay);
  }
  delay(10); // Wait a second

  
}


  


void stepup(){
  
  Serial.println("stepup");
  digitalWrite(dirPin1, HIGH);
  digitalWrite(dirPin2, LOW);

  
  for(int x = 0; x < 10; x++)
  {   
    digitalWrite(stepPin1, HIGH);
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin1, LOW);
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(2000);
  }
  delay(100);

}


void stepdown(){
  
  Serial.println("stepup");
  digitalWrite(dirPin1, LOW);
  digitalWrite(dirPin2, HIGH);

  
  for(int x = 0; x < 10; x++)
  {   
    digitalWrite(stepPin1, HIGH);
    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin1, LOW);
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(2000);
  }
  delay(100);

}

void initialize(){


  //start position  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Baslangic Konumu");
  lcd.setCursor(0,1);
  lcd.print("ayarlayin");
  
  while(1){    
    j1swVal=digitalRead(j1sw);
    j1yVal=analogRead(j1y);
    j1xVal=analogRead(j1x);
    
    testVal=digitalRead(testButon);//test
    if (testVal==1) test();
    
    if (j1yVal<200) stepup();
    if (j1yVal>800) stepdown(); 
    if (j1swVal==0) {       
      while(j1swVal==0){
        j1swVal=digitalRead(j1sw);
        delay(100);
      }
      break;
      }    
  }


//speed define
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Hizi ayarlayin");
  lcd.setCursor(0,1);
  lcd.print("hiz:");

  while(1){    
    j1swVal=digitalRead(j1sw);
    j1yVal=analogRead(j1y);
    j1xVal=analogRead(j1x);

    testVal=digitalRead(testButon);//test
    if (testVal==1) test();
    
    lcd.setCursor(6,1);
    lcd.print(hiz);
    if (j1yVal<200) hiz++;    
    if (j1yVal>800) hiz--; 
    if (j1swVal==0) {       
      while(j1swVal==0){
        j1swVal=digitalRead(j1sw);
        delay(100);
        }
      break;
      }    
    if (hiz<0) hiz=0;
    if (hiz>9) hiz=9;
      
    stepdelay=(4000-(hiz*300))/4;
    delay(150);
  }

//updown distance

    
  while(1){    
  


    j1swVal=digitalRead(j1sw);
    j1yVal=analogRead(j1y);
    j1xVal=analogRead(j1x);
    
    testVal=digitalRead(testButon);//test
    if (testVal==1) test();
    
   
    
    if (j1yVal<200){
      mesafe++; 
      lcd.clear();   
    }
    if (j1yVal>800){
      mesafe--; 
      lcd.clear();
    }
    if (j1swVal==0) {       
      while(j1swVal==0){
        j1swVal=digitalRead(j1sw);
        delay(150);
        }
      break;
      } 
         
    if (mesafe<1) mesafe=1;
    if (mesafe>40) mesafe=40;
      
    steps=(200/8)*mesafe*2;

    lcd.setCursor(0,0);
    lcd.print("Hareket mesafesi");
    lcd.setCursor(0,1);
    lcd.print("mesafe:");
    lcd.setCursor(7,1);
    lcd.print(mesafe);
    lcd.setCursor(10,1);
    lcd.print("mm");
    
    delay(100);

  }
  
//wait between up and down
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("bekleme suresi");
  lcd.setCursor(0,1);
  lcd.print("sure: ");

  while(1){    
    j1swVal=digitalRead(j1sw);
    j1yVal=analogRead(j1y);
    j1xVal=analogRead(j1x);

    testVal=digitalRead(testButon);//test
    if (testVal==1) test();
    
    lcd.setCursor(6,1);
    lcd.print(bekleme);
    lcd.setCursor(10,1);
    lcd.print(" sn");
    if (j1yVal<200) bekleme+=0.1;    
    if (j1yVal>800) bekleme-=0.1; 
    if (j1swVal==0) {       
      while(j1swVal==0){
        j1swVal=digitalRead(j1sw);
        delay(100);
        }
      break;
      } 
         
    if (bekleme<0.1) bekleme=0.1;
    if (bekleme>15) bekleme=15;
      
    beklemeSuresi=bekleme*1000;
    delay(150);   
  
  }
  
  lcd.clear();
  lcd.setCursor(0,0);lcd.print("v:");lcd.print(hiz);
  lcd.setCursor(4,0);lcd.print(mesafe);lcd.print("mm");
  lcd.setCursor(9,0);lcd.print(bekleme);lcd.print("sn");
  counter=0;

}

I would suggest disabling the driver output stages by wiring the A4988 driver enable inputs to an Arduino output. Add a 10K pullup to the enable to keep the enable held high until, in setup(), after setting the step and dir pins to OUTPUT, set the enable pin to OUTPUT and then write a low to the enable pin to enable the drivers outputs.

thanks , but there is still problem before arduino run setup section. there is a gap between powering on and running setup(maybe 1 sec) and this time motors shaking and this is a problem

Did you try it? If the driver output stage is disabled, how can the motors get power to shake. The pullup on the enable pin should keep the drivers output stage disabled from the time of power up till you set the enable pins to output and low.

This means i didnt understand ur solution sorry for that. dı u mean something like this? my component dont have enable pin so i used slp pin instead of it.

Your original post says that you have A4988 stepper drivers. This is how I meant to wire them.

1 Like

i meant the same. This is the solution thank you. i have 1 question. when there is no pinmode definition or digitalWrite command, what exactly does this digital pins. for example in our project how acts digital pins before setup section?

Digital pins default to INPUT on reset, which means they have a(n extremely) high impedance - they are effectively simply disconnected and will easily float to any level. That is why you need a pull-up (or sometimes pull-down) to set the value while the code initialises. :+1:

I/O pins are reset to input at boot time.

Yeah! :grin:

last question about this topic. i heard lots of time "floating pins" . what exactly this means?

It means what I explained in #8. A pin that is "floating" - defined as an input and not connected to anything which in any way sets it to a particular voltage - can pick up charge from static electricity and may be at any voltage between or including Vcc and ground. So it may read as either HIGH or LOW or randomly change between one and the other.

Particularly if for example, the pin is connected to a piece of wire that is not connected to anything else. You can actually use this as a detector for static electricity.

https://www.youtube.com/watch?v=Qvi6dm9QeEI

1 Like

"floating pins" really comes from this similarity? i never think in this way. very good naming then. floating pins , just like needle, can go high and low with little static charge. am i understand correct?

Pretty much.

i love behind electronic facts

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.