No title


快速记录一下 如何使用limit switch

[Arduino - Limit Switch | Arduino Tutorial](https://arduinogetstarted.com/tutorials/arduino-limit-switch

Arduino with Limit Switch

About Limit Switch

It is called Limit Switch because its main functionality is used to detect the moving object reaching a limit.

Pinout

There are various types of limit switches available, but among the most commonly used are the KW12-3 and V-156-1C25. Both of these types have 3 pins:

  • C pin: is the common pin. It is used in both normally open mode and normally closed mode

  • NO pin: is normally open pin. It is used in the normally open mode 通过测量,默认线路不导通(高电平),按压时线路导通(低电平)

  • NC pin: is normally closed pin. It is used in the normally closed mode

Limit Switch Pinout

image source: diyables.io

How It Works

Although The Limit Switch has 3 pins, a normal application usually uses only two pins: C pin and one of two remaining pins. Accordingly, there are four ways to use limit switch. The below is the wiring table for limit switch and the reading state on Arduino in all four ways:

C pin NO pin NC pin Arduino Input Pin’s State
1 GND Arduino Input Pin (with pull-up) not connected HIGH when untouched, LOW when touched
2 GND not connected Arduino Input Pin (with pull-up) LOW when untouched, HIGH when touched
3 VCC Arduino Input Pin (with pull-down) not connected LOW when untouched, HIGH when touched
4 VCC not connected Arduino Input Pin (with pull-down) HIGH when untouched, LOW when touched

For each way, we can swap GND pin and Arduino Input Pin. Therefore, we have 8 way to connect Arduino to a limit switch.

We only need to choose one of the above four ways. The rest of tutorial will use the first way.

Wiring Diagram

Arduino Limit Switch Wiring Diagram

This image is created using Fritzing. Click to enlarge image

To make the wiring connection stable and firm, we recommend using Soldering Iron to solder wires and limit switch’s pin together, and then use Heat Shrink Tube to make it safe.

Arduino Code - Limit Switch

连接方式1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const int limitSwitchPin = 2;  // Connect limit switch to pin 2
const int ledPin = 13; // Built-in LED for visual feedback

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

// Configure limit switch pin with internal pull-up resistor
pinMode(limitSwitchPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);

Serial.println("Limit Switch Test");
Serial.println("Normal State (Not Pressed) = HIGH");
Serial.println("Triggered State (Pressed) = LOW");
Serial.println("-------------------");
}

void loop() {
// Read the state of the limit switch
int switchState = digitalRead(limitSwitchPin);

// Print the actual digital read value and human-readable state
Serial.print("Digital Read Value: ");
Serial.print(switchState);
Serial.print(" | Switch is: ");

if (switchState == HIGH) {
Serial.println("NOT PRESSED (Normal State)");
digitalWrite(ledPin, LOW);
} else {
Serial.println("PRESSED! (Triggered)");
digitalWrite(ledPin, HIGH);
}

delay(200); // Small delay to make output readable
}

对于的步进电机切换

–直接使用高低电平切换有一定概率失败

  1. Switch Bounce Issue:
  • When a mechanical switch is pressed or released, the metal contacts don’t make/break cleanly
  • They actually “bounce” several times over a few milliseconds
  • Original code saw these bounces as multiple triggers
  • Solution: Added debouncing with debounceDelay to wait for the switch to settle
  1. Time-Based State Management:
    1
    2
    3
    if ((millis() - lastDebounceTime) > debounceDelay) {
    // Check state change only after debounce period
    }
  • Keeps track of last trigger time
  • Only accepts new triggers after sufficient time has passed
  • Prevents rapid, unwanted direction changes
  1. State Tracking:
    1
    2
    bool lastSwitchState = HIGH;  // Previous reading
    bool switchState = HIGH; // Debounced state
  • Maintains both immediate and debounced states
  • Only changes direction when debounced state changes
  • Provides cleaner, more reliable state transitions
  1. Direction Change Reliability:
    1
    2
    3
    4
    5
    if (switchState == LOW) {
    moveClockwise = !moveClockwise;
    delay(100); // Brief pause
    digitalWrite(dirPin, moveClockwise ? HIGH : LOW);
    }
  • Added delay before direction change
  • Gives stepper driver time to register new direction
  • Reduces chance of missed direction changes

The original code failed because:

  1. No debouncing → multiple false triggers
  2. No state tracking → missed some real triggers
  3. No delay on direction change → possible timing issues with stepper driver

The improved code succeeds because it:

  1. Filters out false triggers
  2. Properly tracks switch state changes
  3. Ensures reliable direction changes
  4. Provides debug feedback for troubleshooting

Would you like me to explain any part of this in more detail?