0% found this document useful (0 votes)
18 views

SY_E1_CH4_Interfacing_of_Devices_Using_Python_Programming

The document outlines the syllabus and content for a course on Embedded System Design, focusing on interfacing devices using Python programming. It covers basic and advanced interfacing techniques with various devices such as LEDs, switches, cameras, and GSM modules, including example programs for each. Additionally, it discusses the use of Raspberry Pi GPIO pins and various communication protocols like I2C, UART, and SPI.

Uploaded by

ot7sakshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

SY_E1_CH4_Interfacing_of_Devices_Using_Python_Programming

The document outlines the syllabus and content for a course on Embedded System Design, focusing on interfacing devices using Python programming. It covers basic and advanced interfacing techniques with various devices such as LEDs, switches, cameras, and GSM modules, including example programs for each. Additionally, it discusses the use of Raspberry Pi GPIO pins and various communication protocols like I2C, UART, and SPI.

Uploaded by

ot7sakshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

The Poona Gujarati Kelwani Mandal’s

Haribhai V. Desai College, Pune-02

Electronics Paper -1
(ELC-241)
Embedded System Design
Chapter 4: Interfacing of Devices using Python Programming
Prepared for S.Y.B.Sc. (Computer Science)

Mr. Narendra V. Deore


(Dept. of Electronics)
Email ID: nvdeore@gmail.com
Syllabus
.
Unit 4 : Interfacing of devices using Python Programming (10)
Basic interfacing: LED, Switch, LCD
Internal Advanced: Bluetooth, Wifi, Ethernet,
External advanced: Camera, Serial Communication GSM, Ultrasonic Sensor, PIR,
Finger Print reader.
Basic Interfacing
Unit 4:Interfacing of Devices using Python Programming

GPIO- Powerful feature of Raspberry pi

GPIO- General Purpose Input Output

40 Pins

Connect anything –

Fundamental devices – LED, Switch etc.

Advance device – Robot, Camera etc.


Basic Interfacing
Unit 4:Interfacing of Devices using Python Programming
Basic Interfacing
Unit 4:Interfacing of Devices using Python Programming
Basic Interfacing
Unit 4:Interfacing of Devices using Python Programming

I2C- Inter Integrated Circuit ( SDA-3), SCL-5)

GPCLK- General Purpose Clock – 7

UART- Universal Asynchronous Receiver


Transmitter (TXD-8, RXD-10)

PWM- Pulse Width Modulation (PWM0-32,


PWM1-33)
Basic Interfacing
Unit 4:Interfacing of Devices using Python Programming
SPI- Serial Peripheral Interface
MOSI- Master Out Serial IN -19

MISO- Master In Serial Out -21

SCLK- Serial Clock -23

CE0- Chip Select 0-24

CE1- Chip Select 1-26

