π§° What you need:
ESP32 board (like a DevKitC or NodeMCU-ESP32)
USB-A cable with exposed 5V (red) and GND (black) wires
Two resistors for voltage divider (e.g., 10kΞ© + 20kΞ© β drops 5V to ~3.3V)
Breadboard and jumper wires
π Wiring
USB Wire | Connect to |
Red (5V) | Voltage divider β ESP32 GPIO (e.g., GPIO 18) |
Black (GND) | ESP32 GND |
Voltage divider setup:
USB 5V β [20kΞ©] β GPIO 18
β
[10kΞ©] β GND
This drops 5V down to ~3.3V safely.
β Example Code (ESP32, digital read)
const int usbPowerPin = 18; // GPIO18 gets the divided signal
bool lastState = LOW;
void setup() {
pinMode(usbPowerPin, INPUT);
Serial.begin(115200);
}
void loop() {
bool currentState = digitalRead(usbPowerPin);
if (currentState != lastState) {
if (currentState == HIGH) {
Serial.println("USB Power ON");
// YOUR ALERT START CODE GOES HERE
} else {
Serial.println("USB Power OFF");
// YOUR ALERT STOP CODE GOES HERE
}
lastState = currentState;
}
delay(100); // debounce / polling delay
}
β οΈ Important:
Do NOT connect 5V directly to ESP32 pins β always drop it to β€3.3V.
ESP32 pins are usually labeled GPIO on your board β you can pick most, but avoid strapping pins (like GPIO0, GPIO2, GPIO15).
Use Serial.begin(115200) β ESP32 typically uses higher serial baud rates than Arduino Uno.