In the previous article, we discussed the role of an operating system. We explored about Linux as a popular open-source operating system that can be used and modified to be used for desktop, mobile, server, cloud, mainframe, supercomputer, cluster, and embedded computing. Raspbian, the official OS for the Raspberry Pi itself is based on Debian (a Linux distribution). Other Debian based Linux distributions are also widely used on Raspberry Pi. We also discussed that any Linux distribution is a package of Linux Kernel, GNU utilities, shell, desktop environment, and some default application programs together.
We can also run Windows 10 IoT and Android on Raspberry Pi. Using Raspberry Pi as a Linux machine is popular. As noted earlier, OSS developers generate revenues by selling hardware that runs open-source software by providing customer support for OSS and selling closed source software that runs on OSS. Now think about how the Raspberry Pi Foundation generates revenues.
Raspberry Pi as Linux machine
We will be using Raspberry Pi as a Linux machine. Several Linux distributions can run on RPi without any hassle like Ubuntu Mate, Ubuntu Core, Ubuntu Server, Kali Linux, and Centos, etc. Though any Linux Distro can be used, we will stick to Raspbian, the official OS for Raspberry Pi. Raspbian is a Debian based Linux distribution. It comes with Bash Shell as a default shell and its desktop environment. The latest version of Raspbian (released on September 26, 2019) is based on Linux Kernel 4.19.
As we will focus on Python programming to control electronics, we simply need to set up Raspberry Pi as a Linux machine where we can code, test, and run Python applications. Python is the official programming language for Raspberry Pi that comes with IDLE (integrated development environment) by default. There are two versions of Python that can run on Raspberry Pi – version 2.7 and version 3.x. The latest version of Raspbian has IDLE only for Python 3. If you wish to code your applications in Python 2.7, its IDE can be installed as well. Both versions are incompatible with one another. This can be a source of confusion. So, we will stick to using Python 3.5.
Setting up Raspberry Pi desktop
Before anything else, we first need to set up our Raspberry Pi Linux computer. Setting up RPi desktop is similar to setting up any desktop computer except that we don’t need to assemble CPU. RPi is a single board computer with on-board RAM, processor, input/output, and network interfaces. For setting up the Raspberry Pi desktop, the following hardware components will be required –
1) Raspberry Pi 3B/4B
2) Power Adaptor for Raspberry Pi 3B/4B
3) LCD/LED Monitor
4) HDMI Cable
5) USB Keyboard
6) USB Mouse
7) MicroSD Card
Raspberry Pi 3B and 4B has the following layout –
Simply connect an LCD/LED monitor with Raspberry Pi (3B/4B) by HDMI cable. Plug in a USB keyboard and mouse to USB ports of RPi and plug-in the power adapter to Micro USB power. As we will frequently be using GPIO header with breadboards, it will be better to connect female-to-male jumper wires on 40-pin GPIO as follows:
Now we need to load Raspbian on a MicroSD card. For this, download the latest version of Raspbian from the official website of Raspberry Pi. Extract the ZIP folder and copy the image files to the MicroSD card. If the MicroSD card used is 32 GB or below, it must be formatted to FAT32 (file system) before copying the image files, or if the MicroSD card is more than 32 GB, it should be formatted to exFAT before copying the image files. You will need a MicroSD card reader to transfer image files from your laptop or desktop.
Now insert the card in the MicroSD slot of the RPi. Then, connect power adaptor and power on the display monitor as well as the Raspberry Pi. The display monitor will show a window to choose an operating system. Select Raspbian and click on ‘Install’. It will take a few minutes to mount partitions and install Raspbian on the SD card. During installation, the red LED on Raspberry Pi will blink continuously. Once the installation and boot process is completed, the green LED will start glowing on the Raspberry Pi.
When Raspbian boots for the first time after installation, a welcome window will pop up:
Click ‘Next’ and set Country, Language, and Time Zone in the next window.
Click ‘Next’ and set a password for the root user. The default username is ‘pi,’ and the password is ‘raspberry’. Set the password of your choice.
Click ‘Next’ and connect to a Wi-Fi network if available. Finally, reboot Raspberry Pi.
After completing the installation, it is important to set the keyboard layout. The default keyboard layout in Raspbian is English (UK) or the language of the country specified in the welcome window. If you are using US keyboard (which is most commonly used around the world), navigate to Preferences -> ‘Keyboard and Mouse’.
This is a critical step. With improper keyboard layout selected, the wrong characters will type on the screen. This can be particularly inconvenient and confusing.
Connecting Raspberry Pi to internet
Raspberry Pi can be connected to a network through Ethernet or Wi-Fi. On the first boot, the user can configure Raspbian to connect with a Wi-Fi network available. Even if you have not connected RPi to an internet connection at that time, it can be done later on by navigating to the ‘Network & Internet Settings’ icon on the taskbar.
It will be useful to connect Raspberry Pi with an available network. Internet connection will be required to update Raspbian, install additional packages, upgrade installed applications, and allow browsing through chromium or other installed web browsers.
All Linux distributions come with an interactive utility called ‘Shell’. This utility is used for entering system commands and executing them on the Linux kernel. The shell opens up as a command prompt either as a virtual console (Linux console) or a graphical terminal emulator where the user can type and execute shell commands. These are system commands used to direct Linux to perform OS related tasks like managing files and file systems, handling programs, and managing processes.
There are many shell utilities available for Linux distributions. The default shell in Linux Kernel is Bash Shell. Other popular shell utilities available for Linux include Tcsh/Csh shell, Ash Shell, Zsh Shell, Korn Shell, and Fish Shell. Raspbian also has Bash as the default shell utility.
In early versions of Linux, a user can work on Linux only through the Shell. Linux programmers, system users, and system administrators used to enter shell commands through a command-line interface (virtual console). In most Linux distributions, when Linux boots up, it creates five or six virtual consoles. Some Linux distributions can have even more virtual consoles. The virtual consoles are terminal sessions that run in Linux system memory. The terminal sessions are identified by the term ‘teletypewriter’ and are abbreviated as ‘tty’. Different terminal sessions are identified by the term tty followed by a number. Like tty2 denotes terminal session 2 or teletypewriter 2. The virtual consoles can be seen as command-line interfaces outside the Linux GUI.
In Raspbian, there are five virtual consoles or terminal sessions running after boot up. These virtual consoles can be loaded by pressing keys Ctrl + Alt + F2 to Ctrl + Alt + F6. Like on pressing Ctrl + Alt + F2, virtual terminal 2 (identified by tty2 on the terminal window) gets loaded. On loading a virtual console, the command-line interface for the respective terminal session occupies the entire screen where the user is prompted to enter root login and password. The default login (user) on Raspbian is ‘pi’ and the default password is ‘raspberry’ unless it is changed in the welcome window. It should be noted that in virtual consoles, when typing passwords, nothing shows typing on the screen, unlike in graphical interfaces where ‘*’ is commonly shown typing on the screen for each character of the password. On entering the correct root login and password, the terminal session is initiated where shell commands can be written and executed. This command-line interface is also called Linux Console. The user can return to the graphical interface on Raspbian by pressing Ctrl + Alt + F7.
With the development of desktop environments for Linux, virtual consoles became hidden beneath the graphical interface. With a graphical desktop environment, the command line interface for a terminal session can be started through Graphical Terminal Emulators. These are CLI interfaces within the Linux GUI. These are generally non-login console terminals where the user does not need to enter a login name and password.
Raspbian has LXterminal as the default graphical terminal emulator that can be launched by navigating to Accessories -> Terminal or by clicking on the Terminal icon on the taskbar.
Other popular graphical terminal emulator packages include Eterm, Final Term, Guake, GNOME terminal, Konsole Terminal, LillyTerm, mrxvt, rxvt, rxvt-unicode, ROXTerm, Sakura, Terminology, Terminator, tilda, Wterm, UXterm, xterm, xfce4 Terminal, Yakuake etc. These can be installed as well.
Shell commands can be used to start, stop, or change program execution. The shell commands can also be grouped into files called shell scripts. The shell scripts are simple text files that can be easily accessed and viewed by a system administrator (user). These scripts are portable and can be transferred to any UNIX or UNIX-like system.
While high-level languages like Python, C or Java are useful in manipulating data, developing applications and invoking system calls, the shell scripts can be used to execute programs on boot, automate services and functions, system administration, manage processes, system tasks, files, directories, databases, network, and many other OS-level tasks.
Updating and upgrading Raspbian
Now when your Raspberry Pi Linux computer is all set up and connected to an active internet connection, it’s time to update and upgrade Raspbian. And know what, we will use shell commands to do so.
Open LxTerminal by navigating to Accessories -> Terminal or clicking on the Terminal icon on the taskbar. The APT (Advanced Packaging Tool) tool can be used to update and upgrade Raspbian. In the terminal window, type the following shell command and press ‘Enter’ to update Raspbian –
sudo apt update
To upgrade installed packages to their latest version, type the following shell command and press ‘Enter’ –
sudo apt full-upgrade
The full-upgrade command picks up any dependency changes as well in comparison to upgrade command. Before running the command for full-upgrade, it is always advisable to check disk space by running the following shell command –
The downloaded package files are kept in /var/cache/apt/archives. The following shell command can remove these files –
sudo apt clean
This will free up some space for upgrading packages. The Raspbian can also be upgraded using third party solutions or Mender. Mender is an end-to-end open-source update manager.
Raspberry Pi configurations
Raspberry Pi configuration settings can be accessed by navigating to Preferences-> Raspberry Pi Configuration or running the following command in the terminal window:
In the graphical configuration window, under the ‘System’ tab the user can change the password for default user, change the hostname, select a boot option between graphical and command-line interface, select auto-login, set screen resolution and enable/disable ‘overscan’ and ‘pixel doubling’ options.
If you have not changed the default password in the welcome window, it is highly recommended to do so from the configuration settings. If your screen does not fill the entire display monitor, disable the ‘Overscan’ option. Set the highest possible resolution supported by your display monitor to get the best desktop experience.
Under the ‘Interfaces’ tab, you can enable or disable input/output and network interfaces like Camera, SSH, VNC, SPI, I2C, Serial Port, Serial Console, 1-Wire, and Remote GPIO. We will be enabling many of these options when we will need to control electronic circuits by our Python scripts.
Under the ‘Localisation’ tab, you can change settings for language, timezone, keyboard layout, and Wi-Fi country. If you have installed Rasbian through an iso image unlike by copying extracted image files to the SD card mentioned above, Raspbian might not be using the entire space available on the card. To utilize the entire space available on the SD card, you will need to expand the file system on Raspbian. This can be done by accessing Raspberry Pi configuration settings through the terminal. In the terminal, run following shell command to access Raspberry Pi configuration settings –
Navigate to ‘Advanced Options’ and select ‘Expand Filesystem’.
If you have installed Raspbian by copying extracted image files to the SD card as mentioned above, the file system will be already expanded and using the entire disk space available on the SD card. In such case, on prompting Raspbian to expand file system, you will get the following message
Navigate back and select ‘Finish’ to save changes.
Installing other packages
Raspbian comes with many default packages. Other packages and programs can be installed on Raspbian by using sudo apt—get command for the desired application. Another easier way to explore more packages and install them is through ‘Add/Remove Software’ settings. These settings can be accessed by navigating to Preferences -> Add/Remove Software.
A window will open where packages of different categories can be browsed. Select the desired application and click on ‘Ok’ to install the specific package.
Try installing any office application, media player, and browser of your choice. It will be inspiring to personalize your Raspberry Pi desktop with your favorite application programs.
Now your Raspberry Pi computer is all set up and ready to provide full-fledged desktop experience. In this series, we intend to use Raspberry Pi for controlling electronics and showing how python can be used to program enthralling embedded applications. Python can be used to program a variety of applications like games, 3D graphics, network programming, IoT, web development, artificial intelligence, machine learning, etc. Let us see how creative we can be with the power of Python (a high-level language) and low-level electronics combined. So, in the next tutorial, we will start a discussion on Python. Stay connected; we are going to embark on a long Raspberry Pi journey this winter.