How to use Raspberry Pi GPIO Pins? Python Tutorial

Raspberry Pi 4 GPIO Pins

Be it the Raspberry Pi 3 or Pi 4, GPIO pins have always been a staple feature of our favorite single board computer, the RPI. However, do you know it’s functionality and how you can get started with using it through Python Programming? Well, in today’s tutorial, we’ll be going through just that!

Today’s Raspberry Pi GPIO tutorial will cover the following:

  • GPIO definition and how does it work
  • Explaining the Raspberry Pi GPIO pinout
  • Configuring GPIO pins; SPI, I2C
  • Raspberry Pi GPIO tutorial with Python
  • Raspberry Pi GPIO projects

What does GPIO stand for and How does it work?

GPIO, in short for General Purpose Input Output is a standard interface on microcontrollers that allow it to connect with other electronic components, modules, etc. by offering digital input/output.

For GPIO pins to work, software configurations will take place, with Python libraries such as GPIOzero available to make physical computing more accessible for all users. Additionally, for usage with C, C++, and more experienced programmers, GPIO access libraries such as wiringPI are available as well!

Explaining the Raspberry Pi GPIO pinout

Ref

If you’re using the Raspberry Pi B+, 2, Zero, 3 or the latest Raspberry Pi 4 Model B, you’ll discover that your board have a total of 40 GPIO pins. The older iterations of the RPI such as the Raspberry Pi Model B, will only contain a total of 26pins.

  • Any of the designated GPIO pins can be used either as a input/output pin, with a wide variety of applications suitable
Raspberry Pi GPIO Pinout Pin Functionality and Explanation
GPIO GPIO pins seen above are your standard pins to be used for turning devices on and off.
E.g. When connected to an LED source
Power Pins (+) Two 5V and two 3V3 pins are present on the board to draw power from the Raspberry Pi
I2C Pins I2C pins are primarily used for connecting and hardware communication purposes for external modules that support such protocol.
Such I2C communication typically uses 2 pins
SPI SPI (Serial Peripheral Interface Bus) pins share similar functionality as I2C, for hardware communication purposes
The only difference between both is that it uses a different protocol
UART UART (Universal asynchronous receiver/trasmitter) pins are used for serial communication with other devices
DNC DNC pins refer to pins that you shouldn’t connect, as its name (Do Not Connect) suggest
GND GND (Ground) pins refer to pins to ground your devices.
As they are all connected in the same line, it won’t matter which GND pin you use to ground.

For more information on the differences between I2C, SPI, UART protocols, you can refer to the following resources:

How to configure Raspberry Pi GPIO, I2C, and SPI pins

Similarly to other electrical components, we’ll first have to configure the GPIO pins before we get started using them! We’ll be running the configuration guide below on the Raspbian Operating system.

Configuring GPIO

If you’re running on the latest version of Raspbian OS, you can skip this configuration portion and get straight into programming with the GPIO pins!

If not so, you’ll have to run the following commands in the serial terminal to update your RPI:

sudo apt-get update
sudo apt-get upgrade

If for a reason you don’t have the GPIO package installed, you can run the following command for installation:

sudo apt-get install rpi.gpio

Configuring Raspberry Pi I2C Pins

Now, to set up the RPI I2C pins for your hardware communication purposes, we’ll first head to the Raspi-Config tool by entering the following command:

sudo raspi-config

When entered, click on advanced options -> I2C -> Click yes to enable I2C. Once done, reboot your RPI and your I2C pins will be enabled afterwards.

  • To ensure that the necessary modules connected have been enabled, enter the following command below:
lsmod | grep i2c_

You should then see a return command indicating any modules that you’re running through the I2C pins.

Configuring the Raspberry Pi SPI pins

Similarly to the I2C configuration above, configuring the Raspberry Pi SPI pins will be an easy process. First, head to teh Raspi-config tool by entering the following command:

sudo raspi-config

When entered, click on advanced options -> SPI-> Click yes to enable SPI. Once done, reboot your RPI and your SPI pins will be enabled afterwards.

  • To ensure that the necessary modules connected have been enabled, enter the following command below:
lsmod | grep spi_

You should then see a return command indicating any modules that you’re running through the SPI pins.

Essential products when using Raspberry Pi GPIO pins

Before we move on to how you can program the RPI GPIO pins with python, here are some necessities that you’re require!

Raspberry Pi GPIO Entender Board

