Arduino UNO R4 - Limit Switch

This tutorial instructs you how to use the limit switch with Arduino UNO R4. In detail, we will learn:

Arduino UNO R4 with Limit Switch

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×Limit Switch (KW12-3)
1×Limit Switch (V-156-1C25)
1×Wires
1×(Optional) Heat Shrink Tubing
1×(Optional) Soldering Iron
1×(Recommended) Screw Terminal Block Shield for Arduino UNO R4
1×(Recommended) Breadboard Shield For Arduino UNO R4
1×(Recommended) Enclosure For Arduino UNO R4
1×(Recommended) Power Splitter For Arduino UNO R4

Or you can buy the following sensor 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

It is named Limit Switch because it mainly functions to detect when a moving object reaches a limit. It is also called travel switch.

Pinout

Several kinds of limit switches are used frequently, including the KW12-3 and V-156-1C25. Both models have 3 pins.

  • C pin: This is the common pin. It is used in both the normally open mode and the normally closed mode.
  • NO pin: This stands for Normally Open pin. It is used in the normally open mode.
  • NC pin: This stands for Normally Closed pin. It is used in the normally closed mode.
Limit Switch Pinout
image source: diyables.io

How It Works

The limit switch has three pins, but typically only two pins are used: the C pin and one of the other two pins. There are four different ways to connect the limit switch. Here is a table showing how to wire the limit switch and how it reads on the Arduino UNO R4 for all four methods:

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

We can swap between the GND pin and the Arduino UNO R4 input pin for each method. This gives us 8 ways to connect the Arduino UNO R4 to a limit switch.

We just need to pick one from the four methods mentioned above. We will use the first method for the rest of this tutorial.

Wiring Diagram

The wiring diagram between Arduino UNO R4 limit switch

This image is created using Fritzing. Click to enlarge image

Arduino UNO R4 Code - Limit Switch

Just like a button, a limit switch also needs debouncing (Learn more at Why debouncing is needed for the button/limit switch?). Debouncing can complicate the code. Luckily, the ezButton library includes a debouncing feature and uses an internal pull-up resistor, making programming simpler for us.

※ NOTE THAT:

Here are two common scenarios:

  • The first: When the switch is in the TOUCHED state, perform an action. When it is in the UNTOUCHED state, do the opposite action.
  • The second: When the switch changes state from UNTOUCHED to TOUCHED or from TOUCHED to UNTOUCHED, perform an action.
/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-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 instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the limit switch to the Arduino Uno R4 according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Install the ezButton library using this guide: See ezButton Library Guide.
  • Press the Upload button in the Arduino IDE to transfer the code to the Arduino UNO R4.
  • Press and release the limit switch.
  • Check the results on 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!