0% found this document useful (0 votes)
8 views63 pages

Raspberry Pi (Sh)

The document provides an overview of the Raspberry Pi, a low-cost single-board computer suitable for various projects, programming, and interfacing with electronic components through GPIO pins. It details the necessary hardware, installation of the Raspberry Pi OS, and programming with Python, including examples of using GPIO for controlling devices like LEDs. Additionally, it discusses remote access options and integration with cloud services like ThingSpeak for IoT applications.

Uploaded by

kgfkgf100k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views63 pages

Raspberry Pi (Sh)

The document provides an overview of the Raspberry Pi, a low-cost single-board computer suitable for various projects, programming, and interfacing with electronic components through GPIO pins. It details the necessary hardware, installation of the Raspberry Pi OS, and programming with Python, including examples of using GPIO for controlling devices like LEDs. Additionally, it discusses remote access options and integration with cloud services like ThingSpeak for IoT applications.

Uploaded by

kgfkgf100k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

Raspberry Pi

Contents
 Open Platform (like Raspberry Pi)
 Architecture
 Programming
 Interfacing
 Accessing GPIO Pins
 Sending and Receiving Signals Using GPIO Pins
 Connecting to the Cloud
Raspberry Pi
Raspberry Pi is a tiny (about 9x6cm), low-cost ($35+), single-
board computer that supports embedded Linux operating
systems

The recommended Operating


System is called Raspberry Pi OS
(Linux based)

https://www.raspberrypi.org
Raspberry Pi
• The Raspberry Pi is a small computer that can do lots of things
• It has a small footprint (about 9x6cm) and it is cheap ($35+)
• You plug it into a monitor and attach a keyboard and mouse
• It has GPIO pins (General Purpose Input/Output) for connecting sensors
and other electronic components like LEDs, etc.
• Raspberry Pi is well suited for prototyping, datalogging and different
electronics projects, a media center, etc.
• It is used to learn programming, IT and other technical skills, etc.
• RP has limited power (CPU, RAM, etc.) so it cannot normally replace a
desktop computer or laptop for ordinary use
• The “Hardrive” is a Micro SD card
https://www.raspberrypi.org
Raspberry Pi
GPIO Pins

microSD Card Ethernet


(the Back )

Camera
Connector USB A x 4

Power Supply (USB C) microHDMI x 2 https://www.raspberrypi.org


Raspberry Pi Versus Arduino
• Arduino is a Microcontroller
• Raspberry Pi (RP) is a Microcomputer
• Arduino has a Bootloader and not an
• It has an ordinary Operating System (OS)
ordinary operating system
• RP runs Linux applications
• You can connect USB devices, • Arduino is NOT a computer, only a
small controller, whose purpose is to
Keyboard, Mouse, Monitors, etc.
control things
• It has a “hard-drive“ in form of a microSD card
• No Bluetooth, Wi-Fi (some models
• RP has Bluetooth, Wi-Fi, and Ethernet
have), and Ethernet (but can be provided
connection
as so called Shields)
• RP has basically all the features an ordinary
computer has but in a much smaller package • Very little RAM (a few Kb)
• 2, 4 or 8 Gb RAM • Inexpensive

Both have Digital Pins


Both have SPI and I2C
Arduino (UNO) has also Analog Input Pins
What Do You Need?
 Raspberry Pi
 Micro SD Card (+ Adapter)
 Power Supply
 microHDMI to HDMI Cable
 Monitor
 Mouse
 Keyboard
What Do You Need?
USB C Power Supply Monitor (with HDMI) microHDMI to HDMI Cable
15W (5V, 3A)

Micro SD Card (+ Adapter)


Raspberry Pi
Mouse
Keyboard
(8GB or more)

https://www.raspberrypi.org
Raspberry Pi OS
 In order make your Raspberry Pi up and running you need to
install an Operating System (OS)
 The OS for Raspberry Pi is called Raspberry Pi OS
(previously known as Raspbian)
 Raspberry Pi runs a version of an operating system called
