// Define pin numbers
const int ledPin = 4; // Pin connected to toggle LED
const int switchPin = 9; // Pin connected to toggle switch
void setup() {
// Configure the LED pin as an output
pinMode(ledPin, OUTPUT);
// Configure the switch pin as an input
pinMode(switchPin, INPUT_PULLUP); // Use the internal pull-up resistor
}
void loop() {
// Read the state of the switch
int switchState = digitalRead(switchPin);
// Check if the switch is pressed (LOW state)
if (switchState == LOW) {
// Turn the LED on
digitalWrite(ledPin, HIGH);
} else {
// Turn the LED off
digitalWrite(ledPin, LOW);
}
}