Monitoring the Arduinos USB-Port

Hello,
I try to find out if the following is possible with my Arduino UNO R3:

The Arduino iss attached via USB to a mini computer and a battery as well.
I want the Arduino to monitor, if the pc is switched on. If the PC is switched off, the Arduino should recognize this and perform an action.

I tried a lot of different approaches to get an on/off signal via the USB, but I never get it to recognizing, when the PC is switched off.

I would be happy for every idea, if and how to solve this.

Thanks in advance.
Marco

Tell us what you tried.

I think you would need to write special code for the USB microcontroller to report when it does/does not have a USB connection...

Are you allowed to have something running on the PC, which sends a periodic "I'm Alive!" to the Arduino?

Some PCs (especially laptops) will still give power at the USB ports when switched off...

Hello,
at first: It's a special minicomputer for astrophotography. And when it is switched off, all Ports are "dead"

I tried different approaches

1st: try to check just the serial because I would attach the Arduino just to a free UBS-Port and there is no communication. It never jumped into the "else"

void loop() {
  if (Serial) {
    // USB is on
  } else {
    // USB is off
  }
}

2nd approach

void loop() {
   while (!Serial) {
     // USB is off
    delay(500);
   }
}

3rd Measure the voltage

void loop() {
  if (usbVoltagePresent()) {
    // USB is present
  } else {
    // USB is off
  }

  // Eine kurze Verzögerung, um die Ausgabe nicht zu überfluten
  delay(1000);
}

bool usbVoltagePresent() {
  return (analogRead(0) > 500);
}

In the Uno R3, the AVR microcontroller just has a UART - no native USB - so it cannot "see" the USB at all, and Serial will always be "up".

Do you have to use a Uno R3? Could you, instead, choose something with native USB? Then the processor would be able to see the state of the USB connection...

Had you connected A0 to somewhere that would measure the USB voltage only - and not your battery?

Addedndum:

Looks like you would need to read USBVCC:

Maybe the easiest way is to use a led strip that's driven by the computer's USB and detect light on/off ...

Looks like it should be easy enough to access USBVCC...

Hello,
the LED-strip is an idea. But because I take astro photos with this configuration, it has to be as dark as possible.

I'm an absolute newbie on Arduino, so I hope you don't mind my question:
What is USBVCC?

To be more precise I will describe my end goal:
I use an dedicated astrophoto computer (ASIairPlus) which controls my telescope and my astro camera. It has several USB-ports for astrocam, telescope and so on.
And it has von DC 12V outputs for these slave devices (camera, telescope,..).
You take astro photos for several hours at night. So I want to setup the gear and go to bed. When the astro computer has finished its program it switches itself of. And here comes the Arduino to action. It should monitor, if the computer is switched off, and then should trigger (i think of a servo) to close the telescope.

That's my goal.

While writing, the DC 12V ports come to my mind. Because they go "dead" too, when the computer will switch itself off.

Could this be an easier approach to know if the Master device (astro computer) went off?

It's circled (3 places) in the schematic in Post #4 - it is the supply voltage from the USB socket, after the fuse.

As you say your computer turns off the power to its USB ports when it's off, USBVCC will go to zero when the computer is off.

So can you monitor (one of) these outputs to see when the computer turns off.

Does the computer have one of these outputs available that could be used to "close the telescope"?

I opt for the 12V output!

Just use a voltage divider like (from the E12 series)

  • 2.7 kOhm -> 5 V
  • 3.9 kOhm -> 7 V

in total 6.6 kOhm which leads to 12V / 6600 Ohm = about 2 mAmps current.

That should do it.

If you want to be potentialfree you could drive a simple optocoupler with the 12V.

Yes. On that computer, there are 4 12VDC outputs, and only two are used by my gear.

So I can choose one to monitor. And I found an DC Sensor modul on amazon, that brings the 12V down, so I can easily measure it on the arduino on one of the analog ports.

I think, that's the easiest way.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.