Arduino Nano 33 IoT - Switch

An ON/OFF, or toggle, switch only has two positions: ON (closed) and OFF (open). Every time you press the switch, it changes from one position to the other and stays that way even after you let go. This lesson will show you how to use an ON/OFF switch with an Arduino Nano 33 IoT microcontroller.

Arduino Nano 33 IoT ON/OFF Switch

Remember, the ON/OFF switch is different from the items listed below. Do not mix them up.

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Wires
1×ON/OFF Square Switch
1×Alternatively, ON/OFF Round Switch
1×Optionally, DC Power Jack
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter 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.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of ON/OFF Switch

An ON/OFF Switch is a device that switches from on to off (or off to on) when you press it. It stays in that position even after you let go. To change it again, you need to press it once more.

Pinout

Usually, there are two kinds of ON/OFF switches: one with two pins and one with three pins. This guide will use the two-pin switch, because you don’t need to worry about the difference between the pins.

ON/OFF Switch Pinout

How It Works

There are two ways to use an ON/OFF switch. The table below shows the wiring and the state readings on an Arduino Nano 33 IoT for each way.

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

Between the two options, this guide will only cover one. We'll use the first method as an example.

Wiring Diagram

The wiring diagram between Arduino Nano and 33 IoT ON/OFF Switch

This image is created using Fritzing. Click to enlarge image

To keep the wiring connection strong and steady, we suggest soldering the wires to the ON/OFF switch pin and then securing them for safety.

For a strong and safe connection, it’s best to use a soldering iron to join the wires and the ON/OFF switch pins. Then, cover them with heat shrink tubes for extra protection.

※ NOTE THAT:

Please note that the Arduino Nano 33 IoT pins A4 and A5 have built-in pull-up resistors for I2C communication. Although these pins can be used as digital input pins, it is recommended to avoid using them for digital input. If you must use them, do not use internal or external pull-down resistors for these pins

Arduino Nano 33 IoT Code - ON/OFF Switch

Just like a button, an ON/OFF switch needs help to avoid noise when switching (see more at ). This extra work, called debouncing, can make the code more complex. Luckily, the library already includes a debouncing function and uses an internal pull-up register. These features make programming easier for us.

Just like a normal button, an ON/OFF switch also needs debouncing (you can learn more at Whe needs debouce for button. Debouncing can make the code a little more complicated. Luckily, the ezButton library has built-in debouncing and uses internal pull-up registers, which makes programming much easier.

※ NOTE THAT:

Two common ways to use an ON/OFF switch are:

  • First, if the switch is ON, do one action. If it is OFF, do the opposite.
  • Second, when the switch changes from ON to OFF (or from OFF to ON), do a special action.
/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-switch */ #include <ezButton.h> ezButton switch(2); // create ezButton object that attach to the Arduino Nano 33 IoT 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

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Use the wiring diagram above to connect the ON/OFF switch to your Arduino Nano 33 IoT.
  • Attach your Arduino Nano 33 IoT to your computer with a USB cable.
  • Open the Arduino program on your computer.
  • Install the ezButton library by following these instructions
  • In the Arduino program, choose the correct board and port for your device.
  • Click the Upload button to compile and send your code to the Arduino Nano 33 IoT.
  • Turn the switch ON to test it.
  • Look at the Serial Monitor in the Arduino program to see the output.
  • Turn the switch OFF.
  • Check the output again in the Serial Monitor in the Arduino program.
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

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