PCM- Pulse Code Modulation – Audio (


PCM_FS – Frame Synchronization to external
audio devices -35,

PCM_DIN -38 (Microphone),

PCM_DOUT- 40 - DAC
Basic Interfacing
Unit 4:Interfacing of Devices using Python Programming
Basic Interfacing
Unit 4:Interfacing of Devices using Python Programming

Maximum 3.3V on all GPIO pins

GPIO pins will either be Set 0(Logic 0 - False) or 1 (Logic 1- True)

0- below 1.7 V and 1- above 1.7 V

26 Pins are used for GPIO


LED Interfacing
Unit 4:Interfacing of Devices using Python Programming
LED Interfacing
Unit 4:Interfacing of Devices using Python Programming

R
LED Interfacing
Unit 4:Interfacing of Devices using Python Programming

R
LED Interfacing Program
Unit 4:Interfacing of Devices using Python Programming

import Rpi.GPIO as GPIO


Import time
GPIO.setmode(GPIO.BOARD)
LED=40
GPIO.setup(LED, GPIO.OUT)
While True:
GPIO.output(LED, True)
time.sleep(5)
GPIO.output(LED, False)
time.sleep(5)
LED Interfacing Program
Unit 4:Interfacing of Devices using Python Programming

import Rpi.GPIO as GPIO


Import time
GPIO.setmode(GPIO.BOARD)
LED=40
GPIO.setup(LED, GPIO.OUT)
try:
While True:
GPIO.output(LED, True)
time.sleep(5)
GPIO.output(LED, False)
time.sleep(5)
except:
GPIO.cleanup()
Switch Interfacing
Unit 4:Interfacing of Devices using Python Programming

+5V

R
Switch Interfacing Program
Unit 4:Interfacing of Devices using Python Programming

import Rpi.GPIO as GPIO


Import time
GPIO.setmode(GPIO.BOARD)
button=36
GPIO.setup(button, GPIO.IN, Pull_up_down=GPIO.PUD_UP)
While True:
a= GPIO.input(button)
if a==True:
print(“Switch is pressed”)
time.sleep(5)
LED-Switch Interfacing Program
Unit 4:Interfacing of Devices using Python Programming

import Rpi.GPIO as GPIO


Import time
GPIO.setmode(GPIO.BOARD)
LED=40
button=36
GPIO.setup LED, GPIO.OUT)
GPIO.setup(button, GPIO.IN, Pull_up_down=GPIO.PUD_UP)
While True:
a= GPIO.input(button)
if a==True:
GPIO.output(LED, True)
time.sleep(0.5)
else:
GPIO.output(LED, False)
time.sleep(0.5)
GPIO.cleanup()
LCD Interfacing Program
Unit 4:Interfacing of Devices using Python Programming

LCD- Liquid Crystal Display

Widely used display

Cheaper

Display wide variety of symbol, Characters and graphics

Built in refresh controller to refresh the display


LCD Interfacing Program
Unit 4:Interfacing of Devices using Python Programming

VCC
D0
D1
D2 VEE
D3
D4 LCD
D5
D6 VSS
D7

RS E
LCD Interfacing Program
Unit 4:Interfacing of Devices using Python Programming
LCD Interfacing Program
Unit 4:Interfacing of Devices using Python Programming
LCD Interfacing Program
Unit 4:Interfacing of Devices using Python Programming
LCD Interfacing Program
Unit 4:Interfacing of Devices using Python Programming

Sr. No. LCD effect Hex Value


1 Clear display 01
2 Return home 02
3 Shift cursor left 04
4 Shift cursor right 06
5 Shift display right 05
6 Shift display left 07
7 Both display and cursor off 08
8 Display off Cursor on 0A
LCD Interfacing Program
Unit 4:Interfacing of Devices using Python Programming

Sr. No. LCD effect Hex Value


9 Display on, Cursor off 0C
10 Display on, cursor blinking 0E
11 Shift entire display to left 18
12 Shift entire display to right 1C
13 Force cursor to beginning 80
of first line first column
14 Force cursor to beginning C0
of second line first column
15 2 lines, 5 x 7 matrix 38
LCD Interfacing Program
Unit 4:Interfacing of Devices using Python Programming

Command to LCD

Sr. No. Command Hex Code


1 Selecting 4 bit mode with 2 row 28
2 Display on, cursor off 0C
3 Clear display 01
4 Shift cursor right 06
5 Force cursor to beginning of 83
first line first column
LCD Interfacing Program
Unit 4:Interfacing of Devices using Python Programming

Command to LCD

Sr. No. Command Hex Code


1 Selecting 4 bit mode with 2 row 28
2 Display on, cursor off 0C
3 Clear display 01
4 Shift cursor right 06
5 Force cursor to beginning of 83
first line first column
Internal Advance Bluetooth, Wi-Fi &
Ethernet Unit 4:Interfacing of Devices using Python Programming

Most of the Raspberry pi Model have on board connectivity options


