Raspberry Pi

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

Chapter 7

IoT Physical Devices & Endpoints


Outline

• Basic building blocks of an IoT Device


• Exemplary Device: Raspberry Pi
• Raspberry Pi interfaces
• Programming Raspberry Pi with Python
• Other IoT devices
What is an IoT Device

• A "Thing" in Internet of Things (IoT) can be any object that has


a unique identifier and which can send/receive data (including
user data) over a network (e.g., smart phone, smart TV,
computer, refrigerator, car, etc. ).

• IoT devices are connected to the Internet and send information


about themselves or about their surroundings (e.g. information
sensed by the connected sensors) over a network (to other devices or
servers/storage) or allow actuation upon the physical
entities/environment around them remotely.

Book website: http://www.internet-of-things-book.com Bahga & Madisetti, © 2015


IoT Device Examples

• A home automation device that allows remotely monitoring


the status of appliances and controlling the appliances.
• An industrial machine which sends information about its operation
and health monitoring data to a server.
• A car which sends information about its location to a cloud-based
service.
• A wireless-enabled wearable device that measures data about a
person such as the number of steps walked and sends the data to a
cloud-based service.
Basic building blocks of an IoT Device

• Sensing
• Sensors can be either on-board the IoT device or attached to the device.
• Actuation
• IoT devices can have various types of actuators attached that allow taking
actions upon the physical entities in the vicinity of the device.
• Communication
• Communication modules are responsible for sending collected data to
other devices or cloud-based servers/storage and receiving data from other
devices and commands from remote applications.
• Analysis & Processing
• Analysis and processing modules are responsible for making sense
of the collected data.
Book website: http://www.internet-of-things-book.com Bahga & Madisetti, © 2015
Block diagram of an IoT Device

Book website: http://www.internet-of-things-book.com Bahga & Madisetti, © 2015


Exemplary Device: Raspberry Pi

• Raspberry Pi is a low-cost mini-computer with the physical size of a


credit card.
• Raspberry Pi runs various flavors of Linux and can perform almost all
tasks that a normal desktop computer can do.
• Raspberry Pi also allows interfacing sensors and actuators through
the general purpose I/O pins.
• Since Raspberry Pi runs Linux operating system, it supports Python
"out of the box".

Book website: http://www.internet-of-things-book.com Bahga & Madisetti, © 2015


Raspberry Pi
Linux on Raspberry Pi

• Raspbian
• Raspbian Linux is a Debian Wheezy port optimized for Raspberry Pi.
• Arch
• Arch is an Arch Linux port for AMD devices.
• Pidora
• Pidora Linux is a Fedora Linux optimized for Raspberry Pi.
• RaspBMC
• RaspBMC is an XBMC media-center distribution for Raspberry Pi.
• OpenELEC
• OpenELEC is a fast and user-friendly XBMC media-center distribution.
• RISC OS
• RISC OS is a very fast and compact operating system.
Raspberry Pi GPIO

Bahga & Madisetti, © 2015


Raspberry Pi Interfaces

• Serial
• The serial interface on Raspberry Pi has receive (Rx) and transmit (Tx) pins
for communication with serial peripherals.
• SPI
• Serial Peripheral Interface (SPI) is a synchronous serial data protocol used
for communicating with one or more peripheral devices.
• I2C
• The I2C interface pins on Raspberry Pi allow you to connect hardware
modules. I2C interface allows synchronous data transfer with just two pins -
SDA (data line) and SCL (clock line).
UART, or Universal Asynchronous Receiver/ Transmitter

• It is used to implement serial communication between devices in an


