No title
No title
Bin Lian快速记录一下 如何使用limit switch
[Arduino - Limit Switch | Arduino Tutorial](https://arduinogetstarted.com/tutorials/arduino-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
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
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 | const int limitSwitchPin = 2; // Connect limit switch to pin 2 |
对于的步进电机切换
–直接使用高低电平切换有一定概率失败
- 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
debounceDelayto wait for the switch to settle
- Time-Based State Management:
1
2
3if ((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
- State Tracking:
1
2bool 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
- Direction Change Reliability:
1
2
3
4
5if (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:
- No debouncing → multiple false triggers
- No state tracking → missed some real triggers
- No delay on direction change → possible timing issues with stepper driver
The improved code succeeds because it:
- Filters out false triggers
- Properly tracks switch state changes
- Ensures reliable direction changes
- Provides debug feedback for troubleshooting
Would you like me to explain any part of this in more detail?















