Raspberry Pi
Raspberry Pi
Raspberry Pi
• 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
• 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
• 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
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)
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
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 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
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
• pcDuino
• BeagleBone Black
• Cubieboard