old man new to Arduino needs help with coding

Hi folks, new to this type of thing and have built a watch cabinet which has a lift mechanism which comes out of the top of cabinet powered by a Nema 23 stepper motor. ?need to code for push button operate the lift of said cabinet aprox 40cm stopped by a limit switch then push button again to lower back into cabinet which will be stopped by a second limit switch.

Have just found the cigs and collab page, will check there for guidance

These links may help if you want to write your own program
Stepper Motor Basics
Simple Stepper Code

also look up the AccelStepper library

and the Useful Links Thread.


Thank you Robin2, will go give them a look, last time I wrote code it was for A zx81 in basic :o . Would like to give it a go with this new fangled stuff though

I think that a good C++ (the language that Arduino is programmed with) tutorial might help you. Also the Arduino language reference for the Arduino specific functions.

Have cobbled this together from various other searches btu not sure if there is a simpler way to get this to work ? would appreciate comments/suggestions

int Limit01 = 8;
int Limit02 = 9;
int PUL = 7; //define Pulse pin
int DIR = 6; //define Direction pin
int ENA = 5; //define Enable Pin
int UP = 3; //define up button
int DOWN = 2; //define down button
int potPin = A5;
int count = 0;
int interval = 1000;
int val = 0;
int Spd = 500;

#define Limit01 8  // Pin 2 connected to Limit switch out
#define Limit02 9  // Pin 3 connected to Limit switch out

void setup() {
  pinMode (PUL, OUTPUT);
  pinMode (DIR, OUTPUT);
  pinMode (ENA, OUTPUT);

void loop() {

  if (digitalRead(DOWN) == LOW) {  //  If down button pressed
    if (digitalRead(Limit01) == HIGH) {} // check if limit switch is activated

    else {  //  if limit switch is not activated, move motor clockwise

      digitalWrite(DIR, LOW);
      digitalWrite(ENA, HIGH);
      digitalWrite(PUL, HIGH);
      digitalWrite(PUL, LOW);

  if (digitalRead(UP) == LOW) { // If up button pressed

    if (digitalRead(Limit02) == HIGH) {} // check if limit switch is activated

    else {  //  if limit switch is not activated, move motor counter clockwise

      digitalWrite(DIR, HIGH);
      digitalWrite(ENA, HIGH);
      digitalWrite(PUL, HIGH);
      digitalWrite(PUL, LOW);


would appreciate comments/suggestions

Before I look at the code tell me what it actually does and what you want it to do that is different?

If it won't compile then post the compiler error message.


Thanks Robin2, The project is simply to

  1. lift the above cabinet out of the carcass when a button is pressed

  2. to hit an upper limit switch which then stops the motor

  3. press of the button again to reverse the motor to lower back into carcass

  4. hit the lower limit switch to stop

I would like to use 1 push button for up and down. ? would I need to code to clear any input to the button after it is pressed so as to use it again to reverse the action?

I have an Arduino UNO and a Stepper driver TB6560 Mema 23 Stepper motor (as weight of cabinet to lift is 7-8ish Kg)

Is there a ?program/app that you can put your code into to see a visual representation of what your code does
Thanks David

Are you using belt drive then? 8kg isn't hard with a smaller motor using a leadscrew.

Normally for this sort of application (linear actuator with limit switches) a DC motor is a better choice,
but if you have belt drive then clearly need some way of holding position. Normally a leadscrew
or worm gear would be used to give passive position holding, which is safer should the power fail.

Thanks Robin2, The project is simply to

That is a clear description of what you want to do.

However you posted a program in Reply #5 and you have not told us what it actually does.