// Define the pin number where the push button is connected
// Digital pin 2 is used as input from the button
const int buttonPin = 2;
// Define the pin number connected to the LED
// Pin 13 is connected to the onboard LED
const int ledPin = 13;
// setup() runs only once when the board powers up or resets
void setup()
{
// Configure LED pin as OUTPUT
// Control the LED state - send HIGH (5V) or LOW (0V)
pinMode(ledPin, OUTPUT);
// Configure button pin as INPUT_PULLUP
// Internal pull up resistor is enabled
// This means:
// Button NOT pressed -> pin reads HIGH
// Button PRESSED -> pin connects to GND and reads LOW
// External resistor is not required in this configuration
// Button wiring:
// One terminal -> Pin 2
// Other terminal -> GND
pinMode(buttonPin, INPUT_PULLUP);
// Ensure LED starts in OFF state during initialization
// LOW removes voltage from LED pin
digitalWrite(ledPin, LOW);
}
// loop() runs repeatedly after setup finishes
void loop()
{
// Read current state of button pin
// Since INPUT_PULLUP is used:
// LOW = button pressed
// HIGH = button released
if (digitalRead(buttonPin) == LOW)
{
// Button is pressed
// Turn LED ON by setting output HIGH
digitalWrite(ledPin, HIGH);
// Wait for 250 milliseconds
// This delay:
// 1. Prevents very fast switching
// 2. Gives visible LED response
// 3. Slightly reduces effect of switch bouncing
// Note:
// delay() blocks processor execution
delay(250);
}
else
{
// Button is not pressed
// Turn LED OFF
digitalWrite(ledPin, LOW);
}
}Loading
st-nucleo-l031k6
st-nucleo-l031k6