there is a similar post with the same title as this one, but I read it and it’s not the same question.
Project description: I want to automatically turn on my external (professional monitor) speakers as soon as my computer is connected to the big external screen.
The laptop is connected to the screen via a single USB-C cable (M2 Macbook Air), and my external soundcard that provides the sound signal for the speakers is also permanently plugged into the screen. But the power switches for the speakers are on their back side and they're standing far away, so I want everything to turn on/off just on its own.
Not working: I already tried a conventional master/slave power socket where I connect the external screen to the master outlet that controls slave outlets as soon as the power draw of the screen crosses a certain threshold that you can adjust with a potentiometer. This doesn't work reliably. The screen's power consumption jumps up and down even when no computer is connected, and it's not possible for me to find a reliable threshold setting for this to work. I want to avoid using this as it would erratically turn the speakers amps on and off, sometimes in quick succession.
Idea: So instead I would like switch a relay with an Arduino as soon as the Arduino is connected to a computer via USB, meaning there is a data exchange between the computer and the Arduino happening. Since the screen provides 5V power on its USB ports even when no computer is connected, controlling the relay purely through 5V power isn’t going to work. Instead I want to make this rely on actual data connections. However, I want no software to be installed on the computer to switch the relay. The idea is that this is entirely plug&play. The self-made device would be permanently plugged into the screen next to the soundcard, it is expected to receive 5V power from the screen, but is intended to only switch the relay once a computer is also connected to the screen.
I've worked with Arduinos and relays before, but absolutely no experience with USB whatsoever. Is this possible? If so, which Arduino support this, and is there USB-related sample code somewhere that could help me figure this out?
Since the computer need to determine what was just connected, any time you connect any Arduino to any computer via USB there will always be a data exchange.
It's all in the original description, but I will try to repeat:
The Arduino is permanently connected via USB to an external screen. The external screen permanently provides power on its USB outlets, so I can't make this rely on 5V power. I want to switch the relay once a computer is also connected to the screen, making this rely on a data connection rather than USB 5V power availability.
Unless the screen sends something to the Arduino via USB when the screen is connected to a computer, the Arduino has no idea that the screen was ever connected to a computer.
There is a USB hub built into the screen. Most modern screens have one so I didn't care to mention this detail, my apologies. The USB hub permanently receives power via the screen, but only receives data once a computer is connected to the screen.
I use the external soundcard the same way. It's permanently plugged into the screen and only functions once a computer is also connected.
I don't know if this is nitpicking, but technically the screen's USB hub is plugged into the computer, not the other way around.
Saddened by your assessment, I thought I'd try Chat GPT, and it recommended to use a SAMD21-based device such as the Seeeduino XIAO, with this code:
#include <USB/USBDevice.h> // Built-in library for USB status
const int relayPin = D1; // Connect relay to pin D1 (or any other digital pin)
void setup() {
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Ensure the relay starts OFF
}
void loop() {
if (USBDevice.connected()) { // Check if XIAO is connected to a computer
digitalWrite(relayPin, HIGH); // Turn ON the relay
} else {
digitalWrite(relayPin, LOW); // Turn OFF the relay
}
}
You could use a regular Arduino as well by utilizing a script that sends a character every second to the serial device of the Arduino in OSX, letting the Arduino know that it's master is present. Run it forever, when there's no connection transmit will simply fail.