Raspberry Pi Basics

Options
This discussion has a more recent version.
NickBiederman
edited April 2022 in Maker

Updated 3/14/22

Setting up a Raspberry Pi often involves several basic processes that are the same for every project. This post is meant to serve as a beginner’s guide to some of those processes so you can reference it while working on your project. 


Flashing an OS with the Raspberry Pi Imager

Flashing an operating system to your SD card is one of the most basic tasks when setting up a Raspberry Pi. While you can buy SD cards with an operating system or NOOBS pre-installed, it’s cheaper to start with a blank card and flashing the OS yourself opens up a wide range of specialized operating systems. Some of my favorites include Octopi and Volumio.

Start by downloading the Raspberry Pi imager from the Raspberry Pi website and installing it on your computer. You’ll need administrator access to install and run the program. If you plan to use a specialized image like those listed above, download and save the image file from the publisher as well.

After installing the imager, plug your microSD card into the computer (you may need a card reader, like this two in one version) and launch the imager. Click “Choose OS”.

 

Next, you’ll need to decide if you want to use a default OS (like Raspbian or Retropie) or not. If you want to use a default OS, simply click the name of the OS in the window.


If you’re using a different image, scroll to the bottom and click “Use Custom”. This will open a file explorer you’ll use to navigate to the image you downloaded earlier. Select the image and click “Open”. n this case, I selected "MasterAtariNoobs.img".



Next, click “Choose SD card” and select the appropriate drive in the pop up window. Make sure you don’t select the wrong drive as the drive will be erased.


Finally, click “Write”. The imager will go through a writing phase then a verify phase. Once this is done, the card is ejected and you’re good to go.



Setting up Wi-Fi and SSH for “Headless” systems

Not all projects are conducive to a keyboard, mouse, and monitor. Sometimes it’s better to access your pi via SSH. This is easy if you have an ethernet connection available, but can be problematic if you need to use Wi-Fi. Thankfully, there’s a method for configuring Wi-Fi before even booting your pi.

First, we’ll enable SSH. Plug your SD card into your computer and navigate to the drive labeled “Boot”. Create a file names “ssh” with no file extension and save it here. The file should be empty and not have a file extension like “.txt”. You can use any text editor to create an empty .txt file and save it to the boot drive. You can then rename the file to remove the file extension.


If you’re using Ethernet, you can now eject the card from your PC and boot your Pi. You’ll be able to access it from a terminal using “ssh pi@<ip>”, where <ip> is the IP address assigned your pi. If you’re on Windows, you can use Putty or WSL to connect via SSH. It's important to change your password after enabling SSH. This can be done with Raspi Config, described in the next comment.

Connecting Wi-Fi is similar to enabling .ssh. Create a .txt file named “wpa_supplicant.conf” with the following content:

<p>ctrl_interface=DIR=/var/run/wpa_supplicant
GROUP=netdev</p><p>update_config=1</p><p>country=US</p><p>network={</p><p>&nbsp;&nbsp;&nbsp;&nbsp;ssid="<network name>”</p><p>&nbsp;&nbsp;psk="<password>"</p><p>&nbsp;&nbsp;key_mgmt=WPA-PSK</p><p>}</p>

Replace <network name> and <password> with your network name and network password, but leave the quotation marks. Save the file to the root of your boot (just like the ssh file). Next time you boot your Pi it will automatically connect to your Wi-Fi network.


This will work for almost all home networks in America. If you’re in another country change “US” after “country=” to the 2 letter code for your country. If you use something other than WPA-PSK for network security you’ll need to change the key_mgmt as well. Very few people use a different management scheme, so it's unlikely you'll need to worry about changing key_mgmt.

These files are provided in the “ssh_and_wifi.zip” at the bottom of this post.


Configuring SSH, cameras, SPI, and other I/O

Raspi-config is a very powerful tool for changing settings on your Pi. To access raspi-config, you’ll need to connect a monitor and mouse or SSH into your Pi (see the above comment for more info on SSH). Next, run “sudo raspi-config” from the terminal. This will open the following window:

 

Navigation is done using the up and down arrows to scroll through options, enter to select an option, the right arrow to move from “select” to “finish”. From here you can change all kind of hardware settings. Full documentation is available on Raspberry Pi’s website. Some particularly useful options include the display options, “Expand File System” under “Advanced Options”, and “Wireless LAN” under “System Options”. You can also change your password under “System Options”. This is very important if you have SSH enabled. 

After making the changes you need to make, use the right arrow to highlight <Finish> at the bottom of the screen. Press enter to exit the utility and reboot your pi with the command “sudo reboot”




Backing Up Your Installation - Windows

Having an extra copy of your image can be useful in case an SD card fails or you need to duplicate an installation. For Windows, I like to use Win32 Disk Imager. After downloading and installing the program, connect your Micro SD card to the computer (I like to use this two in one card reader) and launch the program. Click the file icon next to the field “Image Name”.


This will open a file explorer. Navigate to the location you want to save the image and enter the file name in the field at the bottom. In my case, I’m creating “example.img” on my desktop. Make sure you don't choose the same location and name as another image our you'll overwrite it.


Ensure the correct card is elected in the “Device” field (circled in red). Check the box next to “Read Only Allocated Partitions” (circled in green), then click “Read” (circled in blue). The program will read your file system to the file you selected and create a file that can be written to a new SD card as a custom image using the Raspberry Pi imager.



Backing Up Your Installation - macOS, Raspbian, and Linux

Backing up a Raspberry Pi installation on a *nix operating system like Linux, Raspbian, or macOS doesn’t require any third-party tools. We’ll use dd, which reads an input file one block at a time and writes it to an output file. The dd command is very simple, but it’s critical to ensure you choose the correct input and output files. If you specify the wrong files you can easily overwrite important files and potentially damage your system.

The method described below will work for any Linux, macOS, or other *nix system. I took the screenshots on an Ubuntu computer. On other operating systems things might look a little different.

The general form of dd is dd if= inputFile of= outputFile. We’ll use our SD card as the input file, so we need to determine the name of our SD card. Plug your SD card into your computer and open a terminal window. Run the following command and enter your password if prompted:

sudo fdisk -l

This will give you a whole long list of drives connected to your computer like the one below. We’re only worried about the last block which lists portable drives like SD cards and USB drives.

You should see something like this after running fdisk.

I know the card I plugged in is 64gb, so I’m looking for a drive with roughly that capacity. /dev/sdb has a size of 58gb, which is pretty close. That’s my SD card. You’ll notice there’s two devices called sdb1 and sdb1. These are partitions of the same drive. Next, I’ll run dd to copy the SD card to an image file. This is the command I used:

sudo dd if=/dev/sdb of=~/OctoPi.img

You’ll notice I used /dev/sdb for my input file rather than specifying sdb1 or sdb2. This will copy everything from both partitions of the SD card to an image file named OctoPi.img in my home directory. It will look like nothing is happening, but just let it run. This process can take time for large images.

After the process is complete run ls -l in the directory you wrote the image file to. This will show a list of all the files along with their size, which will allow you to verify the image was copied successfully.

 

Ls -l lists all the files in a directory along with their size, making it easy to verify the process completed properly.

 

That’s all you need to do to backup a Raspberry Pi on a *nix system. You can write this .img file to another card the same size or larger using the Raspberry Pi imager.


How to Shrink Your Raspberry Pi Image

Once you read a Raspberry Pi SD card to a .img file using dd or Win32DiskImager as described above, you’ll be left with a very large file. My OctoPi image is around 60GB. This can be a problem if you want to write the image to a smaller SD card, or an SD of the same size with slightly less available memory. Fortunately, Drewsif wrote a script called pishrink that will reduce the image to its smallest possible size. Today we’ll be looking at shrinking images on Windows and Linux. 

Windows

Though pishrink is only officially supported on Linux, Windows Subsystem for Linux allows us to run it on a Windows computer. Start by installing WSL2. You’ll need WSL2, as the script will not run on the original WSL. You can choose any Linux distro you’d like from the Windows store. I’ll be using Ubuntu 20.02.03 LTS.

With WSL2 installed, go ahead and launch your Linux distro. You’ll be greeted with a Linux terminal. Depending on the distro you choose you may need to do some first-time setup. Follow the on-screen prompts to complete the setup.

Next, we’ll need to download the script using wget. Run the following command:

wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh

You’ll see a progress bar and a few other notifications as the script is copied. This will save the script to your home directory, also known as ~.


You’ll see a progress bar as the script is downloaded to ~.

Next, we need to make sure it’s executable. Run the command chmod

+x pishrink.sh

Adding scripts to WSL2’s path isn’t very straightforward, so we’ll just run the script from our home directory. The general form for running pishrink is

sudo pishrink.sh pi.img

We’ll start by using the cd command to navigate to the folder our image is saved in. To navigate to a folder, prepend /mnt/ and the letter of your drive to the path. In my case, the file octo.img is in the root folder of my Z: drive, so I’ll navigate to the folder my image is stored in using

cd /mnt/z

If you save the image in your Documents, your command will look something like

cd /mnt/c/Users/<User_Name>/Documents

Once we’re in our folder, we’ll run pishrink on our image. Use the command

sudo ~/pishrink.sh <Image_Name>.img

This tells our system to look for pishrink.sh in ~, and executes the script on our image. For my image I used the command

sudo ~/pishrink.sh octo.img

As the script runs you’ll see some progress notifications, and once the execution is complete, the script will report the original and new sizes of your file. As you can see in the screenshot below, the script shrunk my OctoPi image by more than 90%.


The script reports progress as it runs as well as the original and new file sizes.

You can now install your image on a smaller SD card using the Raspberry Pi imager as described above. Pishrink shrinks the image in place, so the new image file will have the same name and be in the same folder as the original image.

Linux

Executing pishrink on Linux is a little easier than Windows. I’m using a virtual machine running Ubuntu 20.04, but this will work with most Linux installations. Start by opening a terminal window, and run

wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh

This will download the script. You’ll see some progress information in the terminal as you do this.


You’ll see a progress bar as the script is downloaded to ~.

Next, make the script executable with

chmod +x pishrink.sh

Finally, we’ll add the script to our local path so it can be executed anywhere using the command

sudo mv pishrink.sh /usr/local/bin

Running the script is as simple as using cd to navigate to the directory you wrote your image file to. In my case, I have the file stored in a shared folder mounted under /media/. Once you’re in the folder, run the command

sudo pishrink.sh <Image_Name>.img

I used the following commands to navigate to the shared folder containing my image and shrink it:

cd /media/sf_VM_Shared

sudo pishrink.sh octo.img

As the script runs, you’ll see some progress notifications. Once the script has executed it will report the original size and the new size. In my case it shrunk my image by a little over 90%.


The script reports progress as it runs as well as the original and new file sizes. 

With the image shrunk, you’re ready to write it to another SD card using the Raspberry Pi imager. Pishrink shrinks the image in place, so the new image file will have the same name and be in the same directory as the original image.


    Looking for more information about Raspberry Pi? We’ve got a Hobby Board section of the community, as well as Hobby Board guides like The Definitive Micro Computer Buying GuideRaspberry Pi Basics, and How to Create a Retro Game Console with Raspberry Pi! And if you can’t find what you’re looking for, don’t hesitate to post a new discussion and the Community will be happy to help!

    We love seeing what our customers build

    Submit photos and a description of your PC to our build showcase

    Submit Now
    Looking for a little inspiration?

    See other custom PC builds and get some ideas for what can be done

    View Build Showcase

    SAME DAY CUSTOM BUILD SERVICE

    If You Can Dream it, We Can Build it.

    Services starting at $149.99