Arduino Mega - Switch

In this guide, we will learn how to use the on/off switch with an Arduino Mega. Specifically, we will cover:

Arduino Mega ON/OFF Switch

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Wires
1×ON/OFF Square Switch
1×Alternatively, ON/OFF Round Switch
1×Optionally, Heat Shrink Tubing
1×Optionally, Soldering Iron

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

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

Pinout

There are two common kinds 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, you don’t need to tell the two pins apart.

ON/OFF Switch Pinout

How It Works

Here are two easy ways to operate an ON/OFF switch. Below is the wiring guide for the switch and the readings you will see on the Arduino Mega for each method:

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

We only need to choose one of the two methods. The rest of this guide will use the first method.

Wiring Diagram

The wiring diagram between Arduino Mega ON/OFF Switch

This image is created using Fritzing. Click to enlarge image

We recommend using a soldering iron to securely solder the wires and the ON/OFF switch pin. Then cover them with heat shrink tubing to keep things safe.

Arduino Mega Code - ON/OFF Switch

Just like a button, an ON/OFF switch also needs debouncing (Learn more at https://arduinogetstarted.com/faq/why-needs-debounce-for-button). Debouncing can make the code harder. Luckily, the ezButton library (https://arduinogetstarted.com/tutorials/arduino-button-library) includes a debouncing feature and uses an internal pull-up resistor, which makes programming easier.

※ NOTE THAT:

There are two common situations:

1) If the switch is ON, do something. If it's OFF, do the opposite.

2) If the switch changes from ON to OFF or from OFF to ON, do something.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-switch */ #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 steps one by one:

  • Connect the ON/OFF switch to the Arduino Mega as shown in the diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • In the IDE, select the board Arduino Mega and the correct serial port.
  • Install the ezButton library. Follow the guide here: https://arduinogetstarted.com/tutorials/arduino-button-library#content_how_to_install_library
  • Make sure the correct board and port are selected in the IDE.
  • Click the Upload button in the Arduino IDE to send the code to your Arduino Mega.
  • Turn the switch to ON.
  • Check the Serial Monitor for the output.
  • Turn the switch to OFF.
  • Check the Serial Monitor again for the output.
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!