07 - Raspberry Pi
07 - Raspberry Pi
PRACTICAL NO: 07
OBJECTIVES:
1. Overview of architecture and functions of Raspberry Pie Computer.
Introduction
One of the buzzwords used heavily in the Automation and IT industry for the past couple of years is the
term IoT, which stands for Internet of Things. IoT refers to all of the things that are, well, connected to
the Internet, and that's how it got its name. With the maker movement which comes a host of DIY-Do it
itself) electronic platforms, IoT hardware developed.
64 | P a g e
3161716: Industry 4.0
Vishwakarma Government Engineering College
Department of Instrumentation and Control Engineering
IoT boards, also known as development boards or prototyping boards are basically hardware platforms
that are commonly used to build prototypes of manufacturer’s ideas. There’s a wide variety available
hardware and one can choose from between microcontroller-based boards, Single-Board Computers
(SBC) or even cellular-enabled IoT boards.
DIY electronic platform gifted us two main development boards such as Arduino and Raspberry Pi.
Arduino is a small and inexpensive electronic board that allows us to connect to various external
accessories such as sensors and create applications to use the data collected.
Another open-source hardware platform that has gotten very popular with hobbyists these days is
Raspberry Pi.
The Raspberry Pi contains CPU (Central Processing Unit), RAM (Random Access Memory) or GPU
(Graphics Processing Unit) as a whole called System on Chip (SoC) unit. This is essentially the entire
computer on one chip.
The Raspberry Pi uses an ARM1176JZF-S 700MHz CPU which is also installed in a wide variety of
mobile phones, hand held games consoles and e-Readers. This CPU is single core, however it does
have a co-processor to perform floating point calculations. Many calculations required by a program
involve whole numbers (integers). These are easier for the CPU to handle. Integer calculations produce
65 | P a g e
3161716: Industry 4.0
Vishwakarma Government Engineering College
Department of Instrumentation and Control Engineering
accurate results. Floating point or real numbers have a fractional part e.g. 1.5. They are more
demanding for the CPU to process.
Model B
The very first version of the Raspberry Pi created a big splash in the small single-board computers
(SBC) market. This device was both small, competent, and affordable. This version of the Raspberry Pi
set the baseline for its size for all future Pi devices. However, a couple of things you will notice are
quite different compared to the more modern Pi’s.
Model A
In February 2013, the Raspberry Pi model A was launched; it packed the same ARM single-core
processor and the GPU as model B with half of the RAM capacity at 256MB. Priced at Rs.40000 per
unit, the model A could be regarded as the cheaper and less powerful version of model B
66 | P a g e
3161716: Industry 4.0
Vishwakarma Government Engineering College
Department of Instrumentation and Control Engineering
Raspberry Pi 3
Raspberry Pi 3 is the third generation of Raspberry Pi and it packs quite a formidable punch in its credit
card-sized package. Most notably, in addition to the standard features of the Raspberry Pi (such as four
USB 2.0 ports and built-in Ethernet), it has:
A 1.2GHz 64-bit quad-core ARMv8 CPU
67 | P a g e
3161716: Industry 4.0
Vishwakarma Government Engineering College
Department of Instrumentation and Control Engineering
802.11n Wireless LAN
Bluetooth 4.1 Low Energy (BLE)
The powerful CPU coupled with Wireless LAN and Bluetooth 4.1 radio makes it an ideal candidate for
IoT projects, because multiple sensors can be connected to it simultaneously. In addition, the Raspberry
Pi has a 40-pin GPIO (General Purpose I/O) connector for interfacing with external sensors.
Powering the Raspberry Pi
One of the most popular OSs used for the Raspberry Pi is the Raspbian Operating system. The
Raspbian OS is based on the Debian OS, optimized for the Raspberry Pi hardware. The easiest way to
install the Raspbian OS for the Raspberry Pi is to download NOOBS from
https://www.raspberrypi.org/help/noobs-setup/.
NOOBS stands for New Out Of Box Software. The Raspbian OS boots off a micro-SD card and the
entire operating system runs off the card. A typical Class 4 8GB micro-SD card is sufficient for most
purposes, but you have the option to connect it to an external hard disk or flash drive for more storage.
Once the Raspbian OS is installed, you can proceed to log into it and see a full windowed system as
shown in figure -5. The default username is pi and the password is raspberry.
Fig-5: The Raspbian OS uses the LXDE (Lightweight X11 Desktop Environment)
68 | P a g e
3161716: Industry 4.0
Vishwakarma Government Engineering College
Department of Instrumentation and Control Engineering
The GPIO pins on the Raspberry Pi are divided into the following groups:
Power: Pins that are labeled 5.0v supply 5 volts of power and those labeled 3V3 supply 3.3 volts of
power. There are two 5V pins and two 3V3 pins.
GND: These are the ground pins. There are eight ground pins.
Input/Output pins: These are the pins labeled with the # sign, for example, #17, #27, #22, etc.
These pins can be used for input or output.
I2C: I2C is a serial protocol for a two-wire interface to connect low-speed devices like
microcontrollers, EEPROMs, A/D and D/A converters, I/O interfaces, and other similar peripherals
in embedded systems. These pins are labeled SDA and SCL.
UART: The Universal Asynchronous Receiver/Transmitter allows your Raspberry Pi to be
connected to serial peripherals. The UART pins are labeled TXD and RXD.
69 | P a g e
3161716: Industry 4.0
Vishwakarma Government Engineering College
Department of Instrumentation and Control Engineering
SPI: The Serial Peripheral Interface is a synchronous serial communication interface specification
used for short distance communication, primarily in embedded systems. The SPI pins are
labeled MOSI, MISO, SCLK, CE0, and CE1.
ID EEPROM: Electrically Erasable Programmable Read-Only Memory is a user-modifiable read-
only memory that can be erased and written to repeatedly through the application of higher than
normal electrical voltage. The two EEPROM pins on the Raspberry Pi (EED and EEC) are also
secondary I2C ports that primarily facilitate the identification of Pi Plates (e.g., Raspberry Pi
Shields/Add-On Boards) that are directly attached to the Raspberry Pi.
To demonstrate how to use the GPIO to connect to an external sensor, we'll now use a PIR motion
sensor to detect motion. For this, we use the Parallax PIR Motion Sensor (see Figure 9). The PIR
Sensor detects motion by measuring changes in the infrared (heat) levels emitted by surrounding
objects of up to three meters.
Fig-8:The Parallax PIR Motion Sensor Fig-9: A PIR Motion sensor connected to the
Raspberry Pi
70 | P a g e
3161716: Industry 4.0
Vishwakarma Government Engineering College
Department of Instrumentation and Control Engineering
GND: The ground pin. Connect this pin to the GND on the GPIO.
VCC: The voltage pin. Connect this pin to one of the 5V pins on the GPIO.
OUT: The output pin. Connect this to one of the Input/output pins on the GPIO.
When the PIR Motion sensor detects motion, it outputs a high signal on its output pin. We need to write
an application to read the value of this output pin. Figure-11 shows a PIR Motion sensor connected to
the T-Cobbler Plus.
Bonding the Raspberry Pi and the Sensors: The Python Programming Language
Now that the Raspberry Pi is connected to the PIR Motion Sensor, it's time to write the code to make
things work. In the Raspbian OS, Python is a first-class citizen, and the support for Python comes right
out of the box. With its clean syntax and ease of learning, Python is a first choice for hobbyists and
beginners to foray into the world of the Raspberry Pi. Coupled with the huge community support for
Python, it's no wonder that it's the language of choice for developers.
$ nano motiondetection.py
The above command uses the NANO text editor and creates a file named motiondetection.py . Enter the
import time #2
pirsensor = 4 #3
GPIO.setmode(GPIO.BCM) #4
previous_state = False #6
current_state = False
71 | P a g e
3161716: Industry 4.0
Vishwakarma Government Engineering College
Department of Instrumentation and Control Engineering
while True: #7
time.sleep(0.1) #8
previous_state = current_state #9
if current_state: #12
When you are finished typing in the code, exit the NANO editor by pressing Ctrl-X and then
pressing Y to save the file. Press Enter to save it to the current directory. To run the Python script, type
the following command in Terminal:
$ python motiondetection.py
Wave your hand in front of the PIR Motion Sensor. We should see the following output on Terminal:
Motion Detected!
PIR sensor is connected to the GPIO pin. In this example, it's GPIO pin #4.
#4: There are two ways to refer to the pins on the GPIO: either by physical pin numbers (starting from
72 | P a g e
3161716: Industry 4.0
Vishwakarma Government Engineering College
Department of Instrumentation and Control Engineering
pin 1 to 40 on the Raspberry Pi 2/3), or Broadcom GPIO numbers (BCM). Using BCM is very useful
with a ribbon cable (such as the Adafruit T-Cobbler Plus) to connect the Raspberry Pi to the
breadboard. The BCM numbers refer to the labels printed on the T-Cobbler Plus (see Figure 9). For
this example, we're using the BCM numbering scheme. That means that when we say we're getting the
input from pin 4, we're referring to the pin printed as #4 on the T-Cobbler Plus.
#5: Initialize the pin represented by the variable pinsensor as an input pin. Also, we use a pull-down
#11: Compare the previous state and the current state to see if the motion sensor has a change in state.
If there's a change, it means that either the sensor has just detected motion (when the state changes
from false to true ), or that the sensor is resetting itself (when the state changes from true to false ) a
Fig-10: How Google Push Notification works using Google Cloud Messaging (GCM)
Conclusion:
Assignments:
1. Brief the need of Pull up and Pull down resistors with Raspberry Pi GPIO Pins.
74 | P a g e
3161716: Industry 4.0