Bluetooth
Wi-Fi
Ethernet
Raspberry Pi 3, 3B+ , Zero W, Pi4 have all connectivity option
Raspberry Pi can connect Smartphone, Smart TV or any otherdevice
which support Bluetooth and wifi
Interfacing of Camera to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming
Camera – to capture Image(picture) and Video
2 types
Standard Version
NoIR version
Interfacing of Camera to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming
Standard version
Pi Camera module V1.3- 5MP, HD Video recording,
resolution 2592 X 1944
Connected through CSI (Camera Serial Interface) with 15
pin ribbon cable to Raspberry Pi
Interfacing of Camera to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming
Standard version
Pi Camera module V1.3- 5MP, HD Video recording,
resolution 2592 X 1944
Pi Camera module V2- 8MP, HD Video Recording
it supports 1080p30, 720p60 and VGA90 video modes
Connected through CSI (Camera Serial Interface) with 15 pin ribbon cable
to Raspberry Pi
Camera works on all – Raspberry Pi version (1, 2, 3, 4)
Accessed through MMAL, V4L API’s and third party libraries
Python - Picamera library
Interfacing of Camera to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming
Camera – to capture Image(picture) and Video
Interfacing of Camera to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming
CSI – Camera Serial Interface
Interfacing of Camera to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming
Interfacing of Camera to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming
Interfacing of Camera to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming
Step to connect camera module
1. Turn off the Raspberry Pi
2. Locate the camera module port
3. Gently pull up on the edge of the port’s plastic clip
4. Insert camera module ribbon cable
5. Push the plastic cable back into place
6. Start up raspberry pi
7. Go to main menu in OS
8. Open the Raspberry pi Configuration window
9. Select the interface tab and enabled the Camera .
Interfacing of Camera to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming
Program for camera preview
import Picamera as Picamera
import time
camera=Picamera()
camera.start_preview()
time.sleep(5)
camera.stop_preview()
Interfacing of Camera to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming
Program to rotate the image
import Picamera as Picamera
import time
camera=Picamera()
camera.rotation=180 # 90, 180 or 270 degree
camera.start_preview()
time.sleep(5)
camera.stop_preview()
Interfacing of Camera to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming
Program to capture and save the image
import Picamera as Picamera
import time
camera=Picamera()
camera.start_preview()
time.sleep(5)
camera.capture(‘/home/pi/desktop/abc.jpg’)
camera.stop_preview()
Interfacing of Camera to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming
Program to capture and save the 10 images
import Picamera as Picamera
import time
camera=Picamera()
camera.start_preview()
for i in range(10)
time.sleep(5)
camera.capture(‘/home/pi/desktop/abc%s.jpg’, %i)
camera.stop_preview()
Interfacing of Camera to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming
Program to record the video
import Picamera as Picamera
import time
camera=Picamera()
camera.start_preview()
camera.start_recording(‘‘/home/pi/desktop/abc.h264’)
time.sleep(5)
camera.stop_recording()
camera.stop_preview()
Interfacing GSM Module to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming

GSM (Global System For Mobile) is rectify the limitation of dial up MODEM
GSM –data can be transmitted and received by any embedded system
Interfaced to Raspberry Pi- image, video, audio processing, automation and
surveillance
Interface through – USB or GPIO pins(UART- serial)
Modem communication at 9600 bps baud rate and 8N1 UART data encoding
Interfacing GSM Module to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming
Interfacing GSM Module to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming

Insert the SIM card into SIM cardholder of SIM900A modem


Mini SIM -2FF
Micro SIM – 3FF
Nano SIM – 4FF
Connect Power Supply – 5V to 12 V, 2A
Modem status LED or Network LED will start blinking
Fast Blink – while connect to Mobile Network
After connecting to CSP( Communication Service Provider), it blink every three
second.
Interfacing GSM Module to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming

Interfacing of SIM900A with Rpi- TTL Voltage shifter

3.3V

10k
RPi GSM
3.3V Rx 5V Tx
1N4148
3.3V Tx 5V Rx
Interfacing GSM Module to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming

Making a Call from Raspberry Pi


Import serial , Rpi.GPIO , OS and time module
Access the Rpi serial TTL port ( UART pins) using serial.Serial() method.
Port Name- /dev/serial0
/dev/ttyS0
/dev/ttyAMA0
Send AT command
Using - serial.write() method
Read response from the modem
Use serial.read() method
Interfacing GSM Module to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming

