Arduino Mega - Slide Switch

Imagine an escape-room puzzle box with a row of small levers on the lid. The box opens only when every lever is set the right way. Or think of a greenhouse controller with a "manual / auto" lever, or a test rig with a "slow / fast" lever. Each lever is a slide switch: you push the handle to one side, and it stays there until you push it back. With 54 digital pins, the Arduino Mega has room for a lot of them. This tutorial instructs you how to use the Arduino Mega to read one DIYables Mini Slide Switch SS12D10 and report whether it is ON or OFF. In detail, we will learn:

Arduino Mega slide switch SS12D10 - print the ON/OFF state from pin 7

We suggest:

Hardware Preparation

1×Arduino MEGA
1×Alternatively, DIYables MEGA Development Board
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×Mini Slide Switch SS12D10
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega
1×Recommended: Breadboard Shield for Arduino Mega
1×Recommended: Enclosure for Arduino Mega

Or you can buy the following kits:

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 Slide Switch and Its Pinout

SS12D10 mini slide switch pinout - COM in the middle, pin 1 and pin 3 at the ends, and the two handle positions

The DIYables Mini Slide Switch SS12D10 is a small plastic block with a 5mm handle on top and three legs underneath. You push the handle toward one end, and it latches there. It does not spring back like a push button. So the Arduino Mega can read the same position again and again, until somebody moves the handle.

The contact type is SPDT, short for single pole, double throw. Some shops write it as 1P2T. Inside the body, a little metal bridge slides together with the handle. The bridge always rests on the middle leg, and it also reaches the outer leg on the handle's side. As a result, the middle leg is joined to exactly one outer leg at any time.

DIYables SS12D10
Positions 2 (latching)
Contact Type SPDT (1P2T)
Number of Pins 3, vertical through-hole
Pin Spacing 4.7mm
Pin Length 6.9mm
Handle 5mm
Body Dimensions about 12.8 x 6.7 x 18.2mm
Contact Material silver-plated brass
Contact Rating AC 125V 2A
Mechanical Life more than 100,000 switching cycles
Working Temperature -25°C to 80°C

The AC 125V 2A rating tells you the limit of the contacts, nothing more. In this project, the switch only carries a tiny signal current into one input pin of the Arduino Mega, far below that limit.

The three legs and their jobs in this tutorial:

  • Pin 1 (one end of the row): must be connected to GND (0V)
  • Pin 2, COM (the middle leg): must be connected to a digital input pin of the Arduino Mega. It passes on the voltage of whichever outer leg the bridge touches
  • Pin 3 (the other end of the row): must be connected to the 5V pin of the Arduino Mega for the 3-wire wiring. In the 2-wire wiring, this leg gets no wire

What each handle position does, and what the Arduino Mega reads with the 3-wire wiring:

Handle Position Joined Pins Open Pin Pin 7 Reads
Position 1 - toward pin 1 Pin 1 and COM Pin 3 LOW (OFF)
Position 2 - toward pin 3 COM and Pin 3 Pin 1 HIGH (ON)

※ NOTE THAT:

You will not find any marks next to the legs of the SS12D10. Find COM by its place: it is always the leg in the middle. Pin 1 and pin 3 look exactly the same. In this tutorial, pin 1 is simply the outer leg that gets the GND wire. Swapping the two outer wires does no harm - only the ON side and the OFF side of the handle change places.

Wiring Diagram

Three jumper wires are enough. The middle leg (COM) goes to digital pin 7, pin 1 goes to one of the GND pins, and pin 3 goes to the 5V pin of the Arduino Mega. Since COM always touches either 5V or GND, pin 7 is never left floating, and no pull-up or pull-down resistor is needed.

The wiring diagram between Arduino Mega slide switch

This image is created using Fritzing. Click to enlarge image

Slide Switch Pin Arduino Mega Pin
Pin 1 GND
Pin 2 (COM) Pin 7
Pin 3 5V

※ NOTE THAT:

  • The Arduino Mega is a 5V board: its ATmega2560 chip works with 5V logic. That is why pin 3 of the switch goes to 5V. The 3.3V pin of the Mega is not used here.
  • Leave pin 7 as an INPUT in every sketch you upload. Never set it to OUTPUT: the switch connects the pin straight to 5V or GND, and an output that drives the opposite level would cause a short circuit and could damage the pin.

How To Program For Slide Switch

