Arduino UNO R4 - Switch

In this tutorial, we will learn how to use the ON/OFF switch with an Arduino UNO R4. In detail, we will learn:

Arduino UNO R4 ON/OFF Switch

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×ON/OFF Square Switch
1×(Alternative) ON/OFF Round Switch
1×(Optional) Heat Shrink Tubing
1×(Optional) Soldering Iron
1×(Optional) 9V Power Adapter for Arduino UNO R4
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

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. We appreciate your support.

Overview of ON/OFF Switch

A switch labeled ON/OFF changes from ON to OFF, or from OFF to ON, when toggled. It stays in the new state when you let go. Press it again to change the state back.


There are two main types of ON/OFF switches: the two-pin switch and the three-pin switch.

In this guide, we will use a two-pin switch. For this switch, it is not necessary to differentiate between the two pins.

ON/OFF Switch Pinout

How It Works

Here are two methods to operate an ON/OFF switch. Below is the wiring guide for the ON/OFF switch and the corresponding status readings on the Arduino UNO R4 for each method:

pin 1 pin 2 Arduino UNO R4 Input Pin's State
1 GND Arduino UNO R4 Input Pin (with pull-up) HIGH OFF, LOW ON
2 VCC Arduino UNO R4 Input Pin (with pull-down) HIGH ON, LOW OFF

We just need to pick one of the two methods. The rest of the guide will follow the first method.

Wiring Diagram

The wiring diagram between Arduino UNO R4 ON/OFF Switch

This image is created using Fritzing. Click to enlarge image

We suggest using a Soldering Iron to solder the wires and the ON/OFF switch pin securely. Then, cover them with Heat Shrink Tube for safety.

Arduino UNO R4 Code - ON/OFF Switch

Like a button, an ON/OFF switch also needs debouncing (Learn more at Why do buttons and ON/OFF switches need debouncing?). Debouncing can complicate the code. Thankfully, the ezButton library includes a debouncing feature and uses an internal pull-up resistor, simplifying the programming process.


There are two common scenarios:

  • First: If the switch is ON, perform an action. If the switch is OFF, do the opposite action.
  • Second: If the switch changes from ON to OFF (or OFF to ON), perform an action.
/* * This Arduino UNO R4 code was developed by * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * */ #include <ezButton.h> ezButton toggleSwitch(7); // create ezButton object that attach to pin 7; void setup() { Serial.begin(9600); toggleSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { toggleSwitch.loop(); // MUST call the loop() function first if (toggleSwitch.isPressed()) Serial.println("The switch: OFF -> ON"); if (toggleSwitch.isReleased()) Serial.println("The switch: ON -> OFF"); int state = toggleSwitch.getState(); if (state == HIGH) Serial.println("The switch: OFF"); else Serial.println("The switch: ON"); }

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 ON/OFF 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. Follow the guide here.
  • Choose the correct board and port in the IDE.
  • Press the Upload button in the Arduino IDE to transfer the code to your Arduino UNO R4.
  • Turn the switch to the ON position.
  • Check the Serial Monitor for the output.
  • Then turn the switch to the OFF position.
  • Check the Serial Monitor again for the output.
The switch: OFF The switch: OFF The switch: OFF The switch: OFF -> ON The switch: ON The switch: ON The switch: ON The switch: ON The switch: ON The switch: ON The switch: ON -> OFF The switch: OFF The switch: OFF The switch: OFF
Autoscroll Show timestamp
Clear output
9600 baud  

Video Tutorial


  • 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!