Xiao Esp32c3 and mosfet for power shutdown

Hi there,

So here you can USE a Xiao ESP32C3 and the single channel relay board once connected and programmed After it boots up the “boot” button can be used to turn ON/Off the relay.

// Define the pins
const int BOOT_BUTTON_PIN = 9;  // Boot button; for Xiao ESP32C3  adjust if your board uses a different pin
const int LED_PIN         = 3;  // LED connected to R1

bool ledState = false;        // current state of the LED
bool lastButtonState = HIGH;  // to track button state for detecting edges

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    ; // Wait for Serial connection (if needed)
  }
  Serial.println("Boot Button LED Toggle Example");

  // Configure the boot button pin as input with internal pull-up.
  pinMode(BOOT_BUTTON_PIN, INPUT_PULLUP);

  // Configure the LED pin as an output.
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW); // Start with LED off (assuming LED turns on when HIGH; adjust if reversed)
}

void loop() {
  // Read the state of the boot button (active low)
  bool currentButtonState = digitalRead(BOOT_BUTTON_PIN);

  // Check for a button press: transition from HIGH to LOW.
  if (currentButtonState == LOW && lastButtonState == HIGH) {
    // Toggle the LED state
    ledState = !ledState;
    digitalWrite(LED_PIN, ledState ? HIGH : LOW);
    Serial.print("LED toggled: ");
    Serial.println(ledState ? "ON" : "OFF");

    // Simple debounce delay
    delay(50);
  }

  // Save the current state for the next loop iteration
  lastButtonState = currentButtonState;

  // Small delay to avoid rapid polling
  delay(10);
}

Works, Great!
HTH
GL :slight_smile: PJ :v: