Connecting to a Raspberry Pi Zero with just an USB cable (I)

{,}
October 2nd, 2017

The goal of this post is to explain how you can power, connect  and share internet with your Raspberry Pi Zero from your laptop with just one USB cable. After you’ve set it up correctly, connect the cable, let the Pi boot,  ssh to your Pi with this simple command ssh pi0 or update your pi with this oneliner ssh pi0 "sudo apt update && sudo apt dist-upgrade -y".Your Raspberry Pi Zero will have internet access over USB with higher speeds than most WIFI connections.
lll
Raspberry Pi’s are little computers, that are fun to play and develop with, and that come with an affordable price tag. The cheapest version is a Raspberry Zero that can be bought for around 5$ (5€). There is also a variant with integrated Wifi and Bluetooth for twice the price: ca 10$ or 10€.

That Wifi versions seems to be the version to go for – what is a computer without network access these days? – and the Zero only has one USB micro port, so you’ll need some extra peripheral equipment, like a WIFI adapter and a USB OTG cable and a hub or something. Not so.

Welcome USB On-The-Go (OTG)

Happily the Zero has a special trick, an USB OTG chip. That means the Zero can switch USB states (MASTER/SLAVE or HOST/DEVICE), so it can use the USB port just like any other computer (HOST), but also like an USB device, or Gadget, (DEVICE), so it can mimic a keyboard, mouse, USB flash drive/stick, a Webcam or even as an USB ethernet device.

That ethernet functionality is what you need to connect (and power!) your Raspberry Zero. So you’ll need only one ordinary USB cable, the same one that you to use to connect or charge your phone.
But that’s not all, that same USB cable can be used to enable the Pi Zero to access internet. And that USB cable will give your Pi Zero internet at higher speed than most WIFI connections!

One USB cable does it all!

Yes, you can power, access, and provide internet access to your Raspberry Pi Zero with only one USB cable connected to your laptop. From the start, you’ll never need an power adapter, keyboard, mouse or (mini)HDMI cable nor a monitor or screen. ;), to configure your RPI Zero

How do we do that?

Download the latest Raspbian image from here.

We will guide here to do this in Linux, after all, that’s is free software to use.

You’re FREE to CHOOSE proprietary software, but not FREE to USE it, and that is the same for others. What is more fun than freedom, so stop giving tutorials that target Windows and Apple. Free yourself and others, and dump that proprietary shit! We all can use open source software and adapt it to our needs. So we help others by helping ourselves, for all a better world.

Write the image to a Micro SD card.

I advise to use the comand line for this dd. It’s easy, just a one liner, but be careful here, because if you write to the wrong device you can overwrite your harddisk. Check the device-name double. You can find the instructions here.

After you’ve done that, unplug and plug the SD car back in your PC, so it will mount. Browse to the boot partition.

Append this to config.txt:

# Enable USB OTG like ethernet
dtoverlay=dwc2

Above will enable the OTG libraries on boot.

Write an empty file called ssh to the disk (to enable ssh)

touch ssh

Edit cmdline.txt and insert after rootwait ` modules-load=dwc2,g_ether `

That’s all, eject the SD card, insert it in your Raspbery pi zero, connect an USB cable to the correct USB port, the USB data one, the one to the middle, labeled `USB`, not the one labeled `PWR`, we want a connection not just to power the Pi. Don’t be afraid your pi will be powered too this way.

Let the Pi boot, around 60-90 seconds, until the led is solidly glowing green. No it’s booted. A flashing led, means it’s busy using the SD card.

Time to connect to the Pi.

On Linux, Ubuntu (16.04LTS) or Fedora 26, an new internet connection will be visible, something like `Ethernet Network (Linux …. RNDIS/Ethernet Gadget)`. For now you’ll laptop will see this connection, but not be able to use it. It will try to connect to it, as if the Pi is an access point.

For that edit the connection in `Edit connections` and set in the IPv4 tab the Method to `Link-Local Only`. Now it will establish a solid connection.

Connect to your Pi

ssh pi@raspberrypi.local

Provide the password and you’re in.

For now the Pi doesn’t have internet connection itself. For that, disconnect the established connection on your laptop, change the method to `Shared to other computers`, connect, disconnect, change back to Link-Local only, connect, ssh and now your pi has internet access. That feels like a lot of steps, and we will fix that later.

Update your pi:

`sudo apt update && sudo apt dist-upgrade -y`

For now after every restart/reboot the pi will choose a random ID and MAC-address, so the laptop will create another connection. That’s annoying, we will fix those issues in part two of this post.

Tags: ,

5 Responses to “Connecting to a Raspberry Pi Zero with just an USB cable (I)”

  1. Jim Says:

    Awesome!

  2. Dave Says:

    Network bridge worked.

    sudo pico /etc/network/interfaces
    Added following lines:
    bridge_ports eno1 enp0s29u1u5
    bridge_stp off
    bridge_fd 0
    bridge_maxwait 0

    sudo /etc/init.d/networking restart

    SSH works (IP address now allocated by my router)
    Internet works

  3. How to run or boot Raspbian on a Raspberry Pi Zero without an SD-card. - dev.webonomic.nl Says:

    […] an earlier post I explained how you can run and connect to a Raspberry Pi Zero with just an USB cable. Still the […]

  4. Connecting to a Raspberry Pi Zero with just an USB cable (II) - dev.webonomic.nl Says:

    […] What's up? « Connecting to a Raspberry Pi Zero with just an USB cable (I) How to run Firefox headless and make a screenshot […]

  5. 4 ways to connect your Raspberry Pi 4 to the internet - dev.webonomic.nl Says:

    […] more details, see these posts Connecting to a Raspberry Pi Zero with just an USB cable (I) or Connecting to a Raspberry Pi Zero with just an USB cable […]

Leave a Reply