Accessing Raspberry Pi GPIO using Python Script

Raspberry Pi 2 Model B board is having many built in interfaces which one can use easily like 4 nos. of USB 2.0 port, Ethernet port, HDMI, Camera interface connector & Display interface connector. Other that these interfaces one can easily take control of other devices like switching ON/OFF the relays, interfacing 16×2 character LCD display, interfacing 7-segment display, interfacing temperature sensors etc through its programmable GPIOs.

Introduction

In Raspberry Pi2 Model B board the J8 connector provide the external programmable interfaces of your choice. This is a 40 pin connector with 3.3V, 5V, GND and general/special function GPIO pins. The detail is shown in the figure below:

Pi-GPIO-header

Note: The GPIO pins can handle only Digital interfaces and cannot handle Analog interfaces. For Analog signal interfaces you have to use external boards linked with Raspberry Pi.

Note: The logic level for digital signals on the GPIOs is 3.3V and not 5V. For interfacing with 5V logic level one has to use level translator. The logic level translator can be easily built with MOSFET 2N7000 or can be achieved with the help of IC SN7407.

For accessing the GPIOs one can use several programming languages and tools like C, C#, Ruby, Perl, Java etc. But the most popular among the programmers of Raspberry Pi is Python. The Module to access GPIOs of Raspberry Pi using Python i.e. RPi.GPIO is installed by default in Raspbian Operating System. For process on how to install Raspbian Operating System on Raspberry Pi follow my previous post: Getting started with your Raspberry Pi2 Model B board

Note: You must be having root access for running python script for accessing the GPIOs.

Our next task is to interface 4 RED LEDs(5mm LEDs) through the GPIOs and turning it ON/OFF one-by-one with proper delay by writing python script.

DSCN0558

Step: 1 Hardware Setup

Setup the hardware as per the following figure given below. We have used a bread-board, suitable numbers of jumper wires(both male-male and female-male), 4 nos. of 470ohm resistances and 4 nos. of 5mm RED LEDs.

DSCN0570

Note: The GPIOs of Raspberry Pi can source/sink only a limited amount of current when used as an Output/Input port respectively. So, in order to protect the GPIOs suitable value resistances are used.

it is advisable to use any of the resistances of value greater than 330ohms. Greater the resistances value less bright will the LEDs during ON condition.

Take extra care while connecting or using GPIOs as this may permanently damage your Raspberry Pi board.

We will not be discussing the pin to pin connections of the GPIOs with the bread-board, the following image will give you the clear picture on interconnections between the bread-board and the GPIOs.

DSCN0570 (3)

Step: 2 Python script for controlling the GPIO

The python GPIO library for controlling the GPIOs of Raspberry Pi have been included in the latest Raspbian operating system. Only you need to call the appropriate module to access the same.

To write a python script in Raspbian OS open the terminal and type sudo -x to gain access of root. And next type sudo idle3 to open the python3 IDE in Raspbian. This command will open the IDE where in you can write your python code for accessing the GPIOs.

DSCN0581

DSCN0582

Copy and paste the following code listed below and save the code as blink.py, here .py file extension stands for python script

Note: In the above code the line followed by “#” is the single line commenting style in python. While running the compiler will ignore these lines, and these are used for better understanding of the code.

Code Explanation Goes Here:  

The first two lines import RPi.GPIO as GPIO and import time are basically the modules which we are importing into our script for its usage. These modules are already described in the python library. The first module RPi.GPIO is called for the accessing of the GPIOs and the next time module is called for providing the necessary delay in execution of the codes.

Now GPIOs can be used as a Source/Sink or as an Output/Input port. Before using the GPIOs we have to define the GPIOS as the Output/Input Port.

For example the line GPIO.setup(4, GPIO.OUT) in above port declares the GPIO4 as an output port.

The lines GPIO.output(4, 1) & GPIO.output(4, 0) turns the GPIOs ON and OFF respectively. When turned “ON” the LED will glow and when turned “OFF” the LED will not glow. Or we can say that when GPIO is turned ON by GPIO.output(4, 1) command the GPIO4 sources the current out and when GPIO is turned OFF by GPIO.output(4, 0) command the GPIO4 stops sourcing the current out.

The line in the code time.sleep(1) gives a delay of 1 second in the execution of codes.

In the above code the LEDs are connected with GPIOs GPIO4, GPIO17, GPIO27 & GPIO22 respectively, and they are turned ON for 1 second and OF followed by the rest of the LEDs.

To run the complete code just click on Run > Run Module and the LEDs will glow ON and OFF with a delay of 1 second simultaneously.

DSCN0589

Hope you must have understood the full tutorial listed above and must have followed the same to access GPIOs using Python scripting. For any further query please feel free to comment below.

Tags:,

Add a Comment

Your email address will not be published. Required fields are marked *