Raspberry Pi Zero to Hero


Raspberry Pi ZeroHello, Lords and Ladies (yes, that’s a Max Payne reference). As you’ve surely guessed from the title of this article, I’m going to introduce you to the Raspberry Pi Zero. That little bugger on the left. It is, in my sense, the best Raspberry Pi there is. The only one worth your time. Why ? Because it’s cheaper than a microcontroller and can be used pretty much just like one. You know I like to mix technologies you don’t normally find associated together… well the Pi Zero is a lot like that. It bridges desktop-level applications and GUI programming with low-level electrical interfaces like I²C and SPI.

There’s only one problem : while the Pi Zero itself is extremely cheap, using it conventionally requires a lot more expensive hardware. You’re supposed to add a USB hub, keyboard, mouse, display, network interface, mass storage, power supply, and even if you get the cheapest, crappiest, second-hand “made in Elbonia” items you’ll still end-up spending ten times what the Pi Zero itself costs. That ain’t right. So we’ll fix that.

By the time you’re done reading this article you’ll be able to setup and operate a Pi Zero with just an SD card and a USB cable. And by “operate”, I mean accessing its Linux desktop from your PC using VNC as well as enjoying Internet access. This is possible through the magic of so-called “USB Ethernet Gadget” mode.

This whole process should take you about an hour if you have a decent Internet connection and enough fingers to operate a keyboard. The longest part is downloading a Raspbian Linux distribution.

How I Look at the Pi Zero

The Pi Zero is essentially the ESP8266 of ARM-based Linux computers :

  • Its price can’t be beat.
  • It’s poorly documented.
  • It can be difficult to get a hold of.
  • But on paper, you get the feeling it’s worth it.

The Raspberry Pi’s are officially educational computers, intended to let people of all ages easily join the wonderful world of computer programming. To that end, they are reasonably convenient boards, with multiple USB ports, Ethernet and even WiFi on the latest models.

The Pi Zero, however, is a different beast. It has been stripped down to basically a processor on a circuit board. That’s why you should regard it as a microcontroller rather than a computer. Oh sure, it can run a browser and a spreadsheet, and at the same time even ! But “run” is an exaggeration. It’s more like walking. No, more like zombie-shuffling.

The Pi Zero has a single ARM core running at 700 MHz. Compared to a PC, it’s not even on the performance map. But compared to a microcontroller ? Now we’re talking.

With this outlook, let’s see how to make proper use of this teeny tiny beast.

What You’ll Need, and Why

You’ll need the PC you’re using right now. I’m assuming, as usual, that you’re among the vast majority of us whose everyday computer runs on a recent flavor on MS Windows. If you’re running something else, you probably know what you’re doing, at least enough to adapt my instructions. Such is the price of being different !

Next, you’ll need a Raspberry Pi Zero, obviously. Just so you know, what I’m about to show you only works on the Pi Zero because of its very special USB OTG port. If you want to use another Pi, you can get similar results by connecting it to your local network and enabling Raspbian’s VNC server. You can also skip much of this article.

You’ll need a typical Micro-USB cable, which will serve two functions : powering the Pi Zero and emulating a network connection over USB.

And finally you’ll need a Micro-SD card of at least 8 gigabytes to install the Pi Zero’s OS on.

Setting-Up the SD Card

Your SD card will need to contain a regular Raspbian distribution of Linux, which you can get from the official download page. At the time I’m writing, the latest Raspbian is Jessie.

Avoid the “Lite” version : it’s command line only. It will work, though, so keep that in mind for applications that don’t require a GUI.

The Raspbian download page will point you to instructions regarding the flashing of Linux images to SD cards. It’s really a painless process if you’re paying attention to what you’re doing.

Once it’s done, though, do not eject the SD card from your computer. The real magic is about to begin.

Customizing the Boot Partition

Open your file explorer and you’ll notice that your SD card now contains a boot partition you can access. At this point, if you stick that SD card into your Pi Zero, it’ll boot expecting a monitor to be plugged into the HDMI socket, and a keyboard into the USB port. Not what we want.

