2.16 Slide Switch

Share for us


In this lesson, you will get to know something about Switch. A slide switch, just as its name implies, is to slide the switch bar to connect or break the circuit, and further switch circuits. The slide switch is commonly used in low-voltage circuit. It has the features of flexibility and stability, and applies in electric instruments and electric toys widely.

Components Required

Component Introduction

How it works: Set the middle pin as the fixed one. When you pull the slide to the left, the two pins on the left are connected; when you pull it to the right, the two pins on the right are connected. Thus, it works as a switch connecting or disconnecting circuits. See the figure below:

The circuit symbol of the slide switch is shown as below. The pin2 in the figure refers to the middle pin.

Fritzing Circuit

In this example, digital pin 2 is used to read the signal of Switch. In addition, you need to connect a 10 kΩ resistor and a 104 capacitor in parallel to form a RC circuit (Resistor – Capacitance circuit) which is set between pin 2 and GND to realize debounce that may arise from your toggle of switch.

Schematic Diagram


void setup() {
  pinMode(2, INPUT);

void loop() {
  int switchState = digitalRead(2);

After the codes are uploaded to the Mega2560 board, you can open the serial monitor to check the readings of the pin. When the Switch toggles to the left, the serial monitor displays 「1」; when toggles to the right, the serial monitor displays「0」. Refer to Part 1-1.4 Digital Read to check the code explanation.

Phenomenon Picture