How to save stepper position in EEPROM

Anyone have a basic example on how I would save a stepper position in the EEPROM? This would need to be saved upon power down and retrieved later on next power up, after re-homing. Would I set up a int position variable? And would I use something like this:

if (digitalWrite(saveposition1button) == LOW {
EEPROM.put(0,nowPos); 
}

and 

if (digitalWrite(recallposition1button) == LOW {
EEPROM.get(0,nowPos); 
}

Here's my code




// Define the Pins used
#define step_pin 2    // Pin 2 connected to Step pin
#define dir_pin 3     // Pin 3 connected to Direction pin
#define home_switch 9 // Pin 9 connected to Home Switch (MicroSwitch)
#define cw_button 7  // Pin 7 connected to clockwise button
#define ccw_button 8 // Pin 8 connected to counter clockwise button
#define home_button 10 // Pin 10 connected to home button (press to send the stepper home)
#define led_end 11     // Pin 11 connected to LED for end of travel reached

int direction;    // Variable to set Rotation (CW-CCW) of the motor
int steps;        // Used to set HOME position after Homing is completed

void setup() {

   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);  
   pinMode(home_switch, INPUT_PULLUP);
   pinMode(cw_button, INPUT_PULLUP);
   pinMode(ccw_button, INPUT_PULLUP);
   pinMode(home_button, INPUT_PULLUP);
   pinMode(led_end, OUTPUT);

    
}

void loop() {


// Start Homing procedure of Stepper Motor 

  if (digitalRead(home_button) == LOW) {   // Start homing when this button is pressed
    while (digitalRead(home_switch)) {  // Do this until the switch is activated   
    digitalWrite(dir_pin, HIGH);      // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(step_pin, HIGH);
    delay(3);                       // Delay to slow down speed of Stepper
    digitalWrite(step_pin, LOW);
    delay(3);

    }
     

  while (!digitalRead(home_switch)) { // Do this until the switch is not activated
    digitalWrite(dir_pin, LOW); 
    digitalWrite(step_pin, HIGH);
    delay(10);                       // More delay to slow even more while moving away from switch
    digitalWrite(step_pin, LOW);
    delay(10);

    }
  steps=0;  // Reset position variable to zero

  }

  

// Enable movement of Stepper Motor using the CW and CCW buttons

  while (digitalRead(cw_button) == LOW) {
    if (steps > 0) {  //  To make sure the Stepper doesn't go beyond the Home Position
      digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
      digitalWrite(step_pin, HIGH);
      delay(1);
      digitalWrite(step_pin, LOW);
      delay(1);
      steps--;   // Decrease the number of steps taken
    } 

     if (steps < 1000) {
      digitalWrite(led_end, LOW);   // Keep LED off if end of travel is not reached yet
    } 
  }   
  
   
    while (digitalRead(ccw_button) == LOW) {
      if (steps < 1000) {      // Maximum steps the stepper can move away from the Home Position
        digitalWrite(dir_pin, LOW);
        digitalWrite(step_pin, HIGH);
        delay(1);
         digitalWrite(step_pin, LOW);
        delay(1);
        steps++;  // Increase the number of steps taken
      }
       
       
       if (steps == 1000) {
      digitalWrite(led_end, HIGH);  // Turn on LED if end of travel is reached
      }
   
    }
       
}

Do you have an Arduino with an EEPROM? Please state which one.

Keep in mind that when steppers are powered off, it is easy to move the shaft, in which case the stored position will be wrong.

Interesting problem, we know it takes time to write to the EEPROM and to do that the Arduino needs power. How do you plan on accomplishing this. You will need a power down function. What about power failure, you will not have a warning. You can add some backup power source (capacitors etc) I would keep it in FRAM and update every time the stepper is changed. Reason FRAM has no additional delay for writing and it can do almost unlimited writes.

Does your step have an encoder?

I am using the MEGA 2560. And the shafts will never move on power down. If they do, I can always just re-home it.

Power failure will not be an issue. I can always just re-home and set my position memories again if needed.

No encoder. After homing and setting ZERO, the memory saved position can simply count from the zero location. It doesn't have to be perfectly accurate.

Thanks for the link. Ill check it out!

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