Stepper control with potentiometer and buttons.

Hi there!

I am a newbie in the amazing world of Arduino.

So, please be kind with me, for my simple (and maybe stupid) queries.

The project that I have in mind is to control speed and direction of a stepper motor (nema17) with Easydriver.

Well, the whole project is with a pontetiometer and 4 buttons:

  • Set a value of speed/steps from a pontetiometer.

  • Start/stop pushbutton or switch.

  • That value I have set, double it (x2) with a pushbutton or switch.

  • Same, but x16.

  • Change direction for the above speeds with a pushbutton or switch.

I have seen many videos and tutorials with similar projects, but I still can't figure out what I must do with the code.

I have in mind that I have to set the value in the setup mode and set α variable for that speed (from the pontetiometer), and then in the loop to take that value and change it with the buttons with "if" and "else".

I will appreciate any advice,
Thank you.

a stepper motor (nema17)

But what color is it?

Set a value of speed/steps from a pontetiometer.

Which is it? Speed (time between steps) or number of steps?

but I still can't figure out what I must do with the code.

There are several things that you CAN work on.
Read the potentiometer and store the value.
Read the state of each switch.
Has the state changed?

As you get each bit working, perhaps you requirements will solidify in your mind, and you can then build a complete program. Right now, your requirements are too vague to turn into a working program.

For instance, should the speed of the motor change when the 16x switch is pressed while the stepper is moving? Or should the stepper complete the series of steps at the current speed?

The answer to that question has a lot to do with how you structure the program. If the current series of steps completes at the current speed, you can do the steps with a for loop. If the speed needs to change right away, you can't use a for loop (or you need to read the state of the switch in the for loop).

Start with the basics.

Can you make the stepper motor run in the direction and at a fixed speed ?
Can you vary the speed using a potentiometer ?
Can you read the button(s) and multiply the speed set by the pot by a factor of 2 or 16 ?

If you have already changed the speed should the buttons change the speed again ?

This Thread stepper motor basics may be useful. It also links to some simple stepper code.

...R

Thank you a lot for your replies!

I don't know about colour in the motor. I have this one:

As I know until now, I set a speed rpm for my motor (e.g. 20) and then the analogue value from the potentiometer will change the number of steps. Am I right? That's what I can understand from the "MotorKnob" example of the "stepper" library.

So, I know that my first post is too vague, but I think it is better to start with a plan and then analyse it.

PaulS, Yes, I want the speed of the motor to change immediately (while the stepper is moving) when the x2 or x16 switch is pressed.

Robin2, Thanks a lot... This thread is very useful! I 'd just read it now.

HeliBob, No I can't try these examples you say because I don't have a power supply for my motor yet. I will buy one next week. But I can't wait to learn how to build my code... :stuck_out_tongue:

Let's say I have the "AnalogIn, Out serial" example and I want to double the brightness of the LED (that I have set from the potentiometer) with a pushbutton. How can I achieve that?

Thank you again!

You can't run that motor with an Easydriver. The motor requires 1.7amps and the Easydriver can only provide 0.75amps. Use a BigEasydriver or a Pololu A4988 (both very similar) or a Pololu DRV8825 which could better handle the current.

...R

Well, the guy at the retail shop tells me, that I can fit the EasyDriver to this motor, because I want slow speed and not a heavy load (lower than 1kg). So, I don't need too much amps for my project.

Am I right?

sifis83:
Well, the guy at the retail shop tells me, that I can fit the EasyDriver to this motor, because I want slow speed and not a heavy load (lower than 1kg). So, I don't need too much amps for my project.

Am I right?

I am no professional but I don’t think speed has anything to do with it…

Stepper motors are not like regular motors… they use their max current pretty much doing nothing, it’s how they generate their holding torque at standstill and is also why they get so hot in comparison to something like a regular DC motor.

You need to keep in mind they move by stepping, so going slow vs fast doesn't really change the amount of current the motor is going to need… at least that’s how I understand it.

I would be careful because you probably have a much higher probability of burning up that driver.

I'm not professional either, but the guy told me that they use them together in the past, for very low speeds and they don't have any problems. Also, he gives me a heatsink for it to prevent any damage.

sifis83:
I'm not professional either, but the guy told me that they use them together in the past, for very low speeds and they don't have any problems. Also, he gives me a heatsink for it to prevent any damage.

The guy is probably referring to torque when taking about speed... Steppers generate more torque at lower speeds. Slow or fast, steppers are going to try and draw what it wants in regards to current. I would personally be worried about frying the driver.

This is the stepper I am playing with right now... 350mA per phase. It would work great with your driver. Also NEMA 17 size.

You still cant ignore the drivers are undersized by one amp per phase... You might want to read this.

Hope that helps

sifis83:
Well, the guy at the retail shop tells me, that I can fit the EasyDriver to this motor,

If you have already bought the Easydriver you can try it and see what happens. If the motor starts missing steps or the smoke escapes from the Easydriver you will know that you need the more powerful driver.

However if you have the option to exchange the unused Easydriver for another driver take it.

What power supply are you using for the motor?

...R

I don't have any power supply yet.. I ll buy one this week.

Maybe I 'll change the EasyDriver and take the Big one.

However, can you help me to do the "double" value of a potentiometer, with a pushbutton?

Thank you.

However, can you help me to do the "double" value of a potentiometer, with a pushbutton?

val = analogRead(somePin);

state = digitalRea(someOtherPin);

if(state == HIGH) // or LOW if you wired the switch the easy way
  val *= 2;

Now, aren't you embarrassed that you didn't even try?

Haha! No, I'm not embarrassed... As I said firstly, I'm a newbie and maybe my queries are too easy for you.

However, I can't make it again.

I write:

/*
  Trying to double the value of the pontentiometer (0-1023) with a pushbutton.
 */
int buttonPin = 2;
int buttonState = 0;

void setup() {
  Serial.begin(9600);
  
}
void loop() {
  int sensorValue = analogRead(A0);
  int buttonState = digitalRead(buttonPin);
  
  if (buttonState == HIGH);
  sensorValue *=2;

  Serial.println(sensorValue);
  delay(1);

And the result is to take a 0-2046 value on the serial monitor and nothing changes with the button.

I'm trying to e.g. set a 200 value from the potentiometer and when I push the button the value goes 400, leave the button, value again in 200.

Thank you again!

Temporarily try a longer delay() - perhaps delay(1000) - so you can see more clearly what is happening.

Instead of changing sensorValue, create a separate variable so you can print the before and after values.

…R

Ok. The delay is changed.

Why to make another variable for the after and before values?

The whole problem is that the code it seems to ignore the 'if' command.

I can see in serial monitor, that the buttonState is changing from 1 to 0, when the button is pushed,

but the sensorValue it has a range from 0 to 2046 and nothing happens when I push the button...

Should I have to use AnalogWrite somewhere?

The whole problem is that the code it seems to ignore the 'if' command.

The semi-colon on the end of it is the only code executed if the test is true.

I can't unsterstand you, sorry.

if (buttonState == HIGH);

should not have a semi-colon at the end of it.

ha! that’s it!!

many many thanks! :grinning: