Build a Raspberry Pi Zero keyring

By Lucy Hattersley. Posted

Need a Raspberry Pi in your grab-bag? Here’s how you can get a Raspberry Pi Zero up and running and networked just about anywhere

Although our beloved Raspberry Pi is rightly praised for its diminutive nature, the need for a keyboard, mouse, and monitor can somewhat spoil the portability. If you’ve ever had a moment thinking, “If only I had a Raspberry Pi with me right now”, this is the project for you. More than just a Raspberry Pi-in-a-pocket, we’re going to create a travel-ready Raspberry Pi, free of peripherals but with full desktop support, that can join the local network either via its own wireless LAN hotspot, or a hidden feature of Raspbian: USB networking.

Prepare Raspberry Pi keyring

Even though our portable Raspberry Pi will not require a keyboard, monitor or mouse (well, sort of, you’ll see), we want to get the most out of the device, so start by getting the full ‘Raspbian Stretch with desktop and recommended software’ image from the Raspberry Pi website. Write it to a microSD card using your favourite utility (we used balena Etcher). However, just to show that it’s possible to do all this without ever hooking the Raspberry Pi up to a monitor, don’t boot up just yet.

You'll need

Set up for headless

Reinsert the microSD card into your computer. It will mount a drive called ‘boot’. We’re going to make some changes so Raspberry Pi starts with both WiFi and USB networking enabled. That’s right, Raspbian has a feature that allows it to act like a network device to any computer connected by USB. Simply connect Raspberry Pi to your computer with a USB cable and a private network is set up between them, giving headless access without needing to get Raspberry Pi on the network. It’s not enabled by default, so we’ll make some changes to files in the boot directory to fix this.

Enable WiFi and SSH

In the boot directory, create a file called ssh, with no extension. It doesn’t need any content. On UNIX-style systems, you can enter:

touch ssh

Now, using a text editor, create a file in the same directory called wpa_supplicant.conf and enter the following (change country code if appropriate):

country=gb
updateconfig=1
ctrl
interface=/var/run/wpa_supplicant

network={
scan_ssid=1
ssid="Your network name"
psk="Your network password"
}

Replace the values of ssid and psk to match your WiFi network name and password.

Size matters

When Raspberry Pi boots without a monitor attached (headless), the desktop still starts, but at a very small screen resolution. We’re going to be using VNC to remotely access the desktop, so we need to make it a bit more usable. Using a plain-text editor, open the file config.txt in the boot directory. Be very careful making changes here as it can render your Raspberry Pi unbootable. Scroll down the file until you see two lines as follows:

#framebufferwidth=1280
#framebuffer
height=720

Note the ‘#’ before them; this marks them as comments and so they are ignored. Remove the ‘#’ so it looks like this:

framebufferwidth=1280
framebuffer
height=720

Enable USB networking

Scroll down to the bottom of the config.txt file and add the following line:

dtoverlay=dwc2

This instructs Raspbian to apply the USB networking module. Save and close config.txt. To make sure the module is made available, edit the file cmdline.txt, also in the boot directory. The contents of the file are on a single line. Find the end of the line and, making sure you do not add a new line in the process, add a single space followed by:

modules-load=dwc2,g_ether

Now save and close cmdline.txt, then eject the microSD card.

 Always be careful editing the files in the boot folder. Take backups and use a text-only editor like nano

Connect via USB

It’s time to wake up our Raspberry Pi. Insert the microSD card into Raspberry Pi and connect a micro USB cable to the peripheral USB port on Raspberry Pi Zero, not the usual power USB port. If you’re unsure, the peripheral port is the one nearest the mini‑HDMI connector. We need full USB access for networking, and your computer will also supply enough power to keep the Zero happy. Connect the cable to your computer and wait a few minutes so Raspbian has time to resize the file system and set things up.

Log in

From the command line (users of Windows 8 or before may need to install PuTTY), run these commands:

ssh-keygen -R raspberrypi.local
ssh pi@raspberrypi.local

The first command cleans out any previous SSH keys you may have for raspberrypi.local. You do not need to run it again. Hopefully you’ll now be prompted for a password. Enter raspberry and you’re in. If you find you can’t connect to the Pi, re-examine the changes you’ve made to the boot directory or try booting with a monitor and keyboard. You may connect through WiFi rather than USB. Run ifconfig at Raspberry Pi’s command line to see the different networks.

Configuration

We’ve established we have headless access to Raspberry Pi Zero. While you are logged in, start with some housekeeping. Run this command:

sudo raspi-config

Firstly, change your password from raspberry to something harder to guess. Now go to ‘Network Options’ then ‘Hostname’. Here you can rename your Raspberry Pi to something else if you wish, reducing the risk of it clashing with another Raspberry Pi on the network.

Finally, go to ‘Interfacing Options’ and enable the VNC server. This will allow you to stream the desktop to another device. Reboot Raspberry Pi now so the host name change takes effect.

Desktop access

To stream Raspberry Pi desktop, you’ll need a client for your connected computer. A popular choice is VNC Viewer, available for a wide range of platforms. Once installed, enter your Raspberry Pi’s new host name followed by ‘.local’ as the server and press ENTER. In a few seconds, the desktop will appear on your screen.

You should see a ‘Welcome to Raspberry Pi’ dialog box. Now’s the time to go through these menus and also update the current software when prompted. Raspberry Pi will need a working WiFi connection to do this.

 VNC Viewer

Hotspot access

If you want a truly standalone experience, and internet connectivity isn’t a concern, another connection option is to use the Zero’s WiFi capability and create a hotspot. Then, no matter where you are, so long as you can get power to Raspberry Pi, any computer will be able to connect and SSH or VNC in.

Setting up a hotspot doesn’t take long, but does involve several steps, including setting up a DHCP server and hostapd.

Batteries are included?

If you are taking the hotspot route, why not go the whole nine yards and add battery power? A Zero will run quite happily for hours from widely available USB power banks. If you’re feeling fancy, why not add in a LiPo battery so you have a single self-contained unit? LiPo batteries come with safety concerns (see ‘LiPo safety’ box), so we need some circuitry to manage the battery and make sure it supplies the correct voltage. Pimoroni’s LiPo SHIM does just this with the added bonus of not using up any of your valuable GPIO ports. Carefully solder it to the base of the GPIO pins and you’ve got power. Don’t forget to add a switch.

 Pimoroni’s LiPo SHIM allows safe use of a LiPo battery without blocking GPIO ports

3D Print a case

Obviously, we can’t leave our little Zero unprotected, so our final step is to provide a nice case. If you’re wanting to use GPIO, the official case can be adapted to accept a keyring with a small drill bit and a steady hand. If you’ve added battery power, it may be time to get a CAD package out and design something that fits everything into one. We opted for a 3D-printed case from Thingiverse maker Haunt Freaks, which features a ready-to-go hoop to accept a keyring or lanyard clip.

 Using a 3D printer, you can customise and create your own case

 If you need GPIO access, the official case is perfect with its selection of covers. Make two small holes for a thread loop

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.