Basic setup and working with raspberry pi’s RPI GPIO Module of python
Our first post is about basic setup and working with raspberry pi’s RPI GPIO Module of python.
Brief intro to Raspberry Pi:
Raspberry Pi is a credit card size computer designed for education. The first model was launched in 2012 which was basically used for education
Features:
- Small size
- Low-cost Device
- Complete linux computer
- Low power consumption
GPIO pins:
There are 40 powerful General Purpose Input/Output(GPIO) pins which are part of core components of the Raspberry pi. There are two 5V pins and two 3.3v, ground pins and general purpose pins.
RPI GPIO Module:
Python can be used write applications in Raspberry pi. It comes with a powerful package manager called PIP which can be used to install third party packages.
RPI GPIO Module is a third party package which can be used to configure and control the GPIO pins.
The basic setup is pretty easy. After setup we define whether a pin’s value should be high or low.
Basic setup
- Make sure that python>2.7 is installed in the raspberry Pi.
- Install the module using PIP
sudo pip install RPi.GPIO# If you face any problem in installing the package install python-dev and try again
- Import the modules to script
- Set the pin numbering schemes (explained below)
GPIO.setmode(GPIO.BCM)
- Configure the required pins
GPIO.setup(18, GPIO.OUT)# PIN 18(BCM) is now used as output
Pin numbering schemes:
There are two types of pin numbering schemes
GPIO.BOARD: Board numbering scheme
The GPIO.BOARD option specifies that you are referring to the pins by the number of the pin the the plug i.e the numbers printed on the board.
GPIO.BCM: Broadcom chip-specific pin numbers.
The GPIO.BCM option means that you are referring to the pins by the “Broadcom SOC channel” number.
Once the basic setup is completed, Based on the requirement we set the pins to high or low
# Import modules
import RPi.GPIO as GPIO
import time
Import atexit#PIN setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUTPUT)# Loop to send HIGH and LOW to the mentioned pin
While True:
GPIO.output(4,GPIO.HIGH)
time.sleep(1)
GPIO.output(4,GPIO.LOW)
time.sleep(1)#Garabage Cleaner
def clean_up():
GPIO.cleanup()atexit.register(clean_up)
The above program sends HIGH signal to 4th pin(BCM) for one second and LOW signal for next one second continuously until the program is exited. To verify the output of the process we can connect LED and test it.
The first 5 lines of the program are basic setup and the following loop continuously sends HIGH and LOW signal to the 4th pin until the program is existed.
GPIO.cleanup() function is used as garbage cleaner and is recommended to use in every program which uses GPIO pins of raspberry pi.
atexit.register() function is used to trigger a function before exiting.
Based on the requirement of application, we can the set the pin in different modes.
Eg: To generate a Analog Output
pwm = GPIO.PWM(18, 1000)
pwm.start(50)
Execution:
Once the program is completed, It can run by providing following command into the terminal
sudo python <program_name>.py
To exit the program
press ctrl+c
Sources:
Rpi.GPIO Module: https://pypi.python.org/pypi/RPi.GPIO
Raspberry Pi Documentation: https://www.raspberrypi.org/documentation