Raspberry Pi-5 Essentials
Raspberry Pi-5 Essentials
books books
books
Raspberry Pi 5
Essentials Raspberry Pi 5
Program, build, and master over 60 projects
Essentials
B u s = 0 , device=0
The Raspberry Pi 5 is the latest single-board computer from the Raspberry Pi
Program,) build, and master over 60 projects
#
spi.open(0, 0
Foundation. It can be used in many applications, such as in audio and
with
0000Python
0
video media centers, as a desktop computer, in industrial controllers,
robotics, and in many domestic and commercial applications. In addition
p e e d _ h z = 3 9
_s
spi.max
Prof Dogan Ibrahim has a BSc
to the well-established features found in other Raspberry Pi computers, (Hons) degree in Electronic
R I E D output
S
# GPIO26 is C
the Raspberry Pi 5 offers Wi-Fi and Bluetooth (classic and BLE), which Engineering, an MSc degree in
ED •
T
Automatic Control Engineering,
makes it a perfect match for IoT as well as in remote and Internet-based
CS = LED(26)
and a PhD degree in Digital Signal
• T
# Disable CESS T
control and monitoring applications. It is now possible to develop many Processing and Microprocessors.
real-time projects such as audio digital signal processing, real-time digital
CS.on()
filtering, real-time digital control and monitoring, and many other real-time Dogan has worked in many
operations using this tiny powerhouse. organizations and is a Fellow of
the Institution of Engineering
ta in „d
and Technology (IET) in UK as
a
The book starts with an introduction to the Raspberry Pi 5 computer well as a Chartered Electrical
D A C . T h e d
ements the
and covers the important topics of accessing the computer locally and Engineer. He has authored over
remotely. Use of the console language commands as well as accessing 100 technical books and over
u n c t i o n i m p l
and using the desktop GUI are described with working examples. The 200 technical articles on electronics,
# This f
# to the DAC
microprocessors, microcontrollers,
remaining parts of the book cover many Raspberry Pi 5-based hardware
and related fields. Dogan is a
projects using components and devices such as certified Arduino professional and
has many years of experience with
> LEDs and buzzers numerous types of microprocessors
:
def DAC(data) # Enable CS
> LCDs and microcontrollers.
> Ultrasonic sensors
>
>
Temperature and atmospheric pressure sensors
The Sense HAT
CS.off()
> Camera modules
#
yte
Example projects are given using Wi-Fi and Bluetooth modules to send # Send HIGH b
and receive data from smartphones and PCs, and sending real-time
# # Get upper
8) & 0x0F
temperature and atmospheric pressure data to the cloud.
> 0
temp = ( d a t a > # OR with 0x3
All projects given in the book have been fully tested for correct operation.
0x30
Only basic programming and electronics experience are required to follow Elektor International Media
temp = temp + # Send to DAC
mp])
spi.xfer2([te
the projects. Brief descriptions, block diagrams, detailed circuit diagrams, www.elektor.com
and full Python program listings are given for all projects described.
Readers can find the program listings on the Elektor Store website,
www.elektor.com (search for: book title).
#
Dogan Ibrahim
Dogan Ibrahim
● All rights reserved. No part of this book may be reproduced in any material form, including photocopying, or
storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this
publication, without the written permission of the copyright holder except in accordance with the provisions of the
Copyright Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licencing Agency
Ltd., 90 Tottenham Court Road, London, England W1P 9HE. Applications for the copyright holder's permission to
reproduce any part of the publication should be addressed to the publishers.
● Declaration
The author, editor, and publisher have used their best efforts in ensuring the correctness of the information contained
in this book. They do not assume, and hereby disclaim, any liability to any party for any loss or damage caused by
errors or omissions in this book, whether such errors or omissions result from negligence, accident or any other cause.
All the programs given in the book are Copyright of the Author and Elektor International Media. These programs
may only be used for educational purposes. Written permission from the Author or Elektor must be obtained before
any of these programs can be used for commercial purposes.
Elektor is the world's leading source of essential technical information and electronics products for pro engineers,
electronics designers, and the companies seeking to engage them. Each day, our international team develops and delivers
high-quality content - via a variety of media channels (including magazines, video, digital media, and social media) in
several languages - relating to electronics design and DIY electronics. www.elektormagazine.com
●4
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.4 Accessing your Raspberry Pi 5 Console from your PC – the Putty program . . . . . . . 20
3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.3.5 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
4.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
●5
4.2.4 Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.2.6 Wi-Fi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
6.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
7.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
7.4 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
7.8 Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
7.10 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
7.11 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
●6
7.26 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
●7
9.9 Project 6 – Analog temperature sensor thermometer – output to the screen . . . . 156
●8
11.3 Project 1 – Generating a square wave signal with any peak voltage up to +3.3 V . 205
●9
14.4 Project 2 – Two-way communication with the smartphone using TCP/IP . . . . . . 262
15.4.2 Project 4 – Play audio (e.g. music) on Bluetooth speaker via Raspberry Pi 5 . . 303
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
● 10
This book is about the Raspberry Pi 5 computer and its use in various control and moni-
toring applications. The book explains in simple terms and with many tested and working
example projects how to configure the Raspberry Pi 5 computer, how to use the latest
operating system (Bookworm), and how to write application programs using the popular
Python programming language.
The book starts with an introduction to the Raspberry Pi 5 computer and covers the impor-
tant topics of accessing the computer locally and remotely. Use of the console command
language as well as accessing and using the desktop GUI have been described with working
examples.
The remaining parts of the book cover many Raspberry-Pi-5-based hardware projects us-
ing components and devices such as LEDs, buzzers, LCDs, ultrasonic sensors, temperature
sensors, Sense HAT, camera modules, etc. Example projects are given using Wi-Fi and
Bluetooth modules to send and receive data from smartphones, from the PC, and sending
real-time temperature and atmospheric pressure data to the cloud.
All the projects presented in the book have been tested and are working. Complete circuit
diagrams and full program listings are given for each project, with detailed descriptions of
the operation of each project. The following subheadings are used in every project wher-
ever necessary:
• Project title
• Project description
• Block diagram
• Circuit diagram
• Program listing
• Suggestions for future work
● 11
I hope the readers find the book helpful and enjoy reading it, and use a Raspberry Pi 5 in
their next new projects.
● 12
1.1 Overview
The Raspberry Pi 5 is the latest credit card size computer from Raspberry Pi. In this chapter,
we will look at the specifications of this new computer and compare it with the Raspberry
Pi 4.
The Raspberry Pi 5 is claimed to have two or three times the processing power of The
Raspberry Pi 4, which is already a very popular single board computer. The Raspberry Pi 5
is currently available in 4 GB and 8 GB memory capacities, but smaller memory devices
may appear later. Although the Raspberry Pi 5 is the same size and shape as the Model 4B,
it has a number of interesting new features such as PCIe connector, power button, built-in
real-time clock and some others that we will investigate in this chapter.
The Raspberry Pi 5 is based on a 2.4 GHz Cortex-A76 ARM processor with a new south-
bridge for handling the peripheral interface. A new VideoCore VII GPU is provided with
800 MHz speed. The dual camera interface is another nice feature of the Raspberry Pi 5.
The microSD card interface now supports cards that work at much higher speeds.
Table 1.1 shows a comparison of the Raspberry Pi 4 and 5. Notice that both devices have
dual 2 × 4kp60 HDMI display interfaces, although Pi 5 supports HDR output. The 2 × 20 pin
GPIO interface is the same in both devices. The Raspberry Pi 5 additionally has two camera
interfaces, a PCIe bus connector, a UART interface, an RTC clock power connector, and a fan
power connector. Wi-Fi and Bluetooth are supported by both devices. The on-board power
switch on Pi 5 is a useful addition and was requested by many users. Pi 5 is powered from
5 V/4 A USB-C type power supply, where Pi 4 is powered from a 3 A power supply. Pi 5 is
slightly more expensive than Pi 4.
Raspberry Pi 4 Raspberry Pi 5
SoC BCM2711 SoC BCM2712 SoC
Cortex-A72 CPU at 1.8 GHz Cortex-A76 CPU at 2.4 GHz
CPU 4 core 4 core
Instruction set ARMv8-A ARMv8-2
Display 500 MHz VideoCore Vi GPU 800 MHz VideoCore VII GPU
L2 Cache 1 MB (shared) 2 MB
L3 Cache None 2 MB (shared)
RAM 1, 2, 4, 8 GB LPDDR4 4, 8 GB LPDDR4X
SD Card microSD microSD (high speed SDR104
compatible)
GPIO 2 × 20 pin 2 × 20 pin
● 13
There are two micro-HDMI based monitor ports on both devices, with both having the same
specifications.
The Ethernet port and USB ports are swapped. As a result of this, the Raspberry Pi 4 case
is incompatible with the Pi 5 and a new case is required.
The camera and display connectors on the Raspberry Pi 5 are 15-pin and smaller, instead
of the original 22-pin connector used on Pi 4. A ribbon cable with 22-pin on one side and
15-pin on the other side is required to connect an existing Raspberry Pi 4 camera to the
Raspberry Pi 5. The Raspberry Pi 5 has two connectors, allowing two cameras or DSI dis-
plays (or a mix of either) to be connected. The PCIe connector is for fast external PCIe
compatible peripherals, such as SSDs.
The new power button on the Raspberry Pi 5 could be very useful. When the device is On,
pressing the button brings the shutdown (logout) menu. A safe shutdown will occur with
another press of the power button.
Figure 1.1 shows the front view of the Raspberry Pi 5 with the components labelled for
reference.
● 14
The Raspberry Pi 5 gets rather hot, and it is recommended to use a cooler to lower the
CPU temperature. Although the idle CPU temperature is around 50°C, it can go higher than
85°C under a stress test. An active cooler is available for the Raspberry Pi 5. Holes and
power points are provided on the board to install and power the active cooler. Figure 1.2
shows the Raspberry Pi 5 with the active cooler installed. The active cooler cools down the
SoC, RAM, and the southbridge chip. When the CPU is idle, the active cooler keeps the CPU
temperature at around 40°C. The fan of the cooler operates automatically when the CPU
temperature goes just above 50°C.
The Raspberry Pi 5 operating system (OS) is based upon Debian 12 with the code name
Bookworm. This OS, released in July 2023, comes with a new Python interpreter (Py-
thon 3.11). This means that a Python package cannot be installed using the pip commands.
● 15
Another major software change is that the RPi.GPIO library (created by Ben Croston) was
not available at the time of writing this book. As a result of this, all the GPIO-based Python
programs in the book have been developed using the gpiozero library. Most third party
HATs are based on RPi.GPIO and these will not work until their software is changed by their
manufacturers. It is hoped that the manufacturers will change their software by the time
Raspberry Pi 5 becomes officially widely available.
● 16
Chapter 2 • I
nstalling the Raspberry Pi 5 Operating
System
2.1 Overview
The Raspberry Pi 5 operating system Bookworm is available either on a pre-installed mi-
croSD card, or you can download the operating system image on a blank microSD card. In
this chapter, you will learn to install the operating system using both methods.
• You should see the Raspberry Pi booting the first time and asking you various
questions to set up the device, such as the username, password, Wi-Fi network
name and password, any updates if necessary, etc. (see Figure 2.2 for some
displays on the monitor). In this book, the username is set to pi.
• The Raspberry Pi will boot in Desktop mode and will display the default screen.
You can press Ctrl+Alt+F1 at any time to change to the Console mode
● 17
● 18
● 19
• Click OK
You will now have to install a terminal emulation software on your PC. The one used by the
author is the popular Putty. Download Putty from the following website:
● 20
https://www.putty.org
• Putty is a standalone program and there is no need to install it. Simply double
click to run it. You should see the Putty startup screen as in Figure 2.8.
• Make sure that the Connection type is SSH and enter the IP address of your
Raspberry Pi 5. You can obtain the IP address by entering the command
ifconfig in console mode (Figure 2.9). In this example, the IP address was:
192.168.1.251 (see under wlan0:)
• Click Open in Putty after entering the IP address and selecting SSH
• The first time you run Putty, you may get a security message. Click Yes to
accept this security alert.
• You will then be prompted to enter the Raspberry Pi 5 username and password.
You can now enter all Console-based commands through your PC.
● 21
pi@raspberrypi: ~ $ passwd
• To shut down the Raspberry Pi, enter the following command. Never shutdown
by pulling the power cable, as this may result in the corruption or loss of files:
• Restart Putty
• Set the Default Foreground and Default Bold Foreground colours to black
(Red:0, Green:0, Blue:0)
• Set the Cursor Text and Cursor Colour to black (Red:0, Green:0, Blue:0)
• Select Appearance under Window and click Change in Font settings. Set
the font to Bold 12.
• Select Session and give a name to the session (e.g. MyZero) and click Save.
• Next time you restart Putty, select the saved session and click Load followed
by Open to start a session with the saved configuration
● 22
pi$raspberrypi:~ $ tightvncserver
You will be prompted to create a password for remotely accessing the Raspberry
Pi desktop. You can also set up an optional read-only password. The password
should be entered every time you want to access the Desktop. Enter a password
and remember your password.
pi$raspberrypi:~ $ vncserver :1
You can optionally specify screen pixel size and colour depth in bits as follows:
• We must now set up a VNC viewer on our laptop (or desktop) PC. There are
many VNC clients available, but the recommended one which is compatible with
TightVNC is TightVNC for the PC, which can be downloaded from the following
link:
https://www.tightvnc.com/download.php
• Download and install the TightVNC software for your PC. You will have to
choose a password during the installation.
• Start the TightVNC Viewer on your PC and enter the Raspberry Pi IP address
followed by ':1'. Click Connect to connect to your Raspberry Pi (Figure 2.10)
● 23
• Enter the password you have chosen earlier. You should now see the Raspberry
Pi 5 Desktop displayed on your PC screen (Figure 2.11)
• The VNC server is now running on your Raspberry Pi 5 and you have access to
the Desktop GUI.
In this section, you will learn how to fix your IP address so that it does not change between
reboots. The steps are as follows:
● 24
• Find the IP address of your router by entering the command ip r (Figure 2.13).
In this example, the IP address was: 192.168.1.254
• Add the following lines to the bottom of the file (these will be different for your
router). If these lines already exist, remove the comment character '#' at the
beginning of the lines and change the lines as follows (you may notice that
eth0 for Ethernet is listed):
● 25
interface wlan0
static_routers=192.168.1.254
static domain_name_servers=192.168.1.254
static ip_address=192.168.1.251/24
• Save the file by entering CTRL + X followed by Y and reboot your Raspberry Pi
• In this example, the Raspberry Pi should reboot with the static IP address:
192.168.1.251
• Click on the Bluetooth icon on your Raspberry Pi 5 at the top right-hand side,
and select Make Discoverable. You should see the Bluetooth icon flashing
• You should now see the message Connected Successfully on your Raspberry
Pi 5 and you can exchange files between your other device and the Raspberry
Pi computer.
Step 1: Connect a network cable between your Raspberry Pi 5 and your Wi-Fi router.
Step 2: Connect the keyboard, mouse and monitor to your Raspberry Pi and power up as
normal
Step 4: Providing your network hub supports DHCP (nearly all network routers support
DHCP), you will be connected automatically to the network and will be assigned a unique
IP address within your network. Note that DHCP assigns IP addresses to newly connected
devices.
Step 5: Check to find out the IP address assigned to your Raspberry Pi 5 by the network
router. Enter the command ifconfig as described earlier
● 26
In most cases, it is very unlikely that the network cable is faulty. Also, most network hubs
support the DHCP protocol. If you are having problems with the network, it is possible that
your Raspberry Pi is not configured to accept DHCP issued addresses. The Raspberry Pi is
normally configured to accept DHCP addresses, but it is possible that you have changed the
configuration somehow.
To resolve the wired network connectivity problem, follow the steps given below:
Step 1: find out whether your Raspberry Pi is configured for DHCP or fixed IP addresses.
Enter the following command:
If your Raspberry Pi is configured to use the DHCP protocol (which is normally the default
configuration), the word dhcp should appear at the end of the following line:
If, on the other hand, your Raspberry Pi is configured to use static addresses, then you
should see the word static at the end of the following line:
Step 2: To use the DHCP protocol, edit file interfaces (e.g. using the nano text editor)
and change the word static to dhcp. It is recommended to make a backup copy of the file
interfaces before you change it:
You should now restart your Raspberry Pi and an IP address will probably be assigned to
your device.
Step 3: To use static addressing, make sure that the word static appears as shown above.
If not, edit file interfaces and change dhcp to static
Step 4: You need to edit and add the required unique IP address, subnet mask and gate-
way addresses to file interfaces as in the following example (this example assumes that
● 27
the required fixed IP address is 192.168.1.251, the subnet mask used in the network is
255.255.255.0, and the gateway address is 192.168.1.1):
Save the changes and exit the editor. If you are using the nano editor, exit by pressing
Ctrl+X, then enter Y to save the changes, and enter the filename to write to as /etc/net-
work/interfaces.
• Insert a microSD card into your PC. You may need to use an SD card adapter
• Click to download the Raspberry Pi Imager. At the time of writing this book,
this file was called: imager_1.7.5.exe
• Click Operating System and select the operating system at the top of the list
as: Raspberry Pi OS (64-bit). See Figure 2.15
● 28
• Click Save
● 29
If you have a monitor and keyboard, you can log in to your Raspberry Pi 5 directly and
start using it. Otherwise, find the IP address of your Raspberry Pi 5 (e.g. from your router,
or there are many apps for smartphones, such as who's on my wifi that shows all the
devices connected to your router with their IP addresses). Then log in to your Raspberry
Pi 5 and start using it.
● 30
3.1 Overview
Raspberry Pi is based on a version of the Linux operating system. Linux is one of the most
popular operating systems in use today. Linux is very similar to other operating systems,
such as Windows and UNIX. Linux is an open operating system based on UNIX and has
been developed collaboratively by many companies since 1991. In general, Linux is harder
to manage than some other operating systems like Windows, but offers more flexibility and
configuration options. There are several popular versions of the Linux operating system,
such as Debian, Ubuntu, Red Hat, Fedora and so on.
Linux commands are text-based. In this chapter, you will be looking at some of the useful
Linux commands and see how you can manage your Raspberry Pi using these commands.
When you apply power to your Raspberry Pi 5, the Linux command line (or the Linux shell,
or Console commands) is the first thing you see, and it is where you can enter operating
system commands.
pi@raspberrypi: ~$
● 31
Command uname –s displays the operating system kernel name, which is Linux. Com-
mand uname –a displays complete detailed information about the kernel and the operat-
ing system. An example is shown in Figure 3.2.
Command cat /proc/meminfo displays information about the memory on your Raspber-
ry Pi. Information such as the total memory and free memory at the time of issuing the
command are displayed. Figure 3.3 shows an example, where only part of the display is
shown here.
Command whoami displays the name of the current user. In this case, pi is displayed as
the current user.
● 32
A new user can be added to your Raspberry Pi 5 using the command useradd. In the ex-
ample in Figure 3.5, a user called John is added. A password for the new user can be added
using the passwd command followed by the username. In Figure 3.4, the password for
user John is set to mypassword (not displayed for security reasons). Notice that both the
useradd and passwd are privileged commands, and the keyword sudo must be entered
before these commands. Notice that the –m option creates a home directory for the new
user.
You can log in to the new user account by specifying the username and the password as
shown in Figure 3.5. You can type command exit to log out from the new account.
Command sudo apt-get upgrade is used to upgrade all the software packages on the
system.
Figure 3.6 shows part of the Raspberry Pi 5 directory structure. Notice that the root direc-
tory is identified by the '/' symbol. Under the root we have directories named such as bin,
boot, dev, etc, home, lib, lost+found, media, mnt, opt, proc, and many more. The impor-
tant directory as far as the users are concerned is the home directory. The home directory
contains subdirectories for each user of the system. In the example in Figure 3.7, pi is the
subdirectory for user pi. In a new system, this subdirectory contains two subdirectories
called Desktop and python_games.
● 33