Arduino Nano ESP32 - Switch

An ON/OFF or toggle switch has two states: ON (closed) and OFF (open). Each time the switch is pressed, its state is toggled between ON and OFF and it remains in that state even when released. This tutorial will guide you on how to use an ON/OFF switch with an Arduino Nano ESP32 microcontroller.

Arduino Nano ESP32 ON/OFF Switch

It is important to note that the ON/OFF switch is distinct from the following and should not be confused with them:

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×Wires
1×ON/OFF Square Switch
1×(Alternative) ON/OFF Round Switch
1×(Optional) DC Power Jack
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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

An ON/OFF Switch is a switch that, when pressed, changes its state between ON and OFF, and then maintains that state even when released. To changes the state again, the switch must be pressed again.

Pinout

There are generally two types of ON/OFF Switches, the two-pin switch and the three-pin switch. This tutorial will focus on using the two-pin switch, where the distinction between the two pins is not necessary.

ON/OFF Switch Pinout

How It Works

There are two methods for using an ON/OFF switch. The table below illustrates the wiring and reading state on an Arduino Nano ESP32 for each of these two methods:

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

Out of the two methods, the tutorial will focus on one, specifically the first method will be used as an example.

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and ON/OFF Switch

This image is created using Fritzing. Click to enlarge image

To make the wiring connection stable and firm, we recommend using to solder wires and ON/OFF switch's pin together, and then use to make it safe.

For a stable and secure connection, it is recommended to use a soldering iron to solder the wires and ON/OFF Switch pins together, and then use heat shrink tubes for added safety.

Arduino Nano ESP32 Code - ON/OFF Switch

Just like a button, a ON/OFF switch also needs to be debounced (See more at ). Debouncing make the code complicated. Fortunately, the supports the debouncing functionm, The library also uses internal pull-up register. These make easy to us to program.

Similar to a button, an ON/OFF switch also requires debouncing (more information can be found at Why needs debounce for the button, ON/OFF switch?). Debouncing can make the code more complex. Fortunately, the ezButton library offers debouncing functionality and utilizes internal pull-up registers, making programming easier.

※ NOTE THAT:

Two common use cases for an ON/OFF switch are:

  • The first use case: If the switch is in the ON state, perform a certain action. If the input state is OFF, perform the opposite action.
  • The second use case: If the switch's state changes from ON to OFF (or OFF to ON), perform a specific action.
/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-switch */ #include <ezButton.h> ezButton switch(D2); // create ezButton object that attach to the Arduino Nano ESP32 pin D2 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

  • Follow the wiring diagram provided above to connect the ON/OFF switch to Arduino Nano ESP32.
  • Connect your Arduino Nano ESP32 to your PC using a USB cable.
  • Open the Arduino IDE software.
  • Install the ezButton library. Refer to the instructions
  • Select the appropriate board and port in the Arduino IDE.
  • Compile and upload the code to the Arduino Nano ESP32 board by clicking the Upload button in the Arduino IDE.
  • Test the ON/OFF switch by turning it ON.
  • Check out the output on the Serial Monitor in the Arduino IDE.
  • Turn the switch OFF.
  • Check out the output on the Serial Monitor in the Arduino IDE.
COM6
Send
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  
Newline  

Video Tutorial