Mission 13 · Stage 2

Security Switch

switch state vs momentary button

Use a slide switch to toggle an LED on and off.

Security Switch circuit diagram

Pin connections

Part 1Part 2

Slide switch

pin 2

Arduino

pin 2

Slide switch

pin 1

Arduino

GND

Arduino

pin 13

Resistor

pin 1

Resistor

pin 2

LED

anode (+)

LED

cathode (-)

Arduino

GND

See it

A switch stays where you leave it!

Unlike a momentary button, the slide switch keeps its ON or OFF state.

Light switches on walls are latching — they hold their position.

The story

The problem

A push button only signals while pressed — sometimes you need a lasting state.

Think of it like

A button is a doorbell; a switch is a light switch on the wall.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Stays ON or OFF

Slide switch

Input

Loading part…

Protects the LED

Resistor

Safety

Loading part…

Shows switch position

LED

Output

Loading part…

How it works

1

Read switch position

digitalRead works the same — but the switch stays LOW or HIGH until you move it.

bool active = (digitalRead(SWITCH_PIN) == LOW);
2

Show state on LED

LED stays on while the switch is ON — no need to keep holding anything.

digitalWrite(LED_PIN, active ? HIGH : LOW);

Then loop back to step 1

Build the circuit

Follow these steps in order. Match the wires to the colors shown.

  1. 1

    Place Arduino

    Place the Arduino (uno) on the breadboard.

    Arduino placed!

    Loading part…
  2. 2

    Place Slide switch

    Place the Slide switch (sw1) on the breadboard.

    Loading part…
  3. 3

    Place Resistor

    Place the Resistor (r1) on the breadboard.

    Loading part…
  4. 4

    Place LED

    Place the LED (led1) on the breadboard.

    Loading part…
  5. 5

    Connect Slide switch (sw1) 2 to Arduino pin 2

    Connect Slide switch (sw1) 2 to Arduino pin 2.

  6. 6

    Connect Slide switch (sw1) 1 to Arduino GND

    Connect Slide switch (sw1) 1 to Arduino GND.

  7. 7

    Connect Arduino pin 13 to Resistor (r1) 1

    Connect Arduino pin 13 to Resistor (r1) 1.

  8. 8

    Connect Resistor (r1) 2 to LED (led1) anode (+)

    Connect Resistor (r1) 2 to LED (led1) anode (+).

  9. 9

    Connect LED (led1) cathode (-) to Arduino GND

    Connect LED (led1) cathode (-) to Arduino GND.

Try it

  • Flip the switch — LED should stay on or off.
  • Compare with pushbutton: no need to hold it!

Peek at code

Switch pin setup

void setup() {
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Slide Switch Demo");
}

Same INPUT_PULLUP pattern as a button — the part behaves differently in hardware.

Read and mirror

void loop() {
  bool active = (digitalRead(SWITCH_PIN) == LOW);
  digitalWrite(LED_PIN, active ? HIGH : LOW);
  Serial.println(active ? "Switch ON  -> LED ON" : "Switch OFF -> LED OFF");
  delay(100);
}

loop() keeps checking the switch; the LED reflects the latched position.

Show full sketch (slide-switch.ino)
const int SWITCH_PIN = 2;
const int LED_PIN    = 13;
void setup() {
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Slide Switch Demo");
}
void loop() {
  bool active = (digitalRead(SWITCH_PIN) == LOW);
  digitalWrite(LED_PIN, active ? HIGH : LOW);
  Serial.println(active ? "Switch ON  -> LED ON" : "Switch OFF -> LED OFF");
  delay(100);
}

Quick quiz

Q1. Where does repeating work belong?

  • A. loop()
  • B. setup()
  • C. pinMode only
Why: Correct—loop() runs again and again.

Q2. How is a slide switch different from a push button?

  • A. It keeps its position until you move it
  • B. It only works in setup()
  • C. It does not use digitalRead()
Why: Correct — a switch holds ON or OFF; a button returns when released.

Code lab — try on your own

  1. Change delay(100) to delay(200) in loop().

    Hint: Last line of loop().

  2. Add a comment on line 10: // switch ON when LOW

    Hint: The active = digitalRead line.

Code walkthrough

A line-by-line tour of the sketch — the same steps as in Robo Gurukul Studio.

Program overview

Technical

Sketches have globals, then setup() once, then loop() forever.

In this project

Use a slide switch to toggle an LED on and off.

Why here

Read from top to bottom. Hover words or lines for help!

const int SWITCH_PIN = 2;
const int LED_PIN    = 13;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Slide Switch.

Why here

One-time setup belongs here—not in loop().

void setup() {
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Slide Switch Demo");
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Slide Switch.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  bool active = (digitalRead(SWITCH_PIN) == LOW);
  digitalWrite(LED_PIN, active ? HIGH : LOW);
  Serial.println(active ? "Switch ON  -> LED ON" : "Switch OFF -> LED OFF");
  delay(100);
}

Try this: Change numbers in loop(), then compile and run the simulator.

pinMode

Technical

Tells a pin if it listens or drives something.

In this project

Gets the Slide Switch circuit ready in the simulator.

Why here

Goes in setup() because we only set pins once at the start.

  pinMode(SWITCH_PIN, INPUT_PULLUP);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in Slide Switch.

Why here

Goes in loop() so it can keep changing while the program runs.

  digitalWrite(LED_PIN, active ? HIGH : LOW);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Slide Switch.

Why here

Goes in loop() so we can react when something changes.

  bool active = (digitalRead(SWITCH_PIN) == LOW);

delay

Technical

Waits for some time. Nothing else runs during the wait.

In this project

Controls speed so you can see Slide Switch in the simulator.

Why here

Right after an action that should stay the same for a moment.

  delay(100);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Slide Switch print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for Slide Switch.

Why here

In loop() when each reading should appear on its own line.

  Serial.println("Slide Switch Demo");