stepper losing power

HI friends, please help,
i am having problem with my stepper. the code works but my stepper is not moving at the stepping delay as desired, here is the code thanks

#include<Wire.h>
const long interval = 1000; 
unsigned long previousMillis = 0;
const int MPU_addr=0x68;
int16_t axis_X,axis_Y,axis_Z;
int minVal=265;
int maxVal=402;
double x;
double y;
double z;
int Stepping = false; 
void setup(){
   pinMode(7, OUTPUT);     
  pinMode(6, OUTPUT);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);

  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  pinMode(12, OUTPUT);
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  Serial.begin(9600);
  int xAng = map(axis_X,minVal,maxVal,-90,90);
  int yAng = map(axis_Y,minVal,maxVal,-90,90);
  int zAng = map(axis_Z,minVal,maxVal,-90,90);
}
void loop(){
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true);
  axis_X=Wire.read()<<8|Wire.read();
  axis_Y=Wire.read()<<8|Wire.read();
  axis_Z=Wire.read()<<8|Wire.read();
  int xAng = map(axis_X,minVal,maxVal,-90,90);
  int yAng = map(axis_Y,minVal,maxVal,-90,90);
  int zAng = map(axis_Z,minVal,maxVal,-90,90);
    
    
       //x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
         y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
       //z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
     /*Serial.print("Angle of inclination in X axis = ");
     Serial.print(x);
     Serial.println((char)176);*/
     //Serial.print("Angle of inclination in Y axis= ");
     //Serial.print(y);
     //Serial.println((char)176);
     /*Serial.print("Angle of inclination in Z axis= ");
     Serial.print(z);
     Serial.println((char)176);*/
     //Serial.println("-------------------------------------------");
     unsigned long currentMillis = millis();
      if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    Serial.println(y);
      }
     
     if (y<300){
              digitalWrite(12,HIGH);      
              digitalWrite(8, LOW);
              digitalWrite(7, HIGH); 
              Stepping = true;
              
            }
            
      else{
        Stepping=false;
        digitalWrite(12,LOW);
      }
     if (Stepping == true){
      digitalWrite(12,HIGH);
      digitalWrite(9, HIGH);
      delayMicroseconds(120);          
      digitalWrite(9, LOW); 
      delayMicroseconds(120);

      digitalWrite(6, HIGH);
      delayMicroseconds(120);          
      digitalWrite(6, LOW); 
      delayMicroseconds(120);
      } 
      
            }

What stepper? Post a link to the data sheet. What stepper driver? ditto What stepper motor power supply? (voltage and current).

I used a 7.4V battery 9A supply
2x easy Drivers thanks for helping

That stepper motor requires 1.8amps and an EasyDriver can supply a max of 0.7amps. You need a Pololu DRV8825 or equivalent. A Pololu A4988 (or a BigEasydriver) would be right at their limit and not really suitable.

Stepper motors work better with higher voltages. I think the DRC8825 can go up to 35v or 45v. At the very least use 12v.

Stepper motors are very inefficient and not really suited to battery power.

...R Stepper Motor Basics Simple Stepper Code

the stepper works well with the power source and the easy drivers i gave them, the only problem is that when i plant in my code to read a analog value, the power decrease, can i know why it lose power and how to solve it? ty Sincerely Gabriel

gabrielislost: the only problem is that when i plant in my code to read a analog value, the power decrease, can i know why it lose power and how to solve it? ty

Can you post 2 separate complete programs so we can see the changes. {A} post the program that works properly and {B} post the program with the extra code.

And what exactly do you mean by "power decrease"? I assume you mean that the motor has lower torque (which is why I commented on the power supply). But maybe you mean it just runs more slowly and that could be because the extra code is slowing down the rate at which step pulses are produced.

A stepper motor does not slow down like a DC motor when it "loses power". If its power (torque to be precise) it too low is starts missing steps.

...R

Dear Robin2, thanks for replying:)

it just runs more slowly and that could be because the extra code is slowing down the rate at which step pulses are produced.

that should be the problem i am facing, the code which the motor is supposed to be running is:

int val = analogRead(0);

void setup() { 
  Serial.begin(9500);               
  pinMode(7, OUTPUT);     
  pinMode(6, OUTPUT);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);

  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
}

void loop() {
      
   
  digitalWrite(6, HIGH);
  delayMicroseconds(70);          
  digitalWrite(6, LOW); 
  delayMicroseconds(70);

   digitalWrite(9, HIGH);
  delayMicroseconds(70);          
  digitalWrite(9, LOW); 
  delayMicroseconds(70);
}

the code with the extra code is this:

#include<Wire.h>
const long interval = 1000; 
unsigned long previousMillis = 0;
const int MPU_addr=0x68;
int16_t axis_X,axis_Y,axis_Z;
int minVal=265;
int maxVal=402;
double x;
double y;
double z;
int Stepping = false; 
void setup(){
   pinMode(7, OUTPUT);     
  pinMode(6, OUTPUT);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);

  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  pinMode(12, OUTPUT);
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  Serial.begin(9600);
  int xAng = map(axis_X,minVal,maxVal,-90,90);
  int yAng = map(axis_Y,minVal,maxVal,-90,90);
  int zAng = map(axis_Z,minVal,maxVal,-90,90);
}
void loop(){
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true);
  axis_X=Wire.read()<<8|Wire.read();
  axis_Y=Wire.read()<<8|Wire.read();
  axis_Z=Wire.read()<<8|Wire.read();
  int xAng = map(axis_X,minVal,maxVal,-90,90);
  int yAng = map(axis_Y,minVal,maxVal,-90,90);
  int zAng = map(axis_Z,minVal,maxVal,-90,90);
    
    
       //x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
         y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
       //z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
     /*Serial.print("Angle of inclination in X axis = ");
     Serial.print(x);
     Serial.println((char)176);*/
     //Serial.print("Angle of inclination in Y axis= ");
     //Serial.print(y);
     //Serial.println((char)176);
     /*Serial.print("Angle of inclination in Z axis= ");
     Serial.print(z);
     Serial.println((char)176);*/
     //Serial.println("-------------------------------------------");
     unsigned long currentMillis = millis();
      if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    Serial.println(y);
      }
     
     if (y<300){
              digitalWrite(12,HIGH);      
              digitalWrite(8, LOW);
              digitalWrite(7, HIGH); 
              Stepping = true;
              
            }
            
      else{
        Stepping=false;
        digitalWrite(12,LOW);
      }
     if (Stepping == true){
      digitalWrite(12,HIGH);
      digitalWrite(9, HIGH);
      delayMicroseconds(120);          
      digitalWrite(9, LOW); 
      delayMicroseconds(120);

      digitalWrite(6, HIGH);
      delayMicroseconds(120);          
      digitalWrite(6, LOW); 
      delayMicroseconds(120);
      } 
      
            }

thanks for helping btw

The program in Reply #6 is not complete.

The program in Reply #7 does not seem to be the other version with stuff added.

Your code is very difficult to figure out because you have not given names to the I/O pins. For example

digitalWrite(stepPin, LOW);

would be much more meaningful than

digitalWrite(8, LOW);

What is the purpose of the WIRE code? I can't imagine that you need to read the MPU between every single step - which is what seems to be happening.

I think your code would probably work better if you got rid of all the delayMicroseconds(120); and used non-blocking code as illustrated in the second example in this Simple Stepper Code

Your code will also be much easier to develop and test if you use functions to encapsulate different parts. Have a look at Planning and Implementing a Program

...R

Dear Robin2,
thanks for replying:)
the code still did not work after i remove all the delays, i think the wire read is causing the stepper to move along with the normal code.
i need to read the values off the mpu because the stepper motor relies on its values to move, is there anyway around this?
i have updated my code, hope it is more readable , and i read your tutorials, is it possible not to put so many loops, it looks hard.

#include<Wire.h>

