ef86
Posts: 30
Joined: Tue Apr 26, 2016 3:43 pm

RPI 4 Gadget Mode

Mon Aug 03, 2020 7:47 am

Using the rpi4 gadet mode feature and SSH over USB to an Android phone what would be the best way of powering the pi as I doubt the smartphone is up to it.

Is there a USB c to c cable that is data only so the pi can be powered via the gpio pins from a power bank. Or does anyone know of a USB c otg y cable that allows power in one side and data on the other.

cleverca22
Posts: 843
Joined: Sat Aug 18, 2012 2:33 pm

Re: RPI 4 Gadget Mode

Mon Aug 03, 2020 7:54 am

you could also use a jumbled mess of adapters:

first, plug this into the phone, to get a normal usb-a host port:
Image

then you can use a usb Y cord that has A host on the device end

then chain that to an A<->C adapter for the pi4

and now youve got a mess of 3 adapters, a pi4, a phone, and a battery bank!

ef86
Posts: 30
Joined: Tue Apr 26, 2016 3:43 pm

Re: RPI 4 Gadget Mode

Mon Aug 03, 2020 8:18 am

Thanks.. Will pull out the cables and adapters box. But obviously trying for the neatest solution because lots of adapters will be a recipe for power glitches.

User avatar
Imperf3kt
Posts: 3674
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: RPI 4 Gadget Mode

Mon Aug 03, 2020 8:43 am

I haven't done this myself, but you could take a regular (not e-marked) USB-C cable and snip the 5v wire, insulate and isolate the two halves, then add 5v and ground over the GPIO.

Do not cut any other wires, including ground or it will not work.

No guarantees that the phone will recognise the OTG connection without supplying 5v though, that would depend on the phone software.
55:55:44:44:4C
52:4C:52:42:41

cleverca22
Posts: 843
Joined: Sat Aug 18, 2012 2:33 pm

Re: RPI 4 Gadget Mode

Mon Aug 03, 2020 8:47 am

have a look at https://twitter.com/whitequark/status/1 ... 2274015232 to see what a fun mess usb-c can be

you wind up with problems like you cant charge a nintendo switch from a laptop, because the switch wants to dump its battery into the laptop instead!

ef86
Posts: 30
Joined: Tue Apr 26, 2016 3:43 pm

Re: RPI 4 Gadget Mode

Mon Aug 03, 2020 11:19 am

Yes USB c is a daft minefield.

cleverca22 wrote:
Mon Aug 03, 2020 8:47 am
have a look at https://twitter.com/whitequark/status/1 ... 2274015232 to see what a fun mess usb-c can be

you wind up with problems like you cant charge a nintendo switch from a laptop, because the switch wants to dump its battery into the laptop instead!

User avatar
thagrol
Posts: 3178
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: RPI 4 Gadget Mode

Mon Aug 03, 2020 12:37 pm

ef86 wrote:
Mon Aug 03, 2020 8:18 am
Thanks.. Will pull out the cables and adapters box. But obviously trying for the neatest solution because lots of adapters will be a recipe for power glitches.
Assuming you have a smart phone, drop USB. Use WiFi. Set your phone to act as a hotspot (almost all smart phones can) connect your 4B over that.

As for using a Y cable, I wouldn't. At least not without taking steps to prevent power transfer to/from your phone. If you don't your 4B will end up drawing from your phone if/when the voltage on the battery pack drops below that of the phone. Then there's the problem of one source trying to back feed the other.

Powering via GPIO while connected to the phone? You might get away with it but I wouldn't do so without cutting the +5v wire in the USB cable.

If memory serves, and if your phone has micro USB not USB C, there is a standard method using a resistor to tell the phone not to supply power when in USB host mode. Don't have the details to hand so hit your favourite search engine.
Arguing with strangers on the internet since 1993.

ef86
Posts: 30
Joined: Tue Apr 26, 2016 3:43 pm

Re: RPI 4 Gadget Mode

Mon Aug 03, 2020 4:25 pm

No it's USB C and WiFi and LAN are not an option as they will be used for monitoring traffic.

I did wonder if a powered USB hub could be an option might need a better battery to power it but USB A 2 C cables are easily sourced.

ef86
Posts: 30
Joined: Tue Apr 26, 2016 3:43 pm

Re: RPI 4 Gadget Mode

Mon Aug 03, 2020 5:24 pm

Anyone have the pinout of the PI 4 USB c port?

User avatar
thagrol
Posts: 3178
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: RPI 4 Gadget Mode

Mon Aug 03, 2020 5:34 pm

ef86 wrote:
Mon Aug 03, 2020 5:24 pm
Anyone have the pinout of the PI 4 USB c port?
The internet almost certainly does :) but about all that's connected to it on the Pi end is +ve, ground, USB 2 D+, USB 2 D-, and a couple of resistor so smart/emarked cables and changes work with it.

Those connections will be in the standard places.
Arguing with strangers on the internet since 1993.

trejan
Posts: 2234
Joined: Tue Jul 02, 2019 2:28 pm

Re: RPI 4 Gadget Mode

Mon Aug 03, 2020 5:41 pm

ef86 wrote:
Mon Aug 03, 2020 5:24 pm
Anyone have the pinout of the PI 4 USB c port?
https://www.raspberrypi.org/documentati ... educed.pdf has the pinout. It isn't particularly useful as already noted by thagrol above.

User avatar
thagrol
Posts: 3178
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: RPI 4 Gadget Mode

Mon Aug 03, 2020 5:49 pm

ef86 wrote:
Mon Aug 03, 2020 4:25 pm
No it's USB C and WiFi and LAN are not an option as they will be used for monitoring traffic.
Couple of followup questions:
  • Have you considered using bluetooth? It has a network profile.
  • Regarding WiFi, will you be using both bands: 2.4 and 5 GHz?
Lastly, I'm curious. What's you use case? I'm not sure I can come up with a legitimate reason for a battery powered rig that does network snooping.
Arguing with strangers on the internet since 1993.

ef86
Posts: 30
Joined: Tue Apr 26, 2016 3:43 pm

Re: RPI 4 Gadget Mode

Mon Aug 03, 2020 6:55 pm

Purely research and mapping, the alternative is to SSH over the GPIO and power normally.

ejolson
Posts: 5477
Joined: Tue Mar 18, 2014 11:47 am

Re: RPI 4 Gadget Mode

Tue Aug 04, 2020 4:43 am

ef86 wrote:
Mon Aug 03, 2020 6:55 pm
Purely research and mapping, the alternative is to SSH over the GPIO and power normally.
All you need is a powered USB2 hub that can deliver enough 5V current for the 4B and some short USBA to USBC cables.

The cables

https://www.amazon.com/gp/aw/d/B07MFZM8WZ

worked for me.

Note, since only the USB2 data lines are connected in the Pi 4B USBC socket, you don't need a fancy USB3 hub with power delivery (which may not even work if you've got an older Pi 4B that gets detected as an audio adapter). I think there are plenty of powered USB2 hubs that would work. I made my own from

Image
https://www.amazon.com/Sabrent-Portable ... 00L2442H0/

It comes apart just like in the picture after a little prying with a knife. Since the hub is USB2, the back side of the sockets have four legs: ground, power and the pair for data. I found it easily to cut the legs for 5V power with a razor, bend them up off the circuit board and wire in a suitable power supply with a fuse.

I now have four such hubs which all pull power from an old ATX supply that had a strong 5V rail. More details are in

viewtopic.php?f=36&t=279176

ef86
Posts: 30
Joined: Tue Apr 26, 2016 3:43 pm

Re: RPI 4 Gadget Mode

Tue Aug 04, 2020 5:22 am

Thank you this will work....

ejolson
Posts: 5477
Joined: Tue Mar 18, 2014 11:47 am

Re: RPI 4 Gadget Mode

Tue Aug 04, 2020 5:40 am

ef86 wrote:
Tue Aug 04, 2020 5:22 am
Thank you this will work....
Upon rereading your original post, I don't understand why you want to run the Pi in gadget mode rather than the other way around. Every Android phone I've ever owned is happy to run in gadget mode, either tethered as an Ethernet device or using the Android debug bridge.

When USB tethering the phone, you'd just power the Pi normally and then plug the phone into one of the host USB ports on the Pi behind the PCIe lane.

ef86
Posts: 30
Joined: Tue Apr 26, 2016 3:43 pm

Re: RPI 4 Gadget Mode

Tue Aug 04, 2020 7:46 am

It's so we can SSH from the phone to the pi tethered. I want to keep the onboard WiFi, Bluetooth an LAN free for monitoring.

ejolson wrote:
Tue Aug 04, 2020 5:40 am
ef86 wrote:
Tue Aug 04, 2020 5:22 am
Thank you this will work....
Upon rereading your original post, I don't understand why you want to run the Pi in gadget mode rather than the other way around. Every Android phone I've ever owned is happy to run in gadget mode, either tethered as an Ethernet device or using the Android debug bridge.

When USB tethering the phone, you'd just power the Pi normally and then plug the phone into one of the host USB ports on the Pi behind the PCIe lane.

User avatar
thagrol
Posts: 3178
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: RPI 4 Gadget Mode

Tue Aug 04, 2020 11:52 am

ef86 wrote:
Tue Aug 04, 2020 7:46 am
It's so we can SSH from the phone to the pi tethered. I want to keep the onboard WiFi, Bluetooth an LAN free for monitoring.

ejolson wrote:
Tue Aug 04, 2020 5:40 am
ef86 wrote:
Tue Aug 04, 2020 5:22 am
Thank you this will work....
Upon rereading your original post, I don't understand why you want to run the Pi in gadget mode rather than the other way around. Every Android phone I've ever owned is happy to run in gadget mode, either tethered as an Ethernet device or using the Android debug bridge.

When USB tethering the phone, you'd just power the Pi normally and then plug the phone into one of the host USB ports on the Pi behind the PCIe lane.
At the network level (ssh etc) it doesn't matter which device is the master and which the slave at the USB level. There may even be a small advantage in running the phone as the USB gadget: it won't try to push power out the USB port. It may try to draw power from it though.

Edit:
The downside with using the phone as the gadget is that you'll probably have to take steps on the phone to prevent to pi going out to teh internet and using up your cellular data.
Arguing with strangers on the internet since 1993.

Return to “Advanced users”