Why Code is Important — Same Circuit, 5 Different Behaviours!
Same Arduino, same LED, same button, same breadboard — change only the code and watch the circuit behave completely differently. Full wiring guide + all 5 code examples to copy.
In my last video, I built a push button + LED circuit on Arduino. It worked perfectly! But then I started wondering…
🤔 Can the same circuit do completely different things… without changing a single wire?
The answer is YES — and the secret is code.
Change the instructions → same body does different things. That's the power of programming!
🔌 What You Need
🧩 Wiring Guide
Connect everything once. Same wiring for all 5 demos.
LED short leg (−) → GND rail on breadboard
Button right leg → GND rail on breadboard
⚠️
INPUT_PULLUP = no external resistor needed
Basic Push Button
"This is the code from my last video."
const int buttonPin = 2; const int ledPin = 13;void setup() { pinMode(buttonPin, INPUT_PULLUP); pinMode(ledPin, OUTPUT); }
void loop() { if (digitalRead(buttonPin) == LOW) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } }
Toggle Switch
"Same button... but now it works like a light switch!"
const int buttonPin = 2; const int ledPin = 13;bool ledState = LOW; bool lastButton = HIGH;
void setup() { pinMode(buttonPin, INPUT_PULLUP); pinMode(ledPin, OUTPUT); }
void loop() { bool button = digitalRead(buttonPin);
if (button == LOW && lastButton == HIGH) { ledState = !ledState; digitalWrite(ledPin, ledState); delay(200); }
lastButton = button; }
Blink While Pressing
"Now it blinks instead!"
const int buttonPin = 2; const int ledPin = 13;void setup() { pinMode(buttonPin, INPUT_PULLUP); pinMode(ledPin, OUTPUT); }
void loop() { if (digitalRead(buttonPin) == LOW) { digitalWrite(ledPin, HIGH); delay(200); digitalWrite(ledPin, LOW); delay(200); } else { digitalWrite(ledPin, LOW); } }
SOS Signal
"Wow! It can even send an SOS message!"
const int buttonPin = 2; const int ledPin = 13;void dot() { digitalWrite(ledPin, HIGH); delay(200); digitalWrite(ledPin, LOW); delay(200); }
void dash() { digitalWrite(ledPin, HIGH); delay(600); digitalWrite(ledPin, LOW); delay(200); }
void setup() { pinMode(buttonPin, INPUT_PULLUP); pinMode(ledPin, OUTPUT); }
void loop() { if (digitalRead(buttonPin) == LOW) { dot(); dot(); dot(); dash(); dash(); dash(); dot(); dot(); dot(); delay(1000); } }
Random Blink Speed
"Every press gives me a new blink speed!"
</ div>const int buttonPin = 2; const int ledPin = 13;int speed = 200; bool lastButton = HIGH;
void setup() { pinMode(buttonPin, INPUT_PULLUP); pinMode(ledPin, OUTPUT); randomSeed(analogRead(A0)); }
void loop() { bool button = digitalRead(buttonPin);
if (button == LOW && lastButton == HIGH) { speed = random(100, 700); delay(200); }
lastButton = button;
digitalWrite(ledPin, HIGH); delay(speed);
digitalWrite(ledPin, LOW); delay(speed); }
🔍 What Changed Across All 5 Demos?
| Feature | Demo 1 | Demo 2 | Demo 3 | Demo 4 | Demo 5 |
|---|---|---|---|---|---|
| Circuit | Same | Same | Same | Same | Same |
| LED behavior | On while pressed | Toggle on/off | Blink while held | SOS signal | Random blink |
| What's new in code? | — | + ledState + lastButton |
+ delay(200) inside if |
+ dot() + dash() |
+ random() + analogRead |
The only thing that changed: CODE. 🎯
✅ Key Takeaways
🚀 What's Next?
Today I learned that changing the code can completely change how a circuit behaves. But now I have another question…
🤔 How does Arduino actually understand the code we write?
How does my program turn into actions like blinking an LED? Let's discover that in the next video!
If you're learning Arduino, electronics, coding, or STEM just like me, join me on this exciting journey. 👍 Like, 💬 Comment, 🔔 Subscribe on Nitvi Talks!