Raspberry Pi: Step by Step Guide
Raspberry Pi: Step by Step Guide
By Ashraf AlMadhoun
Mechatronics Engineer
What is a Raspberry Pi?
• University of Cambridge’s Computer Laboratory
– Decline in skill level
– Designed for education
• A credit card sized PC
• Plugs into a TV or monitor
• Inexpensive(ish) ~$35 each
• Capability:
– Programming
– Electronic Projects
– Office
– Play HD Videos
Kit Components
• Essential:
– Raspberry Pi board
– Prepared Operating System SD Card
– USB keyboard
– Display (with HDMI, DVI, or Composite input)
– Power Supply
• Highly suggested extras include:
– USB mouse
– Internet connectivity - LAN cable
– Powered USB Hub
– Case
Programming Languages
• The Raspberry Pi Foundation recommends Python
• Any language which will compile for ARMv6 can be
used
• Installed by default on the Raspberry Pi:
– C
– C++
– Java
– Scratch
– Ruby
Raspberry Pis in Primary Ed.
http://www.youtube.com/watch?v=KdTwQXre1DU
SETUP
Power
5v micro
USB
connector
10/100Mb
Ethernet
Internals LAN Controller
DSI
(display
interface) CSI
(camera
interface)
SOC (System On a Chip)
Broadcom BCM2835 700Mhz
Storage
SD Card Slot
(supports SD cards up to 32GB)
10 USES FOR
A RASPBERRY PI
10 Office
9 8 7 6 5 4 3 2 1
Office
10 9 8 7 6 5 4 3 2 1
9 Programming
10 8 7 6 5 4 3 2 1
Programming
10 9 8 7 6 5 4 3 2 1
Game
8
Console
10 9 7 6 5 4 3 2 1
Game
Console
10 9 8 7 6 5 4 3 2 1
7 Web Server
10 9 8 6 5 4 3 2 1
Web Server
10 9 8 7 6 5 4 3 2 1
6 Tor Router
10 9 8 7 5 4 3 2 1
Tor Router
10 9 8 7 6 5 4 3 2 1
5 HTPC
10 9 8 7 6 4 3 2 1
HTPC
10 9 8 7 6 5 4 3 2 1
4 Bird House
10 9 8 7 6 5 3 2 1
Bird House
10 9 8 7 6 5 4 3 2 1
Super
3
Computer
10 9 8 7 6 5 4 2 1
Supercomputer
10 9 8 7 6 5 4 3 2 1
2 Clock
10 9 8 7 6 5 4 3 1
Clock
10 9 8 7 6 5 4 3 2 1
1 PiBot!
10 9 8 7 6 5 4 3 2
PiBot!
10 9 8 7 6 5 4 3 2 1
Let’s Begin the Raspberry Pi Journey
Different versions of Raspberry Pi
• First Version
- It came with a very small and cheap size, but it came too small
that it’s really hard to use, it has the size of two coins with only
one USB port and one HDMI port to connect it to high screens.
Different versions of Raspberry Pi
• Second Version
- It came with a larger size than the previous one with an additional
GPIO port which allows connecting electronics components such as
switches and Leds.
Different versions of Raspberry Pi
• More advance Versions
- It came with more improvements, smaller size and a very low
cost, at only 25$.
- Image below shows the three versions of the Pi in the hand of
their manufacturers
Different versions of Raspberry Pi
• Model A and Model B
Different versions of Raspberry Pi
• Model A and Model B
Raspberry Pi Components
Raspberry Pi Components
Broadcom BCM2835
CPU
RAM
Raspberry Pi Components
Raspberry Pi Components
OS Version Available for Raspberry Pi
Main OS Version for Raspberry Pi
• Raspbian:
• It is the Foundation’s official supported
operating system. You can install it with
NOOBS or download the image below
and follow our installation guide.
TV Screen
What do you need to Run Raspberry Pi
HDMI Cable
Female-to-male wires
Components needed for Electronic
Experiments
Raspberry Pi Breakout Cable Optional
Components needed for Electronic
Experiments
You can get Adafruit board which
You can mount it above the Raspberry Pi Board to
easy access all ports
Other Components
Getting OS version on Raspberry Pi
Download Raspbian OS from the website below
http://www.raspberrypi.org/downloads
After downloading Unzip the file using WinRAR or 7zip
Disadvantages:
1- Can’t simulate electronic control projects or GPIO projects
2- Can consume a bit of your CPU power and RAM.
Section III: A tour inside Raspberry Pi
Operating system
Section III: A tour inside Raspberry Pi
Operating system
Raspberry Pi Command Line
- Raspian has a program Called LXterminal which gives us
access to the system shell, we will call it command line
interface .
- You can run it from the desktop or from the LXDE menu
Raspberry Pi Command Line
- A list of most commonly used command line commands and
the use of each of them can be seen below.
Updating Software Packages
- A tool called apt-get is used to update all the software
packages within the system.
- You can install any software that you want using this too, by
simply writing install + program name.
Updating Software Packages
- You can also get software packages from the Pi Store, which
is similar to ITunes and Google play store
Installing debian Software Packages
- Installing a .deb software package that you can get online
from any software developer website.
- More Sources:
• http://ss64.com/bash
• http://www.pixelbeat.org/cmdline.html
• http://linuxcommand.org/learning_the_shell.php
Section IV: Controlling Raspberry Pi From
another device
Control using SSH (Secure Shell)
- Control using SSH (Secure Shell):
- It mainly consist of a server and a client.
1- We will need to activate SSH Server in the Raspberry Pi side
Control using SSH (Secure Shell)
Control using SSH (Secure Shell)
2- Knowing the IP Address of the Raspberry Pi using the ifconfig
command.
Enter this data inside the file, then Press Ctrl+x to save the file.
[Desktop Entry]
Type=Application
Name=TightVNC
Exec=vncserver :1
StartupNotify=false
Control using VNC Protocol
Assign a Fixed IP Address for Raspberry Pi
Board
- In both cases VNC and SSH, we get the IP using ifconfig command,
which means that we need to connect Raspberry Pi at least once
every time we want to remote control it, to know it’s IP.
- To solve this problem we are going to assign a Fixed IP Address to
our Raspberry Pi board by going to our Internet Router and look for
the Mac address of the Board and assign it to a fix IP address which
can’t be changed no matter what.
Steps:
1- Go to your main internet router page: usually written in the
back of the router and could be any of these:
192.168.1.1 – 192.168.0.1 – 192.168.254.0
2- The explanation will be on TP-Link Router but it’s the same
process for all
Assign a Fixed IP Address for Raspberry Pi
Board
- After entering the main router page you will be asked to
provide the username and password
- Enter the Boot folder and open the cmdline.txt which has the start
up settings for the Raspberry Pi, head to the end of the file and add
the network IP as follow.
- ip=192.168.1.10
- After finishing the editing, save the file and re-attach the SD
Card to the Raspberry Pi.
Setting the Computer
- We will give the computer an IP Address, to do this attach the
Raspberry Pi to the computer using the network cable, then
enter the network settings.
Setting the Computer
- Now you can connect to the Board using PuTTY or VNC viewer, note that
you need to reset the network setting when you finish working with the
Board so that your computer can get an automatic IP address from the
internet provider.
Section V: Programming GPIO with Python
GPIO – General Purpose Input/Output
- Raspberry Pi Board has 28 connection point/Pin called GPIO, some of the is
used for electronic control Output, while others are used to sense signals
called Input providing Power and communication protocol.
GPIO – Power & Control Pins
- Pin 1,7: Provide 3.3 volt, 50 mA, incase you need more than
50 Ma, you need to use an external relay to control, or you
might damage the board.
- Pin 2,4: Provide 5 volt and is connected to Micro USB on the
board
- Pin 6,9,14,20,25: are for Ground.
- Pin (3, 5, 7, 8, 10, 11, 12, 13, 15, 16, 18, 19, 21, 22, 23, 24,
26): for control operation as Input or Output, you can control
this using the written software.
- Mathematical Calculations:
- In python you can do any mathematical operation easily, just
type the two numbers and the result will come:
- You can write it by doing to Menu > Accessories > Leafpad and
write the code then click on File > Save and assign the name
sum.py store it inside the /home/pi folder
- To run the program write python sum.py in the command line
Installing the Control Library
- Before start using Python to control GPIO, We will install the
control library rpi.gpio which we can get by writing:
Nano: Code editor to Be Used
- Nano is one of the best text editor in Command Line
environment inside Linux systems.
- We will use it as our default coding program.
- It’s really simple to run it, just write nano Yourfilename and it
will open a new file with the name you assigned in the
previous command.
Nano: Code editor to Be Used
- To save the file click on Ctrl+X a message will appear, hit Y then
enter.
Example 1: Blinking Led
• Goal From Example:
- Running a Led and make it turn on
and off indefinitely.
• Required Components:
- Bread Board
- Raspberry Pi Board
- LED
- 300 Ohm Resistor
- Connection Wires
Example 1: Blinking Led
• Preparing Project Parts:
- Running a Led and make it turn on
and off indefinitely.
Example 1: Blinking Led
• Code:
Example 1: Blinking Led
• Applying the code and see the result:
• This message appear when running a software after another at the very
same port, you can ignore this message and if you would like to hide it use
this sentence GPIO.setwarnings(False)
Explaining the Code
• This is used to call libraries, this is always the
start of each program, calling time library for
time measurements and calling the GPIO
Library.
• Required Components:
- Bread Board
- Raspberry Pi Board
- Switch
- 10k Ohm Resistor
- Connection Wires
Example 2: Reading Input Values From
A Switch
Example 2: Reading Input Values From
A Switch
• The result
• Explaining If statement
Example 2: Reading Input Values From
A Switch
• Explaining If statement
Example 3: Running the Led using LDR
• Goal From Example:
- Running a Led and make it turn on
and off using Light emitting diode.
• Required Components:
- Bread Board
- Raspberry Pi Board
- LED
- 10k Ohm Resistor
- 300 Ohm Resistor
- LDR Light emitting diode
- Connection Wires
Example 3: Running the Led using LDR
Example 3: Running the Led using LDR
Example 3: Running the Led using LDR
• Save the file and run the code
• Now close the light room or place your hand at the LDR and see what will
happen to the LED.
• Improvements to Example 3
1- You can change True to False and see the result.
2- You can add Print sentence when executing the two output commands
Example 4: PIR Motion Detector
• PIR is one of the most important sensors in practical life, used
in many projects such as protection and smart doors.
• You can see it mostly in Centers and Companies doors which
opens once you get close to it.
• These type of sensors can use different methods to detect:
Ultrasonic waves, infrared signal and many more.
• In this lesson we will use the IR based PIR Motion Detector.
Example 4: PIR Motion Detector
• This sensor can sense the presence of an object at a distance
of 7 to 10 Meter and an angle between 90 to 110 degree.
• This is enough for a small room.
Example 4: PIR Motion Detector
• This sensor has 3 connection points which are: volt in 3 to 5
volt red wire, digital out yellow wire, Ground black wire.
Example 4: PIR Motion Detector
• We will connect the red wire to the pin 1 in the Pi which is the
5 volt output.
• Connect the yellow wire to the 23 pin
• Connect the black wire to pin number 6 which is Ground.
Example 4: PIR Motion Detector
• Start the editor
Example 4: PIR Motion Detector
• Run the code and see the result.
Board
Numbering
Where is the difference?
• The only difference is in the calling method of the GPIO library
in Python, if we want to run the terminals in GPIO order we
will call the library using:
• Note:
• This is important thing to do because you will find
lots of projects online use BCM numbering so you
need to take good care of this and connect all
components to pins the right way.
Adafruit Protection Box and
Connection Cable
• Adafruit is considered one of the biggest open source
electronics companies that have many product for Arduino,
Raspberry Pi and much more.
• Note:
• The cable uses BCM numbering, so be sure to import the BCM
library in Python.
Assembling the Protection Box
Assembling Adafruit GPIO breakout
cable
Questions?
Skype: EngASM89
Email: Eng.asm.89@gmail.com
Mobile: +972595399311