embedded system.
• In UART communication, two UARTs communicate directly with each
other;
• UART on the sender device receives parallel data from the CPU (MP or
MC) and converts it to serial data.
• This serial data is transmitted to the UART on the receiver device, The
receiving UART converts the received serial data back to parallel data
and sends it to the CPU.
• In order for UART to convert serial-to-parallel and parallel-to-serial
data, shift registers on the transmitting and receiving UART are used.
• In UART communication, only two wires are required for
communication: data flows from the Tx pin of the transmitting UART
(Transmitter Tx) to the Rx pin of the receiving UART (Receiver Rx).
SPI, or Serial Peripheral Interface
• It is a serial communication protocol often used in
embedded systems for high-speed data exchanges between
devices on the bus.
• It operates using a master-slave paradigm that includes at
least four signals: a clock (SCLK), a master output/slave
input (MOSI), a master input/slave output (MISO), and a
slave select (SS) signal.
• The SCLK, MOSI, and MISO signals are shared by all devices
on the bus. The SCLK signal is generated by the master
device for synchronization, while the MOSI and MISO lines
used for data exchange.
• Additionally, each slave device added to the bus has its own
SS line. The master pulls low on a slave's SS line to select a
device for communication.
I2C, or Inter-Integrated Circuit
• I2C, or Inter-Integrated Circuit, is a simple communication
protocol often used in embedded systems as a way to transfer
data between a master (or multiple masters) and a single slave
(or multiple slaves) device.
• It is a bidirectional two-wire serial bus that uses serial clock
(SCL) and serial data (SDA) wires to send and manage data bit
by bit between devices connected to the bus.
• In I2C operations, the master controls the exchange of data
between the devices.
• A master device will signal to a slave in order to send data or
request a response. To accomplish this, all slave devices must
have a unique address that is included in the I2C message.
• I2C uses a two-wire interface where slave devices share the data and clock lines. Because of this,
adding multiple devices to the bus is simple and reduces complexity of the circuit.
• I2C also includes flow control and error handling, making it a more reliable protocol.
• I2C can support multi-masters in a configuration, while SPI can only support one master.
• I2C is often a good choice for connecting short-distanced, low-speed devices like microcontrollers,
EEPROMs, I/O interface, and other peripheral devices like sensors in an embedded system.
• SPI is superior in speed compared to I2C. allowing for even quicker data exchanges.
• While SPI has a speed advantage, it is more difficult and costlier to add multiple slave devices to
the bus. This is because each slave needs its own slave select line, so the number of wires needed
to communicate increases with each device.
• SPI is often used for connecting short-distanced devices within an embedded system, but it is also
ideal for memory applications. For instance, many memory devices like SD cards, multi-media
cards, EEPROMs, and Flash memory use SPI to store data that can easily be erased/rewritten as
needed.
• UART is often used as a form of device-to-device communication in computer and microcontroller
applications.
• Two different numbering systems you can use
with RPi.GPIO.
• The main GPIO header (P1) of the Raspberry
Pi contains 26 pins.
• In RPi.GPIO we can use either pin numbers
(BOARD) or the Broadcom GPIO numbers
(BCM)
 for GPIO numbering, choose BCM  
GPIO.setmode(GPIO.BCM)  

 for pin numbering, choose BOARD  
GPIO.setmode(GPIO.BOARD) 
Set up a GPIO port as an input:
Syntax: GPIO.setup(Port_or_pin, GPIO.IN)
Ex: GPIO.setup(25, GPIO.IN)

Reading inputs
Syntax: GPIO.input(Port_or _Pin)

Set up a GPIO port as an output:


Syntax: GPIO.setup(Port_or_pin, GPIO.OUT)
Ex: GPIO.setup(25, GPIO.OUT)

Giving Outputs:
Syntax: GPIO.output(Port_or _Pin, value)
Raspberry Pi Example:
Interfacing LED with Raspberry Pi
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
#LED Pin
GPIO.setup(18, GPIO.OUT)

while True:
GPIO.output(18,True)
time.sleep(1)
GPIO.output(18,False)
time.sleep(1)
Bahga & Madisetti, © 2015
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
#LED Pin
GPIO.setup(12, GPIO.OUT)

while True:
GPIO.output(12,True)
time.sleep(1)
GPIO.output(12,False)
time.sleep(1)
PWM

• it is a technique used in controlling the brightness of LED, speed


control of DC motor, controlling a servo motor or where you have to
get analog output with digital means.
• The Raspberry pi GPIO pins either gives us 3.3V (when turned HIGH)
or 0V (when turned LOW) and the output is a square wave signal.
• So if we want to dim a LED, we cannot get the voltage between 0 and
3.3V from the GPIO pin but we can change the ON and OFF time of
the signal.
• If we will change the ON and OFF time fast enough then the
brightness of the led will be changed.
PWM

Before going further, let’s discuss some terms associated with PWM.
TON (On Time): It is the time when the signal is high.
TOFF (Off Time): It is the time when the signal is low.
Period: It is the sum of on time and off time.
Duty Cycle: It is the percentage of time when the signal was high during
the time of period.
Using PWM in RPi.GPIO
To create a PWM instance:
p = GPIO.PWM(channel, frequency)
Frequency, in Hertz (Hz) is the number of times per second that a pulse is generated.
To start PWM:
p.start(dc) # where dc is the duty cycle (0.0 <= dc <= 100.0)
Ex: Frequency 50 Hz, duty cycle 50%.
This gives a pulse 50 times per second (or every 0.02 seconds ). During each 0.02
second time period, the port will be “High” (3.3V) half the time and “Low” (0V) the
other half.
To change the frequency:
p.ChangeFrequency(freq) # where freq is the new frequency in Hz

