Runs your sketch
Arduino
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 |
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 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.
Runs your sketch
Arduino
Brain
Stays ON or OFF
Slide switch
Input
Protects the LED
Resistor
Safety
Shows switch position
LED
Output
Read switch position
digitalRead works the same — but the switch stays LOW or HIGH until you move it.
bool active = (digitalRead(SWITCH_PIN) == LOW);
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
Follow these steps in order. Match the wires to the colors shown.
Place Arduino
Place the Arduino (uno) on the breadboard.
Arduino placed!
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.
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.
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 repeating work belong?
Q2. How is a slide switch different from a push button?
Change delay(100) to delay(200) in loop().
Hint: Last line of loop().
Add a comment on line 10: // switch ON when LOW
Hint: The active = digitalRead line.
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");