r22 Iot Unit 3 Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 63

Popular Commands used in Raspberry PI

Introduction to raspberry pi
What is Raspberry Pi?
The Raspberry Pi is a device: which is fully functional computer in a tiny and low-cost package.
The Raspberry Pi is known as a single-board computer, which means exactly what it sounds
like: it’s a computer, just like a desktop, laptop, or smartphone, but built on a single printed
circuit board. Like most single-board computers, the Raspberry Pi is small – roughly the same
footprint as a credit card – but that doesn’t mean it’s not powerful: a Raspberry Pi can do
anything a bigger and more power-hungry computer can do, though not necessarily as quickly.
in simply Raspberry PI is a:
• Computer in your palm.
• Single-board computer.
• Low cost.
• Easy to access.

Fig 1: Raspberry PI

Specifications:

The Raspberry Pi’s ports


The Raspberry Pi has a range of ports, starting with four Universal Serial Bus (USB) ports
(Figure 1) to the middle and right-hand side of the bottom edge. These ports let you connect
any USB-compatible peripheral, from keyboards and mouse to digital cameras and flash drives,
to the Pi. Speaking technically, these are known as USB 2.0 & 3.0 ports, which means they are
based on version two and three of the Universal Serial Bus standards.
To the left of the USB ports is an Ethernet port, also known as a network port (Figure 1). You
can use this port to connect the Raspberry Pi to a wired computer network using a cable with
what is known as an RJ45 connector on its end. If you look closely at the Ethernet port, you’ll
see two light-emitting diodes (LEDs) at the bottom; these are status LEDs, and let you know
that the connection is working. Just above the Ethernet port, on the left-hand edge of the
Raspberry Pi, is a 3.5 mm audio-visual (AV) jack (Figure 1). This is also known as the
headphone jack, and it can be used for that exact purpose – though you’ll get better sound
connecting it to amplified speakers rather than headphones. It has a hidden, extra feature,
though: as well as audio, the 3.5 mm AV jack carries a video signal which can be connected to
TVs, projectors, and other displays.
High-Definition Multimedia Interface (HDMI) port:
It is the same type of connector you’ll find on a games console, set-top box, and TV. The
multimedia part of its name tells you that it carries both audio and video signals, while high-
definition tells you that you can expect excellent quality. You’ll use this to connect the
Raspberry Pi to your display device, whether that’s a computer monitor, TV, or projector.
Micro USB power port:
which you’ll use to connect the Raspberry Pi to a power source. The micro USB port is a
common sight on smartphones, tablets, and other portable devices. So, you could use a standard
mobile charger to power the Pi, but for best results you should use the official Raspberry Pi
USB Power Supply.
Raspberry Pi’s peripherals:
To work, the Raspberry Pi needs peripherals: at the minimum, you’ll need a microSD card for
storage; a monitor or TV so you can see what you’re doing; a keyboard and mouse to tell the
Pi what to do; and a 5 volt (5 V) micro USB power supply rated at 2.5 amps (2.5 A) or better.
With those, you’ve got yourself a fully functional computer.
USB power supply:
A power supply rated at 2.5 amps (2.5A) or 12.5 watts (12.5W) and with a micro USB
connector. The Official Raspberry Pi Power Supply is the recommended choice, as it can cope
with the quickly switching power demands of the Raspberry Pi.

The GPIO pins on the Raspberry Pi are divided into the following groups:
Power: Pins that are labelled 5.0v supply 5 volts of power and those labelled 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 labelled with the # sign, for example, #17, #27, #22,
etc. These pins can be used for input or output.
UART: The Universal Asynchronous Receiver/Transmitter allows your Raspberry Pi to be
connected to serial peripherals. The UART pins are labelled TXD and RXD.
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 labelled MOSI, MISO, SCLK, CE0, and CE1.
• MISO (Master In Slave Out): Master line for sending data to the master.
• MOSI ( Master Out Slave In): Slave line for sending data to the Peripherals.
• SCK ( Serial Clock): Clock generated by master to synchronize data transmission.
• CE0 ( chip Enable 0): to enable or disable devices.
• CE1 ( chip Enable 1): to enable or disable devices.
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.SPI (Serial
peripheral interface)
I2C (Inter Integrated chip)
The I2C interface pins on raspberry pi allow you to connect hardware modules. I2C interface
allow synchronous data transfer with two pins -SDA (data line) and SCL (clock line).
Serial
The serial interface on Raspberry Pi has receive (Rx) and Transmit (Tx) pins for
communication with serial peripherals.

Basic Architecture
Raspberry Pi GPIO
• Act as both digital output and digital input.
• Output: turn a GPIO pin high or low.
• Input: detect a GPIO pin high or low.