Linux (Windows and macOS are other operating systems).
 To install the necessary OS, you need a microSD card
 Then you use the Raspberry Pi Imager in order to download the
OS to the microSD card
 https://www.raspberrypi.org/software/
Raspberry Pi Imager
Start using Raspberry Pi

• Put the microSD card into the Raspberry Pi


• Connect Monitor, Mouse and Keyboard
• Connect Power Supply
• Follow the Instructions on Screen to setup Wi-Fi
Raspberry Pi OS etc.
Connect to Wi-Fi Network
Use WiFi network

• The first thing you typically


need to do is to connect your
Raspberry Pi to a Wi-Fi
network.
• Your home network normally
works fine. If you are in a
company or a university, you
may get some trouble.
• In these situations, you can
configure a Mobile hotspot
using your Windows 10/11
PC
Shut-down Raspberry Pi

Select the Raspberry Pi icon in upper left corner and select “Logout”
The Terminal
The Linux Terminal

 The Raspberry Pi OS is a Linux based OS and comes with


a GUI with limited features, so very often you need to type
“low-level” commands using the Terminal.
 The Linux terminal is a powerful tool for “low- level”
operations on the OS.

https://www.raspberrypi.org/documentation/usage/terminal/
Update Raspberry Pi OS
 Run the following commands in the Terminal window:
 First run the following
sudo apt update
(refreshes the system's package list by downloading the latest information
about available software packages from the configured repositories,
ensuring your system knows about the newest versions and dependencies.)
Then
sudo apt full-upgrade
(a command that upgrades your system to the latest version of packages by
removing packages if necessary to satisfy dependencies)

https://www.raspberrypi.org/documentation/raspbian/updating.md
sudo
 sudo is a program for Unix-like computer operating systems (like
Linux) that allows users to run programs with the security
privileges of another user, by default the superuser.
 It originally stood for "superuser do”
 Some commands that make permanent changes to the state of your
system require you to have root privileges to run.
 The command sudo temporarily gives your account (if you're not
already logged in as root) the ability to run these commands.
 You typically use it in your Terminal window for
installing/upgrading software, etc.
https://www.raspberrypi.org/documentation/linux/
How to Power off your RP
 Unlike your other electronic devices, Raspberry Pi doesn’t
come with an “off” switch. Or use the Graphical Interface:
 You should not just “pull out the plug“
 Enter the following in the Terminal:
sudo poweroff
 This will give you a clean shutdown. After that, you can unplug
the computer.
 How to turn on Raspberry Pi after shutdown? It’s simple. Just
plug it in.
https://www.proqsolutions.com/shutting-down-your-raspberry-pi/
Tools- Scrot
 Scrot is a built-in tool for Screen Capture
 Scrot is a Command-line tool with no GUI
 Hit “Print Screen” key and the image will be saved in the
“/home/pi” folder
Gnome Screenshot
• Gnome Screenshot is better than “Scrot” since it has a desktop GUI
• You need to install it (the Terminal):
sudo apt update && sudo apt upgrade sudo apt install
gnome-screenshot
• It will then appear in Menu > Accessories > Screenshot
https://magpi.raspberrypi.org/articles/take-screenshots-raspberry-pi
Remote Access
Typically, you want to have Remote Access to you
Raspberry Pi from your ordinary Desktop Computer.
Different options:
 SSH – Command Line Access
Or use some kind of Remote Desktop software:
 XRDP/RDC
 TeamViewer
 VNC
Remote Access

Make sure to Enable the


Feature you want to use
Alt1: Remote Desktop
1. Install XRDP https://en.wikipedia.org/wiki/Xrdp

– XRDP is a free and open-source implementation of Microsoft RDP


(Remote Desktop Protocol) server. Install it by enter the following:
– sudo apt-get install xrdp
2. Open Remote Desktop Connection (RDC) on your
Windows Computer. RDS is also available for
macOS
– Enter Computer Name or IP Address
– Default UserName is “pi” and default Password is “raspberry” (unless
you have changed it)
Alt1: Remote Desktop
Open Remote Desktop Connection (RDC) on
your Windows Computer:
Enter the IP address

