0

Gemma M0 - detecting device status by sensing their USB port
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Gemma M0 - detecting device status by sensing their USB port

by l8gravely on Mon Apr 16, 2018 11:42 am

Hi,
I'm working on a project to sense the power status of two devices, my TV and sound bar, using a Gemma M0. Both devices have USB ports on the back, and by using a voltage divider with 1k ohm resistors, I can safely (I hope!) sense the presense of power from each. The idea is that when the TV is on, I check the sound bar for power. If it's not on, I send the proper IR codes to turn it on.

This is *mostly* working, but I need to tweak the timeouts and measure the decay rate of the voltage I get from the remote USB ports to make it more robust, and so that I don't get into a toggle loop with the sound bar.

So, I also had a thought, that how can I reliably detect if I'm actually plugged into the devices? I'm only using the 5v and GND connections from the two devices, each led to a pin on the Gemma M0 and using AnalogIn to sense the presense of the voltage. I really don't want to go with anything bigger, the GemmaM0 is the perfect size and has just the right number of pins for this application. But... can I make it more robust, so that if it does freak out, my wife won't go crazy and smash it, and then me. Which is why I'd like to figure out a way to detect when it's unplugged, so as to shutdown gracefully.

I'm not sure if I can measure resistance and voltage at the same time, or maybe have it pulled low if unhooked so it reads zero voltage? Am I making sense?

Thanks,
John

l8gravely
 
Posts: 21
Joined: Wed Feb 11, 2009 12:58 pm
Location: Worcester, MA

Re: Gemma M0 - detecting device status by sensing their USB

by vnenov on Tue Apr 17, 2018 10:43 am

Hi, Sorry this is not a reply to your question.
I am actually looking forward to seeing an answer to your question myself since I have apparently the same question.

I am using the trinket and want to be able to programmatically tell if the micro USB cable is plugged in or not (assuming that it is already powered on at the other end by being plugged into a PC/Mac USB port or into USB power adapter).

Disclamer: I need to detect this so I can programmatically turn off the brightness of the LEDs (12 bit ring) so that they are off during charging.
I have an Adafruit battery backpack connected to the trinket and a LiPoly battery.

Is there a command (bool) like "isUSBpowerOn" or the like in any of the libraries that does the job?
Alternatively, does the LOW/HIGH state of any of the IO pins reflect the USB on/off state? If so, how can I read this state?

Any suggestions, greatly appreciated.

Thanks,

Val

vnenov
 
Posts: 4
Joined: Tue Sep 08, 2009 1:54 am

Re: Gemma M0 - detecting device status by sensing their USB

by l8gravely on Tue Apr 17, 2018 3:22 pm

Val,
I think we're talking about different things here. I've got my GemmaM0 powered by it's regular power input. Then I have two other USB cables hooked upto pins A0 and A1 on the Gemma, where I just sense the presense of 5V (through a voltage divider, so I'm only seeing 2.5V max). I should put up a schematic to be more concise.

In your case, I think you want to know when the Trinket is hooked up via USB to it's built in USB port, or when it's running off the battery. Thinking about this, it might be possible to sense the voltage provided by the trinket board for other devices, and measure that voltage using a voltage divider on an Analog pin.

The idea would be that when powered by USB 5v, you see a higher voltage, when powered by the 3.7V LiPoly battery, you see less voltage. Hmm... re-reading the Trinket page, this might not work since it can accept upto 16V battery power. Not good. So I'd probably sense if you're getting 5V on the USB pin, above pin 0 on the board using a voltage divider. If you're running on battery only, it will be near zero. Maybe.

I've got a trinket kicking around at home somewhere, but I won't get to this any time soon.

l8gravely
 
Posts: 21
Joined: Wed Feb 11, 2009 12:58 pm
Location: Worcester, MA

Re: Gemma M0 - detecting device status by sensing their USB

by l8gravely on Tue Apr 17, 2018 3:26 pm

This page will answer your question too!

https://learn.adafruit.com/introducing-trinket/pinouts

USB+ is the USB + Output pin. If you want to use the USB 5V power for something, like charging a battery, or if you need more than 150mA of current (this pin can supply 500mA+ from USB ports) or to detect when the Trinket is plugged into USB, this pin will have 5V power on it if and only if its plugged into something via the mini B connector

l8gravely
 
Posts: 21
Joined: Wed Feb 11, 2009 12:58 pm
Location: Worcester, MA

Re: Gemma M0 - detecting device status by sensing their USB

by vnenov on Tue Apr 17, 2018 3:55 pm

Thanks for thefts and detailed response John,

yes, I get 5V on the USB pin when the Mini-B cable is plugged in and powered, but I also get 3.7V on the same pin when running off 3.7V LiPoly battery.
Unfortunately, I am so new to the Trinket/Arduino programming and not quite familiar with the hardware itself yet so I don't know how to programmatically distinguish between 5V and 3.7V on pin #0. I don't know if I set pin 0 as AnalogIn if I will get an actual voltage value that I can use in an IF statement.

Will try to explore this adea. I hope I don't damage the gadgets:-)

Val

vnenov
 
Posts: 4
Joined: Tue Sep 08, 2009 1:54 am

Re: Gemma M0 - detecting device status by sensing their USB

by l8gravely on Tue Apr 17, 2018 4:39 pm

Val,
Make sure you read the docs on the trinket closely, especially the part on which pins you can use. I was wrong in my reading, you can't use pin 0, I think you need to use pin 4 or 5 for an AnalogIn. Also, make sure you put a pair of 1k ohm resistors in series between the 5V pin and GND, then hook a wire from the analog input to the middle of the two resistors. This will reduce the voltage so you can just read it to determine which way you're powered up.

Does this make sense?
John

l8gravely
 
Posts: 21
Joined: Wed Feb 11, 2009 12:58 pm
Location: Worcester, MA

Please be positive and constructive with your questions and comments.