Function to detect when the arduino is connected through COM?

Hi,
I would like to know if there is an arduino function or library that exists, that detects when the arduino is connected to a computer and establishes a COM connection, but I need it in a way so that while it is powered through battery it can detect when connected to a computer if that makes sense. So basically my arduino is powered through battery, but when it is connected to the computer (this is so the PC and arduino can exchange serial data) the arduino detects and acts accordingly (e.g light up an led).
Thanks in advance.

Which Arduino? For the Unp and Meags - No because there is no way to tell.

For the Leo Serial can do that.

Other rules on the boards and on which of the Ardyinos ports is being used.

Mark

Ok thanks for the response, just to clarify, you're saying it's impossible for the arduino uno (I have the UNO) to know when it's connected?

With out extra software to Run on the PC then Yes!

Mark

I had not thought about the idea before, of detecting a serial connection, but it is interesting. On a conventional Arduino such as Uno, it should be possible to locate the DTR-to-reset 0.1uF capacitor, and connect the side which is towards the USB-to-serial chip to an input pin of the Arduino’s main processor. I would think connecting it through a 1K resistor is a good idea. When DTR is low, a connection is currently established. When DTR is high, it is disconnected.
pic1.jpg

@dmjlambert - no that's no how the DTR line is used and its not how reset works.

Take the reset pin high and the processor is stopped. while the pin is high the processor does not run. When the pin goes low the processor starts running again from the beginning.

The processor is not reset when you connect it to the USB - it is reset when the comm port is opened.

Mark

I think I have the high/lows for the reset pin the right way round.

M

Yeah, I was thinking the only option really would be to make something on that pc that sends serial data to the arduino. Ty for the answers.

What do you want to detect?

When the board is plugged into the computer, or when a serial connection is opened by an app on the computer?

Unless you clobber autoreset, when a connection is opened (by most software at least), the board will reset via the DTR reset thing.

If you're talking detecting when it's plugged in, that's probably possible, but only with hardware modification.

holmes4:
@dmjlambert - no that’s no how the DTR line is used and its not how reset works.

I am a pretty good expert in this area, that is how the DTR to reset line works.

I went ahead and hooked it up, works great. My sketch can now tell when a serial connection is established, and when disconnected. The wiring from DTR to pin 2 is just as shown in my schematic. LED attached to pin 7 comes on when DTR is low as read on pin 2. I open the Serial Monitor to establish the serial connection. Sample code:

void setup() {
  pinMode(2, INPUT);
  pinMode(7, OUTPUT);
}

void loop() {
  digitalWrite(7, !digitalRead(2));   
  delay(100);             
}

Connection to DTR signal on the USB-to-serial chip side of DTR capacitor:
pic2.jpg

Wired for sample sketch:
pic3.jpg

edit— minor edits for clarity

Interesting, but I am not sure how useful it may be. However I have bookmarked Reply #8 just in case.

...R