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

unit 4 python sy (1)

The document provides an overview of interfacing various input/output devices with the Raspberry Pi using its GPIO pins, including LEDs, switches, LCDs, and ultrasonic sensors. It explains the setup process, programming in Python, and the necessary configurations for each device. Additionally, it includes sample Python code for controlling these devices and highlights the importance of GPIO pin numbering and the use of libraries for effective programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

unit 4 python sy (1)

The document provides an overview of interfacing various input/output devices with the Raspberry Pi using its GPIO pins, including LEDs, switches, LCDs, and ultrasonic sensors. It explains the setup process, programming in Python, and the necessary configurations for each device. Additionally, it includes sample Python code for controlling these devices and highlights the importance of GPIO pin numbering and the use of libraries for effective programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Unit 4

Introduction
 Raspberry Pi has General Purpose Input/output (GPIO) pins which
can be used to interface many input output devices.
 The devices like LEDs, Sensors, display, camera etc. can be
interfaced through these GPIO pins.
 Raspberry Pi can be programmed using Python scripting language
and various I/O devices can be controlled by programming
Raspberry Pi.
 Here interfacing of LED, switch, LCD, camera, serial
communication GSM, ultrasonic sensor, PIR and finger print
reader are discussed.
 Raspberry Pi has 40 pins GPIO connector.
 The latest version of the Raspberry Pi series, Raspberry Pi Model
B, has 40 GPIO pins which include power pins and ground pins.
• Fig. above shows Raspberry Pi Model B board.

• GPIO pin number and physical pin number of Raspberry Pi are different.

• In GPIO numbering, pin number refers to number on Broadcom SoC and in


physical numbering, pin number refers to the pin 40-pin header on Raspberry Pi
board.

• So, the GPIO pins of Raspberry Pi have to be defined.

• As shown, the numbers in the center which are circled are the physical pins of
Raspberry Pi. These pins are known as Board Pins or numbers.

• The GPIO numbers such as Physical Pin 3 is GPIO 2 which is seen by the
processor.

• These numbering is called as GPIO numbering or BCM numbering. So, while


programming, user has to decide which type of numbering is used in
programming.

• Python is an interpreted language that means a Python program or script can be


directly executed rather than compiling it into machine code.
• Interpreted languages such as Python are easy and
quicker to program. Interpreter itself can identify
the type of variable.

• Python interpreter can be run in two ways:


1. As an interactive shell to execute individual
commands or
2. As a command-line program to execute standalone
programs or scripts.

• The integrated development environment (IDE)


bundled with Python and the Raspberry Pi is called
as IDEL
Setting Raspberry Pi Module

• First that module has to be downloaded, configured and installed. For example:
numPy module can be installed as follows:
sudo apt-get install python-numpy

• The Raspberry Pi is actually minicomputer which is capable of doing many


things.

• To write a Python program for Raspberry, first Operating System has to be


installed and then Raspberry has to be configured.

• Install Raspbian with Desktop and connect keyboard, mouse and monitor to
Raspberry Pi. Windows, macOS or Linux can be used for installing OS
Steps for Booting Raspberry Pi
Booting Raspberry Pi for the first time include
following steps:
1. Push the microSD card into the socket on the
bottom of the board.
2. Plug in a USB keyboard and mouse.
3. Plug the HDMI output into TV or monitor. Make
sure that, monitor is ON and set to the correct
input.
4. Plug in power supply and switch ON the board.
5. Once the board is ON, Raspberry Pi logo appears
on screen.
Raspberry Pi configuration tool
The tool allows user to change many important settings on
Raspberry Pi such as:
• System Expand Filesystem, change password, Boot,
Overscan etc.
• Interface SSH(Secure shell), GPU memory, Overclock
• Localization Set keyboard, Set Locale, Set Timezone etc.

 Raspbian is configured automatically and get synchronized


its time and date with a network Time Protocol (NTP)
server when plugged into a network.
 To write a Python program for Raspberry Pi GPIO library is
required.
 In Raspbian, Python and GPIO library are preinstalled.
