Skip to content
ImagineWorld

Preparing a Headless Raspberry Pi for a Robot

node_idnode-book_learn-robotics-programming_chapter-03-preparing-a-headless-raspberry-pi-for-a-robot
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num3
chapter_titlePreparing a Headless Raspberry Pi for a Robot
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Chapter 3 — Preparing a Headless Raspberry Pi for a Robot

Section titled “Chapter 3 — Preparing a Headless Raspberry Pi for a Robot”

Preparing a Headless Raspberry Pi for a Robot

Section titled “Preparing a Headless Raspberry Pi for a Robot”

In this chapter, we’ll explore the Raspberry Pi Operating System (Raspberry Pi OS**)**, learn why the Raspberry Pi controller on a robot should be wireless and headless, what headless means, and why it is useful in robotics.

You will see how to set up a Raspberry Pi directly as a headless device, how to connect to this Raspberry Pi once it’s on the network, and then send your first instructions to it. By the end of the chapter, you will have your own ready-to-use Raspberry Pi without needing to connect a screen, keyboard, or wired network to it so that it can be mobile in a robot.

We’ll cover the following topics in this chapter:

  • What is Raspberry Pi OS?
  • Understanding headless systems
  • Preparing an SD card with Raspberry Pi OS
  • Setting up Wi-Fi on the Raspberry Pi and enabling SSH
  • Finding your Raspberry Pi on the network
  • Using SSH to connect to your Raspberry Pi
  • Configuring Raspberry Pi OS

To complete the exercises in this chapter, you will require the following:

  • A Raspberry Pi 4 or 3 that you chose in the previous chapter
  • A USB power supply capable of 2.1 amps with the correct cable
  • A microSD card that stores 16 GB or more
  • A Windows ( 10 or later), Linux, or macOS computer connected to the internet that can read/write to microSD cards
  • Your Wi-Fi details
  • A text editor on your computer, e.g., VS Code

Disclaimer: This chapter is based on the hardware components listed in Appendix A: Shopping for Robot Parts. Specific hardware models or component versions may change over time due to product updates or availability.

Raspberry Pi OS is an operating system for Raspberry Pi. An o peratin g s ystem (OS) provides layers above the raw hardware, which offer tools like asking for memory or using hardware services. Built on the operating system is a software distribution, which provides file access, network tools, user interfaces or commands, and tools for everyday use.

Raspberry Pi OS is both an OS and a distribution, specifically a variant of the Debian Linux distribution customized for the Raspberry Pi. The OS’s software is optimized for Raspberry Pi hardware. It has neat ways to configure the features that Raspberry Pi users might need.

We’ll use Linux’s strong support for the Python programming language and the network tools that Linux provides. Raspberry Pi OS is widely used in the Raspberry Pi community and is among the easiest to find answers for when help is needed. It is not the only OS for the Pi, but it is the most useful choice for someone starting on Raspberry Pi.

How will we use Raspberry Pi OS, and which version would we use? We’ll explore the versions later in the chapter, but first, let’s see why we’ll use it headlessly!

A headless system is a computer designed to be operated from another computer via a network. They are useful at times or situations where keyboard, screen, and mouse access to a device is inconvenient—for example, running after a robot with a keyboard on a wire.

Here’s how a conventional setup works with a computer tied to a screen, keyboard, and mouse:

Figure 3.1 – A Raspberry Pi tied to a screen, keyboard, and mouse

Figure 3.1 – A Raspberry Pi tied to a screen, keyboard, and mouse

Figure 3.1 – A Raspberry Pi tied to a screen, keyboard, and mouse

Figure 3*.*1 shows a system where a user sits in front of the device. The Raspberry Pi is tied to a screen, keyboard, and mouse; hence, it is not very mobile. You could attach/detach them as required, but that limits options for driving and getting debug feedback.

There are portable systems designed to dock with Raspberry Pis like this, but when a robot moves, you’ll need to disconnect it or move with the robot.

In this book, we use a robot as a headless device:

Figure 3.2 – A Raspberry Pi on a robot in a headless configuration

Figure 3.2 – A Raspberry Pi on a robot in a headless configuration

Figure 3.2 – A Raspberry Pi on a robot in a headless configuration

The Raspberry Pi in Figure 3*.*2 is mounted on a robot as a headless device. This Raspberry Pi is not weighed down by a screen and keyboard; those are handled by another computer. Code, instructions, and information are sent to and from the Raspberry Pi via a wireless network, from a laptop or smartphone. Many code examples run autonomously, being remotely started/stopped.

We will use a smartphone to drive the robot in *Chapter 7,*Driving the Robot with a Phone and Python. The phone can start and stop autonomous behaviors, view the robot’s status, or just help drive the robot. This Raspberry Pi is free from the screen and keyboard.

Although you won’t usually need a screen and keyboard, it is worth having these around in case you lose contact with the Raspberry Pi and it refuses to respond via the network. Raspberry Pis usually have an HDMI connector, so you can then use a screen and keyboard to connect to it and see what is going on.

To send code and headlessly configure our robot, we will use a Secure Shell (SSH). This sends commands and data to and from a device over a secured, encrypted connection.

The Linux and Raspberry Pi OS are written so that you can use SSH to send command-line instructions to the Pi and transfer files onto it. This makes it a good fit to program a robot.

Raspberry Pi OS comes in two main versions. The main desktop version comes with a lot of bundled software, including Virtual Network Computing (VNC), offering a remote point-and-click desktop much like a normal PC.

There is also a Raspberry Pi OS Lite flavor, which uses less SD card storage; however, the minimal loadout can make getting multimedia systems like camera and sound more problematic. For this reason, we will use the default Raspberry Pi OS desktop system.

Before we flash our SD card, to help to connect to the robot, we will generate an SSH key. Skip this if you have already done so. On your Mac/Linux, bring up a Terminal. In Windows, use Command Prompt. Then, type ssh‑keygen to create an ID to connect with the headless devices. From here, accept the default choices.

Making a Pi headless frees it to roam around. It keeps a robot light by not needing to carry or power a screen and a keyboard. It also encourages you, the maker, to think about autonomous behavior, since you can’t always type commands to the robot.

Let’s get ready for autonomous robotics, using headless Raspberry Pi OS on an SD card for our robot.

To use Raspberry Pi OS on a Raspberry Pi, you need to put the software onto a microSD card. Raspberry Pi created the Raspberry Pi Imager for this SD card. Let’s download it and get the right image on our card:

  1. Visit the Raspberry Pi software downloads page at raspberrypi.org/software, and select the Download for button for your computer, as shown in the following screenshot:Figure 3.3 – Downloading the Raspberry Pi Imager Figure 3.3 – Downloading the Raspberry Pi Imager Figure 3.3 – Downloading the Raspberry Pi Imager
  2. Install this using the instructions from Raspberry Pi.
  3. InsertFigure 3.4 – The Raspberry Pi Imager Figure 3.4 – The Raspberry Pi Imager
  4. We will not click the CHOOSE DEVICE button. Ensure that this filter is not active.
  5. On the screen shown in the preceding image, we select the OS by clicking the CHOOSE OS button in the middle of the screen.
  6. When you select this button, it will bring up a list of OSes to flash on the card: Figure 3.5 – The OS list
  7. Figure 3.5 shows the list of OSes offered by the image. Choose Raspberry Pi OS (64-bit).
  8. Next, click CHOOSE STORAGE:Figure 3.6 – Choosing storage Figure 3.6 – Choosing storage Figure 3.6 – Choosing storage
  9. This will pop up a list of SD cards, which should show the card you are using:Figure 3.7 – The SD card selection Figure 3.7 – The SD card selection Figure 3.7 – The SD card selection Now select your plugged-in SD card to continue.

Your image is nearly ready to write; however, we need to set up Wi-Fi and SSH on this card so that it can be used in a headless robot.

Setting up Wi-Fi on the Raspberry Pi and enabling SSH

Section titled “Setting up Wi-Fi on the Raspberry Pi and enabling SSH”

We’ve selected an image and storage but have not yet written it to the SD card. The Raspberry Pi Imager lets us configure more before we do so:

Figure 3.8 – Raspberry Pi Imager ready to proceed

The preceding screenshot shows the Raspberry Pi Imager, with the OS and storage chosen. There is now a NEXT button illuminated at the bottom right. Click NEXT to start configuring Wi-Fi and SSH.

The following screen will appear:

Figure 3.9 – Option to customize

Figure 3.9 – Option to customize

Figure 3.9 – Option to customize

This screen asks if you want to customize your image by applying OS customisation settings. We will use these to connect our Raspberry Pi to Wi-Fi and SSH. Select EDIT SETTINGS to make the customizations.

It may ask if you would like to prefill the wifi password. If so, click Yes, and then you can skip that part later.

The following advanced settings screen should appear:

Figure 3.10 – The OS Customisation general tab

Figure 3.10 – The OS Customisation general tab

Figure 3.10 – The OS Customisation general tab

The preceding screenshot shows the advanced settings available in the GENERAL tab. Let’s configure each:

  1. Set hostname – this will be the hostname you used to contact your robot. I’ve set mine to learnrob3. It will always have an automatic .local suffix.
  2. Enable Set username and password, and then type a username and password for the Raspberry Pi. I used danny. It could be your name or robot.
  3. Ensure Configure wireless LAN is enabled. You will need to fill in your SSID (Wi-Fi network name) and password.
  4. Set the Wireless LAN country, as Wi-Fi might not work without this.
  5. Tick Set locale settings, and choose your time zone and keyboard layout. Make a note of the username and hostname you used, as you will need them later.
  6. Before youFigure 3.11 – The OS Customisation SERVICES tab Figure 3.11 – The OS Customisation SERVICES tab
  7. Enable SSH – tick this so that the Pi starts with it enabled.
  8. Allow public-key authentication only – select this, and it should enable the default option in the Set authorized_keys for box. This will enable password-less secure access from your computer. If you have a blank box, click RUN SSH-KEYGEN to make an SSH key.
  9. The OPTIONS tab has some non-essential but handy settings for completion.
  10. Click SAVE.

This will return you to the Use OS customisation**?** screen:

Figure 3.12 – OS customization ready to be applied

Figure 3.12 – OS customization ready to be applied

Figure 3.12 – OS customization ready to be applied

This screen now has a YES button enabled. Click it to proceed. The next screen just asks you to confirm if you want to write this to the SD card; click YES again to create the microSD image. This will take some time.

When this is completed, it will tell you that you may remove the card from the reader. Eject the microSD. It is ready to use in the Raspberry Pi. Let’s try it out!

  1. Plug the microSD card into the slot on the underside of the Raspberry Pi. It only fits into the slot in the correct orientation. Do not force it.
  2. Finally, plug the power supply into the side of the Raspberry Pi and connect it to a power supply. You should see lights blinking to show it starting up.

Your Raspberry Pi headless system has now been set up. With this, we have taken a major step toward being mobile.

W arning: Do not insert or remove the SD card while the Raspberry Pi is turned on.

Now, we’ll need to go and find our Raspberry Pi on our network so that we can connect to it.

When Raspberry Pi’s green light has stopped blinking and only the red power light is visible, this indicates that the Pi has finished booting and is connected to the network. However, now you need to find it.

Wi-Fi uses dynamic addresses (DHCP – Dynamic Host Configuration Protocol). Every time you connect Raspberry Pi to your network, it may get a different address. Visiting the admin page on your Wi-Fi router and writing down the IP address works in the short term. However, doing that every time the address changes is frustrating and may not be available in some situations.

Luckily, the Raspberry Pi uses a technology known as mDNS (Multicast Domain Name System) so that nearby computers can find it. A client computer will broadcast a local message to ask for devices with the name learnrob3.local, and the Raspberry Pi will respond with the address to find it. This technology is also known by the names Zeroconf and Bonjour. Recent versions of macOS, Windows and Linux desktops have this built in.

On versions of Windows before Windows 10.1511, please see https://superuser.com/a/491750/58659 for mDNS support.

Let’s test this setup and troubleshoot it.

In Windows, summon a command line by pressing the Windows key and typing CMD in the Windows search bar. In Linux or macOS, open Terminal. We will try to ping the Raspberry Pi (that is, find the Pi on the network) and send a short message to get a response with ping <hostname>.local —for example:

ping learnrob3.local

Be sure to use the hostname you gave to your Raspberry Pi. If everything has gone right, the computer will show that it has connected to the Pi:

PING learnrob3.local (192.168.0.53) 56(84) bytes of data.
64 bytes from 192.168.0.53 (192.168.0.53): icmp_seq=1 ttl=64 time=0.113 ms

You may need to press C trl + C to stop this process. My output shows that it found learnrob3.local at address 192.168.0.53 and sent it some test data. After this, it received a 64-byte response, after 0.113 milliseconds.

The command ping is like a game of ping-pong. You send a message, and the receiving system should return a message to you if it got it.

What if you cannot reach the Raspberry Pi? In the next section, we’ll try some troubleshooting steps.