A slide switch needs no library. The built-in functions of the Arduino IDE are enough.

  • The first step is to give digital pin 7 a clear name, so the code shows what is wired there:
#define SLIDE_SWITCH_PIN 7
  • Next, make that pin an input inside setup() with pinMode(). Plain INPUT is the right mode, because the switch drives the pin HIGH or LOW by itself:
pinMode(SLIDE_SWITCH_PIN, INPUT);
  • Read the level with digitalRead(). HIGH means the handle is on the pin 3 side, LOW means it is on the pin 1 side:
int switchState = digitalRead(SLIDE_SWITCH_PIN);
  • While the handle moves, the metal contacts touch and let go several times within a few milliseconds. This is called bounce. To ignore it, compare each new reading with the last accepted one. When they differ, wait 50 milliseconds and read the pin again before you accept the change:
if (reading != confirmedState) { delay(50); reading = digitalRead(SLIDE_SWITCH_PIN); if (reading != confirmedState) confirmedState = reading; // the handle really moved }

Arduino Mega Code for Slide Switch - Print ON/OFF on Each Move

This sketch has two jobs. In setup(), it reports where the handle is when the board starts. In loop(), it keeps an eye on pin 7 and prints a new line only after the handle has really moved and passed the 50 ms check.

/* * 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-slide-switch */ #define SLIDE_SWITCH_PIN 7 // The Arduino Mega pin wired to the middle pin (COM) of the slide switch #define SETTLE_TIME 50 // milliseconds to wait while the metal contacts stop bouncing int confirmedState; // the last position that the Arduino Mega accepted as real // Write the position of the handle to the Serial Monitor void showState(int state) { if (state == HIGH) // handle toward pin 3: COM passes on 5V Serial.println("The slide switch is ON"); else // handle toward pin 1: COM passes on GND Serial.println("The slide switch is OFF"); } void setup() { Serial.begin(9600); pinMode(SLIDE_SWITCH_PIN, INPUT); // plain INPUT - the switch itself ties pin 7 to 5V or GND Serial.println("Slide switch on pin 7 - ready"); confirmedState = digitalRead(SLIDE_SWITCH_PIN); // the handle can be on either side at power-up showState(confirmedState); } void loop() { int reading = digitalRead(SLIDE_SWITCH_PIN); if (reading != confirmedState) { // the level changed - a real move or just a bounce? delay(SETTLE_TIME); // give the contacts time to settle reading = digitalRead(SLIDE_SWITCH_PIN); // look at the pin a second time if (reading != confirmedState) { // still different, so the handle really moved confirmedState = reading; // remember the new position showState(confirmedState); } } }

Detailed Instructions

  • New to the Arduino Mega? First, follow how to set up the Arduino IDE for the Arduino Mega.
  • Wire the components as shown in the diagram.
  • Connect the Arduino Mega board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Pick Arduino Mega or Mega 2560 as the board, then pick the COM port of the Mega.
  • Copy the code above and paste it into an empty sketch.
  • Click the Upload button.
  • Push the handle toward pin 3, then open the Serial Monitor at 9600 baud. If the first two lines do not appear, press the RESET button on the Mega.
  • Slide the handle toward pin 1, toward pin 3, toward pin 1, and toward pin 3 again.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' on 'COM15')
New Line
9600 baud
Slide switch on pin 7 - ready The slide switch is ON The slide switch is OFF The slide switch is ON The slide switch is OFF The slide switch is ON
Ln 11, Col 1
Arduino Mega Or... on COM15
2

The second line is the start-up position. After that, four moves give exactly four lines. Would you like the pin 1 side to be ON? Swap the GND and 5V wires on the two outer legs - the code stays the same.

Does the Serial Monitor jump between ON and OFF while you do not touch the switch? Then the GND wire or the 5V wire is loose, and pin 7 is floating. Push both wires in firmly.

Arduino Mega Code for Slide Switch - Two Wires with INPUT_PULLUP

Every digital pin of the ATmega2560 has a pull-up resistor inside the chip. With INPUT_PULLUP, that resistor holds pin 7 at HIGH whenever COM is not joined to anything. So the 5V wire on pin 3 can go. This helps when the switch sits on a front panel far away from the board, or when the 5V pin already feeds other modules.

The wiring diagram between Arduino Mega slide switch INPUT_PULLUP

This image is created using Fritzing. Click to enlarge image

Slide Switch Pin Arduino Mega Pin
Pin 1 GND
Pin 2 (COM) Pin 7
Pin 3 not connected

Be careful: with this wiring, the logic is the opposite of the first sketch.

Handle Position Pin 7 Reads The Code Prints
Toward pin 1 (COM joined to GND) LOW ON
Toward pin 3 (COM open, pulled up) HIGH OFF

In short, LOW means ON when you use INPUT_PULLUP.

/* * 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-slide-switch */ // 2-wire wiring: COM (pin 2) -> Arduino Mega pin 7, pin 1 -> GND, pin 3 -> no wire. // INPUT_PULLUP switches on the pull-up resistor inside the ATmega2560. // It keeps pin 7 HIGH whenever COM is not joined to anything, so the 5V wire is not needed. // Because of this, the logic is the opposite of the 3-wire sketch: // LOW = handle toward pin 1 (COM joined to GND) = ON // HIGH = handle toward pin 3 (COM open, pull-up holds it) = OFF #define SLIDE_SWITCH_PIN 7 // The Arduino Mega pin wired to the middle pin (COM) of the slide switch #define SETTLE_TIME 50 // milliseconds to wait while the metal contacts stop bouncing int confirmedState; // the last position that the Arduino Mega accepted as real // Write the position of the handle to the Serial Monitor (inverted logic) void showState(int state) { if (state == LOW) // handle toward pin 1: COM pulls pin 7 down to GND Serial.println("The slide switch is ON"); else // handle toward pin 3: nothing on COM, the pull-up wins Serial.println("The slide switch is OFF"); } void setup() { Serial.begin(9600); pinMode(SLIDE_SWITCH_PIN, INPUT_PULLUP); // turn on the internal pull-up, so pin 7 never floats Serial.println("Slide switch on pin 7 (INPUT_PULLUP) - ready"); confirmedState = digitalRead(SLIDE_SWITCH_PIN); // the handle can be on either side at power-up showState(confirmedState); } void loop() { int reading = digitalRead(SLIDE_SWITCH_PIN); if (reading != confirmedState) { // the level changed - a real move or just a bounce? delay(SETTLE_TIME); // give the contacts time to settle reading = digitalRead(SLIDE_SWITCH_PIN); // look at the pin a second time if (reading != confirmedState) { // still different, so the handle really moved confirmedState = reading; // remember the new position showState(confirmedState); } } }

Detailed Instructions

  • Take the wire off pin 3 of the switch. The COM wire and the GND wire stay where they are.
  • Paste the code above into the Arduino IDE in place of the first sketch, and click Upload.
  • Open the Serial Monitor. The handle is still on the pin 3 side, so the first state line now says OFF.
  • Move the handle toward pin 1, toward pin 3, and toward pin 1 once more.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' on 'COM15')
New Line
9600 baud
Slide switch on pin 7 (INPUT_PULLUP) - ready The slide switch is OFF The slide switch is ON The slide switch is OFF The slide switch is ON
Ln 11, Col 1
Arduino Mega Or... on COM15
2

The same handle position that gave ON in the 3-wire test now gives OFF. Nothing is wrong - this is the flipped logic of INPUT_PULLUP.

Additional Knowledge

3 Wires or 2 Wires?

3 Wires (INPUT) 2 Wires (INPUT_PULLUP)
Wires to the switch COM, pin 1 and pin 3 COM and pin 1 only
Pin mode in the code INPUT INPUT_PULLUP
Handle toward pin 3 HIGH = ON HIGH = OFF
Handle toward pin 1 LOW = OFF LOW = ON
What holds pin 7 HIGH the 5V pin, through the switch the weak resistor inside the chip
5V pin of the Mega used by the switch free for other parts

It is evident that the 3-wire wiring gives the strongest signal and the more natural logic (HIGH = ON), while the 2-wire wiring saves one wire and keeps the 5V pin free. For short wires on a desk, both work equally well.

Many Slide Switches on One Arduino Mega

Each extra slide switch needs only its own input pin. All switches can share the GND wire, and in the 3-wire wiring also the 5V wire. For example, eight switches on pins 22 to 29 can set an 8-bit number from 0 to 255, just like a DIP switch, but with bigger handles that are easier to move by hand. For the escape-room box from the start of this page, the sketch would simply check that every pin reads the expected level before it opens the lock.

If you need a key that returns by itself after each press, use a push button instead of a slide switch.

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!