The above Raspberry Pi GPIO Triple Expand baord allows you to convert a single GPIO header on your Raspberry Pi into three! With this, you can add breakout boards, add-on boards, or just about anything desined to connect on a Raspberry Pi B+/A+ GPI!

It’s features include:

  • Plug-and-play system that requires no drivers or set up
  • Splits the single GPIO header into three
  • Three GPIO headers to plug in boards of your liking
  • Compatible with Raspberry Pi A+, B+, 2

Raspberry Pi A+&B+&2 40pin to 26pin GPIO Board

If you’re using the Raspberry Pi Module B+, this product is a true neccessity for you!

With the RPI B+ having more GPIO, it results in incompatibility with some of the original accessories, hence this downgrade module will allow you to use accessories for Module B on the B+

If you’re interested to find out more about this module, you can click the link above!

How to program the Raspberry Pi GPIO pins through Python?

What’s unique of running python on Raspberry Pi instead of the typical PC is that you can run codes specifically to control hardware components through its GPIO pins! In today’s tutorial, I’ll start by teaching you how you can code an LED to turn it on and off!

You’ll need the following hardware components:

Hardware Assembly and Configurations:

  • Step 1: Plug the Grove Base Hat into Raspberry Pi
  • Step 2: Select any GPIO port on the Base Hat and connect the Purple LED to it
  • Step 3: Connect the Raspberry Pi to PC through USB cable

For step 2, you can connect it to the Grove Port as well, which would result in the pairing to look like this:

Software Configurations with Python

  • Step 1: Follow Setting Software to configure the development environment
  • Step 2: Download the source file by cloning the grove.py library
cd ~
git clone https://github.com/Seeed-Studio/grove.py
  • Step 3: Execute the following command. Take note of the GPIO port you’ve connected the Grove – Purple LED to and enter it under port number.
cd yourpath/grove.py/grove
python grove_led.py portnumber

If your port number is 12, your command should look like this:

cd yourpath/grove.py/grove
python grove_led.py 12

You should now be able to see the LED on and Off!

Raspberry Pi GPIO projects

Of course, we couldn’t end a tutorial without introducing some raspberry pi GPIO projects you can get started with right? With a plethora of projects available due to the endless possibilities that the Raspberry Pi GPIO pins offer, I’ll just be providing a few of my favorites today!

  • If you’re looking for a comprehensive list of projects you can get started with, do check out this!

1) Raspberry Pi Smart Clock with GPIO and Python

Want to build a smart clock with your Raspberry Pi that shows time on an OLED display? This project not only does that but also allows you to hear time at different intervals! All software configurations are done with Python!

What do you need?

Hardware components:

Software apps and other services:

  • Raspbian or Noobs on Raspberry Pi
  • Python GPIO library installed in Pi
  • Python SSD OLED Library

Interested to find out more? You can check out the full tutorial alongside the Python Code by Ashwini Kumr Sinha on Hackster.io!

2) E Ink Calendar for Raspberry Pi

Want to program a calendar for your Raspberry Pi with Python? This project allows you to do so! Not only does it use an E ink display that doesn’t harm the eyes, but with a stronger light, the display turns clearer!

What do you need?

Hardware components:

Software configurations:

Interested to find out more? You can check out the full tutorial by Seeed on Seeed project hub!

3) Smart Control of Raspberry Pi Fan using Python and ThingSpeakAPI

Worry about the thermal issues present in the Rasberry Pi 4 and only want the fans to turn on when the processor really needs it? With this project, you can implement a Python script to control a fan based on the current temperature of the Raspberry CPU using On-off control!

  • Cool your Raspberry Pi and learn Python at the same time!

What do you need?

Hardware components:

Software apps and online services:

Interested to find out more? You can check out the full tutorial by Nurgaliyev Shakhizat on Seeed Project Hub!

Summary

That’s all for today’s guide on Raspberry Pi and its GPIO pins. I hope with this, you get a deeper understanding of how this important row of pins on your RPI works and how you can get started!

If you’re looking for everything you need to know about programming python on the Raspberry Pi, you can refer to my previous tutorial on it!

Please follow and like us:

1 thought on “How to use Raspberry Pi GPIO Pins? Python Tutorial

  1. I am so happy to read this. This is the type of manual that needs to be given and not the accidental misinformation that is at the other blogs. Appreciate your sharing this best doc.

Comments are closed.

Calendar

February 2020
M T W T F S S
« Jan   Mar »
 12
3456789
10111213141516
17181920212223
242526272829  
Wordpress Social Share Plugin powered by Ultimatelysocial