DEV Community

Surya Teja
Surya Teja

Posted on

Raspberry pi’s RPI GPIO Module of python

Basic setup and working with raspberry pi’s RPI GPIO Module of python

The 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-sized computer designed for education. The first model was launched in 2012 which was basically used for education



  • 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 to 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

Note: 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)
  • 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 option specifies that you are referring to the pins by the number of the pin the plug i.e the numbers printed on the board.

GPIO.BCM: Broadcom chip-specific pin numbers.

The 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.setup(4, GPIO.OUTPUT)

    # Loop to send HIGH and LOW to the mentioned pin
    While True:

    #Garabage Cleaner
    def clean_up():


The above program sends a HIGH signal to 4th pin(BCM) for one second and LOW signal for the 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 a basic setup and the following loop
continuously sends a HIGH and LOW signal to the 4th pin until the program is

GPIO.cleanup() function is used as a garbage cleaner and is recommended to use in
every program which uses GPIO pins of the raspberry pi.

atexit.register() function is used to trigger a function before exiting.

Based on the requirement of the application, we can set the pin in different

Eg: To generate an Analog Output


Once the program is completed, It can run by providing the following command into
the terminal

sudo python <program_name>.py

To exit the program

press ctrl+c


Rpi.GPIO Module:

Raspberry Pi Documentation:

Discussion (0)