Arduino Mega - Limit Switch

This guide shows you how to use a limit switch with the Arduino Mega. Here’s what we will learn:

Arduino Mega with Limit Switch

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Limit Switch (KW12-3)
1×Limit Switch (V-153-1C25)
1×Limit Switch (V-155-1C25)
1×Limit Switch (V-156-1C25)
1×Wires
1×Optionally, Heat Shrink Tubing
1×Optionally, Soldering Iron

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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.
Limit Switch Pinout
image source: diyables.io

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

The wiring diagram between Arduino Mega limit switch

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.
/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-limit-switch */ #include <ezButton.h> ezButton limitSwitch(7); // create ezButton object that attach to pin 7; void setup() { Serial.begin(9600); limitSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { limitSwitch.loop(); // MUST call the loop() function first if(limitSwitch.isPressed()) Serial.println("The limit switch: UNTOUCHED -> TOUCHED"); if(limitSwitch.isReleased()) Serial.println("The limit switch: TOUCHED -> UNTOUCHED"); int state = limitSwitch.getState(); if(state == HIGH) Serial.println("The limit switch: UNTOUCHED"); else Serial.println("The limit switch: TOUCHED"); }

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.
COM6
Send
The limit switch: UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED -> TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED -> UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!