If you use a Mobile hotspot in


Windows 10, you can see if your
Raspberry Pi is up and running.
You can also see the IP address
You are ready to start using
Raspberry Pi remotely
from your PC

Alt1: Remote
Desktop
Alt1: Remote Desktop
Do you get a blue screen after login using RDC/RDP on your Window
Computer?

You may need to create another user on your Raspberry Pi. The Pi
root user cannot RDP for whatever reason?
 In the Terminal: sudo adduser <username>
 Choose password
 Confirm password
 Hit enter for defaults
 Try RDC/RDP again with that login
Alt2: TeamViewer

There exists a separate version of TeamViewer for Raspberry Pi

https://pimylifeup.com/raspberry-pi-teamviewer/
Alt2: TeamViewer
Start TeamViewer on your Raspberry Pi:
Here you see TeamViewer on your Raspberry Pi:

Open TeamViewer by enter


“teamviewer” in the Terminal
Alt2: TeamViewer
Install TeamViewer on your PC as well. Then use TeamViewer to connect to your Raspberry Pi:
Python with Raspberry Pi
 Python is a fairly old Programming Language (1991) compared to
many other Programming Languages like C# (2000), Swift (2014),
Java (1995), PHP (1995).
 Python has during the last 10 years become more and more popular.
 Today, Python has become one of the most popular Programming
Languages.
 The Raspberry Pi OS comes with a basic Python Editor called
"Thonny".
https://www.raspberrypi.org/documentation/usage/python/
Python with Raspberry Pi
• The Raspberry Pi OS comes with a basic Python Editor
called Thonny

But you can install and use


other Python Editors if you
prefer

https://www.raspberrypi.org/documentation/usage/python/
Print Hello World
Tools -> Manage packages…

Python Packages with


Thonny
GPIO

Raspberry Pi is well suited for prototyping, datalogging and different electronics


projects using the GPIO Pins
GPIO Features
The GPIO pins are Digital Pins which are either True (+3.3V) or False
(0V). These can be used to turn on/off LEDs, etc.
In addition, some of the pins also offer some other Features:
 PWM (Pulse Width Modulation)
Digital Buses (for reading data from Sensors, etc.):
 SPI
 I2C
GPIO with Python
 You can make all kinds of Python program on your Raspberry Pi
 The UNIQUE thing with Raspberry Pi compared to an ordinary
PC is the GPIO connector.
 With GPIO you can connect LEDs, Sensors, control Motors, etc.
 You typically use Python in order to communicate with GPIO
connector.
 That what's makes the combination Raspberry Pi + Python
UNIQUE!
GPIO Zero
• The GPIO Zero Python Library can be used to communicate with
GPIO Pins
• The GPIO Zero Python Library comes preinstalled with the
Raspberry Pi OS
Resources:
• https://www.raspberrypi.org/documentation/usage/gpio/pytho n/
• https://www.raspberrypi.org/documentation/hardware/raspber
rypi/gpio/
• https://pypi.org/project/gpiozero/
• https://gpiozero.readthedocs.io/en/stable/
Raspberry Pi GPIO Pins LED Example

LED

R=270Ω

GND (Pin 32)

GPIO16 (Pin 36)


Breadboard
LED Example
from gpiozero import LED
from time import sleep

pin = 16
led = LED(pin)

while True:
led.on()
sleep(1)
led.off()
sleep(1)

https://www.raspberrypi.org/documentation/usage/gpio/python/
Blinking of LEDs
from gpiozero import LED // imports the LED class from the gpiozero library

from time import sleep //pause the program for a specific number of seconds

pin = 16 //defines the GPIO pin number 16 connected to LED


led = LED(pin) // an object named led which is an instance of the LED class

while True:
led.on() // Turn LED on
sleep(1) // Wait for 1 second
led.off() // Turn LED off
sleep(1) // Wait for 1 second
LED Example using Thonny
GPIO Features
In addition to ordinary Digital Pins the Raspberry Pi GPIO
connector has other Features as well:
 PWM – Pulse Width Modulation

 SPI – Serial Peripheral Interface Communication


Interfaces for
 I2C – Inter-Integrated Circuit connection
different types
of Sensors, etc.
PWM
 We've seen how to turn an LED on and off, but how do we
control its brightness levels?
 An LED's brightness is determined by controlling the
amount of current flowing through it, but that requires a lot
more hardware components.
 A simple trick we can do is to flash the LED faster than
the eye can see!
 By controlling the amount of time the LED is on versus
off, we can change its perceived brightness.
 This is known as Pulse Width Modulation (PWM).
PWM
Below we see how we can use PWM to control the brightness of a LED

https://www.electronicwings.com/raspberry-pi/raspberry-pi-pwm-generation-using-python-and-c
SPI
• SPI is an interface to communicate with different types of
electronic components like Sensors, Analog to Digital
Converts (ADC), etc. that supports the SPI interface
• Thousands of different Components and Sensors supports
the SPI interface
https://www.raspberrypi.org/documentation/hardware/raspberrypi/spi/
I2C
 I2C is another interface to communicate with different
devices like Sensors, Analog to Digital Converts (ADC),
etc.
 Thousands of different Components and Sensors supports
the I2C interface.
MATLAB Support Package for Raspberry Pi

Getting Started with MATLAB Support Package for Raspberry Pi: https://youtu.be/32ByiUdOwsw
Test Hardware
Documentation and Examples
Blinking LED Example

Observe that the built-in LED


on the Raspberry Pi is blinking
Blinking LED Example
clear
We use the following Function: clc

writeLED(r, "LED0", 1); r = raspi;

for i = 1:10
disp(i);
writeLED(r, "LED0", 0);
pause(0.5);
writeLED(r, "LED0", 1)
pause(0.5);
end
Blinking LED GPIO Example
Blinking LED GPIO Example
clear rpi
rpi = raspi();
gpiopin = 16
ledvalue = 1;
writeDigitalPin(rpi, gpiopin, ledvalue);

for i = 1:10
ledvalue = 1;
writeDigitalPin(rpi, gpiopin, ledvalue);
pause(0.5);
ledvalue = 0;
writeDigitalPin(rpi, gpiopin, ledvalue);
pause(0.5);
end
ThingSpeak
 ThingSpeak is a Web Service (REST API) that lets you collect and
store sensor data in the cloud and develop Internet of Things
applications.
 It works with Arduino, Raspberry Pi and MATLAB (premade
libraries and APIs exists).
 But it should work with all kind of Programming Languages, since it
uses a REST API and HTTP.

https://thingspeak.com
ThingSpeak

https://thingspeak.com
ThingSpeak
 ThingSpeak is an IoT analytics platform service that lets you collect
and store sensor data in the cloud and develop Internet of Things
applications.
 The ThingSpeak service also lets you perform online analysis and act
on your data. Sensor data can be sent to ThingSpeak from any
hardware that can communicate using a REST API.

https://thingspeak.com
ThingSpeak

https://thingspeak.com
ThingSpeak + Labview

https://thingspeak.com
Configurations and Settings

https://thingspeak.com
import thingspeak
import time Python Example
from gpiozero import MCP3002 running on Raspberry Pi
adc = MCP3002(channel=0, differential=False)

channel_id = xxxxxxx
write_key = ”xxxxxxxxxxxxxxxxxx”

channel = thingspeak.Channel(id=channel_id, api_key=write_key)

N = 10
for x in range(N):
#Get Sensor Data
adcdata = adc.value #Scaled Value between 0 and 1
voltvalue = adcdata * 5 # Value between 0V and 5V
tempC = 100*voltvalue-50 # Temperature in Celsius
tempC = round(tempC,1)
print(tempC)

#Write to ThingSpeak
response = channel.update({'field1': tempC})
time.sleep(15)
Datalogging Application

https://thingspeak.com
Datalogging Application

https://thingspeak.com
Datalogging Application

https://thingspeak.com

You might also like