USB plugged into TV connected as GPIO input to detect ON state

Hello. First time poster here.

I will be using either an ESP32 or a Pi Zero W for this project. It will be placed behind a TV.

I have a USB cable, which I have cut to expose the power and the ground. I will plug this cable into a TV's USB port and lead it to a GPIO pin on the microcontroller. The microcontroller will send the state to Home Assistant so I can create a "dim the bedroom lights" automation when the TV (and its USB port) is powered on.

I have a 3.3v to 5v bidirectional level shifter.
I have a relay.

My plan is to power the relay with the microcontroller. I will run the TV USB cable through the relay. This will ensure that the USB power only reaches the GPIO pin when the microcontroller is powered on.

Is the level shifter the best way to bring the USB power down to 3.3v? If so, which pins should I use, specifically?
There are 6 pins on either side (and there is no HV label):

Top Row Pin Labels (from left to right)
TX0
RX1
(no label)
GND (actually, the GND label is positioned in middle alignment with pins 3 and 4)
RX1
TX0

Bottom Row Pin Labels (from left to right)
TX1
RX0
LV
GND
RX0
TX1

So, when the TV is powered on, the USB port sends 5v through the cable and through the relay, then through the level shifter, arriving at the GPIO.

I wonder whether there might be a concern about power somehow flowing out the GPIO when the TV is off, thus frying the TV's USB port. Is there a unidirectional voltage regulator I should use instead of the level shifter? Maybe an AMS1117 will work?

I am also concerned about mA. I assume the USB power exceeds 1000mA (maybe too high for the GPIO?) so perhaps that's all the more reason to use something other than a level shifter? But, if the level shifter is in fact sufficient, I could really use some specific guidance as to which pins to use.

Could I please get someone's assistance on this? I'm a total noob.

Hello, welcome.

You just need 2 resistors, no level shifter, no relay.

5V

10k resistor

Input pin

20k resistor

0V

Don't forget this:

I am also concerned about mA. I assume the USB power exceeds 1000mA (maybe too high for the GPIO?)

This shows that you misunderstand something basic about electricity. How much power is available at a wall socket in your house? How much damage does a low power device, such as a table lamp, suffer when you plug it in to a a socket capable of powering an electric heater?

Electrical devices take the power they require, it is not 'pushed' into a device that doesn't need it. The power or current rating of a power supply is just the maximum amount it can safely supply without being damaged.

You need to understand Ohm's Law and Kirchhoff's circuit laws
These laws are fundamental to all electrical circuits and if you learn them a lot of the mysteries of electricity will become clear. In many cases applying both laws to a circuit will provide whatever answer you are looking for.

Actually, it would be more appropriate to use an opto-isolator - PC817 - between the TV and ESP.

1k resistor in series with the opto-isolator LED from the USB and the output transistor between input pin and ground of the ESP or Pi with pullup enabled.

The "+5V" and "Input" will be the 5 V and ground lines from the USB cable.

Your description is so reministic of Rube Goldberg.

Just plug in a Wemos D1 Mini to the TV USB port.

Done.

PerryBebbington:
Don't forget this:
https://forum.arduino.cc/index.php?topic=653831.0

PerryBebbington:
You need to understand Ohm's Law and Kirchhoff's circuit laws
These laws are fundamental to all electrical circuits and if you learn them a lot of the mysteries of electricity will become clear. In many cases applying both laws to a circuit will provide whatever answer you are looking for.

Many thanks for the replies, and the links. I will spend some time and read them.

Paul__B:
Actually, it would be more appropriate to use an opto-isolator - PC817 - between the TV and ESP.

1k resistor in series with the opto-isolator LED from the USB and the output transistor between input pin and ground of the ESP or Pi with pullup enabled.

The "+5V" and "Input" will be the 5 V and ground lines from the USB cable.

Thank you for your response, also. It seems that this device includes a 1k resistor (though, it seems to be on the opposite side of the pc817 in this configuration as compared to the diagram you provided). Would this do the job? If so, would this be the wiring?
USB 5V --> "+ Input" on pc817
USB GND --> "- Input" on pc817
ESP32 5V out --> "VCC" on pc817
ESP32 GND --> "GND" on pc817
ESP32 GPIO0 --> "Out" on pc817

(Apparently GPIO0 is "pulled up" on my ESP32 DEVKIT V1 DOIT)

SteveMann:
Your description is so reministic of Rube Goldberg.

Just plug in a Wemos D1 Mini to the TV USB port.

Done.

I'd prefer the timing of the dimming action be prompt, and consistent.

I'm going to answer my own question. That pc817 in my previous post appears to be for amplifying from 3v up to 5v. So, that's not the right device.

Toreupfeet:
I’m going to answer my own question. That pc817 in my previous post appears to be for amplifying from 3v up to 5v. So, that’s not the right device.

No, it is an optocoupler module. It is in fact, quite perfect for the job, immediately ready to go Just connect the input side to your USB cable and the ground and “OUT” to the Pi or ESP.

If you use INPUT_PULLUP on the Pi or ESP, you do not need to connect the “Vcc” pin to 3.3 V but looking at the resistor values, it should not hurt to do so.

Toreupfeet:
Apparently GPIO0 is “pulled up” on my ESP32 DEVKIT V1 DOIT

On an ESP8266, that would be a problem if the optocoupler was activated when you booted the ESP. I am not sure how that would affect an ESP32.

Thank you again! I will place the Ali order.

I think I’ll use the ESP32. I had been considering the Pi because I will place one somewhere in my room for Room Assistant, but behind the TV might not be the best place to track bluetooth signals.

Is there a pin that would be more suitable than GPIO0 for the optocoupler? I plan to run an HC-SR501 on the same ESP32 as well. So, that’s three active pins. I know enough about this sort of thing to know I know very little. So, if you have any advice as to which pins to use for these devices, please don’t hesitate.

  1. Relay (or is the relay redundant if there is an optocoupler?)
  2. Optocoupler
  3. HC-SR501

This site is probably well known around these parts. It is where I found this:

I’m most interested in which pins to use for logic, but power would be good to nail down as well.
The ESP32 will be powered via micro USB. From there, could/should I…

  1. connect ESP32 5V to the relay VCC
  2. connect ESP32 3.3V to optocoupler module VCC
  3. connect a separate dupont wire to each of the two modules’ GND, and crimp them into one female to connect to ESP32 GND
  4. Power the HC-SR501 with a standalone 5V source and connect only the OUT logic pin to the ESP32

Cannot presently answer for the ESP32. Look up which pins control the boot process and avoid them.

For the ESP8266 I know they are GPIO0, GPIO2 and GPIO15, but the ESP32 may be different.

The Wemos D1 Mini is cheaper and smaller than the ESP32.

Thanks guys.

The D1 Mini would be a great choice, but I have a few of these DoIt V1 lying around and I'll use them up before purchasing any more ESP.

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