🧰 What you need:
Arduino Uno (or similar)
USB-A cable with the 5V (red) and GND (black) wires exposed
Connect the 5V wire to an Arduino digital pin (or analog for voltage measurement)
Optionally use a resistor divider if needed
✅ Simple Example Code
This version monitors the 5V USB line connected to digital pin 2:
const int usbPowerPin = 2; // USB 5V line connected here
bool lastState = LOW;
void setup() {
pinMode(usbPowerPin, INPUT);
Serial.begin(9600);
}
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
}
Wiring
USB 5V (usually red) → Pin 2
USB GND (usually black) → GND on Arduino
⚠️ Important: Ensure the USB power doesn’t exceed 5V. If you’re unsure, use a voltage divider (e.g., 10kΩ and 10kΩ) to cut voltage in half and measure with an analog pin.