Simple USB Detection with Transistor

Thread Starter

Mywk

Joined May 10, 2018
16
Hello Everyone,

I'm trying to detect if an Arduino-based device is connected to the USB with a simple transistor (the Arduino is also battery powered but the VCC connection from the USB and the battery don't interfere with each-other so lets simply ignore that as it should not matter).

I made a simple circuit with a NPN transistor which works great for doing this but I would like to check first with you guys as I'm not sure if this would cause any problem long-term:

upload_2018-5-24_10-25-55.png

USB VCC is 5V when connected
The Arduino PIN is set to pull-up (3.3V)

The Arduino pin has an interrupt so it when it changes it simply reads the PIN, if it's 0 then the USB is connected, otherwise it's disconnected.

Should I change something in my circuit?

Thanks in advance!
 
Last edited:

DickCappels

Joined Aug 21, 2008
10,152
Three things pop out:

You would do better if you swap the emitter and the collector.

You should add a resistor in series with the base to limit the current.

I don't think you need any of the diodes.
 

kubeek

Joined Sep 20, 2005
5,794
Hmm, I am not sure how you made it work, but it should not. You want to have a resistor in series with the base, say 10k, and the transistor looks connected upside down.

Aaand, Dick is faster and has it better thought out, yes the diodes are not necessary.
 

Thread Starter

Mywk

Joined May 10, 2018
16
Three things pop out:

You would do better if you swap the emitter and the collector.

You should add a resistor in series with the base to limit the current.

I don't think you need any of the diodes.
That was oddly fast! Thank you!

Swapping the emitter and collector causes an interesting effect: The event gets triggered but takes almost a second to happen instead of instantly (as if the voltage was somehow still decreasing? no really sure why this happens).

Removing any of the diodes causes the circuit not to work at all!
 

Thread Starter

Mywk

Joined May 10, 2018
16
Got it, thank you @Sensacell I didn't even consider that, it's way simpler.

Had to put a diode between the second resistor and GND otherwise it seems that the Arduino doesn't trigger correctly..

upload_2018-5-24_12-42-36.png

This seems to work flawlessly and I get to spare a transistor. Thanks once again!
 

Sensacell

Joined Jun 19, 2012
3,432
Ditch the diode!

Your voltage divider should not be 50/50 ratio, it then gives you 50% (2.5V) high signal level.
Change the input resistor to 500 ohms, ditch the diode.

The diode leaves the input floating somewhere between 0 and 0.6 Volts with no input - bad!
 

ScottWang

Joined Aug 23, 2012
7,397
I'm not sure why you always like to in series with a diode?
Normally you just need to use the R1 = 2.2K and R2 = 3.9K then you will get the voltage close to 3.2V, or use R2 = 4.7K to get the voltage close to 3.4V.
 

Thread Starter

Mywk

Joined May 10, 2018
16
I changed the design to have R1 2K and R2 1K, the Arduino PIN needs at least 1V to get triggered and no more than 5.5V

I agree that it makes no sense to have the diode there, the problem is that without the diode the Arduino doesn't recognize the signal (lots of noise). I'm assuming something else may be broken between the USB VCC and the R1 (I didn't include the whole schematic) so I'm going to attempt to investigate a bit further tomorrow!
 

Thread Starter

Mywk

Joined May 10, 2018
16
@DickCappels you were absolutely right!

As simple as this:

upload_2018-5-28_10-31-24.png

The problem was before the R1 and some messed up settings related to the Arduino PIN, using it as Open-drain instead of Pull-up/down fixes everything.

Thank you all once again!!
 

BobTPH

Joined Jun 5, 2013
8,814
Open drain refers to an output. Nothing you have said makes any sense. If you have an Arduino running at 5V that needs to detect a 5V signal that might or might not be connect, all you need is to connect the signal to the INPUT pin and use a pulldown to ground, either internal or external.

Bob
 
Top