Stepper motor misbehaving

I'm using a Nano driven stepper to tune a capacitor, but I am having a lot of trouble trying to get repeatable positioning.
I also have a 'homing' routine to establish the starting position, but when the system returns home, it 'bounces' against the limit switch.
this is the sketch.....



#include "Arduino.h"
#include "LiquidCrystal_I2C.h"
#include <Wire.h>
#include "BasicStepperDriver.h"
#include <Encoder.h>

#define outputA 2  
#define outputB 3   
#define button_pin 6  
#define upswitch 4
#define dnswitch 5
#define DIR 7  
#define STEP 8 
#define limitswitch 9 
#define SLEEP 10  
#define MICROSTEPS 8
#define MOTOR_STEPS 200
#define RPM 5 

BasicStepperDriver stepper(MOTOR_STEPS, DIR, STEP, SLEEP);
Encoder myEnc(2, 3); 
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); 


int button;
int position;
int stepcount = 0;
int limit = 0;
int currpos;
long oldPosition  = 0;
long newPosition = 0;

 
void setup() {

  lcd.begin(20, 4);
  lcd.display();                      
  lcd.backlight();
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode (3, INPUT_PULLUP); 
  pinMode (2, INPUT_PULLUP);
  pinMode (6, INPUT_PULLUP);
  pinMode (9, INPUT_PULLUP);
  pinMode (4, INPUT_PULLUP);
  pinMode (5, INPUT_PULLUP);

  stepper.begin(RPM, MICROSTEPS);
  Serial.begin (9600);



}

void home(){
    delay(10);

    Serial.println ("Homing");
limit = digitalRead(limitswitch);
while (limit == 1) {
  limit = digitalRead(limitswitch);
  stepper.move(-1); // clockwise
   delay(5);
  

}
myEnc.write (0);
    stepcount = 0;  

  updateDisplay();
}

void updateDisplay(){

   lcd.clear();
 
  lcd.setCursor(0, 0);
  lcd.print("  MAGLOOP CONTROL   ");
  lcd.setCursor(0, 1);
  lcd.print("Position  ");
  lcd.setCursor(16,1);
  lcd.print(currpos);   
  lcd.setCursor(0, 2);
  lcd.print("");
  lcd.setCursor(16,2);
  lcd.print("");
  lcd.setCursor(0, 3);
  lcd.print("");
  lcd.setCursor(14, 3);
  lcd.print("");
 
  delay(10);
}




void loop() {
  button = 1;
  button = digitalRead (button_pin);
  if (button == 0){
    home();
    button = 1; 
    delay(50);
  }
  
  long newPosition = myEnc.read();
  Serial.println (newPosition);
    if (newPosition < 0) {
      newPosition = 0;
    }
    if (newPosition > 400) {
      newPosition = 400;
    }
    if (newPosition > oldPosition) {
      stepcount = (newPosition - oldPosition);
      stepper.move (stepcount);
      delay(10);
    }
    if (newPosition < oldPosition) {
      stepcount = (oldPosition - newPosition);
      stepper.move (- stepcount);
      delay(10);      
    }
  oldPosition = newPosition;
   currpos = newPosition;
   
   updateDisplay(); 
  
}

Now, I'm just a novice programmer, so any help would be appreciated.
Thanks.

This is what the limit switch pin looks like to your homing function.

You will want a debounce routine to ensure the limit switch is pressed (and not ringing).
https://docs.arduino.cc/built-in-examples/digital/Debounce/

Or, you can try an interrupt and get the first syllable.
https://docs.arduino.cc/language-reference/en/functions/external-interrupts/attachInterrupt/

Ok, thanks for that. I'll try a debounce routine.

At this point you should move the stepper AWAY from the limit switch by ONE step and see if the switch is still closed. IF it is, move away another single step and repeat until the switch is open. That is the TRUE zero position of the stepper that needs to be remembered. Then when moving the stepper back to the zero point, it will NOT contact the limit switch.

2 Likes

The actual position is not too important, repeatability is.
Thinking of all the systems I have worked on, the limit is the point at which the limit switch operates and that is what I am attempting to achieve.

The actual position is the key for stepper motors!

I'm not familiar with it, but that StepperDriver library doesn't seem to track actual position, making all the move()s relative and also requiring the user keep track of actual position somehow. I'd expect that the home() routine should zero out all the oldPostion, currpos, newPosition variables.

If the motor is properly specified for the application, then it shouldn't lose position/steps after homing. If you re-home frequently, then the positioning is dependent on the repeatabilty of the (mechanical?) switch, rather than the repeatability of the stepper.

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