Sure! Here's the Arduino code to control an LED using a push button.
---
✅ Scenario:
When the button is pressed, the LED turns ON
When the button is released, the LED turns OFF
---
🧾 Arduino Code:
const int buttonPin = 2; // Push button connected to digital pin 2
const int ledPin = 13; // LED connected to digital pin 13 (onboard LED)
void setup() {
pinMode(buttonPin, INPUT); // Set button pin as input
pinMode(ledPin, OUTPUT); // Set LED pin as output
}
void loop() {
int buttonState = digitalRead(buttonPin); // Read button state
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on LED
} else {
digitalWrite(ledPin, LOW); // Turn off LED
}
}
---
⚠️ Important Notes:
Connect one side of the push button to 5V
Connect other side to pin 2, with a 10kΩ resistor to GND (pull-down)
LED connected to pin 13, or use an external LED with a 220Ω resistor
---
Would you like:
A Tinkercad link with this code and circuit?
Or a version using internal pull-up resistors (no 10kΩ needed)?
Let me know!