Connecting Arduino to PC

Just a little question...

I'm working on a project that involves a RTC. One thing I would like to know: the project is stand alone, but I would like to set the time automatically when the UNO or Mega2560 is connected with a computer over the USB.

Is there a way to "sense" that the USB is connected to the PC, son, not only by looking at the power on the USB bus?

Thanks in advance!


You could check to see if any data is available from Serial: if (Serial.available())...

You will need a program on the PC to send the data to the Arduino. When that program opens the serial port it will cause the Arduino to reset - which may not be what you want.

If you disable that automatic reset you could have code in your Arduino to check if data has arrived from the PC. I suspect the 3rd example in serial input basics may be sufficient. The start and end markers should enable you to avoid garbage that may arrive.

If the automatic reset is disabled it will be harder to upload code to the Arduino.


Thanks already for the replies. I've bookmarked the page about serial inputs, hold it for when I've some spare time :slight_smile:

Isn't it so that, when you start uploading a sketch, at the moment it stops compiling (and show the used bytes), is the right moment to release the reset on the board?