const long interval = 1000; 
unsigned long previousMillis = 0;
const int MPU_addr=0x68;
int16_t axis_X,axis_Y,axis_Z;
int minVal=265;
int maxVal=402;
double x;
double y;
double z;
int Stepping = false; 
void setup(){
   pinMode(7, OUTPUT);     
  pinMode(6, OUTPUT);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);

  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  pinMode(12, OUTPUT);
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  Serial.begin(9600);
  int xAng = map(axis_X,minVal,maxVal,-90,90);
  int yAng = map(axis_Y,minVal,maxVal,-90,90);
  int zAng = map(axis_Z,minVal,maxVal,-90,90);
}
void loop(){
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true);
  axis_X=Wire.read()<<8|Wire.read();
  axis_Y=Wire.read()<<8|Wire.read();
  axis_Z=Wire.read()<<8|Wire.read();
  int xAng = map(axis_X,minVal,maxVal,-90,90);
  int yAng = map(axis_Y,minVal,maxVal,-90,90);
  int zAng = map(axis_Z,minVal,maxVal,-90,90);
    
    
       //x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
         y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
       //z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
     /*Serial.print("Angle of inclination in X axis = ");
     Serial.print(x);
     Serial.println((char)176);*/
     //Serial.print("Angle of inclination in Y axis= ");
     //Serial.print(y);
     //Serial.println((char)176);
     /*Serial.print("Angle of inclination in Z axis= ");
     Serial.print(z);
     Serial.println((char)176);*/
     //Serial.println("-------------------------------------------");
     unsigned long currentMillis = millis();
      if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    Serial.println(y);
      }
     
     if (y<300){
              digitalWrite(12,HIGH);   //this is for led   
              digitalWrite(8, LOW);    //this is for direction pin of stepper 1
              digitalWrite(7, HIGH);   //this is for direction pin of stepper 2
              Stepping = true;
              
            }
            
      else{
        Stepping=false;
        digitalWrite(12,LOW);
      }
     if (Stepping == true){
      digitalWrite(12,HIGH);
      digitalWrite(9, HIGH);  //this is for step pin for stepper2
                
      digitalWrite(9, LOW); 
      

      digitalWrite(6, HIGH);  //this is for step pin for stepper 1
                
      digitalWrite(6, LOW);

gabrielislost: i need to read the values off the mpu because the stepper motor relies on its values to move,

I don't have time to study your code now. Hopefully in the morning.

Can you describe (in English, not code) the relationship between the mpu and the motor. What is the mpu measuring? Why and how should the motor respond? etc etc

...R

Dear Robin2, thanks again for replying:) i am trying to read the angle off the mpu and using the stepper motor to move according to the angle that i want. the main bulk of my project is to do a self balancing robot with wheels to balance itself, so if the angle of the robot is more than the stable angle, it will move the stepper to counter the movement, and vise versa.

i tried to do another method which is currently making the motor run as i expected but now i am having problems with the mpu, when it moves a certain direction, it is supposed to either increase or decrease in angle, but there is a range a of readings when it is moving in a particular direction and it is causing my motor to jitter, what should i do? thanks:)

gabrielislost: i am trying to read the angle off the mpu and using the stepper motor to move according to the angle that i want.

Apologies. I forgot to follow-up your program and I don't think I will have time to do so today.

What is the maximum acceptable interval between reads of the MPU?

I have zero knowledge of the principles of programs for self-balancing robots so this project may be out of my depth.

...R

Dear Robin,

Robin2: What is the maximum acceptable interval between reads of the MPU?

i do not understand this questions haha.

the problem with my reading is that the values goes into a buffer zone which has high and low readings when the angle is changed, i am thinking of using a debouncing technique to try to remove that buffer zone, but i do not know where is the readings coming from from the MPU, can i ask why there must be two analog outputs SDA and SCL for the MPU? i read the MPU playground but i did not get the answer i want, please help? thanks:) Sincerely Gabriel

All I was trying to find out was whether you need to read the MPU every millisecond or whether once per second would be sufficient.

I have thought some more about your project and I don't propose to contribute further as I don't know enough about how to balance robots.

If you edit your Original Post and change the title to "Help with balancing a robot" you may catch the attention of people with useful knowledge.

...R