To change that, we’re going to modify two files :

  • “config.txt” : use the Wordpad to edit it, since Notepad can’t deal with Unix-style carriage returns.
  • “cmdline.txt” : here you can use the Notepad because this file doesn’t contain any carriage returns.

Add this tidbit to “cmdline.txt”, just after “rootwait” :

modules-load=dwc2,g_ether

“cmdline.txt” is rather touchy about separators : make sure to use only a single space before and after.

Add this line at the end of “config.txt” :

dtoverlay=dwc2

While you’re here, get to the top of “config.txt”. You should see settings to force a default resolution (framebuffer_width and framebuffer_height) : uncomment them by removing their leading ‘#’. If you don’t, everything will still work but you’ll get a very small desktop resolution. And this being Linux and VNC, mere mortals can’t change it on the fly. Pick something reasonable : 1280 by 720 will be very usable without overloading the Pi Zero.

Now save those two files and close them.

Final touch : create an empty file named “ssh” (no extension) at the root of the SD card. This will tell the system to enable SSH. If you don’t know what SSH is, don’t worry, you’re about to find out.

Fire It Up !

Make sure your PC is connected to the Internet. It should be, if you’re reading this.

Politely ask Windows to give you back your SD card and stick it into your Pi Zero, then use the USB cable to connect said Pi Zero to your PC. You must use the Micro-USB port closest to the HDMI connector of the Pi Zero. The other one is only for powering the board.

Though you won’t get much visual feedback, the Pi Zero will boot. You’ll know it’s done when Windows suddenly detects a new peripheral and start installing it. Do the usual plug-n-play dance. Be patient, this isn’t exactly the most interactive process ever.

Eventually, your Pi Zero will appear in the Windows Device Manager as USB Ethernet/RNDIS Gadget, like so :

0008_pi_zero_win10_device_manager

 

If its icon shows there’s a problem, try double-clicking and finding a solution (such as updating the drivers). If that doesn’t work, wait a minute, unplug the Pi Zero from your PC and plug it back in. Not sure why but this seems to happen when an SD card is booted for the first time.

Talking to the Pi Zero

Now that your Pi Zero has booted and Windows recognizes it as a network interface, you can connect to it using the SSH protocol. SSH stands for Secure Shell. It’s secure because encryption.

You’ll need a terminal program for your PC. I suggest you go with PuTTY. Why ? It’s free, it’s open-source, and it’s lightweight. It doesn’t even need to be installed. Nuff said. Launch it and you’ll be greeted by this dialog box :

0008_pi_zero_putty

Tick SSH and enter “raspberrypi.local” as the host name. That’s the default Bonjour name for your Pi Zero. Bonjour is a protocol intended for simplifying network configuration. Here it saves you having to figure out the IP address for your Pi.

Since you’re connecting to the Pi for the first time, PuTTY will warn you about a security risk. Assuming you trust yourself, bravely click “Yes”. Note that if you hesitate too long, the connection will time out and you’ll need to launch PuTTY again. So type lively !

Enable the Pi’s VNC Server

You should now be looking at a typical terminal window, prompting you for a login. The default Raspberry Pi login is “pi”, password “raspberry”. Enter those magic words and you’re in !

You’ve got command line access to your Pi Zero. That’s enchanting. But our goal is the Pi’s desktop. For that we’ll use the VNC server baked into Raspbian. First we’ll need to activate it.

If you’re new to remote desktop technology, well, first, prepare to have your mind blown. Second, know that VNC is a protocol supported by many operating systems and tools.

As your very first command, enter :

sudo raspi-config

This’ll get you this lovely menu :

0008_pi_zero_raspi_config

It’s is very similar to an old-school PC BIOS setup utility. You can navigate the menu using the arrow keys and the tab key. You’ll want to start with “Expand Filesystem”, so Linux will use the entire SD card. Next you should change your password, mostly because “raspberry” is a pain to type. I suggest something really safe like “sex”. Hey, it’s not like you’re trying to protect your bank accounts here. Now for the important part : go to “Advanced Options” and select “VNC”, then enable it.

