How would I safely connect an Arduino so that it can detect if a laptop is on or off?

The Arduino would be powered separately by a normal USB charger, since it needs to be turned on even when the laptop is turned off. So I'm thinking I can connect an input on the Arduino to one of the USB ports on the laptop somehow, but how do I do this safely?

Even if I play it safe and connect a current-limiting resistor between the USB voltage and input pin, and perhaps a voltage divider, is there still a problem with connecting one device to another like this? I'm thinking that since each has its own power supply from mains, could there potentially be a problem if I connect the USB port of the laptop to a pin on the Arduino?

Do need to use an optocoupler or relay between them? Or is there an even better way of doing this that I didn't think of?

  • 1
    do you know that there is 5 V on USB pim if the laptop is on, and there is no 5 V if the laptop is 'off'?
    – Juraj
    Commented Jul 19, 2019 at 12:00
  • @Juraj I haven't tested it thoroughly, but nothing I connect to the USB seems to have any power when the laptop is off, so I would think that the 5V is off?
    – Magnus
    Commented Jul 19, 2019 at 12:04
  • 1
    By the way, consider my cheap solution. A sad part of these StackExchange forums is that people "downvote" when the don't like what you offered, Those downvotes often have nothing to do with actual measurable data.
    – Randy
    Commented Jul 19, 2019 at 19:53

4 Answers 4


If your chosen power supply is of a good quality and provides proper ground isolation then it shouldn't be a problem to connect the grounds of the two systems together.

However, since there are so many bad power supplies available on the internet (and even some "good" power supplies are somewhat iffy when it comes to grounding) it is safest to hedge your bets and add your own galvanic isolation.

The simplest method is to use an opto-isolator. Use the +5V and GND from your computer's USB port (assuming it's not an always-on charging port - look for a lightning bolt symbol) along with a resistor (470Ω - 1kΩ is good) to drive the LED side of the opto-isolator. The other side then acts as a button to the Arduino:


simulate this circuit – Schematic created using CircuitLab

In this arrangement your input will be LOW when the PC is on. Also R2 could be replaced by the internal pullup of the GPIO pin (pinMode(PIN, INPUT_PULLUP);)

As I say, this most likely won't be strictly necessary, but for the minimal cost of an opto-isolator it's silly not to add that extra layer of protection. After all, an opto-isolator is cheaper than a new Arduino or (heaven forbid) a new laptop.


The wall-wart type USB chargers are always double insulated - you'll typically see the square within a square symbol for European markets, though one for the US market alone may not have that marking.

That means that the secondary side is free to float, and can be safely connected to the ground connection of the laptop - whose power supply will typically also be double insulated.

That means you can just connect the 5V USB output to an input on the Arduino - put a high enough current limiting resistor in to allow for differences in voltages - especially in the case that the laptop output is on when the Arduino supply is off, to avoid the input feeding the Arduino through the protection diodes.

You can use an analog input, but if you're only looking for an on/off indication, a digital interpretation would work.

Check the output of the USB socket on the laptop - some are intended to remain live for charging accessories when the laptop is powered but not on. On one of my laptops all the ports are live like this and there's nothing that indicates this.

  • "double insulated" has nothing whatsoever to do with what is safe or not to do with ground. It is purely to do with the way the physical device is insulated. You are thinking of "galvanic isolation" which is a completely different thing.
    – Majenko
    Commented Jul 19, 2019 at 15:25
  • Double insulation to UL1097 removes the requirement that the secondary circuit, considered accessible dead metal, be grounded - in fact it forbids it, and so ensures that the galvanic isolation afforded by the separation of the windings on the flyback inductor, and the creepage spacing of the board it is attached to is not compromised by it being connected to any point of the supply that would cause a ground loop to exist when connected to other equipment, grounded or not.
    – Phil G
    Commented Jul 19, 2019 at 16:42
  • You're confusing ground "earth" with ground "0v reference point in a DC circuit".
    – Majenko
    Commented Jul 19, 2019 at 16:46
  • No, I'm not. Whether it's safe to connect the GND of the laptop USB port to the GND of the Arduino's supply depends on there being no other connections to both of those that could hold them at different potentials, and the connection to the supply earth through both power supplies could do that. UL unfortunately uses the term "grounding" to mean earthing in a lot of places. en.wikipedia.org/wiki/Ground_loop_(electricity)
    – Phil G
    Commented Jul 19, 2019 at 17:07
  • What about through the Y capacitor...? The leakage current ("touch current") could be fatal to one or other device...
    – Majenko
    Commented Jul 19, 2019 at 17:15

I don't see any problems connecting the them together like you described.

Make sure not to forget to also connect the USB ground to the Arduino ground, so the electrons have a path to go back.

Adding a current limiting resistor is advisable, for the case that the Arduino isn't powered, but the laptop is. Resistor divider is only needed for Arduino's that run on 3.3V.


The only problems are that (a) the Laptop USB outputs 5V, and the arduino input should not exceed 3.3, and (b) when the Laptop is no longer powered, the 5V pin on its USB no longer has a defined state, and could theoretically spike.

Here's a simple and cheap way to do this with passives. The diode isolates the USB from any "back" voltage. The USB 5V is measured across the resistor, and I'd place a capacitor there to to absorb any voltage spikes from the laptop when it loses power. Finally, the second resistor also indirectly protects the digital input and acts like a voltage converter, between the 5V (4.5 after the diode) and the 3.3V the arduino wants to see. The over-voltage will simply be routed to ground via the chips internal protection diodes, and the current will be limited to about 120 microamps ( 4.5- 3.3 ) / 10K.

You could use an opto isolator, but this is a cheap solution that will work. You will have to share ground between the laptop's USB and your arduino (not shown in the diagram). That will not be an issue however. Especially considering all power adapters offer significant line-to-load isolation. By the way, the 1uF capacitor can be decreased to 100nF or less, depending on how quickly you want to respond to the power loss.


simulate this circuit – Schematic created using CircuitLab


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.