If the Raspberry Pi does not appear to be responding to the ping operation, these are some steps you can take to try to diagnose and remedy the situation. Try the following:

  1. Sometimes, it’s just not quite done yet, on a first boot. You may need to wait for another set of rapid blinks and retry the ping. It can take a minute or so.
  2. Double-check your connections. You should have seen a few blinks of the green light and a persistent red light. If not, unplug the power, ensure that the SD card is seated firmly and that the power supply offers 2.1 amps, and then try again.
  3. Ensure that you are using the hostname you set previously, followed by .local.
  4. Use your Wi-Fi access point settings with the Raspberry Pi booted, and see if it has taken an IP address there:
    • If you find the Raspberry Pi on your Wi-Fi router, this may mean that mDNS is not running on your computer correctly. Sometimes, you’ll need to use the IP address from the router instead.
  5. If not, turn the power off, take out the SD card, place this back into your computer, and go back through the imager settings, double-checking the Wi-Fi details and country code.
  6. Finally, you may need to boot the Pi with a screen and keyboard connected to attempt to diagnose the issue. The display will tell you whether there are other issues. Use the screen text and search the web for answers. I cannot reproduce all those here, as there are many kinds of problems that could occur. I also recommend asking on the book’s Discord community at https://discord.gg/aKnfbgyRpC, on Stack Overflow, or in the Raspberry Pi Forums at https://www.raspberrypi.org/forums/.

We have now verified that our Pi is connected to the network, troubleshooting issues along the way. We’ve been able to find it with ping. Now we know it is there, let’s connect to it using SSH.

Earlier, when writing the SD card, we enabled SSH. This is a secure remote shell that lets us send instructions and files to and from the Raspberry Pi without having physical access to it.

Windows, Linux, and macOS computers all have a terminal ssh tool to access remote SSH systems, like this Raspberry Pi. Let’s connect to the Raspberry Pi with SSH.

  1. Use the command ssh <username>@<hostname>.local —for example:
  2. The first time you do this, you will be asked to accept a host key:
    The authenticity of host 'learnrob3.local (1ea7:540b:071c:5a3b:f2ed)' can't be established.
    ED25519 key fingerprint is ...
    This is a unique key generated by your Raspberry Pi. Type yes to continue and press Enter. If you have done this before with the same hostname, you may also see this problem:
    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
    Offending ECDSA key in /userfolder/.ssh/known_hosts:39
    If you see this, you will need to edit that file, remove the line for a previous entry, and then try again. I recommend using VS Code to edit it.
  3. You will now be logged into the Raspberry Pi:
    Linux learnrob3 6.1.0-rpi4-rpi-v8 #1 SMP PREEMPT Debian 1:6.1.54-1+rpt2 (2023-10-05) aarch64
    .
    .
    .
    Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law.
    danny@learnrob3:~ $
    Notice that the prompt here, waiting for you to type a command, is <username>@<hostname>:<path> $.
  4. We can test running commands on our Raspberry Pi via the SSH connection. df lets us see how much of the SD card we are using, so that is a good candidate. Type df ‑h, and you should see something like the following:
    Filesystem Size Used Avail Use% Mounted on
    udev 1.6G 0 1.6G 0% /dev
    tmpfs 380M 836K 379M 1% /run
    /dev/mmcblk0p2 29G 1.6G 26G 6% /
    tmpfs 1.9G 0 1.9G 0% /dev/shm
    tmpfs 5.0M 16K 5.0M 1% /run/lock
    /dev/mmcblk0p1 510M 93M 418M 19% /boot/firmware
    tmpfs 380M 0 380M 0% /run/user/1000
    Linux commands have a command name and parameters. The command name df is an abbreviation of disk free. Many Linux commands are abbreviated like this. The parameter ‑h makes df display this output in human-readable numbers. It uses G, M, and K suffixes for gigabytes, megabytes, and kilobytes. This shows that we are using 6% of the SD card and, importantly, that we are connected and working.

You should now be able to connect and run commands on the Raspberry Pi; however, if not, try this troubleshooting section.

If you are not logged in to the Raspberry Pi but were able to ping it in the previous section, there are a few things you can check:

  • You should not get asked for a password. If you are, you may have missed the allow public-key authentication step in preparing the image:
    1. Type the password you set in the set username and password stage, and you should be able to access the Raspberry Pi. 2. If you are unable to log in, ensure that you are using the username and hostname you set previously. Watch out for key modifiers like caps lock. 3. If the password you noted doesn’t work, you may need to write an image again with the above process. 4. Once you are able to log into the Raspberry Pi, log out with C trl + D. 5. At your computer terminal, type ssh‑copy‑id <username>@<hostname>.local. 6. Try to SSH again, and it should not ask for a password.
  • Be aware that a poor-quality Raspberry Pi power supply or USB cable can cause issues. Ensure that you use a 2.1 A capable USB supply and a good cable.

