Raspberry Pi 4 2020 Beginners Guide - A Complete 2020 Manual To Get Started With Raspberry Pi 4 Proj
Raspberry Pi 4 2020 Beginners Guide - A Complete 2020 Manual To Get Started With Raspberry Pi 4 Proj
Ted Humphrey
Copyright©2020 Ted Humphrey
All rights reserved. No part of this book may be reproduced or used in any manner without the prior
written permission of the copyright owner, except for the use of brief quotations in a book review.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal responsibility
for any errors or omissions that may be made. The publisher makes no warranty, express or implied,
with respect to the material contained herein.
Printed on acid-free paper.
You can decide to close the welcome page above by tapping on the cancel
tab. But you need to address some basic information so that features of the
Raspberry such as wireless network will work. When you tap the next tab
in the image above, you will be taken to a window where you can select
your country, time zone and language. You will also see a dropdown where
you will have access to customize the keyboard layout you want Raspbian
to be using. You can customize the Raspbian desktops and programs to start
showing in English, tap on the “use English language” checkbox to allow
you set English language as your preferred language and then tap Next to
finish.
Another screen will be prompted that will enable you to choose your
preferred Wi-Fi network from a list. Check the list of networks, find your
network’s name and tap on it, then choose Next . If your chosen wireless
network is secure (which is actually expected), you will be prompted to
enter its password, which is its pre-shared key; check for the pre-shared key
which is usually inscribed on a card with the router or on the bottom of the
router. Tap Next to connect to the network. If you prefer not to connect to a
wireless network, simply tap Skip . Note that Built-in wireless networking
is only available on the Raspberry pi 3, pi 4, and pi Zero W families. If you
want to use another model of Raspberry Pi with a wireless network, you
will need a USB Wi-Fi adapter.
Another screen that will be displayed will enable you to check for updates
and then install those updates for Raspbian OS. The Raspbian OS is
designed to update itself on a regular basis which enables it to fix bugs,
modify existing updates to add new features, and also improve software
performance. Tap Next to install these updates, otherwise, click Skip. You
will need to be patient while downloading the updates as the downloading
process might take a little time to be completed. When the updates have
been completely installed, a window will pop up telling you that ‘System is
up to date’; tap the OK button to proceed.
The final screen will be prompted where you will be able to reboot the
Raspberry pi. Any change you made will only be effective when you restart
the Raspberry Pi, a process known as rebooting. If you are asked to reboot,
select the Reboot button and your Raspberry Pi will restart.
When you tap on the Raspberry icon at the extreme left of the menu bar,
you will be prompted with a list of categories containing some programs.
For instance, when you select the programming category, you will be able
to access some software which will help you to write your own program as
you desire while the internet category contains some list of programs that
will enable you to access the internet.
Using your Raspberry Pi 4
Browsing the internet with the Raspberry pi 4: The chromium
web browser
The chromium web browser on the pi 4 has the interface much like the
regular chrome browser on your desktop computers or PCs. You can start
the chromium web browser by tapping on the Raspberry icon at the top left
section of the menu bar to access the internet category. Tap the internet
category and select Chromium Web Browser to begin. When you launch
the chromium web browser, you will get a welcome with “welcome to
chromium.” The Chromium web browser is not exactly different from the
normal chrome browser. With the Chromium browser, you will be able to
access the internet, download and install other software, play games and
read from the media. To get more from the Chromium web browser, try and
maximize the browser so that it can take up the full desktop space. To do
this, locate the three icons at the top section of your chromium title bar. The
up-arrow symbol (which is found at the middle of the three icons) will help
you to maximize the screen to take up the whole desktop space. The
minimize button (down-arrow icon) is found to the left of the up-arrow icon
(maximize button) and will enable you to hide the chromium web browser’s
screen to top of the task bar where you can always restore it when needed.
The cross icon (at the left side of the maximize button) will close the
chromium web browser’s window if you tap it. You are not advised to close
the chromium web browser interface when you still have some important
tasks that you have not saved. When you first load the chromium web
browser, you might see some tabs located along the top of the chromium
window. You will be able to navigate to a different tab by tapping on the tab
you want to switch to. You can also close any tab by tapping the cross
symbol located at the right edge of the tab; this will not close the whole
chromium window. You can open a new tab, which you can use to access
many websites on the chromium web browser without necessarily opening
multiple chromium web apps, simply tap on the tab button located at the
right of the last tab in the list, or alternatively hold down the CTRL key on
your keyboard and press the letter T key before releasing the CTRL key .
When you are done navigating the Chromium web browser, tap the close
button at the top-right of the chromium window.
You can conveniently copy your files from a removable disk, such as a flash
drive, into the pi’s SD card. You can also copy files from the SD card into a
removable device (flash drive). To get started, open your home directory
(on the pi) and the removable item (flash drive) separately. From your pi’s
directory, navigate to the particular files or folders that you want to copy
and then right click on the files or folders and tap copy or press CTRL C
(if there are many files or folders that you want to copy selectively, simply
tap on one of the folders and then scroll down while holding the CTRL key
to select and highlight other folders or files). Open the second window,
which is the removable device’s window, and paste the copied file by right-
clicking and then choose copy. If you select the move option in the SD
card, this will delete the file from the SD card and the file will be available
anywhere you move it to on the removable device. You can alternatively
use the drag and drop method. To use the drag and drop method, tap on the
file or folder that you want to drag to another location inside the removable
item, and then open the removable item’s window to drop the file or folder
by taking your hand off the mouse. The shortcut for copy is CTRL + C.
You can paste a file by tapping the CTRL button + V. When you tap CTRL
+ X, you are about to cut the file you have highlighted. To get more from
the File manager window, try and maximize the platform so that it can take
up the full desktop space. To do this, locate the three icons at the top section
of your file bar. The up-arrow symbol (which is found at the middle of the
three icons) will help you to maximize the screen to take up the whole
desktop space. The minimize button (down-arrow icon) is found to the left
of the up-arrow icon (maximize button) and will enable you to hide the file
manager screen to the top of the task bar where you can always restore it
when needed. The cross icon (at the left side of the maximize button) will
close the file manager’s window if you tap it. You are not advised to close
the chromium file manger’s interface when you still have some important
files that you have not saved or copied. When you are done navigating
through the file manager, you can tap the close symbol located at the top-
left of the file manager’s window. If you have more than one file manager
window open, close them one by one. If you attached a removable storage
device to your Pi, eject the device by tapping the eject icon at the top right
section of the screen, find the removable item on the list, and then select it
before unplugging the device from the Raspberry pi.
Writing on the Raspberry Pi 4: The LibreOffice Productivity
Suite
The LibreOffice writer does the same job that the Microsoft office suite
(MS word) on your desktop computer will do. To have access to the
LibreOffice Suite, navigate to the pi’s menu at the left section and then
select Office and from the Office dropdown, tap on the LibreOffice writer.
If the LibreOffice writer has not been installed automatically with the
Raspbian operating system, you can install it on your own by running a
simple command “sudo apt install libreoffice.” You will need a disc space
of about 649MB to fully install and enjoy the LibreOffice writer. The writer
is the word processor that comes with the LibreOffice. The writer is very
identical to the MS word and it is used essentially for creating and editing
word documents. It is easy to open the LibreOffice software with the
command below;
If you specify a specific file name that you want to open in the
[FILENAME], you will be able to open that file.
With the LibreOffice writer, you can carry out most tasks you do with your
MS word app. You can format your document by changing the font size,
font style, font color and add a powerful effect to give your texts some nice
appeals. While writing with the LibreOffice writer, you can be sure of
grammar correction because every misspell word is highlighted in red so
that you can correct it. You can also save any file right from the LibreOffice
writer by pressing CTRL + S.
The LibreOffice suite also features LibreOffice Calc which is just like
Microsoft's spreadsheet software. The Calc software can be used to analyze
both large and small data samples just like Microsoft Excel. It is easy to
open the LibreOffice Calc software with the command below;
If you specify a specific file name that you want to open in the
[FILENAME], you will be able to open that file.
There is another software that comes with the LibreOffice suite is the
LibreOffice Impress which is just like the Microsoft PowerPoint app. The
LibreOffice Impress is used specifically to create a PowerPoint
presentation. It is easy to open the LibreOffice Calc software with the
command below;
If you specify a specific file name that you want to open in the
[FILENAME], you will be able to open that file.
The LibreOffice draw is a LibreOffiec software for making and editing
pictures and diagrams.
The Recommended Software tools
It is understandable that the Raspbian OS comes with a lot of installed
software, but you can still install much other compatible software. A list of
some of the best software that you can install in your Raspberry pi can be
accessed in the Recommended Software tool. The Recommended Software
tool requires a strong connection to the internet for full access. After
successfully connecting your pi to the internet, simply tap on the Raspberry
menu icon at the left section of the toolbar, and select Preferences. Tap on
Recommended Software to get started. The Recommended tool will be
prompted and you can start downloading information about software that is
available. After some time, the list of compatible and available software
will show up. The tool will load, and you can then begin downloading
information about available software. After a few seconds, a list of
compatible software packages will appear. These tools, just like the
software in the raspberry menu, are systematically arranged into various
categories. Tap on a category in the pane on the left to access software from
that category, or choose All Programs to see all the software. In the “All
programs” section, if you see software that has a tick beside it, it shows
that the app has already been installed. If there is no tick beside the
software, you can tick it for installation. You can as well uninstall any
software that has already been installed by unmarking the tick beside it.
After selecting the list of software that you either want to install or
uninstall, proceed by tapping on the “OK” button to start installing or
uninstalling as the case may be.
You can exercise patience for one command to end before prompting the
second one or you can run both at a time by;
The && in the command above is a Boolean (logical) AND, which implies
that if the first command fails to run well, the second command will not be
executed. The reason is because for a logical AND to be true, both of its
conditions must be essentially true. It is always worth running the update
command before installing new software, too – minor updates are made
even in stable distributions such as Raspbian, to address any issues. For
instance, to install a game called bsd, just prompt the command below;
CHAPTER FOUR
PYTHON PROGRAMMING FOR RASPBERRY
Python is a free programming language which has become the most used
language being used among developers, hobbyists and students of
knowledge. The python programming language works on a wide variety of
platforms such as Windows (all versions of Windows), Linux based
operating system and Apple OS. The python language has straight forward
commands which are very easy to understand. These commands are called
syntax. These simple commands make it especially easy for beginners with
zero knowledge of python to grasp the basics and move to being a pro
within hours or days of learning. The python language is a high-level object
oriented language with improved readability. The language is exploited by
many companies such as Google, YouTube, NASA and a host of others in
many of their programming related activities. The python programming
language came into existence in 1991 with the help of Guido van Rossum.
Recently, the object-oriented language – python- has gone through some
series of changes moving from the latter version 2 to the new version 3. The
python v3 works based on UNICODE which can operate with both English
and non English characters, while the v2 can only work with only English
characters. Python v3 is a compressed version which makes the program
smaller and very easy to understand than the v2. In this guide, you will be
coming across the new version 3 of the python programming language.
Many operating systems support both the v2 and the v3. Raspbian, the
operating system powering your Raspberry pi 4, also supports the v2 and v3
python programming language. The Raspbian OS comes with many
graphical interfaces (which define the desktop outlook) and you can
configure any of them to start using. In this python guide, the LXDE
graphical interface which is based on Linux will be used. The Linux is the
engine of the Raspbian operating system. A popular programming
environment for the LXZDE is the IDLE. When you tap the LXDE program
menu, you have access to a lot of features and menus. One of such menus is
the programming menu (IDLE). The programming menu features the IDLE
(python v2) and IDLE 3 (python v3).
Understanding the python environment
The Raspbian Linux distribution package comes loaded with python 3 and
all the necessary tools you will need to make your programming easy. The
following items are preloaded with v3; python interpreter, interactive
python shell, a python development environment and a text editor. You can
check the Python interpreter and interactive shell versions on your system
by opening the LXTerminal in the LXDE Graphical UI. Type python3 -V
and press Enter. If you are greeted with the message command not found,
then for some reason, the Python v3 interpreter is not installed. You can also
check if a Python development environment has been installed, open the
LXDE graphical interface and look for the IDLE 3 icon on the desktop. If
you cannot find the IDLE 3 icon on your desktop, check in the LXDE menu
by clicking on the LXDE Programs Menu icon and hovering your mouse
pointer over the Programming menu option. You should see the IDLE 3
icon there.
The text editor called nano will be required for the python programming.
Check if it has been installed by opening the LXTerminal in the LXDE
graphical interface, and then type nano -V and press Enter.
If any of; python interpreter, interactive python shell, a python development
environment and a text editor is missing, you can install it with a simple
sudo command. Just open the LXTerminal and enter the prompt sudo apt-
get install python3 idle3 nano in the command prompt, and then press
ENTER. This prompt will fix any of the four tools that are missen. To
install them, make sure that the pi is connected to the internet and booted
up.
Check the Raspberry keyboard: for most users in some part of the world,
like the United States, the keyboard is not usually configured properly. If
you carry out a simple check with your keyboard by pressing the shift + @
key and you are getting a double quote symbol instead of the @ symbol,
then you need to set your keyboard so that it can work effectively for
python programming by following the steps below;
- Boot up the pi, if it has not been booted on, and navigate to the LXDE
graphical user interface.
- Tap the LXTerminal icon twice to access the LXTerminal window.
- Enter the prompt sudo raspi-config on the command prompt and press
Enter.
- In the Raspbi-config window, scroll down to configure_keyboard and then
press Enter. You need to be patient here because it may take several seconds
for the next window to open.
- A new window will be prompted asking you to select the model for the
keyboard, tap ENTER to continue with the default keyboard mode.
- Another window will be displayed telling you to select the layout
matching the keyboard for this machine, scroll up the menu and select
English, then press enter.
- On the next three screens listed, modify the selections or press Enter to
accept the defaults: Key to function as AltGr screen, Compost Key screen
and Use Control Alt Backspace screen
- In the Raspbi-config window, press Tab until you reach the <Finish>
selection and
then hit Enter.
-Because the keyboard changes will not take effect until you reboot your
system, type sudo reboot in the LXTerminal window and press Enter.
-After your Raspberry Pi reboots, test your keyboard. See if pressing the @
key now produces the symbol @ and pressing the " key produces a double
quote (").
The python development environment shell is an essential tool that can be
used to create, test and modify python script. The development environment
shell can help you to troubleshoot any incorrectly written python syntax,
and you won’t need to rerun the entire python script if there is any error.
The default python environment development shell in Raspbian is the
IDLE.
Reassigning a variable
The value you assigned to a variable is not its birth right, as the value can
be switched to be assigned to another variable. That is why it is called
variable in the first place. The variable coffee_cup can have its value
switched from coffee to butter. You can reassign a value by entering the
assignment syntax with an entirely new value at the end.
>>> coffee_cup = 'coffee'
>>> print ("My cup is full of", coffee_cup)
My cup is full of coffee
>>> coffee_cup = 'tea'
>>> print ("My cup is full of", coffee_cup)
My cup is full of tea
>>>
Order of operation
Just like in every mathematical calculation that involves BODMAS which
provides a template for simplifying addition, subtraction, multiplication and
all other functions. The python programming language also follows rules
while solving mathematical problems involving addition, multiplication and
subtraction. In the example below, python carry out the multiplication
operation first before the addition operation;
>>> 7 + 3 * 2
13
>>>
But the order of operation above can be changed with the help of
parentheses;
>>> (7 + 3) * 2
20
>>>
Using variables in calculation
One of the most fascinating features of using math in python is that it
enables you to place variables inside your equation. If you mix data type in
your equation, python will display the result in form of floating-point data
type;
>>> test1 = 7
>>> test1 * 3.0
21.0
>>>
You need to assign a value to a variable before you deploy it in your
calculation, otherwise python won’t agree. See the example below;
>>> test1 * 5
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
test1 * 5
NameError: name 'test1' is not defined
>>>
While working with python calculation, you must have observed some
strangeness in the floating point outputs. See below;
>>> 5.2 * 9
46.800000000000004
>>>
When you, ordinarily, multiplied 5.2 by 9, the result should be 46.8. But the
IDLE displays the output with some stray numbers added to it. This is due
to the way the CPU computes floating-point calculation. Because the
floating point data type changes the numbers into a special format, the
calculations are somewhat inaccurate. This problem is difficult to by-pass in
your calculation but you can deploy some tricks to reduce the extent of the
stray values and make your calculation presentable. You can deploy the
format function inside the result to modify how the result is being
displayed. The format () function enables you to separate the variable from
the output text in the string object just by using the {} placeholder symbol;
>>> print ("The result is {}".format (result))
The result is 46.800000000000004
>>>
You can then use the placeholder symbol {} to further format your result.
All that you need to do is to specify an output template inside the
placeholder {} and python will deploy it to format the number output. For
instance, you can restrict the output of the calculation above to two decimal
places by using the template {0:.2f} to produce the output:
>>> print ("The result is {0:.2f}".format (result))
The result is 46.80
>>>
The first number in the above template tells python what position in the
number it should start to display from. The second number (the .2) tells
python the number of decimal places it should include in the output. The f
in the template informs Python that the number is actually a floating-point
format.
Creating complex numbers in python
In python, you can specify complex numbers by deploying the complex
function (). To get started, indicate the real component digit as the first
parameter and then put the imaginary component as the second parameter.
For instance, if you have complex function 2 + 5j, the real part is 2 while
the imaginary part is 5. See example below;
>>> test = complex(2, 5)
>>> print (test)
(2+5j)
>>>
Python will usually display the complex number value using the j format
which makes it look presentable and easy to view.
It is possible to carry out complex number calculation within another
complex number. See below;
>>> test1 = complex (1, 2)
>>> test2 = complex (2, 3)
>>> result = test1 + test2
>>> print (result)
(3+5j)
>>> result2 = test1 * test2
>>> print (result2)
(-4+7j)
>>>
CHAPTER FIVE
USING PROGRAMMING IN THE RASPBERRY
PI
The Graphical User Interface (GUI) Programming
The graphical user interface (GUI) on all operating systems enables you to
enter data and see the processed result in a jiffy. Like most other operating
systems, the Linux OS – which is the building block of your Raspberry pi –
also has its own graphical user interface. There are many graphical desktop
environments in the Linux OS, but the Raspbian OS which is used by the
Raspberry pi deploys the LXDE desktop environment to give you an
appealing graphical desktop interface. You can use the LXDE desktop
environment together with python script to invent a more appealing
window-oriented interface that can give your programming work a very
good look. You can go through some terms used in the graphical user
interface programming first;
- Frame : this is the main section in a window. It is usually made up
of objects called widgets.
- Widgets: helps display and retrieve information. The following are
the types of widgets you might come across with their functions;
A GUI program shows all the widgets at once inside the window. It is the
user that will get to determine which widget he wants to activate next since
the code cannot determine which widget the user is planning to use at any
point in time. The GUI program deploys a technique called event-driven
programming to process codes. In event-driven programming, Python
initiates different methods inside the program, using the series of actions
that has happened in the GUI window. The program flow doesn’t exactly
have a set flow; it only runs in response to a triggered event. For instance,
your user can input their data or information into the text widget, and
nothing will happen unless the user taps a button in the program window to
submit the text. The tapped button initiates an event, and your program
code needs to detect that event and then prompt the code method to read the
text in the text field and then start processing it.
The basis of the event-driven programming is linking widgets inside the
window to some specific events and then linking these events to the code
modules inside the program. The event handler is the one carrying out this
process.
Python GUI packages
There are many excellent GUI packages that will enable you to create GUI
widgets using your python scripts. These widgets will allow you to create
your own graphical program. The most common GUI packages include;
tkinter, PyGTK, PyQT and wxPython .
The python programming language features the tkinter package by default,
hence it is mostly used to create python’s graphical interface on your
Raspberry pi. The tkinter is also among the oldest graphical packages in
python programming.
Two lines of codes are used to specify a Label widget for the window in the
create_widgets() method. The Line 13 above specifies the exact Label
object, and line 14 uses the grid() method to position the Label widget in
the window.
- Defining the event handler for the window: Another step
involved in building a Graphical User Interface application is to specify the
event that the window uses. Widgets that can generate events (such as when
the application user clicks a button) use the command parameter to define
the name of a method Python calls when it detects the event. For example,
to link a button to an event method, you write code like this:
def create_widgets(self):
self.button1 = Button(self, text="Submit", command =
self.display)
self.button1.grid(row=1, column=0, sticky = W)
def display(self):
print("The button was clicked in the window")
The create_widgets() method will be able to create a single button to
show in the window area. The Button class constructor makes the
command parameter to be self.display , which points to the display ()
method in the class. For now, the test display () method will deploy a
print () statement to show a message back in the command line,
where you initiated the program.
The script script1804.py below will create a window with the button
and the event specified;
In this project, you will need the following hardware and software;
- Raspberry pi and a Sense HAT (which are the hardware)
- Python v3 and Sense HAT for python v3 (these are the software and
are already installed with the Raspbian operating system).
But if the Sense HAT package is not in your Raspberry pi, for one reason or
another, enter the command sudo apt-get install sense-ha t in the
LXTerminal of the Raspberry to get it.
The Sense HAT is literally like an add-on board made specially for the
Raspberry pi. This board (Sense HAT) will enable users to measure
temperature, pressure, humidity and some other measurements. It can bring
out information by using the built-in LED matrix.
- You can display color on the Sense HAT with a python script.
Follow the steps;
The LED matrix will show white light when you prompt
the code above. This is because the maximum value of the
red, green and blue colors has been used. Hence the color
will be white.
Try something by changing the value of any of the colors
above and note what you see.