Using Sensor Switches with Arduino Uno

Hello!

I am new to using this forum so I will start with apologizing for any mistakes.

I am currently setting up a way to rotate a stepper motor using a sensor switch. I have code written that rotates the motor how I like, but when it comes to implementing the switch, I cannot seem to figure out where to connect the wires to get the two connected. I also have a sample code written that should record the switches motion but when I click the switch nothing happens.

If anyone could offer code to add that would make sure that when the button is pushed, the motor will rotate, and when it is lifted the motor stays still. Also, if anyone could offer pictures / an idea of how to connect the switch to the CNC shield it is much appreciated. As of now, the switch is plugged into the "Y+" pin on the shield, and the driver and motor are also plugged into the "Y" section of the shield.

Devices Used:

  • Arduino Uno
  • CNC Shield (V3)
  • Driver
  • Nema 17 (42mm) motor (1.8 degree)
  • X-Stop switch (Normally used in 3D printers)
#include <Stepper.h>

#define EN        8  

//Direction pin
#define X_DIR     5 
#define Y_DIR     6
#define Z_DIR     7

//Step pin
#define X_STP     2
#define Y_STP     3 
#define Z_STP     4 


int endstop_y; 

//DRV8825
int delayTime=30; //Delay between each pause (uS)
int stps=6400;// Steps to move
int endStop = 0;
int endstopPin = 10;

void step(boolean dir, byte dirPin, byte stepperPin, int steps)

{

  digitalWrite(dirPin, dir);

  delay(100);

  for (int i = 0; i < steps; i++) {

    digitalWrite(stepperPin, HIGH);

    delayMicroseconds(delayTime); 

    digitalWrite(stepperPin, LOW);

    delayMicroseconds(delayTime); 

  }

}

void setup(){
  Serial.begin(9600);

  pinMode(X_DIR, OUTPUT); pinMode(X_STP, OUTPUT);

  pinMode(Y_DIR, OUTPUT); pinMode(Y_STP, OUTPUT);

  pinMode(Z_DIR, OUTPUT); pinMode(Z_STP, OUTPUT);

  pinMode(EN, OUTPUT);

  digitalWrite(EN, LOW);

}

void loop()
{
endStop = digitalRead(endstopPin);
Serial.print("End Stop = ");
Serial.println(endStop);
delay(1000);

if(endStop == 1)
  {
    step(false, X_DIR, X_STP, stps); //X, Clockwise
    step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
    step(false, Z_DIR, Z_STP, stps); //Z, Clockwise
    delay(100);
    
    endStop--;
  }
} 
}

You may be able to adapt the code from the second sketch in Robin2's simple stepper code tutorial for your use. It uses 2 push buttons, one to make the the motor rotate clockwise and the other button to make the motor rotate counter clockwise when pressed.

I have the code working correctly that when a 1 appears the motor will rotate. My issue is more so the connections between the shield and the switch. I have followed pinout guides that say pin 10 is the one needed, but the recordings of if the switch is open or closed is messed up. It will go randomly between 0 and 1 and pushing down the switch has no effect on it.

You need to set the pinMode of the switch pin INPUT_PULLUP. Then connect one side of the switch to the black header at the Y- position and the other side of the switch to the white header at position Y-.

This is the aforementioned simple stepper code modified to use one switch to rotate a stepper. Tested and working on my Uno with CNC shield V3.

// testing a stepper motor with a Pololu A4988 driver board or equivalent

// this version uses millis() to manage timing rather than delay()


byte directionPin = 6;
byte stepPin = 3;
byte enablePin = 8;

byte buttonCWpin = 10;

boolean buttonCWpressed = false;

byte ledPin = 13;

unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 10; // milliseconds

void setup()
{

   Serial.begin(9600);
   Serial.println("Starting Stepper Demo with millis()");

   pinMode(directionPin, OUTPUT);
   pinMode(stepPin, OUTPUT);
   pinMode(ledPin, OUTPUT);
   pinMode(buttonCWpin, INPUT_PULLUP);
   pinMode(enablePin, OUTPUT);
   digitalWrite(enablePin, LOW);
}

void loop()
{

   curMillis = millis();
   readButtons();
   actOnButtons();

}

void readButtons()
{
   buttonCWpressed = false;

   if (digitalRead(buttonCWpin) == LOW)
   {
      buttonCWpressed = true;
   }
}

void actOnButtons()
{
   if (buttonCWpressed == true)
   {
      digitalWrite(directionPin, LOW);
      singleStep();
   }
}

void singleStep()
{
   if (curMillis - prevStepMillis >= millisBetweenSteps)
   {
      // next 2 lines changed 28 Nov 2018
      //prevStepMillis += millisBetweenSteps;
      prevStepMillis = curMillis;
      digitalWrite(stepPin, HIGH);
      digitalWrite(stepPin, LOW);
   }
}

If you have a 3 terminal limit switch wire the NO terminal to one of the Y- header pins (white) and the COM terminal to the other Y- pin (black).