USE COMMAND BELOW TO CONNECT TO REMOTE HOST
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
Smart buildings, the Internet of Things (IoT), robotics, smart energy, smart cities, human-computer interaction (HCI), cyber-physical systems, 3D printing, advanced vehicular systems
It is revolutionary that you can build a low-level electronics circuit and then install a Linux web server, using only a few short commands, so that the circuit can be controlled over the Internet.
The $ prompt indicates that a regular Linux user is executing a command, and a # prompt indicates that a Linux superuser is executing a command.
Raspberry Pi Basics
Raspberry Pi Hardware
Introduction to the Platform
The RPi models are capable general-purpose computing devices
are also capable physical computing
They are powerful computing devices. For example, the RPi 3 contains a 1.2 GHz ARM Cortex-A53 processor that can perform more than 700 million Whetstone instructions per second (MWIPS).
RPi Zero or Compute Module for deployment)
HATs (Hardware Attached on Top)
Who should Use the RPi
integrating high-level software and low-level electronics is not an easy task.
providing each type of reader with enough Linux, electronics, and software exposure to ensure that you can be productive, regardless of your previous experience level.
When to Use the RPi
The RPi is perfectly placed for the integration of high-level software and low- level electronics in any type of project.Whether you are planning to build an automated home management system, robot, multimedia display, Internet of 6 Part I ■ Raspberry Pi Basics Things (IoT) application, vending machine, or Internet-connected work of inter- active art, the RPi has the processing power to do whatever you can imagine of an embedded device.
When to NOT Use the RPi
The Linux OS was not designed for real-time or predictable processing., the RPi is not an ideal platform for real-time systems applica- tions.
the RPi can be used as the “central intelligence.”
The RPi platform is not ideal for project developments that are likely to be commercialized.
“ … only be used for the purposes of developing for, running or using a Raspberry Pi device.”
embedded Linux, software devel- opment, and general electronics,
- The Raspberry Pi Foundation website: www.raspberrypi.org
- ■ The Raspberry Pi Documentation: www.raspberrypi.org/documentation/.
- Broadcom BCM2835 ARM Peripherals Datasheet:
- m The BCM2836 Document
At their heart, the RPi boards use the Broadcom BCM2835, BCM2836, and BCM2837 system on a chip (SoC).
The Compute Module 3 contains the guts of a Raspberry Pi 3 (the BCM2837 processor and 1GB RAM) as well as an optional 4GB eMMC Flash device (which is the equivalent of the SD card in the Pi).
The new Compute Module 3+ sports the Broadcom BCM2837B0 chip, albeit clocked at 1.2GHz and with 1GB LPDDR2 SDRAM.
Raspberry Pi Versions
■ If you want to design your own PCB that uses the RPi (or multiple RPi boards), investigate the Compute module
|Raspberry Pi 2||B||Standard[a]||Yes||No||2015|
|Raspberry Pi Zero||Zero||Zero[c]||No||No||2015|
|Raspberry Pi 3||B||Standard[a]||Yes||Yes||2016|
|Raspberry Pi 4||B (1 GiB)||Standard[a]||Yes (Gigabit Ethernet)||Yes||2019||Yes|
|B (2 GiB)|
|B (4 GiB)|
|B (8 GiB)||2020|
The Raspberry Pi Hardware
On recent RPi models (A+, B+, 2, 3, and Zero), there are 40 pins in total on this header (2 × 20);
however, not all are available for general-purpose input/outputs (GPIOs). Several of the connections have a fi xed confi guration:
■ 4 pins are allocated to voltage supplies: 3.3 V (up to 50 mA) and to 5 V (up to 300 mA).
External 5 V Power Supply (for Powering the RPi)
you should measure the voltage level on the RPi. On newer models, you use PP1 or PP2 and GND (or any of the metal shielded components
Micro-SD Card (for Booting an Operating System)
Class 10 or greater,
Ethernet Cable (for Network Connection)
HDMI Cable (for Connection to Monitors/Televisions)
The RPi Zero uses USB On-The-Go (OTG) to connect to USB peripherals.
USB to Serial UART TTL 3.3 V (for Finding Problems)
- The black ground (GND) wire to Pin 6 on the GPIO header, which is the RPi GND pin
- The yellow receive (RXD) wire to Pin 8 (GPIO14) on the GPIO header, which is the UART transmit pin (TXD0)
- The orange transmit (TXD) wire to Pin 10 (GPIO15) on the GPIO header, which is the UART receive pin (RXD0)
The RPi is 3.3 V tolerant but makes a 5 V supply available on the GPIO header pins 2 and 4.
- USB Hub (to Connect to Many USB Devices)
Most RPi models have a built-in USB hub
One issue that you have to be aware of with powered USB hubs is that many are back feeding. Back feeding (back powering) is where a USB hub connected to the RPi hub (not the micro-USB power) supplies power back into the RPi through the RPi hub.
- Micro-HDMI to VGA adapters (for VGA Video and Sound)
- Wi-Fi Adapters (for Wireless Networking)
- USB Storage Devices (for Additional Storage)
- USB Webcam (for Capturing Images and Streaming Video)
- USB Keyboard and Mouse (for General-Purpose Computing)
- Cases (for Protecting the RPi)
HATs (Hardware Attached on Top)
Raspberry Pi Software
Linux on the RPi
high-end server owners might install Red Hat Enterprise, Debian, or OpenSUSE; desktop users might install Ubuntu, Debian, Fedora, or Linux Mint. At the core of all distributions is a common Linux kernel, which was conceived and created by Linus Torvalds in 1991.
In deciding which Linux distribution to use for your embedded system plat- form, it would be sensible to choose one for which the following apply:
- stable and well supported.
- package manager
- The distribution is lean and suited to a low storage footprint.
- There is good community support for your particular device.
- There is device driver support for any desired peripherals.
Linux Distributions for the RPi
Raspbian, Ubuntu, OpenELEC, and Arch Linux
Debian (contraction of Debbie and Ian)
“Debian is the rock upon which Ubuntu is built.”
OpenElec distributions typically use a read-only fi le system (e.g., squashfs) for performance and reliability.
Create a Linux SD Card Image for the RPi
Use the dd disk cloning tool (carefully). First identify the device. It should appear as /dev/mmcblkXp1 or /dev/sddX under Linux, or /dev/rdiskX under Mac OS, where X is a number. You must be certain that X refers to the SD card to which you want to write the image—for example, check that the available capacity of the device (e.g., use cat /proc/partitions) matches the SD card capacity. Then using a terminal window use the dd command with root privileges, where if is the input fi le name and of is the output device name (a block size bs of 1M should work fi ne):
sudo dd bs=1M if=RPi_image_file.img of=/dev/XXX
Connecting to a Network
There are two main ways to connect to and communicate with the RPi over the network: using regular Ethernet or using an Ethernet crossover cable
By default, the RPi is confi gured to request a Dynamic Host Confi guration Protocol (DHCP) IP address.In a home network environment, this service is usually provided by a DHCP server that is running on the integrated modem- fi rewall-router-LAN (or some similar confi guration) that connects the home to an Internet service provider (ISP).
- With a port-scanning tool: Use a tool like nmap
Issue the com- mand
nmap -T4 -F 192.168.1.* to scan for devices on a subnet.
- With zero-configuration networking(Zeroconf
Zeroconf is a set of tools for hostname resolution, automatic address assignment, and service discovery.
By default the RPi Raspbian distribution uses an avahi service to support Zeroconf on your network, which makes the hostname visible.
systemctl status avahi-daemon
avahi-daemon.service - Avahi mDNS/DNS-SD Stack Loaded: loaded (/lib/systemd/system/avahi-daemon.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2020-08-20 17:09:38 +06; 1 months 2 days ago Main PID: 354 (avahi-daemon) Status: "avahi-daemon 0.7 starting up." Tasks: 2 (limit: 4915) CGroup: /system.slice/avahi-daemon.service ├─354 avahi-daemon: running [raspberrypi.local] └─380 avahi-daemon: chroot helper Aug 20 17:09:38 raspberrypi avahi-daemon: No service file found in /etc/avahi/services. Aug 20 17:09:38 raspberrypi avahi-daemon: Network interface enumeration completed. Aug 20 17:09:38 raspberrypi systemd: Started Avahi mDNS/DNS-SD Stack. Aug 20 17:09:38 raspberrypi avahi-daemon: Server startup complete. Host name is raspberrypi.local. Local service Aug 20 17:11:27 raspberrypi avahi-daemon: Joining mDNS multicast group on interface wlan0.IPv6 with address fe80 Aug 20 17:11:27 raspberrypi avahi-daemon: New relevant interface wlan0.IPv6 for mDNS.
- With a USB-to-TTL serial connection:
A fi nal option is to use a USB-to- TTL serial connection to connect to the RPi and type ifconfig to fi nd the IP address. The address is the “inet addr” associated with the eth0 adapter.
Ethernet Crossover Cable
Communicating with the RPi
fter you have networked the RPi, the next thing you might want to do is communicate with the RPi. You can connect to the RPi using either a serial connection over USB-to-TTL or a network connection,
The network connection should be your main focus, because that type of con- nection provides your RPi with full Internet access.
Serial Connection with the USB-to-TTL 3.3V Cable
Serial connections are particularly useful when the RPi is close to your desk- top computer and connected via a USB-to-TTL cable
It can also be used to confi gure wireless networking on an RPi device that does not have wired network support.
A terminal emulator is included by default under Mac OS X (e.g., use a command such as screen /dev/cu.usbserial-XXX 115200)
To connect to the RPi over the USB-to-TTL serial connection, you need the following information:
- Port number:
- Connection speed: By default, you need to enter 115,200 baud to connect to the RPi.
- Other information you may need for other terminal applications: Data bits = 8; Stop bits = 1; Parity = none; and Flow control = XON/XOFF.
On a Linux desktop computer, you can install the screen program and con- nect to the USB-to-TTL device with these commands:
sudo apt-get install screen screen /dev/cu.usbserial-XXX/ 115200
Connecting Through Secure Shell (SSH)
Secure Shell (SSH) on port 22
- ■ Log in remotely to the RPi and execute commands.
- ■ Transfer fi les to and from the RPi using the SSH File Transfer Protocol (SFTP).
- ■ Forward X11 connections, which allows you to perform virtual network computing.
Transferring Files Using PuTTY/psftp over SHH
It supports serial and SSH connections
Controlling the RPi
Basic Linux Commands
uname -a Linux raspberrypi 5.4.51-v7l+ #1333 SMP Mon Aug 10 16:51:40 BST 2020 armv7l GNU/Linux
X number changes only rarely
The Y value used to change rarely (every 2 years or so)
The Z value changes regularly.
Next, you could use the passwd command to set a new password for the pi user account:
more /etc/issue Returns the Linux distribution you are using
╰─❯ more /etc/issue Raspbian GNU/Linux 10 \n \l
ps -p $$ Returns the shell you are currently using (e.g., bash)
ps -p $$ PID TTY TIME CMD 9026 pts/0 00:00:12 zsh
whoami Returns who you are currently logged in as
uptime Returns how long the system has been running
top Lists all of the processes and programs executing. Press Ctrl+C to close the view.
uptime 17:13:31 up 2:07, 2 users, load average: 0.06, 0.04, 0.00
Finally, you can fi nd out specifi c information about your RPi using the
hostnamectl application, which can be used to query and change some system settings (e.g., the chassis description and hostname):
╰─❯ hostnamectl Static hostname: raspberrypi Icon name: computer Machine ID: a176c5fc4d874855b8cd38221facb879 Boot ID: fca3d3ecf7f2468888f6986cf234996c Operating System: Raspbian GNU/Linux 10 (buster) Kernel: Linux 5.4.51-v7l+ Architecture: arm
Basic File System Commands
When using Raspbian/Debian and Ubuntu user accounts, you often must prefi x certain commands with the word sudo. That is because sudo is a program that allows users to run programs with the security privileges of the superuser.
Get the calendar cal Display a text-based calendar. cal 04 2016
❯ cal September 2020 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Sometimes it is possible to recover fi les that are lost through accidental deletion if you use the
extundelete command immediately after the deletion.
sudo apt install extundelete
Environment variables are named values that describe the confi guration of your Linux environment, such as the location of the executable fi les or your default editor.
To get an idea of the environment variables that are set on the RPi, issue an env call, which provides a list of the environment variables on your account. Here, env is called on the Raspbian image:
Basic File Editing
What Time Is It?
╰─❯ date Tue 22 Sep 2020 05:19:58 PM +06
The answer is that they could not. Unlike a desktop computer, the RPi has no battery backup to ensure that the BIOS settings are retained; in fact, there is no BIOS!
but for the moment, you need a way to set the time, and for that you can use the Network Time Protocol (NTP)
The NTP is a networking protocol for synchronizing clocks between computers. If your RPi has the correct time, that is only because your RPi is obtaining it from your network using an NTP service that is running the board:
After setting the time, you can set your time zone.
sudo dpkg-reconfigure tzdata
─❯ sudo dpkg-reconfigure tzdata Current default time zone: 'Asia/Almaty' Local time is now: Tue Sep 22 17:21:49 +06 2020. Universal Time is now: Tue Sep 22 11:21:49 UTC 2020.
If your RPi is not connected to the Internet, you can manually set the date using the timedatectl tool:
sudo timedatectl set-time '2017-1-2 12:13:14'
a battery-backed real-time clock (RTC) can be connected to the RPi to solve that problem.
A package manager is a set of software tools that automate the process of installing, confi guring, upgrading, and removing software packages from the Linux operating system.
buntu and Raspbian/Debian use APT (Advanced Packaging Tool) over DPKG (Debian Package Management System)
Is nano installed?
dpkg-query -l | grep nano
Is a package containing the string nano available?
apt-cache search nano
Get more information about a package.
apt-cache show nano
Download a package to the current directory.
apt-get download nano
Remove a package.
sudo apt remove nano
Clean up old packages.
sudo apt-get autoremove
sudo apt-get clean
Wavemon is a useful tool that you can use in confi guring Wi-Fi connections
╰─❯ apt-cache search wavemon wavemon - Wireless Device Monitoring Application
sudo apt install wavemon
Configuring the Raspberry Pi
The Raspberry Pi Configuration Tool
Updating the RPi Software
Interacting with the Onboard LEDs
Shutdown and Reboot
Exploring Embedded Linux Systems
Introducing Embedded Linux
Advantages and Disadvantages of Embedded Linux
Is Linux Open Source and Free?
Booting the RPi
Managing Linux Systems
The Super User
Other Linux Topics
Using Git for Version Control
A Practice-Bases Introduction
Common Git Commands
Using Desktop Virtualization
Analyzing Your Circuits
Basic Circuit Principles
Voltage, Current, Resistance and Ohm’s Law
Implementing RPi Circuits on a Breadboard
Digital Multineters and Breadboards
Example Circuit: Voltage Regulation
Switches and Buttons
Programming on the Raspberry Pi
Performance of Languages on the RPi
Setting the RPi CPU Frequency
A First Circuit for Physical Computing
Scripting Language Options
Dynamically Compiled Languages
Java on the RPi
C and C++ on the RPi
C and C++ Language Overview
LED Control in C
The C of C++
Overview of Object-Oriented Programming
Objected-Oriented LED Control in C++
Interfacing to the Linux OS
Glibc and Syscall
Improving the Performance of Python
Extending Python with C/C++
Interfacing, Controlling and Communicating
Interfacing to the Raspberry Pi Input/Outputs
GPIO Digital Output
GPIO Digital Input
Internal Pull-Up and Pull-Down Resistors
Interfacing to Powered DC Circuits
C++ Control of GPIOs Using
More C++ Programming
An Enhanced GPIO Class
Memory-Based GPIO Control
GPIO Control Using devmem2
GPIO Control Using C and /dev/mem
Changing the Internal Resistor Configuration
The GPIO Commmand
Programming with WiringPi
Button Press - LED Response