Making a Call from Raspberry Pi


To make a call send (Dial)
“ATD <Mobile Number>”
To end a call , send (Hang)
“ATH”
Interfacing GSM Module to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming

import serial
import os , time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
port=serial.Serial(“/dev/ttyS0”, baudrate=9600, timeout=1)
port.write(b‘AT\r’)
a=port.read(10)
print(a)
time.sleep(1)
port.write(b ‘ATD9766705885;\r’)
print(“Calling….”)
time.sleep(30)
port.write(b ‘ATH\r’)
print(“Hang Call…”)
Interfacing GSM Module to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming

Send Message using Raspberry Pi


import serial
import os , time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
port=serial.Serial(“/dev/ttyAMA0”, baudrate=9600, timeout=1)
port.write(‘AT ‘+’ \r\n’)
a=port.read(10)
print(a)
time.sleep(1)
port.write(‘ATE0 ‘+’ \r\n’)
a=port.read(10)
print(a)
time.sleep(1)
Interfacing GSM Module to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming

port.write(‘AT+CMGF =1 ‘+’ \r\n’ )


a=port.read(10)
print(a)
time.sleep(1)
port.write(‘ AT+CNMI =2, 1, 0, 0, 0 ‘+’ \r\n’)
a=port.read(10)
print(a)
time.sleep(1)
port.write(‘AT+CMGS= “9766705885” ‘+’ \r\n’)
a=port.read(10)
print(a)
time.sleep(1)
Interfacing GSM Module to
Raspberry Pi Unit 4:Interfacing of Devices using Python Programming

port.write(‘Hello Narendra‘+’ \r\n’ )


a=port.read(10)
print(a)
time.sleep(1)
port.write(“\x1A”)
for i in range(10):
a=port.read(10)
print(a)
time.sleep(1)
Interfacing Ultrasonic Sensor
Unit 4:Interfacing of Devices using Python Programming

Transmit and detect ultrasonic waves


It transmits ultrasonic waves into the air and detect reflected waves from
an object
Interfacing Ultrasonic Sensor
Unit 4:Interfacing of Devices using Python Programming

Ultrasonic waves are sounds which cannot be hears by humans


Ultrasonics frequencies are of above 20 kHz
Metal, wood, concrete, glass, rubber and paper reflect approximately 100 % of
ultrasonic waves and can be easily detected.

Types
1. Electrostatic sensors
2. Piezoelectric sensors
Interfacing Ultrasonic Sensor
Unit 4:Interfacing of Devices using Python Programming

2. Piezoelectric sensors
Piezoelectric material to generate the ultrasonic waves.
Interfacing Ultrasonic Sensor
Unit 4:Interfacing of Devices using Python Programming

HC-SR04
Used to measure distance upto 400 cm
It has
Transmitter
Receiver
Control Circuit

Output is 5V (echo) so required level shifter


Interfacing Ultrasonic Sensor
Unit 4:Interfacing of Devices using Python Programming

Send trigger signal to sensor through RPi


It transmits 40kHz waves
Echo pin change the state to High
Waves goes to target and reflected to sensor
( receiver)
Echo pin change the state to Low
Measure the time as per Echo pin state
(High to Low)
Calculate distance using following formula
Distance= speed x time
Interfacing Ultrasonic Sensor
Unit 4:Interfacing of Devices using Python Programming
Interfacing Ultrasonic Sensor
Unit 4:Interfacing of Devices using Python Programming
Interfacing Ultrasonic Sensor
Unit 4:Interfacing of Devices using Python Programming
Interfacing Ultrasonic Sensor
Unit 4:Interfacing of Devices using Python Programming

Program to calculate the distance


import Rpi.GPIO
import time
GPIO.setmode(GPIO.BOARD)
Trig=16
Echo=18
GPIO.setup(Trig, GPIO.IN)
GPIO.setup(Trig, GPIO.OUT)
try:
while True:
Interfacing Ultrasonic Sensor
Unit 4:Interfacing of Devices using Python Programming

Program to calculate the distance


