const int buttonPin = D3;
int oldValue = LOW; // default/idle value for pin 8 is low.
int ledState=LOW;
void setup() {
Serial.begin(115200);
pinMode(D2, OUTPUT);
pinMode(D3, INPUT);
Serial.println("");
Serial.println("Hello, XIAO ESP32-C3!");
Serial.begin(115200);
Serial.println("Press the button.");
// Initialize the pin for reading the button.
pinMode(buttonPin, INPUT);
}
void loop() {
// Read the value of pin 8.
int newValue = digitalRead(buttonPin);
// Check if the value was changed,
// by comparing it with the previous value.
if(newValue == HIGH && oldValue==LOW){
ledState= !ledState;
digitalWrite(D2, ledState);
if( ledState == HIGH ){
Serial.println("Red");
}else{
Serial.println("The device is turned off.");
}
}
oldValue=newValue;
}
Loading
xiao-esp32-c3
xiao-esp32-c3