Raspberry Pi Intro Readthedocs Io en Latest
Raspberry Pi Intro Readthedocs Io en Latest
Raspberry Pi Intro Readthedocs Io en Latest
Release 0.0
Greg Loyse
1 Overview 3
1.1 Layers of Abstraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 General Purpose Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Tinkering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Open Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 Getting Started 5
2.1 Hardware Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 Start up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.4 System configuration raspi-config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.5 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.6 Starting the Desktop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.7 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.8 Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3 Navigation 11
3.1 echo Hi - Print to screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 Location . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.3 Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.4 Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.5 Building a file system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.6 man the docs & less . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.7 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.8 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.9 Installing tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.10 executing a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.11 Exercise: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.12 Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4 Operating Systems 17
4.1 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.2 System specs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.3 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.4 Modules & Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.5 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.6 Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
i
4.7 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.8 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.9 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
5 Files 21
5.1 The nano editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.2 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.3 Deleting files & directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
5.4 Text as ASCII . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5.5 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5.6 Compiling C programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5.7 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5.8 Recap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6 Scratch 25
6.1 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
7 Python 27
8 Electronics 29
8.1 GPIOs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
8.2 Our project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
8.3 A Breadboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
8.4 Light up LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
8.5 Blinking LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
8.6 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
8.7 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
8.8 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
9 Resources 33
ii
raspberry-pi Documentation, Release 0.0
Contents:
Contents 1
raspberry-pi Documentation, Release 0.0
2 Contents
CHAPTER 1
Overview
Welcome to our session. What follows are our essential teaching goals. The high level take away.
In a sense all a computer can do is store 0s and 1s, move them around, and add.
Yet the computers of today (software and hardware) are incredibly complex.
This complexity is managed by using layers of abstraction.
We will see how you can use computers creatively by designing and implementing layers of abstraction; combining
simple components to build ever more complex and useful building blocks.
The components of a Raspberry Pi are similar to those you will find in any modern device (phone, tablet, laptop,
desktop...).
We focus on the concepts that can be translated to all other devices and operating systems. Android, iOs, Windows,
Mac OSX, and the various Linuxes.
1.3 Tinkering
What sets the Raspberry Pi apart is that it exposes as much as possible and deliberately tries to get you to play.
It is the antithesis of the design princple don’t make me think.
The intention is to encourage tinkering in a safe environemnt that exposes far more of the innner workings than the
more protected mainstream experiences that other devices provide.
3
raspberry-pi Documentation, Release 0.0
You can’t break anything that can’t be fixed! If you do break something, congratulations you have done something
cretive and it is a leaning opportunity awaits.
Open source broadly means that a the author(s) grant permission for anyone to freely read, modify, and build their
software or hardware designs.
Linux and GNU really speerheaded the movement. It is now quite common place. Android, a linux variant, is open
source. However the majority of our devices aren’t.
This is one of the reasons why the Raspberry Pi is so accessible. There are no locked doors. How far you explore is
limited only by your interest.
4 Chapter 1. Overview
5
raspberry-pi Documentation, Release 0.0
CHAPTER 2
Getting Started
2.2 Exercise
2.3 Start up
Linux raspberrypi 3.12.22+ #691 PREEMPT Wed Jun 18 18:29:58 BST 2014 armv6l
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
The pi@raspberrypi ~ $ is called a prompt. You type commands after the ‘$’.
pi@raspberrypi ~ $ startx
2.2. Exercise 7
raspberry-pi Documentation, Release 0.0
| |
| <Select> <Finish> |
| |
---------------------------------------------------
2.5 Exercise
pi@raspberrypi ~ $ startx
2.7 Exercise
2.8 Recap
2.7. Exercise 9
raspberry-pi Documentation, Release 0.0
Navigation
The GUI is a visual metaphor for interacting with a computer. Images represent things such as files, programs, and
folders. Windows act as containers where programs live.
The command line or shell is another way of doing the same. Instead of using a mouse and clicking, we type com-
mands. In many use cases it is far more powerfull than a GUI.
We are going to introduce the basics of this programming language. Keep in mind the equivalent GUI way of doing
things.
3.2 Location
You are always somewhere in the computer’s filesystem. You can get lost, but its always easy to find out where you
are.
Type this:
pi@raspberrypi ~ $ ls
pi@raspberrypi ~ $
pi@raspberrypi ~ $ ls
Desktop Documents my_turtle.py networking-lessons ocr_pi.png python_games
˓→Scratch
pi@raspberrypi ~ $ pwd
/home/pi
11
raspberry-pi Documentation, Release 0.0
Note the prompt helps you remember where you are. Here ~ represents the current users’ home directory.
3.3 Users
You are a user. Every user needs a name. The default users is pi.
If you ever forget who you are, ask:
pi@raspberrypi ~ $ whoami
3.4 Navigation
Just like we are used to navigating using our mouse and clicking on folders, we can do the same in the shell.
Type the following. At each step explain what it is doing:
pi@raspberrypi ~ $ cd python_games
/home/pi
pi@raspberrypi ~/python_games $ pwd
/home/pi/python_games
pi@raspberrypi ~/python_games $ ls
[... lots of files, many ending in .py ...]
Note navigation gets easier if you keep a mental image in your head of where you are. Visually, a file system is just
like a tree. There is a root and the branches are folders, the files are leafs. We will install a program to help.
Note also that the prompt tells you where you are relative to the home directory. Your home directory is your users’
home. An absolute location is the location of a file or folder in terms of the root of the computer’s file system.
Note: We will drop the pi@raspberrypi part of the prompt from now on.
~ $ mkdir music
~ $ cd music
~/music $ ls
~ $ ls
~/music $ mkdir rock classical jazz
~/music $ ls
classical jazz rock
~/music $ cd rock
~/music/rock $ ls
~/music/rock $ touch albums.txt
~/music/rock $ ls
rock.txt
~/music/rock $ cat rock.txt
~/music/rock $ echo "Beatles - The White Album - 1968" > rock.txt
~/music/rock $ cat rock.txt
Beatles - The White Album - 1968
~/music/rock $ cd ..
12 Chapter 3. Navigation
raspberry-pi Documentation, Release 0.0
~/music $ pwd
/home/pi/music
We have seen the following commands: ls, mkdir, cat, touch, pwd
To inspect the documentation for a command try:
~ $ man ls
Welcome to less. This is what is called a pager. It’s a file reader, a bit like a very lite browser for your desktop.
Some less commands:
`space` up a page,
`w` up a page
`q` exit
`h` help on many other commands (displayed in `less` of course)
3.7 Exercise
For each command we have seen inspect the man documentation. Parse the many options. Write in one sentance what
it does.
3.8 Exercise
Complete the above music collection database. Ensure that each music category folder has its own file with at least
one album example in it.
The end result should look like this:
~ $ tree music/
music/
- classical
| - albums.txt
- jazz
| - albums.txt
- rock
- albums.txt
cd followed by ls to explore directories becomes tedious. tree is a program that shows a directory and its contents.
First we need to install it. Typically you are used to browsing for software, downloading it and then clicking on a
package to install it. You may also have to answer some questions.
Things are similar in linux except the vast majority of software exist in vast centralised repositories. This is thanks to
the open source nature of most Linux software.
http://www.raspbian.org/RaspbianRepository
In Linux we use apt-get to install new software:
Not any user can install software. The sudo part is to have permissions to install new software. You may need to enter
your password.
Now we can take a look at our creation:
tree music
So far we have been programming interactively, executing one command at a time. It would be useful to group
commands so that they can be executed together. It would also be useful so as not to forget the useful sequence of
commands.
We do this by writing commands in a file and executing that file.
Create a file named make-jazz.sh and insert this:
mkdir jazz
cd jazz
touch albums.txt
echo "Miles Davis - Kind of Blue - 1959" > albums.txt
chomod +x make-jazz.sh
./make-jazz.sh
The ./ prefix indicates where to find the program to the shell. Effectively it means run the make-jazz.sh command
which is here (.).
3.11 Exercise:
3.12 Recap
14 Chapter 3. Navigation
raspberry-pi Documentation, Release 0.0
3.12. Recap 15
raspberry-pi Documentation, Release 0.0
16 Chapter 3. Navigation
CHAPTER 4
Operating Systems
4.1 Exercise
In which level have all of the programs we have run so far existed?
17
raspberry-pi Documentation, Release 0.0
4.3 Exercise
lsmopi@raspberrypi ~ $ lsmod
Module Size Used by
joydev 9096 0
evdev 10497 3
snd_bcm2835 18169 0
snd_soc_bcm2708_i2s 5486 0
regmap_mmio 2818 1 snd_soc_bcm2708_i2s
snd_soc_core 128166 1 snd_soc_bcm2708_i2s
regmap_spi 1913 1 snd_soc_core
snd_pcm_dmaengine 5481 1 snd_soc_core
snd_pcm 81518 3 snd_bcm2835,snd_soc_core,snd_pcm_dmaengine
snd_page_alloc 5168 1 snd_pcm
regmap_i2c 1657 1 snd_soc_core
snd_compress 8136 1 snd_soc_core
8192cu 551136 0
leds_gpio 2055 0d
pi@raspberrypi ~ $ lsusb
pi@raspberrypi ~ $ lsusb
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 7392:7811 Edimax Technology Co., Ltd EW-7811Un 802.11n
˓→Wireless Adapter [Realtek RTL8188CUS]
Some names are useful. For example you can clearly identify the wireless adapter and mouse. But which one is my
keyboard?
4.5 Exercise
4.6 Processes
Commands:
top
htop # requires installing
4.5. Exercise 19
raspberry-pi Documentation, Release 0.0
4.7 Exercise
Using top or htop, identify the process that is running the current terminal.
How do you kill it? Try using your intution combined with documentation, then Google.
4.8 Networking
We all have personal networks at home. Mostly we interact with it by setting it up with users and passwords.
Here we introduce some tools to explore our network.
commmands:
ifconfig
sudo iwlist wlan0 scan | grep ESSID
4.9 Exercise
Files
Computers fundamentally only process 0s and 1s. In order for them to be useful we need to find ways to represent
things that are relevant to us.
Files are the basic unit of storage. When we save to file, the information persists even after the power is switched off.
There are many types of files and many different ways to represent types of things that are relevant to us. Examples
are images, videos, sound files...
We are going to look at one of the ways to represent text.
There are many editors available. Perhaps the most common is Microsoft Word. Nano is much more basic.
We use simple editors as we are working at a lower level of abstraction than those typically provided by document
editors such as Windows or Open Office.
launch nano:
namo
The list of nano commands are at the bottom. ^ signifies the Ctrl key. Exit is denoted as ^X this translates to Ctrl + X.
5.2 Exercise
Create a file named file, enter some text and save it.
21
raspberry-pi Documentation, Release 0.0
rm my_file
rmdir my_dir
Computers need some way of representing roman characters. An industry standard which has been in use for quite
some time is ASCII.
You can get some documenation on ASCII by typiing:
man ascii
5.5 Exercise
What do the numbers 68 69 0a represent? Map them to characters using man ascii as reference
cd /opt/vc/src/hello_world
ls # Makefile world.c
nano world.c
make
ls # hello_world.bin Makefile world.c
./hello_world.bin # Hello world!
5.7 Exercise
22 Chapter 5. Files
raspberry-pi Documentation, Release 0.0
5.8 Recap
5.8. Recap 23
raspberry-pi Documentation, Release 0.0
24 Chapter 5. Files
CHAPTER 6
Scratch
6.1 Exercise
Using either:
• scratch in the browser http://scratch.mit.edu/
• scratch on the desktop
Create an animation of the cat going round in a square.
Then Draw a square.
Use the control panel to refactor your solution and reduce the repeated instructions.
25
raspberry-pi Documentation, Release 0.0
26 Chapter 6. Scratch
CHAPTER 7
Python
27
raspberry-pi Documentation, Release 0.0
28 Chapter 7. Python
CHAPTER 8
Electronics
8.1 GPIOs
The board.
Symbolically.
29
raspberry-pi Documentation, Release 0.0
8.3 A Breadboard
30 Chapter 8. Electronics
raspberry-pi Documentation, Release 0.0
8.6 Exercise
8.7 Exercise
8.8 Resources
32 Chapter 8. Electronics
CHAPTER 9
Resources
33