Arduino Mega - Limit Switch
This guide shows you how to use a limit switch with the Arduino Mega. Here’s what we will learn:
- How to hook up a limit switch to the Arduino Mega.
- How to program the Arduino Mega to read the limit switch state.
- How to program the Arduino Mega to detect events from the limit switch.

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (30 sensors/displays) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Limit Switch
This is called a limit switch because its main job is to detect when something that moves reaches a limit. It is also called a travel switch.
Pinout
Many types of limit switches are used a lot, including KW12-3 and V-156-1C25. Both have three pins.
- C pin: The common pin. It works in both normally open and normally closed modes.
- NO pin: The normally open pin. It is used when the switch is in the normally open position.
- NC pin: The normally closed pin. It is used when the switch is in the normally closed position.

How It Works
The limit switch has three pins, but usually you use only two: the C pin and one of the other two pins. There are four different ways to connect the limit switch. Here is a table that shows how to connect the switch and what it reads on the Arduino Mega for all four ways:
| C pin | NO pin | NC pin | Arduino Mega Input Pin's State | |
|---|---|---|---|---|
| 1 | GND | Arduino Mega Input Pin (with pull-up) | NOT connected | HIGH when untouched, LOW when touched |
| 2 | GND | NOT connected | Arduino Mega Input Pin (with pull-up) | LOW when untouched, HIGH when touched |
| 3 | VCC | Arduino Mega Input Pin (with pull-down) | NOT connected | LOW when untouched, HIGH when touched |
| 4 | VCC | NOT connected | Arduino Mega Input Pin (with pull-down) | HIGH when untouched, LOW when touched |
We can switch the ground pin and the Arduino Mega's input pin for each method. This gives us eight ways to connect the Arduino Mega to a limit switch.
We only need to choose one of the four methods mentioned above. We'll use the first method for the rest of this tutorial.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Arduino Mega Code - Limit Switch
Like a button, a limit switch also needs debouncing (Learn more at Why debouncing is needed for the button/limit switch?). Debouncing can make the code harder. Luckily, the ezButton library has a debouncing feature and uses a built-in pull-up resistor, which makes programming easier for us.
※ NOTE THAT:
Here are two common cases:
- Case 1: When the switch is ON, do one thing. When it is OFF, do the other thing.
- Case 2: When the switch changes from OFF to ON or from ON to OFF, do something.
Detailed Instructions
Follow these steps one by one.
- Connect the limit switch to the Arduino Mega as shown in the diagram.
- Connect the Arduino Mega to your computer with a USB cable.
- Open the Arduino IDE on your computer.
- Choose the correct Arduino Mega board (for example Arduino Mega) and the COM port.
- Install the ezButton library by following this guide: ezButton Library Guide (https://arduinogetstarted.com/tutorials/arduino-button-library#content_how_to_install_library)
- Click the Upload button in the Arduino IDE to upload the code to the Arduino Mega.
- Press and release the limit switch.
- Check the results in the Serial Monitor.