First Python Program on Raspberry Pi
To write first Python program, open Python's IDEL editor,
The message containing Python version, date, time,
license will appear on screen.
Python's IDEL editor
• Type "print ("Hello!") into the Python window and
press enter, „Hello!‟ word will appear on screen.
• For every new program, open Python's IDLE editor and
open a new window (CTRL + N), enter the code below
and save it.
• To run the Python program, type
sudo python filename.py
Interfacing of LED to Raspberry Pi
Raspberry Pi has 28 General Purpose Input Output pins through which any I/O devices
can be connected.
• Pin number 3, 5, 7, 8, 10, 11, 12, 13, 15, 16, 18, 19, 21, 22, 23, 24, 26, 27, 28, 29,
31, 32, 33, 35, 36, 37, 38, 40 are the GPIO pins of Raspberry Pi.
• LED can be interfaced or connected to any of these I/O pins.
• Connect the LED between ground and 25 (GPIO) with a 1 Kohm resistor in series
as shown in below Fig. 4.4.
• As shown in above Fig. 4.4, the anode of the LED is connected to GPIO25
(physical pin 22) with a series resistance of 1 Kohm value.
• The cathode of the LED is connected to GND pin.
• The GPIO25 pin has to be configured as an output pin so that Raspberry Pi can
drive the
• LED If GPIO25 pin made High for some time and LOW for some time and if this
process is kept in a loop then LED will start blinking.
• A Python program is written to make GPIO25 pin high and low.
Algorithm for Blinking LED
 Import the code needed for GPIO control
 Import the code needed for the sleep function.
 Use the chip's signal numbers.
 Set pin 25 as an output pin.
 Create an infinite loop consisting the required code.
 Turn the LED ON
 Wait for one second.
 Turn the LED OFF,
 Wait for one second.
 Procedure of Interfacing and Blinking of LED
1) Open Terminal.
2) Launch IDEL by typing sudo idle.
3) After the IDLE is launched, open a new window by FILE OPEN or Ctrl+N.
4) Type the below code in that window:

» import time #Import time library.


» import RPI.GPIO as GPIO #Import GPIO library
» GPIO.setmode(GPIO. BOARD) #Use board pin numbering
» GPIO.setup(25, GPIO.OUT) #Setup GPIO Pin 25 to OUT
» while True:
» GPIO.output(25, True) # Turn ON LED
» time.sleep(1) # Wait for one second
» GPIO.output(25, False) # Turn OFF LED
» time.sleep(1) #Wait for one second

Save the code by FILE SAVE or Ctrl+S.


RUN the code RUN RUN or Ctrl+F5..
Once the program will start executing, the LED will start blinking.
Interfacing of a Switch to Raspberry Pi
• The switch or push button can be connected to Raspberry
Pi.
• GPIO pin where the switch is connected has to be
configured as an input pin so that it will read the
incoming data from the switch.
• One end of the switch will be connected to GPIO pin
through a pull-up or pull-down resistance as shown in
below.
• If an input pin is pull-down then it will read 'Low' when
the switch is pressed.
• In pull-up resistance, it will read 'High' when the switch is
pressed,
• Raspberry Pi has an internal pull-up and pull-down
resistors, which can be enabled through software.
• External pull-up or pull-down resistors can be used.
Interfacing of switch to Raspberry Pi
Algorithm for Interfacing a Switch
• Connect a switch as shown
• Set pin GPIO23 as an input pin,
• Create a variable called count and store 0 in it.
• Save the value of pin GPIO23 into input
value.
• Check that value continuously.
• If the switch is pressed, then the value is true.
• If it is true then increment the counter.
• Print the text to the terminal.
Execute the program switch:
» import RPi.GPIO as GPIO #Import GPIO library

» import time #Import time library.

» GPIO.setmode (GPIO. BOARD) #Set GPIO pin numbering

» GPIO.setup(23, GPIO, IN, pull up down-GPIO PUD UP)


#Enable input and pull down resistors

» while True:
» Input value = GPI0.Input (23) #Read and store value of input to a variable

» if input_value = = True: #Check whether switch is pressed

» print(“Switch is Pressed”).
» time.sleep(0.5) #time sleep(0.5) command will insert delay
between two consecutive switch
Interfacing LCD to Raspberry Pi
• LCD is used to display the alphanumeric characters.
• LCD can be connected to GPIO pins of Raspberry Pi and through
program any characters can be displayed on LCD.
• Out of 40 pins of Raspberry Pi, 17 GPIO pins can be used to interface
LCD.
• There are +5 V (Pin 2 and 4) and +3.3V (pin 1 and 17) power output pins
are available on Raspberry Pi board.
• LCD requires +5 V, so provide +5 V to 16 x 2 LCD.

