const int buttonPin = D2; // pushbutton pin
const int ledPin = D3; // LED pin
int buttonState = HIGH; // matches the resting (unpressed) state with INPUT_PULLUP
void setup() {
Serial.begin(115200);
// enable internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// LED output
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the state of the pushbutton (LOW = pressed, HIGH = not pressed)
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
// button pressed?
if (buttonState == HIGH) { // not pressed
digitalWrite(ledPin, HIGH); // turn LED on
} else { // pressed
digitalWrite(ledPin, LOW); // turn LED off
}
delay(1000);
}Loading
xiao-esp32-c3
xiao-esp32-c3