GPIO.output( Trig, True)
time.sleep(0.00001)
GPIO.output(Trig, False)
while GPIO.input(Echo)==1
t_start= time.time()
while GPIO.input(Echo)==0
t_end= time.time()
duration= t_end-t_start
distance= 17150 * duration
Interfacing Ultrasonic Sensor
Unit 4:Interfacing of Devices using Python Programming

Program to calculate the distance


distance= round(distance, 2)
if distance >2 and distance <400;
print (“ Distance: “ distance, “cm”)
else:
print(“ object is out of range”)
except keyboardInterrupt :
GPIO.cleanup()
Interfacing of PIR sensor
Unit 4:Interfacing of Devices using Python Programming

PIR- Passive Infra Red


use to detect the motion

Vcc
GND Vout

Measure infrared light radiating from object which temp is above 00k (absolute
zero)
Every object that has temp above absolute zero emits thermal energy(heat) in the
form of radiation.
Interfacing of PIR sensor
Unit 4:Interfacing of Devices using Python Programming
Interfacing of PIR sensor
Unit 4:Interfacing of Devices using Python Programming
Interfacing of PIR sensor
Unit 4:Interfacing of Devices using Python Programming

Vcc
GND Vout
Interfacing of PIR sensor
Unit 4:Interfacing of Devices using Python Programming
Interfacing of PIR sensor
Unit 4:Interfacing of Devices using Python Programming

Program to detect the motion


import Rpi.GPIO as GPIO
import time
motion=8
LED =10
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(motion, GPIO.IN)
GPIO.setup(LED, GPIO.OUT, initial_value=GPIO.LOW)
Interfacing of PIR sensor
Unit 4:Interfacing of Devices using Python Programming

Program to detect the motion


while True:
A= GPIO.input(motion)
if A==True:
GPIO.output(LED, True)
else:
GPIO.output(LED, False)
GPIO.cleanup()
Interfacing Fingerprint Reader
Unit 4:Interfacing of Devices using Python Programming

Fingerprints are unique


Most popular technology
Capture the image, extract the features of fingerprint, store it in digital format
Compare the current image with stored image
Fingerprint Reader / Scanner
The device which records a digital image of the fingerprint pattern
Interfacing Fingerprint Reader
Unit 4:Interfacing of Devices using Python Programming

R307 Finger Print Sensor


1000 fingerprints can store
3.3 V operating voltage
Other Features
Normal working current: 50 mA
Peak current: 80 mA
Supply voltage: DC 4.2- 6.0 V
Window area: 14x18 mm
Matching method: Comparison method(1:1)
Interfacing Fingerprint Reader
Unit 4:Interfacing of Devices using Python Programming

R307 Finger Print Sensor


1000 fingerprints can store
3.6 V operating voltage
Other Features
Supply voltage: DC 3.6- 6.0 V
Normal working current: 120 mA
Window area: 14x18 mm
Matching method: Comparison method(1:1)
Interface: TTL serial
Baud Rate: 9600, 19200, 28800,
38400, 57600(default)
Interfacing Fingerprint Reader
Unit 4:Interfacing of Devices using Python Programming

CP2102 USB to TTL Convertor


Creates virtual port
USB supports various baud rate for serial communication
Easy to use- plug and play
Interfacing Fingerprint Reader
Unit 4:Interfacing of Devices using Python Programming

CP2102 USB to TTL Convertor


3.3 /5 V Operation level compatible
USB 2.0
No external crystal required
Internal EEPROM for device ID
Baud rate: 300 bps to 921600 bps
Interfacing Fingerprint Reader
Unit 4:Interfacing of Devices using Python Programming

Interface Fingerprint and Raspberry Pi


Interfacing Fingerprint Reader
Unit 4:Interfacing of Devices using Python Programming

Program
Unit 4:Interfacing of Devices using Python Programming

Thank
you !
Unit 4:Interfacing of Devices using Python Programming

D- Switching Diode

R C
Vin +VCC
+VCC
-
- R1 A
Vnon-inv
A + Vsat
Vint
+ -VCC
-VCC
R2
R

You might also like