• shows the interfacing of LCD to Raspberry Pi.
• As shown in above, LCD is interfaced to 10 GPIO pins of Raspberry Pi.
• GPIO pin 21, 20, 16, 12, 25, 24, 23 and 18 are used as 'BYTE and created
'PORT function to send the data to LCD.
• GPIO 21 is LSB and GPIO 18 is MSB
• 16 x 2 LCD module has 16 pins.
• Table 4.2: Pin Configuration of LCD
Procedure of Sending Data to LCD
• Enable the module by setting E high and RS low to
enable commands to LCD.
• Clear screen by sending value 0 x 01 to data port as a
command.
• Provide ASCII code of characters to be displayed.
• Once E pin goes low, the LCD process the received
data and display it.
• So E will be set high before sending the data and then
low after sending the data.
• The characters will be sent one by one serially.
The Python program for displaying characters on
LCD is as under
» import RPi.GPIO as GP10
» Import time
» Import sys
» IO. setmode (IO.BCM)
» IO.setup(6, IO. OUT)
» IO.setup(22, IO.OUT)
» IO.setup(21, IO.OUT)
» IO. setup (20, IO.OUT)
» IO.setup(16, IO.DUT)
» IO.setup(12, IO.OUT)
» IO. setup(25, IO.OUT)
» IO.setup(24, IO.OUT)
» IO.setup(23, IO.OUT)
» IO.setup(18, IO.OUT)
• def send_a_command (command): #execute the loop when "send a command"
is called pin=command
Contd…
» PORT (pin); #calling "PORT to assign value to data port
» IO.output (6,0) # putting 0 in RS to tell LCD we are
sending command
» IO.output (22,1) # telling LCD to receive command/data at
the port by pulling EN pin high
» time.sleep(0.05)
» IO.output (22,0) #pulling down EN pin to tell LCD we have
sent the data
» Pin=0
» PORT (pin); #pulling down the port to stop transmitting
» def send_a_character (character): #execute the loop when "send_a_character"
is called Pin=character
» PORT (pin);
» IO.output(6,1)
» IO.output (22,1)
» time.sleep(0.05)
» IO.output(22,0)
» Pin=0
» PORT(pin);
» def PORT(pin):
» if(pin=0&0x01= = 0x01):
» IO.output (21,1)
» else:
» IO.output(21,0)
» if(pin&0x02= =0x02):
» IO.output(20,1)
» else:
» IO.output (20,0) I
» f(pin&0x04= =0x04):
» IO.output(16,1)
» else:
» IO.output (16,0)
» if(pin&0x08= =0x08):
» IO.output (12,1)
» else:
» IO.output(12,0)
» if(pin&0x10= = 0x10):
» IO.output(25,1)
» else:
» IO.output(25,0)
» if(pin&0x20= =0x20):
» IO.output(24,1)
» else:
» IO.output (24,0)
» if(pin&0x40= =0x40):
» 20.output (23,1)
» else:
» IO.output (23,0)
» if(pin&0x80= =0x80):
» IO.output (18,1) #if bit7 of 8bit pin is true pull PIN18 high
» else:
» IO.output (18,0) #if bit of shit 'pin' is false pull PIN18 low
» while 1:
» send_a_command(0x01); #sending all clear command
» send_a_command(0x38); #16*2 line LCD
» send_a_command(0x0E); #screen and cursor ON
» send_a_character(0x48); #ASCII code for "H"
» send_a_character(0x45); # ASCII code for "E"
» send_a_character(0x4C); #ASCII code for "L”
» send_a_character(0x4C); #ASCII code for 'L'
» send_a_character(0x4F); #ASCII code for „O'
» time.sleep(1)
• After running the program, 'HELLO' message will be displayed on
LCD.
Interfacing ultrasonic sensor to Raspberry PI
• Ultrasonic sensors are used to measure the distance between two objects using ultrasonic
waves.

• Ultrasonic waves are used as they are more accurate and not audible to human ear.

• HC-SR04 ultrasonic sensor is widely used for distance measurement up to 400 cm.

• It consists of transmitter, receiver and control circuit.

