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

Let's make a switch that remembers!

Slide it one way and the light stays on. Slide it back and it stays off.

The light switches on your walls work just like this — they stay where you leave them.

The story

The problem

A button only works while you hold it. Sometimes you want the light to stay on by itself.

Think of it like

A button is like a doorbell. A slide switch is like a wall light switch that stays put.

Meet the parts

It checks the switch and decides about the light.

Arduino

The brain

Loading part…

Slide it and it stays on or off all by itself.

Slide switch

The switch

Loading part…

Slows the power down so the light does not get hurt.

Resistor

The protector

Loading part…

It shows which way the switch is set.

LED

The light

Loading part…

How it works

1

Read switch position

It reads the switch the same way as a button — but the switch stays put until you move it.

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

Show state on LED

The light stays on while the switch is ON — you do not need to hold 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 — ready to build!

    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

  • Slide the switch — the light should stay on or off.
  • Compare it to the button: no need to keep holding it!

Peek at code

Getting the switch ready

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

We set up the switch pin the same way as a button — the part just acts differently.

Read and copy

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, and the light matches where you left it.

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 work that repeats go?

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

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

  • A. It stays where you slide it
  • B. It only works in setup()
  • C. It does not use digitalRead()
Why: Yes! A switch stays on or off; a button pops back when you let go.

Code lab — try on your own

  1. Make the switch check slower — change delay(100) to delay(200).

    Hint: It's the last line inside loop().

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

    Hint: That's the line with active = digitalRead.

Code walkthrough

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

Program overview

Big idea

Every Arduino program has a top part, a setup() part that runs once, and a loop() part that runs again and again.

In this project

Slide the switch and a light turns on or off — and it stays that way.

Tip

Read from the top to the bottom. Tap any word or line if you need help!

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

setup()

Big idea

setup() runs one time when the board turns on.

In this project

It gets the switch ready to listen and pin 13 ready for the light.

Why here

Things we do only once go inside setup().

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

loop()

Big idea

loop() runs again and again, forever.

In this project

It keeps checking where the switch is and turns the light on or off.

Why here

Things that repeat go inside loop().

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 a number inside loop(), then press Run to see what happens.

pinMode

Big idea

pinMode tells a pin if it will listen or push power out.

In this project

It makes pin 2 listen to the switch and pin 13 push power to the light.

Why here

It goes in setup() because we only set it once.

  pinMode(SWITCH_PIN, INPUT_PULLUP);

digitalWrite

Big idea

digitalWrite turns a pin ON or OFF.

In this project

ON lights up the LED, OFF turns it dark.

Why here

It goes in loop() so the light can keep changing.

  digitalWrite(LED_PIN, active ? HIGH : LOW);

digitalRead

Big idea

digitalRead checks if a pin is ON or OFF.

In this project

It checks which way the switch is sliding.

Why here

It goes in loop() so we can react when the switch moves.

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

delay

Big idea

delay means wait. Nothing else happens while it waits.

In this project

A small wait makes the switch read nice and steady.

Why here

Right after we check the switch and set the light.

  delay(100);

begin

Big idea

Serial.begin opens a way for the board to send words to your computer screen.

In this project

It lets the program print whether the switch is on or off.

Why here

It goes in setup() once, before we print anything.

  Serial.begin(9600);

println

Big idea

println sends a line of words to the screen.

In this project

It prints "Switch ON" or "Switch OFF".

Why here

In loop() so each new message shows on its own line.

  Serial.println("Slide Switch Demo");