To change the duty cycle:


p.ChangeDutyCycle(dc) # where 0.0 <= dc <= 100.0

To stop PWM:
p.stop()
An example to brighten/dim an LED:

import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
p = GPIO.PWM(18, 50) # channel=18 , frequency=50Hz
p.start(0)
try:
while 1:
for dc in range(0, 101, 5):
p.ChangeDutyCycle(dc)
time.sleep(0.1)
for dc in range(100, -1, -5):
p.ChangeDutyCycle(dc)
time.sleep(0.1)
# If Keyboard Interrupt (CTRL+C) is pressed
except KeyboardInterrupt:
p.stop()
Raspberry Pi Example:
Interfacing LED and switch with Raspberry Pi
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
#Switch Pin
GPIO.setup(25, GPIO.IN)
#LED Pin
GPIO.setup(18, GPIO.OUT)
state=false

def toggleLED(pin):
state = not state
GPIO.output(pin, state)

while True:
try:
if (GPIO.input(25) == True):
toggleLED(18)
sleep(.01)

except KeyboardInterrupt:

exit()
Interfacing a LDR with Raspberry Pi

When light hits the LDR, its resistance is


very low, but when it's in the dark its
resistance is very high.
By placing a capacitor in series with an
LDR, the capacitor will charge at different
speeds depending on whether it's light or
dark.
Interfacing a LDR with Raspberry Pi
import RPi.GPIO as GPIO
import time
The readLDR() function returns a count which is proportional to the light level.
GPIO.setmode(GPIO.BCM) In this function the LDR pin is set to output and low and then to input.
ldr_threshold = 1000 At this point the capacitor starts charging through the resistor
LDR_PIN = 25 (and a counter is started) until the input pin reads high
LIGHT_PIN = 18 (this happens when capacitor voltage becomes greater than 1.4V). 
The counter is stopped when the input reads high.
def readLDR(PIN): The final count is proportional to the light level as greater the amount of light,
reading = 0 smaller is the LDR resistance and greater is the time taken to charge the capacitor.
GPIO.setup(PIN, GPIO.OUT)
GPIO.output(PIN, false)
time.sleep(0.1)
GPIO.setup(PIN, GPIO.IN)
while (GPIO.input (PIN) ==False):
reading=reading+1

return reading
Interfacing a LDR with Raspberry Pi
def switchOnLight(PIN):
GPIO.setup(PIN, GPIO.OUT)
GPIO.output(PIN, True)

def switchOffLight(PIN):
GPIO.setup(PIN, GPIO.OUT)
GPIO.output(PIN, False)
while True:
ldr_reading = readLDR(LDR_PIN)
if ldr_reading < ldr_threshold:
switchOnLight (LIGHT_PIN)
else:
switchOffLight(LIGHT_PIN)
time.sleep(1)
Interfacing Ultrasonic Sensor With Raspberry
PI
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
#note the start of the pulse at echo pin
triggerPin = 23 while GPIO.input(echoPin)==0:
echoPin = 24 pulseStart = time.time()
GPIO.setup(triggerPin,GPIO.OUT)
GPIO.setup(echoPin,GPIO.IN) #note the end of the pulse at echo pin
while GPIO.input(echoPin)==1:
#make this infinite loop pulseEnd = time.time()
while True:
GPIO.output(triggerPin, False) pulseDuration = pulseEnd – pulseStart
time.sleep(2) distance = 34300 * pulseDuration / 2

#generate 10us pulse #round off to 2 decimal places


GPIO.output(triggerPin, True) distance = round(distance, 2)
time.sleep(0.00001)
GPIO.output(triggerPin, False) print("Distance: %s cm") %distance
DHT11 Interfacing with Raspberry Pi
import Adafruit_DHT
sensor = Adafruit_DHT.DHT11

# Example using a Raspberry Pi with DHT sensor connected to GPIO4.


pin = 4
while True:
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
print(“Temp=“temperature)
print (“Humidity=“,humidity)
Other Devices

• pcDuino
• BeagleBone Black
• Cubieboard

Book website: http://www.internet-of-things-book.com Bahga & Madisetti, © 2015

You might also like