It checks the switch and decides about the light.
Arduino
The brain
switch state vs momentary button
Use a slide switch to toggle an LED on and off.

Pin connections
| Part 1 | Part 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 |
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 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.
It checks the switch and decides about the light.
Arduino
The brain
Slide it and it stays on or off all by itself.
Slide switch
The switch
Slows the power down so the light does not get hurt.
Resistor
The protector
It shows which way the switch is set.
LED
The light
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);
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
Follow these steps in order. Match the wires to the colors shown.
Place Arduino
Place the Arduino (uno) on the breadboard.
Arduino placed — ready to build!
Place Slide switch
Place the Slide switch (sw1) on the breadboard.
Place Resistor
Place the Resistor (r1) on the breadboard.
Place LED
Place the LED (led1) on the breadboard.
Connect Slide switch (sw1) 2 to Arduino pin 2
Connect Slide switch (sw1) 2 to Arduino pin 2.
Connect Slide switch (sw1) 1 to Arduino GND
Connect Slide switch (sw1) 1 to Arduino GND.
Connect Arduino pin 13 to Resistor (r1) 1
Connect Arduino pin 13 to Resistor (r1) 1.
Connect Resistor (r1) 2 to LED (led1) anode (+)
Connect Resistor (r1) 2 to LED (led1) anode (+).
Connect LED (led1) cathode (-) to Arduino GND
Connect LED (led1) cathode (-) to Arduino GND.
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.
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);
}
Q1. Where does work that repeats go?
Q2. How is a slide switch different from a push button?
Make the switch check slower — change delay(100) to delay(200).
Hint: It's the last line inside loop().
Add a note (comment) on line 10: // switch ON when LOW
Hint: That's the line with active = digitalRead.
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");