Raspberry Pi Int
Raspberry Pi Int
Raspberry Pi Int
and
Peripheral
Interfacing
Modern College
What is a Raspberry
Pi?
University of Cambridge‟s Computer Laboratory
Survey Result: Decline in skill level
Designed for education
A credit card sized Personal Computer (PC)
Low Cost - ~ $35 each
Capability:
Programming
Electronic Projects
Office
Play HD Videos
Modern College
Raspberry Pi Versions
Latest Versions
Raspberry Pi Model A+
Raspberry Pi Zero W
Raspberry Pi 4
Modern College
Basic
Architecture
Modern College
Raspberry Pi 3 Model
B/B+
Modern College
Rpi Model B -
Specifications
Processor : Broadcom BCM2837
Version : 64bit , ARMv8, Quad Core
Cortex A53
Speed : 1.2 GHz
Memory : 1 GB
Communications: WiFi, Bluetooth, Ethernet
Media : HDMI Port, Audio Jack
Input Rating : 5V, 2.5A
Modern College
Modern College
OS
Installation
Download the Required Software & Files
Raspbian OS,
7zip, SD Card Formatter,
Win32 disk imager
Unzip the file and install utilities
Format the SD Card
Write the OS disk image on the SD Card
Put the microSD card in your Pi and boot up
Finishing OS Setup.
Configuring Raspberry Pi using raspi-config
Modern College
SD Card
Formatter
Modern College
Win32 Disk
Imager
Browse the .img file of OS that was
Raspbian extracted from
downloaded file.of SD Card.
Select the drive
Then click on Write.
Modern College
Finishing OS
Setup
When you start your Raspberry Pi for the first time, the
Welcome to Raspberry Pi application will pop up and
guide you through the initial setup.
Click Next to start the setup.
click Nextagain.
Enter a new password for your Raspberry Pi
click Next. Your default credentials and
username pi and password raspberry. are
Click Next let the wizard check for updates to Raspbian
complete an update.
Modern College
Configuration of
Raspberry Pi
The raspi-config tool helps you to configure
Raspberry Pi
several settings can be changed with this tool
without having to know the correct
commands to use.
It is written as a bash script, run in a terminal
window, and uses whiptail to create the
windows, menus and messages.
Some changes require "administrator"
permissions
Modern College
Raspi-Config : Menu
Driven
Menu > Preferences > Raspberry Pi Configuration.
Raspberry Pi Configuration window will be
displayed.
Modern College
Raspi-Config : Command Driven
Open Terminal and Give Command
$ sudo raspi-config
Raspberry Pi Configuration window will be displayed.
Modern College
Remote Desktop Connection (1)
Two Methods
SSH : Command line Access
VNC : Graphical Desktop Access
VNC Connect Server from RealVNC is included with
Raspbian.
Enable VNC Server is must before using it.
SSH is also included with raspberry, user
must enable it before use.
Modern College
Remote Desktop Connection (2)
Install VNC Server (if not Present)
sudo apt-get update
sudo apt-get install realvnc-vnc-server realvnc-vnc-viewer
Enable VNC Server and SSH by Graphically
Select Menu > Preferences > Raspberry Pi
Configuration > Interfaces.
Ensure VNC is enabled.
Enabling VNC Server and SSH at
Command Line
sudo raspi-config
Navigate to Interfacing Options
Scroll down, select VNC > Yes, select SSH
Modern College
Getting IP Address of
Rpi
In order to connect to Raspberry Pi from another
machine using SSH or VNC, you need to know
the Pi's IP address.
Run the ifconfig command
sudo ifconfig
This gives complete list of IP address
Alternate option to use hostname –I
$ hostname –I
This gives IP address of active connection
Third option
$ ping raspberrypi.local
Modern College
Remote connection using VNC
On Remote Device
Install VNC Viewer
Open the VNC
Viewer
Enter RPI‟s IP
address
Modern College
Remote Connection using SSH
On Remote Device
Install Putty
Open the Putty
Select SSH
as Connection
type
Enter
Raspberry Pi's
IP address
Port
Modern College
Raspberry Pi -
GPIO
A powerful feature of the Raspberry Pi is the
row of GPIO (general-purpose input/output)
pins along the top edge of the board.
A 40-pin GPIO header is found on all current
Raspberry Pi boards.
Any of the GPIO pins can be designated (in
software) as an input or output pin and used
for a wide range of purposes.
Modern College
Raspberry Pi -
GPIO
Modern College
GPIO Numbering
Systems
Raspberry Pi pins are numbered in two different
ways
Physical numbering (BOARD)
Broadcom numbering (BCM).
Modern College
Python Package for
GPIO
The Python package used for Raspberry Pi
GPIO programming is RPi.GPIO.
The package can be installed by using the
following command:
sudo apt-get install rpi.gpio
Modern College
Programming with GPIO
(1)
Step 1 : Import the RPi.GPIO
package. (import Rpi.GPIO as
GPIO)
Step 2 : Set the numbering style to be
used. We use the method GPIO.setmode()
for this. It takes either GPIO.BOARD or
GPIO.BCM as the parameter.
GPIO.setmode (GPIO.BOARD) stands for
physical numbering.
GPIO.setmode (GPIO.BCM) stands for
Broadcom numbering. Modern College
Programming with GPIO
(2)
Step 3 : Set up the necessary input and
output pins. The setup is used for the same.
To set up as output :
GPIO.setup(Pin No., GPIO.OUT)
To setup as input :
GPIO.setup(Pin No., GPIO.IN)
Modern College
Programming with GPIO
(3)
Step 4 : Read inputs and give outputs. The
output and input functions are used for the
same.
For output :
GPIO.output(13,True)
For reading Pin :
value = GPIO.input(13)
def blink():
GPIO.output(LED,
GPIO.HIGH)
time.sleep(1)
GPIO.output(LED, GPIO.LOW)
Modern College
EXAMPLE: LED Test
(2)
def destroy():
GPIO.output(LED, GPIO.LOW)
GPIO.cleanup()
Modern College
LCD Timing
Diagram
Modern College
Adafruit Library for
LCD
1. Install Git
• sudo apt-get install git-core
2. Download the library:
git clone git://github.com/adafruit/Adafruit_Python_CharLCD
3. Change directories with:
• cd Adafruit_Python_CharLCD
4. Install essential packages for LCD library:
• sudo apt-get install build-essential python-dev
5. Then install the library with:
• sudo python3 setup.py install
Modern College
Use of Adafruit LCD Library (1)
Call Adafruit LCD package
import Adafruit_CharLCD as LCD
Modern College
Feature
s
Full range temperature compensated
Relative humidity & temperature measurement
Calibrated digital signal
Outstanding long-term stability
Extra components not needed
Long transmission distance
Low power consumption
4 pins packaged and fully interchangeable
Modern College
DHT11 / DHT22 Working Principle
They consist of a humidity sensing
component, a NTC temperature sensor (or
thermistor) and an IC on the back side of the
sensor.
Modern College
DHT11 / DHT22 Working Principle
humidity sensing component has two electrodes
with moisture holding substrate between them.
As the humidity changes, the conductivity of the
substrate changes or the resistance between these
electrodes changes.
Change in resistance is measured & processed by
the IC which makes it ready to be read by a
microcontroller.
Modern College
DHT11 / DHT22 Working Principle
A thermistor is actually a variable resistor that
changes its resistance with change of the
temperature.
These sensors are made by sintering of
semiconductive materials such as ceramics or
polymers in order to provide larger changes in the
resistance with just small changes in temperature.
Modern College
Interfacing
DHT11
Response
Data Reading
Modern College
Communication Process: Request
To make the DHT-11 to send you the sensor
readings you have to send it a request.
The request is, to pull down the bus for
more than 18ms in order to give DHT time
to understand it and then pull it up for
40uS.
Modern College
Communication Process: Response
What comes after the request is the DHT-11
response.
This is an automatic reply from DHT which
indicates that DHT received your request.
The response is ~54uS low and 80uS
high.
Modern College
Communication Process: Data Read
The sensor data after response will be packed
in a packet of 5 segments of 8-bits each.
2 segments are Humidity read, integral &
decimal.
2 segments are Temperature read in Celsius,
integral & decimal
last segment is the Check Sum which is the sum
of the 4 first
Modern College
How to Identify
Bits
Each bit sent is a follow of ~54uS Low in the
bus and ~24uS to 70uS High depending on
the value of the bit.
Bit '0' : ~54uS Low and ~24uS High
Bit '1' : ~54uS Low and ~70uS High
Modern College
End Of
Frame
At the end of packet DHT sends a ~54uS Low
level, pulls the bus to High and goes to sleep
mode.
Example Reading
Humidity 0b00101011.0b00000000 = 43.0%
Temperature 0b00010111. 0b00000000 = 23.0 C.
Modern College
Adafruit Library for
DHT
1. Install Git
• sudo apt-get install git-core
2. Download the library:
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
3. Change directories with:
• cd Adafruit_Python_DHT
4. Install essential packages for DHT library:
• sudo apt-get install build-essential python-dev
5. Then install the library with:
• sudo python3 setup.py install
Modern College
Usage of DHT Adafruit Library
Call DHT Adafruit Package
import Adafruit_DHT
Declare the Sensor Pin and Type
Pin = 18
Type = Adafruit_DHT.DHT11
Get the readings
Hum,Temp = Adafruit_DHT.read_retry (Type, Pin)
Print the Value
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'
.format(Temp, Hum))
Presented By: Mr. D. Shridhar Modern College
Ultrasonic Sensor : HC-
An
SR04
ultrasonic sensor, also known as an ultrasonic
transducer.
Mainly used to determine the distance from the target
object.
It measures accurate distance using a non-contact
technology.
Transmitter and receiver are two main parts of the
sensor where former converts an electrical signal to
ultrasonic waves while later converts that ultrasonic
signals back to electrical signals.
These ultrasonic waves are nothing but sound signals
that can be measured & displayed at the receiving
end. Modern College
HC-SR04 :
Specifications
Working Voltage: DC 5V
Working Current: 15mA
Working Frequency: 40Hz
Max Range: 4m
Min Range: 2cm
Measuring Angle: 15 degree
Trigger Input Signal: 10µS TTL pulse
Echo Output Signal Input TTL level signal and
the range in proportion
Dimension 45 * 20 * 15mm
Modern College
How It Works – Ultrasonic Sensor
It emits an ultrasound at 40 KHz which travels
through the air and if there is an object or obstacle
on its path It will bounce back to the module.
Considering the travel time and the speed of the
sound you can calculate the distance.
Modern College
HC-SR04
Pinout
Modern College
Timing
Diagram
Supply a short 10uS pulse to the trigger input to start the
ranging, and then the module will send out an 8 cycle burst
of ultrasound at 40 kHz and raise its echo.
The Echo is a distance object that is pulse width and the
range in proportion .Y
Modern College
Distance
Calculation
Modern College
Routine for Ultrasonic
Sensor
def distance():
GPIO.output(TRIGGER, True) # set Trigger to HIGH
time.sleep(0.00001) # 10 us delay
GPIO.output(TRIGGER, False) # set Trigger to LOW
# Save current time
StartTime = time.time() StopTime =
time.time()
while GPIO.input(ECHO) == StartTime
# save 0: StartTime =
time.time()
while GPIO.input(ECHO) # save
== time
1: ofStopTime
arrival =
TimeElapsed = StopTime – StartTime
time.time()
distance = (TimeElapsed * 34300) /
2 return distance Modern College
LDR
Interfacing
An LDR (Light Dependent Resistor) or a photo
resistor is a photo conductive sensor.
It is a variable resistor that changes it‟s resistance in
a proportion to the light exposed to it.
Its resistance decreases with the intensity of light.
Modern College
LDR Interfacing
Diagram
Modern College
How it
works
As the capacitor gradually charges, the voltage that
passes through the circuit and to the GPIO pin rises.
Once the capacitor is charged to a certain point, it's
voltage rises above 2 volts and the Raspberry Pi will
sense that GPIO pin 13 is HIGH.
If the resistance of the sensor increases, the
capacitor will charge more slowly and the circuit will
take more time to reach 2 volts.
The above script essentially times how long it takes
for pin 13 to turn High and then uses this
measurement to calculate the resistance of the
Photoresistor.
Modern College
LDR
Code
def rc_time (pin_to_circuit):
count = 0
GPIO.setup(pin_to_circuit,
GPIO.OUT)
GPIO.output(pin_to_circuit, GPIO.LOW)
time.sleep(0.1)
GPIO.setup(pin_to_circuit, GPIO.IN)
while (GPIO.input(pin_to_circuit) == GPIO.LOW):
count += 1
return count
try:
while
True:
print(rc_time(pin_to_circuit))
except KeyboardInterrupt:
GPIO.cleanup() Modern College
Make Your Raspberry Pi Speak!
Espeak is software package to convert text
into audio.
Install espeak
$ sudo apt-get install espeak
Run espeak on Command line
espeak "Hello World" 2>/dev/null
To use in Python
import subprocess
from subprocess import call
call(["espeak","Object found is an apple"])
Modern College
Make Your Raspberry Pi Speak!
Modifying the Voice
-v<voice filename>+<voice type>
espeak -ven-us+m1 "Welcome to Dexter tutorial"
2>/dev/null
espeak -ven+f2 "Welcome to Dexter tutorial" 2>/dev/null
Modifying Speech Speed
-s<value> : Default is 175
espeak -s250 "Welcome to
Dexter tutorial" 2>/dev/null
Pause Between
Words
-g<value> : in units of
10ms Modern College
IR Sensor
Module
Detect the presence of objects before them.
The transmitter (IR LED) will emit an IR ray which will get
reflected if there is an object present before it.
This IR ray will be received back by the receiver
(Photodiode) and the output will be made high after
amplified using an op-amp LM358.
Modern College
Motion Sensor: PIR
sensor
A passive infrared sensor (PIR sensor) is an
electronic sensor that measures infrared (IR) light
radiating from objects in its field of view.
They are most often used in PIR-based motion
detectors.
PIR sensors detect general movement, but do not
give information on who or what moved.
Modern College
Analog Output :
PWM
Can a digital devise produce analog output?
Modern College
Presented By: Mr. D. Shridhar Smart Logic
PWM Functions in Python
(2)
start (Duty Cycle)
It is used to start PWM generation of specified Duty Cycle.
For Example: LED_pwm.start(100)
ChangeDutyCycle(Duty Cycle)
This function is used to change the Duty Cycle of
signal.
We have to provide Duty Cycle in the range of 0-100.
For Example: LED_pwm.ChangeDutyCycle(50)
stop()
This function is used to stop the PWM generation.
For Example: LED_pwm.stop()
ChangeFrequency(frequency)
This function
PWM. is used to change the Modern
frequency
College
(inLogic
Smart
DC motor
Interface
Why Motor Driver IC?
The L293D is a H Bridge type IC and can
control DC motors up to 36V
2 motors can be driven with a single
L293D
RaspberryPi Motor Driver
DC Motor
Circuit
Modern College
Presented By: Mr. Shridhar Dudam
Controlling
Direction
Modern College
Presented By: Mr. Shridhar Dudam
DS18B20 Temperature Sensor
3 terminal temperature sensor in the
available TO-92 (transistor type) package.
Communicates over 1-Wire Interface.
Operating voltage: 3-5V
Measuring Range: -55°C to +125°C
Accuracy: ±0.5°C
Resolution: 9-bit to 12-bit
Modern College
Presented By: Mr. D. Shridhar
Interfacing
Diagram
The Raspberry Pi has drivers for one wired devices
to be connected to GPIO pin-4 by default.
1-wire is a device
communication data
bus system
developed by Dallas
semiconductor
providing low speed
data, signalling and
power over a single
signal wire.
Modern College
Presented By: Mr. D. Shridhar
Reading temperature using
Terminal
Enable 1-wire interface
sudo raspi-config
Navigate to Interface options to enable 1-wire
Load the device kernel modules
sudo modprobe w1-gpio
sudo modprobe w1-therm
Point to the address of the temperature sensor,
cd /sys/bus/w1/devices/28*
Read the Temperature Value
cat w1_slave
Modern College
Presented By: Mr. D. Shridhar
DS18B20: Python Code (1)
import os
import glob
import
time
os.system('modprobe w1-gpio„)
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/‟
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave‟
Modern College
Presented By: Mr. D. Shridhar
DS18B20: Python Code (2)
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def
read_temp():
lines =
read_temp
_raw()
while lines[0].strip()[-3:] != 'YES„:
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=„)
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
Presented By: Mr. D. Shridhar Modern College
DS18B20: Python Code (3)
if name ==' main ':
try:
while True:
TempC = read_temp()
print("Temp. in :%0.2f C"%TempC)
Celsius print(" ")
time.sleep(1)
except KeyboardInterrupt:
print ("Program Terminated")
Modern College
Presented By: Mr. D. Shridhar
ThingSpea
k
ThingSpeak is a IoT cloud platform which
uses simple HTTP Protocol to transfer, store
and retrieve information from different
sensors.
Modern College
ThingSpea
k
ThingSpeak Application allows us to log the
sensor data, track locations and even social
networking of things.
ThingSpeak has support from MATLAB. The
close relationship between ThingSpeak and
MATLAB has lead to integrate several key
features of MATLAB into the ThingSpeak
Application.
ThingSpeak takes minimum of 15 seconds to
update sensor readings.
Modern College
Creating Account &
Channel
Create an account with
ThingSpeak (www.thingspeak.com).
After logging in, create a new channel for the
data to be stored. For this go to
Channels–
>My Channels and click on New Channel.
Modern College
Create a Channel and
Fields
Modern College
Getting API Key in ThingSpeak
To send data to ThingSpeak, we need an unique
API key, which is used to upload sensor data to
ThingSpeak Website.
Click on “API Keys” button to get your unique API
key for uploading your CPU data.
Modern College
ThingSpeaK Python
Code
import Adafruit_DHT
import time
from urllib.request import urlopen
import sys
camera.start_preview()
time.sleep(10)
camera.stop_preview()
Modern College
Camera preview :
Rotation
from picamera import PiCamera
Import time
camera = PiCamera()
camera.rotation = 180
camera.start_preview()
time.sleep(10)
camera.stop_preview()
Modern College
Camera preview : Transparency
from picamera import PiCamera
Import time
camera = PiCamera()
camera.rotation = 180
camera.start_preview(alpha=200)
time.sleep(10)
camera.stop_preview()
Modern College
Capturing
Image
from picamera import PiCamera
Import time
camera = PiCamera()
camera.start_preview()
time.sleep(5)
camera.capture('/home/pi/Desktop/img.jpg')
camera.stop_preview()
camera.start_preview()
camera.start_recording('/home/pi/video.h264')
time.sleep(10)
camera.stop_recording()
camera.stop_preview()
Modern College
Camera
Effects
Image Effect
Options:none, negative, solarize, sketch, denois
e, emboss, oilpaint, hatch, gpen, pastel, watercol
or, film, blur, saturation, colorswap, washedout, p
osterise, colorpoint, colorbalance, cartoon, deinte
rlace1, and deinterlace2.
camera.image_effect = 'colorswap'
Set the auto white balance
Options:off, auto, sunlight, cloudy, shade, tungs
ten, fluorescent, incandescent, flash, and horizon.
camera.awb_mode = 'sunlight'
Modern College
Camera
Effects
Set the exposure
Options:off, auto, night, nightpreview,
backlight, spotlight, sports, snow, beach,
verylong, fixedfps
, antishake, and fireworks
camera.exposure_mode = 'beach‟
Modern College
Thanks
smartlogitech@gmail.com
+917745024542
Modern College