I have been searching around the Internet to find if there is a way to check whether an Arduino is connected to a PC or not.

For example: if my PC is turned on, the Arduino will listen to any serial commands it receives, BUT if my PC is turned off, the Arduino will receive some random characters.

How can the Arduino check if it is connected to PC and the PC is on?

  • 1
    Read How does this code line detect if a serial interface is connected?. If it doesn't provide a satisfactory answer, update your question to reflect your additional requirements.
    – JRobert
    Sep 24 '16 at 20:18
  • You'd have to write some application on your PC, that for example, sends "PONG" when it receives "PING". You could then "ping" the PC every now and then, to check if it responds (thus is connected/on).
    – Paul
    Sep 24 '16 at 21:43
  • Alternatively, you could check the 5V USB line, that would indicatie that your PC is on (usually). But that only works if you normally run it off the barrel jack. (So doesn't really apply to Nano, but you could make an alternative circuit.) You could maybe measure resistance on the data lines, but I'm not sure if that's safe/possible.
    – Paul
    Sep 24 '16 at 21:44
  • Have you checked if the USB ports on your PC will remain powered if the PC is off?
    – Gerben
    Sep 25 '16 at 15:48
  • if my PC is turned off, the Arduino will have some predetermined patterns. - what do you mean?
    – Nick Gammon
    Nov 2 '16 at 7:30

As suggested by Paul & Gerben, you could check the 5V USB line to see if it has a voltage or not.

I am assuming from your question that the Arduino is not powered from the USB line. I am also assuming that the USB port is not powered when the PC is turned off.

All you have to do is connect VUSB to a digital input through a resistor like so:
Circuit Diagram

Alternatively, you could have the computer send a pre-determined message telling the Arduino that its alive.

If the Arduino receives this message then it lights up the LED on pin 13. If the Arduino hasn't received the "I'm alive" message for a while (ie. a timeout) it then turns off the LED.

A simple PC program could be written to automate this task.


jumper reset -> ground and rx -> tx (with power disconnected) connect power, open serial monitor in arduino ide and type anything in and input. if the serial monitor echoes what you typed in, then your connection is good and you should look elsewhere for problems.

Found this solution on the arduino.cc community site.

  • How does your suggestion enable the arduino to establish if it is connected and what portion of the code to run, as requested in the original post?
    – fred_dot_u
    Sep 29 '16 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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