Running stepper motor with current sensor reading

Hello to all gurus.
I'm having some trouble on the programming. My project is simple, just consist of 4 buttons, 1 stepper motor, 1 LCD display, and one current sensor.

I need the stepper motor to run indefinitely when i pressed the "Start" button, and stop the motor with "Stop" button. The other two buttons used to change the motor speed on-the-fly.

While the motor is running, i need the current sensor to monitor the stepper motor current. If the current hike up (to detect mechanical stalled/jammed), it need to trigger on an LED.

I'd done the first part, which is the stepper motor running well, along with the speed changing function.
But when i adding the current sensing part, with all the sampling loop, it seems like the motor has slow down drastically. I knew that the sampling loop bottleneck the motor. But i have no clue on how to figure out the method to sampling data, while the motor is running well. :confused:

There is a major flaw in your plan - the current in a stepper motor does NOT change with load. The current is the same whether the motor is loaded or not.

Regards,
Ray L.

RayLivingston:
There is a major flaw in your plan - the current in a stepper motor does NOT change with load. The current is the same whether the motor is loaded or not.

Regards,
Ray L.

But i did some experiment. Shows that when the motor running at higher speed, with an external force applied to it, to make it stall, the current will spike up...

Then post your code, using code tags.

The usual way to detect if a stepper is missing steps is to put a rotary encoder on the shaft to give an independent count of the number of steps.

As well as posting your code please post a diagram of the circuit you used to detect the current spikes.

...R

aarg:
Then post your code, using code tags.

Sorry, here is the code (Without implementing the current sensor A0 yet) :

#include <LiquidCrystal.h>
#include <AccelStepper.h>
#include <EEPROM.h>

#define HALFSTEP 8

// Motor pin definitions
#define motorPin1  A1     // IN1 on the ULN2003 driver 1
#define motorPin2  A2     // IN2 on the ULN2003 driver 1
#define motorPin3  A3     // IN3 on the ULN2003 driver 1
#define motorPin4  A4     // IN4 on the ULN2003 driver 1




const prog_uchar debounceDelay PROGMEM = 80;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);

int Speed_address = 0;  //address to store the speed value
int run_address = 20;    //address to store the run boolean


int speed_value;  //current speed 


//function write int to EEPROM
 void EEPROM_writeint(int address, int value) {
 EEPROM.write(address,highByte(value));
 EEPROM.write(address+1 ,lowByte(value));
}

//function Read int from EEPROM
unsigned int EEPROM_readint(int address) {
 unsigned int word = word(EEPROM.read(address), EEPROM.read(address+1));
 return word;
}



//LCD shield keypad function, with debounce feature
//Determine which button is press, and return the button number
int checkButton(){
  int x;
  x = analogRead (0);
   
  if (x < 60) {
        if (x <60){
        int Time = millis(); 
        while ((millis() - Time) < 80) {    
        }
        
        return 1;                     // Return button state
      }
      else {
        return 0;
      }
  }
  else if (x < 200){
        if (x <200){
        int Time = millis(); 
        while ((millis() - Time) < 80) {    
        }
        
        return 2;                     // Return button state
      }
      else {
        return 0;
      }
  }
  else if (x < 400){
        if (x < 400){
        int Time = millis(); 
        while ((millis() - Time) < 80) {    
        }
        
        return 3;                     // Return button state
      }
      else {
        return 0;
      }
  }
  else if (x < 600){
        if (x < 600){
        int Time = millis(); 
        while ((millis() - Time) < 80) {    
        }
        
        return 4;                     // Return button state
      }
      else {
        return 0;
      }
  
  }
}



//Print LCD with normal string
void lcdDisplay(String top, String bottom){
    lcd.clear();
    lcd.print(top);
    lcd.setCursor(0,1);
    lcd.print(bottom);
}


void setup(void) {
  
   lcd.begin(16, 2);
   
    stepper1.setMaxSpeed(1000.0);
    stepper1.setAcceleration(100.0);
  
  speed_value = EEPROM_readint(Speed_address);
    
  lcdDisplay("Current Speed",String(speed_value));
   
}



void loop(){

   speed_value = EEPROM_readint(Speed_address);    //read speed value from eeprom
   stepper1.setSpeed(speed_value);        
   
     
   
  boolean StartRun = EEPROM.read(run_address);    //run if boolean is true
  
  if (StartRun == true) {    //run motor indefinitely if boolean is true
    stepper1.runSpeed();
     }else{
  stepper1.stop();
    }
        

          

if(checkButton()==4){      //Check if button 4 or START button is pressed
        EEPROM.write(run_address, true);
        lcdDisplay("Motor Started",String(speed_value));
        delay(300);
   }
   
   if(checkButton()==1){    //Check if button 1 or STOP button is pressed
        EEPROM.write(run_address, false);
        lcdDisplay("Motor Stopped",String(speed_value));
        delay(300);
   }
   
   if(checkButton()==2){    //Check if button 2 or increase speed button is pressed
        speed_value = EEPROM_readint(Speed_address);
        speed_value +=50;
        
        if (speed_value >= 1000){      //Capped at 1000 speed
          speed_value = 1000;
        }
        
        EEPROM_writeint(Speed_address,speed_value);
        lcdDisplay("Speed Increased",String(speed_value));
        delay(300);
        }
   
   if(checkButton()==3){    //Check if button 3 or decrease speed button is pressed
        speed_value = EEPROM_readint(Speed_address);
        speed_value -=50;
        
        if (speed_value <= -1000){      //Capped at -1000 speed
          speed_value = -1000;
        }
        
        EEPROM_writeint(Speed_address,speed_value);
        lcdDisplay("Speed Decreased",String(speed_value));
        delay(300);
        }
        
   
}

Objective: Running the stepper motor with a current sensor monitoring the stepper motor current draw. If a sudden spike of 10% higher than the previous sampled current value, blink an LED for 5s.

As for the circuit diagram:

Sorry for the messy circuit diagram.

thumthum:
Sorry for the messy circuit diagram.

Being messy is no problem - but you seem to have forgotten to post it.

I don't see anything in your code for measuring current. It is the code that you have used to do that which I would like to see.

...R

Robin2:
Being messy is no problem - but you seem to have forgotten to post it.

I don't see anything in your code for measuring current. It is the code that you have used to do that which I would like to see.

...R

My code for measuring current is totally disaster!! All i need is to keep monitoring on averaging data coming from the analog pin, and then when spot a 10% higher than the previous averaged sample/data, then it trigger blinking a LED. I have no idea on how to do this part, without interrupting my stepper motor running.

thumthum:
My code for measuring current is totally disaster!!

I am trying to get to grips with your statement in Reply #2. How did you do that? I had assumed you created some sort of circuit and wrote some code in order to detect the current spikes.

If you did it some other way, please explain so that we can get on the same page as yourself.

...R

Given the trouble the OP has in posting, I doubt the claim that a "current spike" due to stalling has been detected.

The OP forgot to post details about the motor, the motor driver and power supply he/she is using, but with switching motor drivers, detecting stalls is extremely difficult, and impossible for a beginner.

jremington:
Given the trouble the OP has in posting, I doubt the claim that a "current spike" due to stalling has been detected.

That's why I have been paying out rope ...

...R