Homing stepper motor using two potentiometer

Hello everyone, I need help for my project. I am newbie and want to learn more about Arduino. I want to create a homing stepper motor using two potentiometers. What I want to happen is upon startup of Arduino the stepper motor will rotate left or right depending on the position value of potentiometers and stops when the two potentiometer matched their values. For example if the value of pot1 is less than pot2, it will rotate to the left direction and if pot2 is less than pot1 and it will rotate to the right direction. And when I adjust the knob of potentiometers and they matched their values the stepper motor will stop moving.

I know that it may be confusing, so I am posting my code.

In this code below the stepper motor doesn't rotate at startup when the two potentiometers values are already matched which is good for me. I already made the stepper motor rotate when the values of potentiometer are not the same, but in one direction only. Also, when it rotates it doesn't stop when the values of the two potentiometers are already matched upon adjusting.

I am using Arduino Uno R4 minima, two 5k ohms potentiometer, nema 17 stepper motor, and a A3967 easy driver stepper motor driver.

Any ideas what I am missing or not understanding?

Thanks in advance for your help Arduino community.

Any kind of help will be deeply appreciated.

#include <AccelStepper.h>
#include <MultiStepper.h>

long CCWDIR=-1; // To move stepper motor counter-clockwise.
long CWDIR=1; // To move stepper motor clockwise.

AccelStepper stepper(AccelStepper::DRIVER, 2, 3); // StepPin 2 and DirPin 3

void setup() {
  // Code to run once:
  stepper.setMaxSpeed(4000);
  stepper.setAcceleration(4000);

  int POT1VAL = analogRead(A0); // To read potentiometer 1 value
  int POT2VAL = analogRead(A1); // To read potentiometer 2 value

  int MPOT1VAL = map(POT1VAL, 0, 1023, 0, 10);
  int MPOT2VAL = map(POT2VAL, 0, 1023, 0, 10);

  // To move stepper motor counter-clockwise at startup if needed to match the values of potentiometer 1 and 2
  while (MPOT1VAL < MPOT2VAL) {
    stepper.moveTo(CCWDIR);
    stepper.run();
    stepper.setSpeed(4000);
}
stepper.setCurrentPosition(0);

// To move stepper motor clockwise at startup if needed to match values of potentiometer 1 and 2
  while (MPOT1VAL > MPOT2VAL) {
    stepper.moveTo(CWDIR);
    stepper.run();
    stepper.setSpeed(4000);
}
  stepper.setCurrentPosition(0);
}

void loop() {

}

You are testing for two conditions:

  • (MPOT1VAL < MPOT2VAL)
  • (MPOT1VAL < MPOT2VAL)

but there is a third condition that you never test for:

  • (MPOT1VAL == MPOT2VAL).

You need to test for the condition (MPOT1VAL == MPOT2VAL), and then take appropriate action in order to stop the motor running.

You should not use setSpeed() together with stepper.run(). setSpeed() is used internally by run() and it will not work as intended if you call setSpeed() too. That's the reason, why your stepper doesn't stop.

Next problem: you never update your MPOTxVAL in the loop, so your while loops will run infinitely if started.
Also CCWDIR and CWDIR are set fixed to -1 rsp. 1 and never changed. Because you use them as a absolute target position your stepper will do only one step and stop ( if if the error with setSpeed() is corrected ).

Can you please explain, how your homing with 2 pots should work? Are the pots connected to the stepper in any way?

Yes I havent test that one before, i will consider and try it. Thank you so much

I am really new to this, thank you so much for the info about stepper.run(). I will remove the stepper.setSpeed() in my code right away. About the update in the loop you mentioned, can u please give me a sample code about that because right now I really don't have an idea how its done. I will greately appreciate it if you do.

And about the homing with 2 pots, my plan is to connect 1 pot to the stepper motor using gears to be connected to each other. And the other 1 pot will be used to control the direction of the stepper motor after homing.

From this description I assume you need only 1 pot for homing? That does not fit really to your homing sequence you showed.
Why can you not use simply a limit switch for homing, as usual? May be it sould be good to tell us a little bit more about your project.

You must read the pot values within the loop, not only before.

Yes I think its easier to use a limit switch but my plan is to create a contoller like throttle governor motor controller which uses 2 potentiometer too. 1 pot for direction controller and 1 pot as position sensor for homing. Throttle governor motors are commonly used in heavy equipment industry and that is my inspiration model for this project

A switch is better. (break before make)

Depends wether it's only direction control but also speed control ( turning left/right ).

I think @szipra96 should tell a little bit more about his project. Without knowing more details, help is mostly a guess.

Can you please give more details about your stepper? NEMA17 tells nothing about the electical data. The A3967 can provide only too less current (0.75A) for most of that steppers.

I would also suggest, to use the MobaTools library to drive the stepper. It's easier to use, because you don't need to care about creating steps in the sketch ( no need for a 'run()' function to be called often ). Also some more classes in that lib may be of interest when going on with your project.

I see. "Rate of turn" would need more than just the switch. Thank you.

Yeah, we simply need more information to help ... :wink:

They will use wire-wound resistors for very long life and no change in resistance over that time span. Is that what you are using?

Hi, this is the specs that I got from the stepper motor that I am using.

1.8° Nema 17 42 stepper motor 17HD-3003-22B 30MM

I hope it helps.

I will try to use mobatools you mention, I'll just have to figure out how it works at the moment. Thank you so much.

It doesn't fit in the design that I want, but I will consider it to my next project. Thank you so much

Hi, I am not using that, in my theory I think that governor motors are just the same with stepper motors, they just give it a different name for the their industry. But maybe I am wrong thats why I am still figuring it out if it will work somehow. Thank you so much for the info

This motor has a rated current of 1.2A. Your driver cannot deliver that much current ( max is 0.75A ). If you set the current to 0.75A you will get much less torque.
What PSU are you using for the stepper?

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