Finally, select “Finish” and accept the gracious offer to reboot. PuTTY won’t like this sudden break in communications, just close it. After a short while (when the Pi Zero’s green LED becomes stable) you should be ready for the next part.

Get a VNC Client

Here’s the simplest (though reductive) way to describe VNC : picture the VNC server as the display output of your Pi Zero, and your network connection (in this case, the USB cable) as the display cable. All you need now is a display, and that’s the VNC client.

Those are typically free and do not require installing, but I won’t debate your preferences if you happen to have any. Personally, I find VNC Viewer does an adequate job.

Download and execute it. As with PuTTY, use the Pi’s Bonjour name (raspberrypi.local) to connect. If all has gone well, you’ll soon see this :

0008_pi_zero_vnc

Now we’re getting somewhere ! Play around Raspbian a bit, you’ve deserved it.

Connect the Pi to the Internet

Unbeknownst to you, Windows has installed an RNDIS driver. The technical details can wait, let’s just say it now sees your Pi Zero as if it were connected through a dedicated Ethernet interface. Open Windows’ Control Panel and, under Network (or whatever it’s called in your version of Windows) go to Adapter Settings. There you will find your Pi Zero among all your “actual” network interfaces. It’ll be the one described as a USB Ethernet/RNDIS Gadget.

0008_pi_zero_win10_adapter_settings

We’re now going to bridge that interface to your Internet connection.

The first thing you’ll want to do, to make your life easier, is rename the Pi’s interface to something more evocative than “Ethernet”. Say “Pi Zero”. Right-click it and select rename, then let your muse guide you.

Second step : identify which adapter connects your PC to the Internet. I’m afraid I can’t help you there, each system is different. If you’re a laptop user it’s a safe bet your Internet comes through your WiFi connection.

Right-click your internet connection and select “Properties”. A dialog box comes up. Under the “Sharing” tab you’ll find a tick box and a drop-list. If they aren’t self-explanatory, here’s what you want them to look like before you click “OK” :

0008_pi_zero_internet_bridging

And once you do click OK, congratulations : you’ll have connected your Pi Zero to the World Wide Series of Tubes. Although you will need to shutdown and power-cycle your Pi Zero for this bridge to start working.

Pro tip : VNC Viewer will attempt to auto-reconnect whenever it loses contact with the server on the Pi. Such as during reboots. Once you’ve issued the reboot command (as with Windows : it’s in the Start Menu under “Shutdown”) just leave VNC Viewer to churn and you’ll eventually get back to your Pi’s desktop.

Health Check

You’ve got access to your Pi Zero’s desktop and it’s supposed to be connected to the Internet, so here’s an easy test to check that everything’s working as it should : open a browser and browse to nefastor.com !

Raspbian’s browser, as of version “Jessie”, is Chromium. You’ll find its icon right next to the Raspberry start menu button. It looks like a blue globe. Type in your favorite URL and something like this should show up :

0008_pi_zero_vnc_chromium

Pro tip : as PC’s go, Raspberry Pi’s are slow as glaciers. And the Pi Zero may be the slowest of them all. Whenever you’re unsure your Pi has actually registered your clicks, check out the CPU load monitor in the top right corner of the display. If it just shot to 100%, your Pi is simply busy complying with your demands. And now you know why I think it’s a better microcontroller than a computer.

Conclusion

Congratulations : moments ago you were looking at a small circuit board, and now you’re remote-controlling a Linux computer from your PC. Obviously, since I keep mentioning the Pi Zero is just a microcontroller, you know what’s coming next ain’t no Libre Office tutorial.

Next time I’ll get you started on programming the Pi Zero’s GPIO using C. It’s not nearly as complicated as those Python zealots would have you believe. More importantly, it’s literally a hundred times faster.

After that, we’ll take a first look at integrating the Pi Zero into larger projects while retaining the ability to operate it from a PC.

Phew… I hadn’t written such a long post in a while. Time for my fingers to rest !

 

Leave a comment

Your email address will not be published. Required fields are marked *