To set up your Raspberry Pi, you'll need to connect a mouse, keyboard, and monitor. After completing the initial setup, you can disconnect the keyboard, mouse, and monitor, and instead, control your Raspberry Pi from another computer using the secure SSH network protocol. However, before you get to that stage, you must have the peripherals connected to Raspberry Pi; otherwise, you won't be able to configure it.
Here's the equipment you'll need for setting up Raspberry Pi:
- A keyboard and mouse with USB connectors (standard PC peripherals will work just fine).
- A monitor or television with an HDMI video output and an HDMI cable.
- A 5V power adapter with a Micro-USB connector for connecting to Raspberry Pi.
- An Ethernet cable for connecting to a router or a USB Wi-Fi adapter.
- A MicroSD memory card. A 4GB card is sufficient, but an 8GB card will provide more space for your files and any programs you want to install on Raspberry Pi. In any case, choose a Class 10 memory card to improve system performance.
- A second computer and a MicroSD adapter to prepare the memory card that will go into Raspberry Pi. Alternatively, you can purchase a special NOOBS (New Out Of Box Software) memory card, which comes preloaded with all the necessary software.
The image shows a typical setup for configuring Raspberry Pi. During the initial setup phase, it's essential to connect to a router connected to the internet wherever possible. Once the operating system is installed, you can configure the USB Wi-Fi adapter if you wish to continue working in wireless mode.
Preparing the MicroSD Memory Card with Preinstalled Software
When Raspberry Pi was first introduced, preparing a memory card for it required using special software to write operating system images. However, the NOOBS technology eliminated these complexities. Now, all you need to do is copy the necessary files to the memory card, and no special formatting is required.
You can find the most up-to-date instructions for setting up a MicroSD card using NOOBS at https://www.raspberrypi.org/help/noobs-setup/. When you boot Raspberry Pi from a NOOBS card, you'll be prompted to select an operating system (see image). Be sure to choose the recommended option, Raspbian.
After a lengthy installation process and several reboots, your Raspberry Pi will be ready to use. Now, if you have a USB Wi-Fi adapter, you can configure it and connect to a wireless network using the Wi-Fi Config utility, which you'll find in the Preferences menu of the Raspbian operating system loaded on Raspberry Pi's desktop.
Setting up the Raspberry Pi
Setting up SSH
For many projects and experiments, a keyboard, mouse, and monitor connected to Raspberry Pi are more of a hindrance than a help. The SSH protocol provides network access to Raspberry Pi via a command line from a computer on the same network.
This means that once you've finished setting up Raspberry Pi, you should only have the power cord connected to it, along with either an Ethernet cable or a USB Wi-Fi adapter.
Click on the terminal icon at the top of the Raspberry Pi desktop (Figure 1) and enter the following command in the LXTerminal window (omit the "$" symbol; it's just a command line prompt):
sudo raspi-config
This will open the raspi-config tool for configuring Raspberry Pi. Using the arrow keys, navigate to the Advanced menu and press <Enter>. Then, use the arrow keys again to select SSH (Figure 2).
Next, choose Enable and then execute the Finish command from the menu. SSH protocol for remote access is now enabled on your Raspberry Pi.
It's time to connect from your computer.
If you're using a Linux or macOS operating system on your computer, it has its terminal program capable of connecting to Raspberry Pi.
Finding the IP Address of Raspberry Pi
Before you can connect to Raspberry Pi via SSH from another computer on your network, you need to determine the IP address of Raspberry Pi. To do this, run the following command in the LXTerminal window on Raspberry Pi:
hostname -I
This will return a 4-group number constructed as follows: 192.168.1.23. This is the IP address of your Raspberry Pi.
SSH on a Windows Computer
If your computer runs the Microsoft Windows operating system, you'll need the PuTTY program to work with Raspberry Pi. You don't need to install the program; you can download the PuTTY executable file from http://www.putty.org/. Simply save it somewhere (e.g., your desktop) and double-click to launch it. The PuTTY configuration window will open (Figure 3).
In the Host Name (or IP address) field, enter the IP address of your Raspberry Pi (refer to the earlier section "Finding the IP Address of Raspberry Pi") and click the Open button. The system will prompt you to log in to pi (Figure 3.7). Enter the username: pi and the password: raspberry - that's it, you're in. Now you can enter commands in PuTTY on your main computer, and they will be executed on Raspberry Pi.
SSH on macOS or Linux
If you're using a computer running macOS or Linux, the programs required for connecting to Raspberry Pi are already pre-installed on your computer. Open a terminal session and enter the following command, replacing the Raspberry Pi board's IP address (192.168.1.23) with the address of your Raspberry Pi board:
ssh 192.168.1.23 -l pi
The first time you do this, you'll see the following message:
To proceed, confirm the authenticity of the computer you're connecting to by entering "yes." From this point on, all the commands you enter will be executed on Raspberry Pi, not on your computer.
Linux Command Line
If you're used to working with the Windows or macOS operating systems, you might not have had to interact with a computer through the command line. However, the Linux operating system used by Raspberry Pi requires you to enter commands in the command line for tasks such as installing software, copying and renaming files, running programs, or editing files.
We already used the Raspberry Pi command line when setting up the SSH connection on Raspberry Pi, and now you can execute Raspberry Pi commands via SSH or directly through the LXTerminal.
You might have noticed that once LXTerminal or the SSH session is ready to accept a command, a "$" symbol appears at the end of the line. This is called the command prompt, and it's how Linux indicates that it's ready to receive the next command.
Working with the Linux command line is based on the concept of the current directory. This is the directory you are currently working in. This means that if you want to run a Python program located in a specific directory, you typically need to navigate to that directory before running the program. The command used for such navigation is called cd (short for "change directory").
Right after starting an LXTerminal session, you're in the current directory, which is typically [/home/pi]. This is called the home directory. If there is a folder named "make_action" in the home directory, you can navigate to it by entering the following command (changing the directory relative to the current one):
cd make_action
Alternatively, you can enter the entire path to the directory:
cd /home/pi/make_action
All code for Raspberry Pi is written in the Python programming language. To run a Python program named "test.py," you would use the following command:
python test.py
Another command you'll often encounter is sudo. It's used to run the command that follows it in superuser mode. Linux tries to protect itself from accidental deletion of important system files and the execution of other critical actions by not granting such rights to regular users. The idea is that a user acting as a superuser knows what they are doing. Nevertheless, working in superuser mode is necessary to access the GPIO pins, which you'll need to do quite frequently in this book.
For example, if the "test.py" program uses GPIO pins, you would need to run it with the following command:
sudo python test.py
The screenshot shows the nano text editor in action.
Since nano is designed to work in a command-line environment (such as LXTerminal or via SSH), you'll need to navigate through the file using arrow keys instead of a mouse. When you're ready to save the file, press <Ctrl>+<X>, then press "Y," and finally, press "Enter" to confirm the save.
For many projects and experiments, a keyboard, mouse, and monitor connected to Raspberry Pi are more of a hindrance than a help. The SSH protocol provides network access to Raspberry Pi via a command line from a computer on the same network.
This means that once you've finished setting up Raspberry Pi, you should only have the power cord connected to it, along with either an Ethernet cable or a USB Wi-Fi adapter.
Click on the terminal icon at the top of the Raspberry Pi desktop (Figure 1) and enter the following command in the LXTerminal window (omit the "$" symbol; it's just a command line prompt):
sudo raspi-config
This will open the raspi-config tool for configuring Raspberry Pi. Using the arrow keys, navigate to the Advanced menu and press <Enter>. Then, use the arrow keys again to select SSH (Figure 2).
Next, choose Enable and then execute the Finish command from the menu. SSH protocol for remote access is now enabled on your Raspberry Pi.
It's time to connect from your computer.
If you're using a Linux or macOS operating system on your computer, it has its terminal program capable of connecting to Raspberry Pi.
Finding the IP Address of Raspberry Pi
Before you can connect to Raspberry Pi via SSH from another computer on your network, you need to determine the IP address of Raspberry Pi. To do this, run the following command in the LXTerminal window on Raspberry Pi:
hostname -I
This will return a 4-group number constructed as follows: 192.168.1.23. This is the IP address of your Raspberry Pi.
SSH on a Windows Computer
If your computer runs the Microsoft Windows operating system, you'll need the PuTTY program to work with Raspberry Pi. You don't need to install the program; you can download the PuTTY executable file from http://www.putty.org/. Simply save it somewhere (e.g., your desktop) and double-click to launch it. The PuTTY configuration window will open (Figure 3).
In the Host Name (or IP address) field, enter the IP address of your Raspberry Pi (refer to the earlier section "Finding the IP Address of Raspberry Pi") and click the Open button. The system will prompt you to log in to pi (Figure 3.7). Enter the username: pi and the password: raspberry - that's it, you're in. Now you can enter commands in PuTTY on your main computer, and they will be executed on Raspberry Pi.
SSH on macOS or Linux
If you're using a computer running macOS or Linux, the programs required for connecting to Raspberry Pi are already pre-installed on your computer. Open a terminal session and enter the following command, replacing the Raspberry Pi board's IP address (192.168.1.23) with the address of your Raspberry Pi board:
ssh 192.168.1.23 -l pi
The first time you do this, you'll see the following message:
Code: Select all
The authenticity of host 1192.168.1.23 (192.168.1.23)' can't be established.
RSA key fingerprint is 48:8f:c3:07:c2:04:9e:8b:59:ed:53:2b:0b:d0:aa:e5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added *192.168.1.23' (RSA) to the list of known hosts.
p10192.168.1.23's password:
To proceed, confirm the authenticity of the computer you're connecting to by entering "yes." From this point on, all the commands you enter will be executed on Raspberry Pi, not on your computer.
Linux Command Line
If you're used to working with the Windows or macOS operating systems, you might not have had to interact with a computer through the command line. However, the Linux operating system used by Raspberry Pi requires you to enter commands in the command line for tasks such as installing software, copying and renaming files, running programs, or editing files.
We already used the Raspberry Pi command line when setting up the SSH connection on Raspberry Pi, and now you can execute Raspberry Pi commands via SSH or directly through the LXTerminal.
You might have noticed that once LXTerminal or the SSH session is ready to accept a command, a "$" symbol appears at the end of the line. This is called the command prompt, and it's how Linux indicates that it's ready to receive the next command.
Working with the Linux command line is based on the concept of the current directory. This is the directory you are currently working in. This means that if you want to run a Python program located in a specific directory, you typically need to navigate to that directory before running the program. The command used for such navigation is called cd (short for "change directory").
Right after starting an LXTerminal session, you're in the current directory, which is typically [/home/pi]. This is called the home directory. If there is a folder named "make_action" in the home directory, you can navigate to it by entering the following command (changing the directory relative to the current one):
cd make_action
Alternatively, you can enter the entire path to the directory:
cd /home/pi/make_action
All code for Raspberry Pi is written in the Python programming language. To run a Python program named "test.py," you would use the following command:
python test.py
Another command you'll often encounter is sudo. It's used to run the command that follows it in superuser mode. Linux tries to protect itself from accidental deletion of important system files and the execution of other critical actions by not granting such rights to regular users. The idea is that a user acting as a superuser knows what they are doing. Nevertheless, working in superuser mode is necessary to access the GPIO pins, which you'll need to do quite frequently in this book.
For example, if the "test.py" program uses GPIO pins, you would need to run it with the following command:
sudo python test.py
The screenshot shows the nano text editor in action.
Since nano is designed to work in a command-line environment (such as LXTerminal or via SSH), you'll need to navigate through the file using arrow keys instead of a mouse. When you're ready to save the file, press <Ctrl>+<X>, then press "Y," and finally, press "Enter" to confirm the save.