In this section, you used SSH to connect to the Raspberry Pi, setting you up to configure it, send commands to it, and interact with it. Next, we’ll see how to configure it.

Now that we are connected, let’s update the Raspberry Pi software and learn more about its commands while doing so. The following section assumes you are now logged into your Raspberry Pi.

Updating the software on your Raspberry Pi

Section titled “Updating the software on your Raspberry Pi”

Before we proceed to the further program and customize it, you should ensure your Raspberry Pi has up-to-date software. Note that for this to work, the Wi-Fi your Raspberry Pi is connected to should be able to reach the internet.

Apt is the Advanced Package Tool. It’s used to install, update, and manage software on Raspberry Pi OS and other Linux operating systems.

Start the update by typing sudo apt update ‑y && sudo apt upgrade ‑y. You should see something similar to the following:

Hit:1 http://raspbian.raspberrypi.com/raspbian bookworm InRelease
Get:2 http://archive.raspberrypi.com/debian bookworm InRelease [23.6 kB]
.
.
.
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
31 packages can be upgraded. Run 'apt list --upgradable' to see them.
...

This will take some time and might be a great time for a cup of tea!

Please let the Pi continue until it is complete here, and do not interrupt or turn the power off until the <username>@<hostname>:~ $ prompt has reappeared.

What is sudo?

The sudo command is used in front of other commands. It tells Raspberry Pi OS to run the following command (such as apt) as a root user, the Linux administrator/superuser. You can read it as superuser does. This is needed for software that will make changes to the system or perform updates.

W arning: Using sudo can be dangerous, as it will let you run commands to remove all files from the SD card. Only use it when needed.

With the Raspberry Pi up to date, you will be able to install additional software for the robot code. When the software gets stale, apt‑get installations may not work. An update will usually solve that.

When you have finished your session with the Pi, shut it down with sudo poweroff.

Wait for the green light activity to stop; SSH will detect that it has disconnected. You can then safely disconnect the power.

W arning: Pulling power from the Raspberry Pi when it is not expected can cause the loss of files and SD card corruption. You may lose your work and damage the SD card. Always use the correct shutdown procedure.

In this chapter, we learned about the Raspberry Pi OS, which is derived from Linux, how to download it, and how to put this software onto a microSD card for use in the Raspberry Pi.

In this chapter, you’ve seen how to free a Raspberry Pi from a screen and keyboard by making it headless. You have set up an SD card to connect to your Wi-Fi and enable SSH so that you can connect to it. You’ve personalized your Pi and secured it with your own password.

You then took the first small steps in exploring the Linux system it has running on it. You also ensured that the Raspberry Pi is up to date and running the most current software. Finally, we saw how to safely put the Pi into shutdown mode so that filesystem damage does not occur when you unplug it.

We can proceed to use it to build Raspberry Pi-powered gadgets, including robots.

In the next chapter, we will look at ensuring you don’t lose valuable code or configuration when things go wrong. We will learn about what can go wrong and how to use Git, Fabric, and SD card backups to protect our hard work.

Try these challenges to further your knowledge and experience:

  • What other gadgets or projects could you build with a headless Raspberry Pi?
  • What alternative Raspberry Pi OS or Raspberry Pi distributions are there? Explore the options, but be sure to return to Raspberry Pi OS 64-bit before continuing in the book.
  • Try giving your Raspberry Pi a different hostname and connecting to it locally with PuTTY and mDNS.
  • Try other Linux commands on the Raspberry Pi, such as cd, ls, cat, and man.
  • Shut down the Raspberry Pi correctly after trying these.

Please refer to the following to get more information:

  • The Raspberry Pi Foundation guide to installing Raspberry Pi operating systems: https://www.raspberrypi.org/documentation/installation/installing-images/README.md.
  • Raspberry Pi By Example, Ashwin Pajankar and Arush Kakkar: It has a section on alternative OSes for a Raspberry Pi, along with many exciting Raspberry Pi projects.
  • Internet of Things with Raspberry Pi 3, Maneesh Rao: This book uses a wired headless Raspberry Pi for its demonstrations and experiments.