0% found this document useful (0 votes)
31 views12 pages

Adafruits Raspberry Pi Lesson 4 Gpio Setup

This document provides an overview of configuring GPIO, I2C, and SPI interfaces on the Raspberry Pi. It explains the GPIO connector pinouts, installing Adafruit code libraries, enabling I2C and SPI support using raspi-config, and testing the interfaces. Basic tutorials for first projects using buttons, LEDs, displays, and sensors are also recommended.

Uploaded by

Acad Marti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views12 pages

Adafruits Raspberry Pi Lesson 4 Gpio Setup

This document provides an overview of configuring GPIO, I2C, and SPI interfaces on the Raspberry Pi. It explains the GPIO connector pinouts, installing Adafruit code libraries, enabling I2C and SPI support using raspi-config, and testing the interfaces. Basic tutorials for first projects using buttons, LEDs, displays, and sensors are also recommended.

Uploaded by

Acad Marti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Adafruit's Raspberry Pi Lesson 4.

GPIO
Setup
Created by Simon Monk

https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup

Last updated on 2023-08-29 02:14:10 PM EDT

©Adafruit Industries Page 1 of 12


Table of Contents

Overview 3

The GPIO Connector 4

Adafruit Pi Code 5

Configuring I2C 6
• Installing Kernel Support (with Raspi-Config)
• Testing I2C

Configuring SPI 10
• Installing Kernel Support (with Raspi-Config)

Test and Configure 12

©Adafruit Industries Page 2 of 12


Overview
One of the great things about the Raspberry Pi is that it has a GPIO connector to
which you can attach external hardware.

The GPIO connector actually has a number of different types of connection on them.
There are:

• True GPIO (General Purpose Input Output) pins that you can use to turn LEDs on
and off etc.

• I2C interface pins that allow you to connect hardware modules with just two
control pins

• SPI interface with SPI devices, a similar concept to I2C but a different standard

• Serial Rx and Tx pins for communication with serial peripherals

In this tutorial, you are not actually building anything, but you will learn how to
configure your Raspberry Pi and install useful libraries ready to start attaching some
external electronics to it.

This tutorial is written for Raspbian based distributions.

©Adafruit Industries Page 3 of 12


The GPIO Connector
The diagram below show GPIO pinouts used on different models of the Raspberry Pi.
The earlier revisions of the Raspberry Pi were 26-pin based while the newer models
are 40-pin.

As well as supplying power (GND, 3.3V and 5V) all the GPIO pins can be used as
either digital inputs or outputs. The pins labelled SCL and SDA can be used for I2C.
The pins labelled MOSI, MISO and SCKL can be used to connect to high speed SPI
devices.

All the pins have 3.3V logic levels and are not 5V-safe so the output levels are 0-3.3V
and the inputs should not be higher than 3.3V. If you want to connect a 5V output to a
Pi input, use a level shifter ()

A popular way to actually make the connections to the Raspberry Pi is to use a Pi


Cobbler Plus ()or a Pi T-Cobbler Breakout () for older 26-pin versions.

This uses a ribbon cable to connect the GPIO connector to solderless breadboard,
where you can add your own components.

©Adafruit Industries Page 4 of 12


Make extra extra double-check sure that the PIN 1 indicator is in the corner of the
Pi. If you have a gray cable its probably a red stripe, for black cables, a white
stripe. That pin must not be next to the TV connector. Turn around or twist the
cable until it is right

Adafruit Pi Code
To make life easy for those wishing to experiment with attaching electronics to their
Pi, Adafruit have produced an extensive and extremely useful collection of code. This
includes simple CircuitPython Libraries () for a large number of modules, including
displays, sensors, actuators and more.

To fetch this code, you need to use some software called 'git'. .

You will find the icon for a Terminal on your desktop.

©Adafruit Industries Page 5 of 12


Before we go any further, issue the following commands in a Terminal. This will ensure
your packages are up to date. It does not matter which directory you are in.

sudo apt-get update


sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y

Run the following command to install the adafruit_blinka CircuitPython Libraries.

pip3 install adafruit-blinka

Configuring I2C
I2C is a very commonly used standard designed to allow one chip to talk to another.
So, since the Raspberry Pi can talk I2C we can connect it to a variety of I2C capable
chips and modules.

©Adafruit Industries Page 6 of 12


The I2C bus allows multiple devices to be connected to your Raspberry Pi, each with
a unique address, that can often be set by changing jumper settings on the module. It
is very useful to be able to see which devices are connected to your Pi as a way of
making sure everything is working.

sudo apt-get install -y python-smbus


sudo apt-get install -y i2c-tools

Installing Kernel Support (with Raspi-Config)


Run sudo raspi-config and follow the prompts to install i2c support for the ARM core
and linux kernel

Go to Interfacing Options

On older versions, look under Advanced

©Adafruit Industries Page 7 of 12


then I2C

Enable!

sudo reboot

©Adafruit Industries Page 8 of 12


Testing I2C
Now when you log in you can type the following command to see all the connected
devices

sudo i2cdetect -y 1

This shows that two I2C addresses are in use – 0x40 and 0x70.

These values will be different for you depending on what is currently attached to the
I2C pins of your Raspberry Pi

Note that if you are using one of the very first Raspberry Pis (a 256MB Raspberry Pi
Model B) then you will need to change the command to:

sudo i2cdetect -y 0

The Raspberry Pi designers swapped over I2C ports between board releases. Just
remember: 512M Pi's use i2c port 1, 256M ones use i2c port 0!

When you are finished in raspi-config reboot for the i2c modules to automatically load
into the kernel.

©Adafruit Industries Page 9 of 12


Configuring SPI
Installing Kernel Support (with Raspi-Config)
Run sudo raspi-config and follow the prompts to install i2c support for the ARM core
and linux kernel

Go to Interfacing Options

Go to Interfacing Options

Then select SPI

©Adafruit Industries Page 10 of 12


When asked if you want to enable select
YES

That's it!

Now reboot your Pi to make the SPI interface appear

Next time you log in you can check that you can see the devices with

ls -l /dev/spidev*

you should see two 'devices' one for each SPI bus

©Adafruit Industries Page 11 of 12


The /dev/spidev0.x will not show up when using the PiTFT screens as it is using
the SPI interface.

Test and Configure


The best way to test what you have done is to pick one of the projects (perhaps a
fairly simple one to start with) and give it a try.

Here are some of the tutorials I would recommend as a first project:

• http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds ()
• http://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-pi ()
• http://learn.adafruit.com/matrix-7-segment-led-backpack-with-the-raspberry-pi ()
• https://learn.adafruit.com/adafruits-raspberry-pi-lesson-12-sensing-movement/
overview ()
• https://learn.adafruit.com/adafruits-raspberry-pi-lesson-13-power-control/
hardware ()

Click Here for the Next Lesson

©Adafruit Industries Page 12 of 12

You might also like