unit 4 python sy (1)
unit 4 python sy (1)
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.
• 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.
• 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
• 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.
» while True:
» Input value = GPI0.Input (23) #Read and store value of input to a variable
» 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.
• 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.
• 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.
• Instead of LED, buzzer can be connected which will buzz when the
movement is detected by the PIR sensor.
•