Stepper Motor Control with buttons

Hi Guys.

Totally new to the forum and totally new to the Arduino scene and need a little help in my first project.

I have an Arduino Uno R3 (Elegoo) and Looking on control a Nema 17 Stepper motor using an a4988 driver with 2 dead man switches; for CW and CCW. Example: - Button one pressed and the stepper will move CW until the switch is released and the stepper will stop and same CCW.

Link to switch for reference: - Switch link

I have Wired up the Uno and the driver and I can send basic commands to make the stepper move one way and they the other so I know I have basic wiring connected right.

Art present, the pin setup is as below from the Uno: -

Pin -6: - Dir pin to stepper drv
Pin -5: - Step pin to stepper drv
Pin 4: - EN pin to stepper drv
Pin 5v: - login 5v to stepper drv
Pin gnd: - ground to stepper drv

Code is as per below I am running: -

int Index;

void setup()

{
  pinMode(6, OUTPUT); //Enable
  pinMode(5, OUTPUT); //Step
  pinMode(4, OUTPUT); //Direction
  
  digitalWrite(6,LOW);
}

void loop()
{
  digitalWrite(4,HIGH);

  for(Index = 0; Index < 2000; Index++)
  {
    digitalWrite(5,HIGH);
    delayMicroseconds(1500);
    digitalWrite(5,LOW);
    delayMicroseconds(1500);
  }

  delay(1000);

  digitalWrite(4,LOW);

  for(Index = 0; Index < 2000; Index++)
  {
    digitalWrite(5,HIGH);
    delayMicroseconds(1500);
    digitalWrite(5,LOW);
    delayMicroseconds(1500);
  }
  delay(1000);

  
}

I want to now add in the buttons but not too sure how to add in the code. I have seen that a pull down resistor is probably the best option but just want to find out from some people who has had experience with this before.

I hope I have given enough info and look forward to the replies.



The best way to wire a switch is one side of the switch goes to ground and the other side of the switch goes to an input set to
pinMode(pin, INPUT_PULLUP);
The input will read HIGH when the switch is unpressed and LOw when pressed.


Shown is typical 6mm tactile switch.

Try a search for "stepper with switches arduino".

1 Like

You code creates the step-pulses "by hand" This is a good exercsie to learn the basics.
I recommend that you use the MobaTools library which makes driving steppermotors more comfortable.

The MobaTools-Library uses a tecnique where creating the step-pulses is done with a timer-interrupt. This enables to create the step-pulses "in the background" while your code can do other things in "parallel" to the step-pulse-creation.

You execute a single line of code

myStepper.doSteps(2000);

and the stepper will run 2000 steps
and in "parallel" to the stepper-motor running you could blink an LED or whatever

The MobaTools have functions to use buttons too. Including things like short/long-press double-press etc.

There are quite a lot examples delivered with the library.
You can install the MobaTools with the library-manager inside the Arduino-IDE

best regards Stefan

... then replace the for loops and delays with if statements to check whether the button is LOW:

const byte CCWpin = A0;
...
  pinMode(CCWpin,INPUT_PULLUP);
...
  //for(Index = 0; Index < 2000; Index++)
  if (digitalRead(CCWpin) == LOW)
  {
...

Hi guys and thanks for the replies. I have been working away from home and only back today. Will go through the replies tomorrow and will let you know what i have managed to get working.

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