CS3691 Embedded Systems and
Internet of Things
1
UNIT 4
4.1 IoT Communication Models and APIs
4.2 IoT Communication Protocols – Bluetooth – WiFi –
4.3 ZigBee – GPS – GSM modules
4.4 Open Platform (like Raspberry Pi) – Architecture
4.5 Open Platform : Programming – Interfacing - Accessing
GPIO Pins
4.6 Sending and Receiving Signals UsingGPIO Pins
4.7 Connecting to the Cloud.
2
RPI Programming with Python
Program for Blinking of LED
import RPi.GPIO as GPIO
import time
LED = 7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED, GPIO.OUT)
while (True):
GPIO.output(LED, True)
time.sleep(1)
GPIO.output(LED, False)
time.sleep(1)
3
RPI Programming with Python
Program for Blinking of LED with a conditional check
import RPi.GPIO as GPIO
import time
LED = 7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED, GPIO.OUT)
i=0
while (True):
print("Value of i is now ", i)
GPIO.output(LED, True)
time.sleep(1)
GPIO.output(LED, False)
time.sleep(1)
i += 1
if i > 10:
break
4
Program for Fading of LED
import time
import RPi.GPIO as GPIO
led_pin = 12
GPIO.setmode(GPIO.BCM)
GPIO.setup(led_pin, GPIO.OUT)
# Initialize pwm object with 50 Hz and 0% duty cycle
pwm = GPIO.PWM(led_pin, 50)
pwm.start(0)
5
Program for Fading of LED Contd…
# Set PWM duty cycle to 50%, wait, then to 90%
pwm.ChangeDutyCycle(50)
time.sleep(2)
pwm.ChangeDutyCycle(90)
time.sleep(2)
# Stop, cleanup, and exit
pwm.stop()
GPIO.cleanup()
6
RPI Programming with Python
Interfacing DHT11 Sensor with RPI
import RPi.GPIO as GPIO
import Adafruit_DHT
sensor = Adafruit_DHT.DHT11
LED=40
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED,GPIO.OUT) #Red LED
GPIO.output(LED,GPIO.LOW)
humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)
7
RPI Programming with Python
Interfacing DHT11 Sensor with RPI
if humidity is not None and temperature is not None:
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature,
humidity))
else:
print('Failed to get reading. Try again!')
if temperature > 30:
GPIO.output(LED, GPIO.HIGH)
else:
GPIO.output(LED, GPIO.LOW)
GPIO.cleanup()
8
Raspberry Pi Interfaces
RPi Serial Communications Methods via GPIO*
Name Description Function
• Half duplex, serial data transmission used
I2 C Inter-Integrated Circuit for short-distance between boards,
modules and peripherals
• Uses 2 pins
Serial Peripheral Interface • Full-duplex, serial data transmission used
SPI for short-distance between devices
bus
• Uses 4 pins
Universal • Asynchronous, serial data
UART transmission between devices
Asynchronous
Receiver-Transmitter • Uses 2 pins
9
Raspberry Pi Interfaces
I2C (Inter-Integrated Circuit)
• I2C is bidirectional, synchronous, serial communications interface
• It operates on two lines in a half-duplex mode
• It was originally created by Philips Semiconductor which later became NXP
Semiconductors
SDL : Serial Data Line
I2C: SCL : Serial Clock Line
10
Raspberry Pi Interfaces
I2C (Inter-Integrated Circuit)
• A single master (the RPi) can communicate with one or more slave
devices
• Each connected device is selected via a 7 bit address (10 or more bit
addressing is possible, but more complex)
• Originally limited to 100 kbits per second, it now supports faster
transmission rates
• The Pi can support 100 kbits standard mode as well as 400 kbits "fast
mode", with some reports citing higher speeds depending on cable length,
board model and software processing
• With its 7 bit addressing, I2C can support up to 127 devices (or nodes)
11
Raspberry Pi Interfaces
I2C (Inter-Integrated Circuit)
The two lines are called SCL and SDA
SCL is the clock line for synchronizing transmission
SDA is the data line through which bits of data are sent or received
During transmission, the first byte includes the 7 bit address plus a
read / write bit. Subsequent bits represent the actual data
I2C connection to the RPi is made using GPIO board pins 3 for SDA
and 5 for SCL (BCM mode GPIO 2 and GPIO 3)
12
Raspberry Pi Interfaces
I2C (Inter-Integrated Circuit)
• The RPi GPIO operates at 3.3v so care MUST be
taken to ensure connections to slave devices are also 3.3v
• A voltage level converter can be used if
necessary to interface to other devices, i.e. 5v
Arduino to RPi 3.3v
• I2C wiring distance is considered relatively short, typically from inches
to a few meters
• Distance is affected by data speed, wire quality and external noise
13
Raspberry Pi Interfaces
SPI (Serial Peripheral Interface)
• SPI is a bidirectional, synchronous, serial communications interface - like
I2C
• Also like I2C, it is used for relatively short distances
• Unlike I2C, however, SPI operates at full duplex, meaning data can be sent
and received simultaneously
SPI:
14
Raspberry Pi Interfaces
SPI (Serial Peripheral Interface)
• Additionally, it can operate at faster data transmission
rates, with rates upwards of 8 Mbits or more possible on the
RPi
• SPI can communicate with multiple devices through two ways
First Way
○ The first is by selecting each device with a Chip Select line
○ A separate Chip Select line is required for each device
○ This is the most common way RPi's currently use SPI
Second Way
○ The second is through daisy chaining where each device is connected
to the other through its data out to the data in line of the next
○ There is no defined limit to the number of SPI devices that can be
connected
15
Raspberry Pi Interfaces
SPI (Serial Peripheral Interface)
• However, practical limits exist due to constraints by the number of hardware
select lines available on the master in the first method, or the complexity of
passing data through devices in the second daisy chain method
• The RPi has two Chip Select (CE0 & CE1) lines readily available
• More can be defined by configuring other GPIO pins and through software
programming
16
Raspberry Pi Interfaces
UART (Universal Asynchronous Receiver / Transmitter)
• UART is a hardware implementation that
supports bidirectional, asynchronous, serial
communications
• It requires two data lines - one for transmit and one for receive
• The transmit line of one device is connected to the receive line of the second
device, and vice versa for transmission in both directions
UART
:
17
Raspberry Pi Interfaces
UART (Universal Asynchronous Receiver / Transmitter)
• A UART can only connect between two devices
• It can operate between devices in:
1) Simplex - data transmission in one direction only;
2) Half Duplex - data transmission in either
direction, but not simultaneously; or,
3) Full Duplex - data transmission in both directions simultaneously
• NOTE: RPi has two UARTs - A fully functional UART and a second one
called a "mini" UART with less capability
• Prior to RPi 3, the full UART was available on GPIO pins 8 and 10
18
Raspberry Pi Interfaces
UART (Universal Asynchronous Receiver / Transmitter)
• However, to support Bluetooth on the RPi 3, the full UART was moved from
the GPIO to the Bluetooth chip and the mini UART was made available on
GPIO pins 8 and 10
• It is possible to redirect the full UART to the GPIO, but requires configuration
changes
• Either UART uses GPIO pin 8 (BCM GPIO 14) for transmit and pin 10
(BCM GPIO 15) for receive
• UART data transmission speed is referred to as BAUD Rate and is set
to 115,200 by default (BAUD rate is based on symbol transmission rate,
but is similar to bit rate) 19
Raspberry Pi Interfaces
UART (Universal Asynchronous Receiver / Transmitter)
• Rates approaching 1 Mbps have been successful with the RPi
• As with any interface to the RPi GPIO, voltage must be considered
• RPi pins operate at 3.3v while other devices may operate at 5v or even
higher
• Voltage level converters are required to interface the RPi to such devices
20
Raspberry Pi Interfaces
Comparison of I2C, SPI and UART
Speed
• UART is the slowest, with I2C faster and SPI as the fastest
• If speed is not important, then any are good
• Otherwise, align device speed requirement with solution
Number of devices required
• UART - only two (RPi + 1 device); SPI - many, but beyond two devices (+ Pi)
gets more complicated with the Pi due to required CS line for each device;
I2C - up to 127 without undue complexity
Duplex
• UART and SPI can be Full Duplex
• I2C is only Half Duplex
21
Raspberry Pi Interfaces
Comparison of I2C, SPI and UART
Number of wires
• UART uses 1 (one-way only) or 2 (bidirectional)
• I2C use 2 wires and SPI uses 4 (Plus a ground wire for all)
Distance
• None of these are long distance solutions - a few inches to a few meters
depending on transmission speed, cable quality and external noise
• Faster speeds will go shorter distances
Transmission confirmation
• I2C is the only protocol among these three that ensures that the data sent
was received by device
• This can lead to a "locked" up state by one device under adverse conditions
17
Raspberry Pi Interfaces
Comparison of I2C, SPI and UART
UART
• Simple and Limited to one device connected to the Pi
• Not high speed and No clock needed
I2C
• Faster than UART, but not as fast as SPI
• Easier to chain many devices
• RPi drives the clock so no sync issues
SPI
• Fastest of the three
• Pi drives the clock so no sync issues
• Practical limit to number of devices on the Pi
23
Raspberry Pi Interfaces
Summary
• RPi Serial Communications Methods via GPIO*
• I2C (Inter-Integrated Circuit)
• SPI (Serial Peripheral Interface)
• UART (Universal Asynchronous Receiver /
Transmitter)
• Comparison of I2C, SPI and UART
24
Raspberry Pi Interfaces
Try Yourself
• Which is faster I2C or UART?
• Is UART full duplex?
• Determine the maximum speed of UART.
• How baud rate is calculated?
• Does SPI need pullup resistors?
25