Raspberry Pi pin configuration

Basic Set up for Raspberry Pi


• HDMI cable.
• Monitor.
• Key board.
• Mouse.
• 5volt power adapter for raspberry pi.
• LAN cable
• Min-2GBmicro sd card

Operating System
Official Supported OS:
• Raspbian
• NOOBS
Some of the third-party OS:
• UBUNTU mate
• Snappy Ubuntu core
• Windows 10 core
• Pinet
• Risc OS

Raspberry Pi Setup
Download Raspbian:
• Download latest Raspbian-image from raspberry pi official site:
https://www.raspberrypi.org/downloads/
• Unzip the file and end up with an .imgfile

Raspberry Pi OS Setup
Write Raspbian in SD card:
• Install “Win32 Disk Imager” software in windows machine.
• Run Win32 Disk Imager
• Plug SD card into your PC
• Select the “Device”
• Browse the “Image File” (Raspbian image)
• Write

Features of 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".

Popular Applications
• Media streamer
• Home automation
• Controlling BOT
• VPN
• Light weight web server for IOT
• Tablet computer

Interfacing Raspberry Pi with basic peripherals:


In this section you will learn how to get started with developing python programs on Raspberry
Pi. Raspberry Pi runs Linux and supports Python out of box. Therefore, you can run any python
program which runs on normal computer. However, it is the general-purpose input/output pins
(GPIO) on Raspberry Pi that makes it useful for IoT. We can interface Raspberry Pi with
sensors and actuators with GPIO pins and SPI, I2C and serial interfaces.

1. Controlling LED with Raspberry Pi


Let us start with basic example of controlling an LED from Raspberry Pi. In this example the
LED is connected to GPIO pin 18. We can connect LED to other GPIO pin as well.
The program uses the RPi.GPIO module to control the GPIO on Raspberry Pi. In this program
we set pin 18 direction to output and then True/False alternatively after a delay of one second.
To begin, we import the GPIO package that we will need so that we can communicate with the
GPIO pins. We also import the time package, so we’re able to put the script to sleep for when
we need to. We then set the GPIO mode to GPIO.BOARD/GPIO.BCM, and this means all the
numbering we use in this script will refer to the physical numbering of the pins.

Program:
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
from time import sleep # Import the sleep function from the time module
GPIO.setmode(GPIO.BCM/BOARD) # Use physical pin numbering
GPIO.setup(18, GPIO.OUT) # Set pin 18 to be an output pin
while True: # Run forever
GPIO.output(18, GPIO.HIGH) # Turn on
sleep(1) # Sleep for 1 second
GPIO.output(18, GPIO.LOW) # Turn off
sleep(1) # Sleep for 1 second

2. Interfacing an LED and Switch with Raspberry PI


Program:
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(25, GPIO.IN) # Switch PIN connected to 25
GPIO.setup(18, GPIO.OUT) # LED PIN connected to 18
state = false
def toggleLED(pin):
state = not state
GPIO.output(pin, state)
while True:
try:
if(GPIO.input(25) == True):
toggleLED(pin)
sleep(.01)
except keyboardInterrupt:
exit()

3.Interfacing Light Dependent Resistor (LDR) in Raspberry Pi:


4. Capture Image using PiCam with Raspberry Pi:
Requirement
• Raspberry Pi
• Raspberry Pi Camera
Raspberry Pi Camera
• Raspberry Pi specific camera module
• Dedicated CSI slot in Pi for connection
• The cable slot is placed between Ethernet port and HDMI port
Connection
• Boot the Pi once the camera is connected to Pi
Configuring Pi for Camera
• In the terminal run the command “sudo raspi-config” and press enter.
• Navigate to “Interfacing Options” option and press enter.
• Navigate to “Camera” option.
• Enable the camera.
• Reboot Raspberry pi.
Capture Image
Open terminal and enter the command
raspistill -o image.jpg
This will store the image as ‘image.jpg’
PiCam can also be processed using Python camera module python-picamera
sudo apt-get install python-picamera
Python Code:
Import picamera
camera = picamera.PiCamera()
camera.capture('image.jpg')

Implementation of IoT with Raspberry Pi:


