🧪 Hands-On Example: Blinking an LED
This is often the very first program for anyone using Arduino. It’s like saying “Hello, world!”
int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
This code turns the LED on for 1 second and off for 1 second in a loop.
👆 Using a Button as Input
Now let’s add a button so you can control when the LED turns on.
int ledPin = 13;
int buttonPin = 2;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
This program checks the button’s state. If it’s pressed, the LED turns on. Otherwise, it stays off.