Arduino Nano - Switch

The toggle switch, also known as the ON/OFF switch, has two states: ON (closed) and OFF (open). When pressed, the switch will toggle between these two states, and the state will remain even when released.

This tutorial instructs you how to use Arduino Nano with the ON/OFF switch. In detail, we will learn:

Arduino Nano ON/OFF Switch

Do not get these two confused:

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
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 Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

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 that can be toggled between two states, ON and OFF, is known as an ON/OFF Switch. When it is pressed, the state of the switch changes from one to the other. The switch will remain in this state until it is pressed again.

The Switch Pinout

There are two varieties of the ON/OFF Switch: two-pin and three-pin.

In this tutorial, we will be using a two-pin switch. With this type, there is no need to differentiate between the two pins.

ON/OFF Switch pinout

How It Works

There are two methods of using an ON/OFF switch. The following is a wiring table for the ON/OFF switch and the reading state on Arduino Nano for both approaches:

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

We must select one of the two options. The remainder of the tutorial will use the first option.

Wiring Diagram

The wiring diagram between Arduino Nano and ON/OFF Switch

This image is created using Fritzing. Click to enlarge image

For a secure and reliable wiring connection, we suggest using a Soldering Iron to solder the wires and pins of the ON/OFF switch together. Then, use Heat Shrink Tube for added safety.

Arduino Nano Code - ON/OFF Switch

As a button, an ON/OFF switch also requires debouncing (for more information, please refer to Why needs debounce for the button, ON/OFF switch?). This increases the complexity of the code. Fortunately, the ezButton library supports the debouncing function and uses internal pull-up register, making programming easier.


There are two common applications:

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

Detailed Instructions

  • Do the wiring as per the wiring diagram.
  • Connect the Arduino Nano to your PC with a USB cable.
  • Open the Arduino IDE.
  • Install the ezButton library - refer to the instructions in the 'How To' section here.
  • Choose the correct board and port.
  • Hit the Upload button on the Arduino IDE to upload the code to the Arduino Nano.
  • Turn the switch to the ON position.
  • Check out the result on the Serial Monitor.
  • Then switch the switch to OFF.
  • Check out the result on the Serial Monitor.
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!