Internet of Things
• Creating an interactive environment
• Network of devices connected together
• The Raspberry Pi is a popular choice when developing IoT products.
• It offers a complete Linux server with a tiny platform at an incredibly low price.
• Actually, the Raspberry Pi is so well-known to IoT that the company has featured
several Internet of Things projects on their site.
• Here you will find projects and community support for a range of IoT activities.
• Take for example, the World’s First Cloud Texting Enabled Espresso Machine –
powered by Raspberry Pi Partnered with the Zipwhip cloud texting application
• the Raspberry Pi connects to an espresso machine and allows people to text a message
to it, that automatically turns it on and starts brewing beverages.
• Raspberry Pi can be plugged into a TV, computer monitor, and it uses a standard
keyboard and mouse. It is user-friendly as it can be handled by all the age groups.
• It does everything you would expect a desktop computer to do like word-processing,
browsing the internet spreadsheets, playing games to playing high definition videos.
• It is used in many applications like in a wide array of digital maker projects, music
machines, parent detectors to the weather station and tweeting birdhouses with infrared
cameras.
• All models feature on a Broadcom system on a chip (SOC), which includes chip
graphics processing unit GPU (a Video Core IV), an ARM-compatible and CPU.
• The CPU speed ranges from 700 MHz to 1.2 GHz for the Pi 3 and onboard memory
range from 256 MB to 1 GB RAM.
• An operating system is stored in the secured digital SD cards and program memory in
either the Micro SDHC or SDHC sizes.
• Most boards have one to four USB slots, composite video output, HDMI and a 3.5 mm
phone jack for audio. Some models have WiFi and Bluetooth
Sensor
• Electronic element
• Converts physical quantity into electrical signals
• Can be analog or digital
Actuator
• Mechanical/Electro-mechanical device
• Converts energy into motion
• Mainly used to provide controlled motion to other components
Python program for Temperature Dependent Auto Cooling System using Raspberry PI:
System Overview
• Sensor and actuator interfaced with Raspberry Pi
• Read data from the sensor
• Control the actuator according to the reading from the sensor
• Connect the actuator to a device
Requirements
• DHT Sensor
• 4.7K ohm resistor
• Relay
• Jumper wires
• Raspberry Pi
• Mini fan
DHT Sensor (Digital Humidity and Temperature Sensor) Pins:
• PIN 1, 2, 3, 4 (from left to right)
• PIN 1-3.3V-5V Power supply
• PIN 2-Data
• PIN 3-Null
• PIN 4-Ground
Relay
Mechanical/electromechanical switch
• 3 output terminals (left to right)
• NO (normal open):
• Common
• NC (normal close)
Sensor interface with Raspberry Pi
• Connect pin 1 of DHT sensor to the 3.3V pin of Raspberry Pi
• Connect pin 2 of DHT sensor to any input pins of Raspberry Pi, here we have used pin
11
• Connect pin 4 of DHT sensor to the ground pin of the Raspberry Pi
Relay interface with Raspberry Pi
• Connect the VCC pin of relay to the 5V supply pin of Raspberry Pi
• Connect the GND (ground) pin of relay to the ground pin of Raspberry Pi
• Connect the input/signal pin of Relay to the assigned output pin of Raspberry Pi (Here
we have used pin 7)
Adafruit provides a library to work with the DHT22 sensor
Install the library in your Pi-
• Get the clone from GIT
git clone https://github.com/adafruit/Adafruit_Python_DHT.g...
• Go to folder Adafruit_Python_DHT
cd Adafruit_Python_DHT
• Install the library
sudo python setup.py install

Program: DHT22 with Pi


import RPi.GPIO as GPIO
from time import sleep
import Adafruit_DHT #importing the Adafruit library
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
sensor = Adafruit_DHT.AM2302 # create an instance of the sensor type
print (‘Getting data from the sensor’)
#humidity and temperature are 2 variables that store the values received from the sensor
humidity, temperature = Adafruit_DHT.read_retry(sensor,17)
print ('Temp={0:0.1f}*C humidity={1:0.1f}%'.format(temperature, humidity))

Connection and Program for Relay


Connect the relay pins with the Raspberry Pi as mentioned in previous
Set the GPIO pin connected with the relay’s input pin as output in the sketch
GPIO.setup(13,GPIO.OUT)
Set the relay pin high when the temperature is greater than 30
if temperature > 30:
GPIO.output(13,0) # Relay is active low
print(‘Relay is on’)
sleep(5)
GPIO.output(13,1) # Relay is turned off after delay of 5 seconds

Connection: Fan
Connect the Li-po battery in series with the fan
NO terminal of the relay -> positive terminal of the Fan.
Common terminal of the relay -> Positive terminal of the battery
Negative terminal of the battery -> Negative terminal of the fan.
Run the existing code. The fan should operate when the surrounding temperature is greater than
the threshold value in the sketch

Result
The fan is switched on whenever the temperature is above the threshold value set in the code.
Notice the relay indicator turned on.

You might also like