• The ultrasonic transmitter transmits waves (of 40 kHz) get reflected by the target which are
received by the receiver.

• The Echo pin will change the state and it becomes high.

• It remains in high state until it hit the target object and returns to receiver.

• Based on the time for which Echo pin stays high, distance can be calculated using the
formula: Speed =Distance/Time.

• The ultrasonic sensor is interfaced to Raspberry Pi as shown. As Raspberry Pi works on 3.3V


and ultrasonic sensor require 5 V, a level converter is connected.
Interfacing ultrasonic sensor to Raspberry PI
• The Raspberry Pi read the Echo pin, this pin is
connected to GPIO and configured as an input
pin.
• The output is available on Trig pin, so it is
configured as output pin.
• The range of ultrasonic sensor is from 2 cm to
400 cm.
The python program for interfacing ultrasonic sensor is given
below:
» import RPi.GPIO as GPIO #Import GPIO library
» import time #Import time library
» GPIO.setmode (GPIO.BCM) #Set GPIO pin numbering
» TRIG =21 #connect pin 21 to TRIG
» ECHO =23 #connect pin 23 to ECHO
» print "Distance measurement starts"
» GPIO.setup(TRIG, GPIO.OUT) #configure Trig as GPIO out
» GPIO.setup(ECHO,GPIO.IN) #configure Echo as GPIO in
» while True:
» GPIO.output(TRIG, False) #Set TRIG as LOW
» print "Waitng For Sensor To Settle"
» time.sleep(2) #Delay of 2 seconds.
» GP10.output(TRIG, True) #Set TRIG as HIGH
» time.sleep(0.00001) #Delay of 0.00001 seconds
» GP10.output(TRIG, False) #Set TRIG as LOW
» while GPIO.input (ECHO)= =0: #Check whether the ECHO is LOW
» pulse start time.time() #Saves the last known time of LOW pulse
» while GPIO. Input (ECHO)= =1: #Check whether the ECHO is HIGH
» pulse end time.time() #Saves the last known time of HIGH pulse
» pulse duration pulse end pulse start #Get pulse duration to a variable
» distance pulse_duration 17150 # Multiply pulse duration by
17150 to get distance
» distance=round(distance, 2) #Round to two decimal points
» if distance >2 and distance < 400: #Check whether the distance is
within range
» print "Distance:", distance- 0.5,"cm” #Print distance with 0.5 cm
calibration
» else:
» print "Object is Out Of Range" #display object is
out of range
» time.sleep(0.00001) #Delay of 0.00001 seconds

After running the program, corresponding message will get displayed.


Interfacing PIR Sensor to Raspberry Pi

• Passive Infrared (PIR) sensor detects any movement


of human or animals which emits infrared.

• PIR sensor detects change in the infrared radiation.

• When any object or human is detected in the field of


view of PIR sensor, the temperature of PIR sensor
rises and again come to ambient temperature, PIR
sensor converts this temperature change into the
change in output which is considered as movement
detection.
• The output of PIR sensor is high (5V) when it senses the motion and low
(0 V) when there is no motion.

• PIR sensors are used in many applications such as lighting control


system, home automation, security systems etc.

• When the movement is detected by the PIR sensor, its output goes high
which will be read by Raspberry Pi and LED can be turned ON as an
indication of movement detection.

• So, when there is a movement, LED will be ON.

• PIR is an input to Raspberry Pi and LED is treated as output.

• So PIR is configured as input pin and LED is configured as output pin of


Raspberry Pi.
The program code for interfacing PIR sensor is given below.
» import RP1.GPIO as GPIO
» PIR_input 29 #read input from PIR
» LED 32 #LED for indication of movement detection
» GPIO.setmode (GPIO.BOARD) #choose pin no. system
» GPIO.setup(PIR_input, GPIO.IN) #Setup GPIO as input
» GPIO.setup(LED, GPIO.OUT) #Setup GPIO as OUT
» GPIO.output(LED, GPIO. LOW) # initially LED is off
» while True:
» if(GPIO.input(PIR_input)):
» GPIO.output(LED, GPIO.HIGH) #when motion detected turn LED ON
» else:
» GPIO.output(LED, GPIO.LOW) #Otherwise LED is Off

• Instead of LED, buzzer can be connected which will buzz when the
movement is detected by the PIR sensor.

You might also like