0

members

Easy Raspberry basics: Project 29a Raspberry PI Zero W board - Raspberry Pi GPIO Pinout

of Acoptex.com in Raspberry Pi 3

Raspberry basics: Project 29a

Project name: Raspberry PI Zero W board - Raspberry Pi GPIO Pinout

Tags: Raspberry, Raspberry PI Zero W board, vers 1.1, v 1.1, Raspberry Pi GPIO Pinout, GPIO Pinout, GPIO

Attachments: None

In this project, you needed these parts (Dear visitors. You can support our project buy clicking on the links of parts and buying them or donate us to keep this website alive. Thank you):

1. Raspberry PI Zero W board or full kit 1 pc

2. Micro SD card with NOOBS and SD card adapter 1 pc

3. Micro USB power supply (2 A 5V or 5V 3A) 1 pc

4. USB keyboard 1 pc

5. USB mouse 1 pc

6. TV or PC monitor 1 pc

7. HDMI cable 1 pc

8. T-Cobbler Breakout and GPIO Cable 1 pc

9. Micro USB 2.0 OTG Cable 1 pc

10. Mini HDMI to HDMI Adapter (HDMI to Mini HDMI Adapter) 1 pc

11. 4-Port USB 2.0 Hub 1 pc

General

We will learn everything we need to know about the Raspberry Pi’s GPIO pins: what they can do, how to use them, and mistakes to avoid while using them.

Please note that experimenting with the GPIO pins is risky and has a chance of bricking your Raspberry Pi. Knowing the power limitations of the GPIO pins and also understanding what you’re plugging in is a must. Do not use 5V for 3V3 components. Do not connect motors directly to the GPIO pins, instead use an H-bridge circuit or a motor controller board.

Most of the pins in the GPIO header go directly to the Broadcom chip. It is important to carefully design the components you attach to them as there is a risk you will permanently damage your Raspberry Pi. Short circuits and wiring mistakes could also ruin your day so double check everything. A multimeter is probably going to help a lot here as you can double check wiring before you connect to the Raspberry Pi.

1. GPIO pins

A powerful feature of the Raspberry Pi is the row of GPIO (general-purpose input/output) pins along the top edge of the board. GPIO pins behavior can be controlled by the user to allow them to read data from sensors, and control components like LEDs, motors, and displays. Any of the GPIO pins can be designated (in software) as an input or output pin and used for a wide range of purposes.

Older models of the Raspberry Pi (Raspberry Pi 1 Model B (Revision 1.0), Raspberry Pi 1 Model A/B (Revision 2.0) had 26 GPIO pins, while the newer models have 40 GPIO pins (Raspberry Pi Zero & Raspberry Pi Zero W, Raspberry Pi 3 Model B, Raspberry Pi 3 Model B+, Raspberry Pi 3 Model A+, Raspberry Pi 2, Raspberry Pi Model A+, Raspberry Pi 1 Model B+).

It's important to be aware of which pin is which. Some people use pin labels (like the RasPiO Portsplus PCB, or the printable Raspberry Leaf).Make sure your pin label is placed with the keyring hole facing the USB ports, pointed outwards.

You can use any pins except the power pins (5V, 3.3V, ground (GND)), pin 27 and pin 28 as regular GPIO pins.

2. GPIO pins pinout



Let's check the Raspberry Pi's 40 GPIO pins (for newer models Raspberry Pi Zero & Raspberry Pi Zero W, Raspberry Pi 3 Model B, Raspberry Pi 3 Model B+, Raspberry Pi 3 Model A+, Raspberry Pi 2, Raspberry Pi Model A+, Raspberry Pi 1 Model B+).

2.1 Voltages

The Raspberry Pi can provide both 5V (pins 2 and 4) and 3.3V (pins 1 and 17) power. It also provides the Ground pins (Ground or GND) for circuits on pins 6, 9, 14, 20, 25, 30, 34, and 39. These pins are all electrically connected, so it doesn't matter which one you use if you're wiring up a voltage supply.

3.3V pins - Anything connected to these pins will always get 3.3V of power

5V pins - Anything connected to these pins will always get 5V of power

Ground (GND) - 0V, used to complete a circuit

There is no single answer to how much current the 5V power pins can draw as it is reliant on what power supply you are using, and what other components you have attached to your Pi.

The Raspberry Pi 3 will only draw 2.5A from its power supply, and requires around 750mA for boot up and normal headless operation. This means that if you are using a 2.5A power supply, the 5V pins can supply a total current of around 1.7A maximum. 

The 3.3V supply pin on the early Raspberry Pi had a maximum available current of about 50 mA. Enough to power a couple of LEDs or a microprocessor, but not much more. All Raspberry Pi since the Model B+ can provide quite a bit more, up to 500mA to remain on the safe side, thanks to a switching regulator. Still, you should generally use the 5V supply, coupled with a 3.3V regulator for 3.3V projects.

2.2 Outputs and Inputs

Other GPIO pins are capable of a 3.3V output, also referred to as setting the pin HIGH in code. When an output pin is LOW this means that it is simply providing 0V.

A GPIO pin designated as an input pin can be read as HIGH (3.3V) or LOW (0V). This is made easier with the use of internal pull-up or pull-down resistors. Pins GPIO 2 and GPIO 3 have fixed pull-up resistors, but for other pins this can be configured in software. Do not provide the pins with greater than 3.3V: this is a quick way to damage your Raspberry Pi!

2.3. PWM

PWM (Pulse Width Modulation) is used with components such as motors, servos and LEDs by sending short pulses to control how much power they recieve.

PWM is also possible on the Raspberry Pi. GPIO 12, GPIO 13, GPIO 18, GPIO 19 are hardware PWM capable, though the Raspberry Pi is also able to provide software PWM through libraries such as pigpio on all pins.

2.4 UART (Universal Asynchronous Receiver/Transmitter)

Pins 8 and 10 (GPIO 14 and 15) are UART pins, designed for communicating with the Pi using the serial port. 

UART is an asynchronous serial communication protocol, meaning that it takes bytes of data and transmits the individual bits in a sequential fashion. Asynchronous transmission allows data to be transmitted without the sender having to send a clock signal to the receiver. Instead, the sender and receiver agree on timing parameters in advance and special bits called 'start bits' are added to each word and used to synchronize the sending and receiving units. UART is commonly used on the Raspberry Pi as a convenient way to control it over the GPIO, or access the kernel boot messages from the serial console (enabled by default).

Pins 8 and 10 (BCM (GPIO) mode -14 and 15, WiringPi mode - 15 and 16) are UART pins, designed for communicating with the Raspberry Pi using the serial port. 

It can also be used as a way to interface an Arduino, ESP8266, etc with your Raspberry Pi. Be careful with logic-levels between the devices though, for example the Raspberry Pi is 3.3V and the Arduino is 5V. Assuming you have WiringPi-Python installed, the following python example opens the Raspberry Pi's UART at 9600 baud and puts 'hello world'

import wiringpi

wiringpi.wiringPiSetup()

serial = wiringpi.serialOpen('/dev/ttyAMA0',9600)

wiringpi.serialPuts(serial,'hello world!')

2.5 SPI (Serial Peripheral Interface bus)

Known as the four-wire serial bus, SPI lets you daisy-chain multiple compatible devices off a single set of pins by assigning them different chip-select pins.

It allows devices to communicate with the Raspberry Pi synchronously, meaning much more data can pass between the master and slave devices. 

SPI0 pins in BCM mode are: 9 (MISO), 10 (MOSI), 11 (SCLK) + 7 (CE1) /8 (CE0)

SPI0 pins in WiringPi are: 12, 13, 14 + 10/11

SPI1 pins in BCM mode are: 20 (MOSI), 19 (MISO), 21 (SCLK) + 17 (CE1) / 18 (CE0) , 16 (CE2)

SPI1 pins in WiringPi are: 28, 24, 29 + 0/1, 27

To talk to an SPI device, you assert its corresponding chip-select pin. By default the Pi has CE0 and CE1.

import spidev

spi = spidev.SpiDev()

spi.open(0, CHIP_SELECT_0_OR_1)

spi.max_speed_hz = 1000000

spi.xfer([value_8bit])

The SPI protocol is not enabled as standard on Raspbian, but it can be enabled with the raspi-config tool, along with I2C.

2.6 I2C (Inter Integrated Circuit)

I2C pins in: BCM (GPIO) mode - 2 and 3; WiringPi - 8 and 9.

The Raspberry Pi's I2C pins are an extremely useful way to talk to many different types of external peripheral.

The I2C pins include a fixed 1.8 kohms pull-up resistor to 3.3v. This means they are not suitable for use as general purpose IO where a pull-up is not required.

You can verify the address of connected I2C peripherals with a simple one-liner:

  • sudo apt-get install i2c-tools
  • sudo i2cdetect -y 1

You can then access I2C from Python using the smbus library:

import smbus

DEVICE_BUS = 1

DEVICE_ADDR = 0x15

bus = smbus.SMBus(DEVICE_BUS)

bus.write_byte_data(DEVICE_ADDR, 0x00, 0x01)

Pins 27 and 28 (ID_SD (EEPROM SDA2) and ID_SC (EEPROM SCL2)) are also I2C. There are used by the Pi for internal functions, and also some HAT boards. 

2.7 SDIO - SD Card Interface

SDIO is the SD host/eMMC interface on the Raspberry Pi. SD host signals are normally used for the microSD slot.

These pins are "SD host" on Alt0 and "eMMC" on Alt3.

2.8 WiringPi

WiringPi is an attempt to bring Arduino-wiring-like simplicity to the Raspberry Pi.

The goal is to have a single common platform and set of functions for accessing the Raspberry Pi GPIO across multiple languages. WiringPi is a C library at heart, but it's available to both Ruby and Python users who can "gem install wiringpi" or "pip install WiringPi" respectively.

Python users note the 2 on the end, the WiringPi-Python library finally brings a whole host of existing WiringPi functionality to Python including brand new features from WiringPi 2.

WiringPi uses its own pin numbering scheme, here you'll learn how WiringPi numbers your GPIO pins, what those pins do and how to do shiny things with them from within Python or Ruby.

Installing to Python couldn't be easier, just type the command in the Terminal: sudo pip install WiringPi

For more information about WiringPi you should visit the official WiringPi website.

2.9 GPCLK - General Purpose CLock

General Purpose Clock pins can be set up to output a fixed frequency without any ongoing software control.

The following clock sources are available:

0     0 Hz     Ground

1     19.2 MHz oscillator

2     0 Hz     testdebug0

3     0 Hz     testdebug1

4     0 Hz     PLLA

5     1000 MHz PLLC (changes with overclock settings)

6     500 MHz  PLLD

7     216 MHz  HDMI auxiliary

8-15  0 Hz     Ground

Other frequencies can be achieved by setting a clock-divider in the form of SOURCE/(DIV_I + DIV_F/4096). Note, that the BCM2835 ARM Peripherals document contains an error and states that the denominator of the divider is 1024 instead of 4096.

2.10 PCM - Pulse-code Modulation

PCM (Pulse-code Modulation) is a digital representation of sampled analog. On the Raspberry Pi it's a form of digital audio output which can be understood by a DAC for high quality sound.

PCM Pins - BCM (GPIO) 18 (CLK), BCM (GPIO) 19 (FS), BCM (GPIO) 20 (DIN), BCM (GPIO) 21 (DOUT).

2.11 W1-GPIO - One-Wire Interface

To enable the one-wire interface you need to add the following line to /boot/config.txt, before rebooting your Pi:

dtoverlay=w1-gpio

or

dtoverlay=w1-gpio,gpiopin=x

if you would like to use a custom pin (default is BCM (GPIO) 4 (DATA) pin).

Alternatively you can enable the one-wire interface on demand using raspi-config, or the following: sudo modprobe w1-gpio

Newer kernels (4.9.28 and later) allow you to use dynamic overlay loading instead, including creating multiple 1-Wire busses to be used at the same time:

sudo dtoverlay w1-gpio gpiopin=4 pullup=0  # header pin 7

sudo dtoverlay w1-gpio gpiopin=17 pullup=0 # header pin 11

sudo dtoverlay w1-gpio gpiopin=27 pullup=0 # header pin 13

once any of the steps above have been performed, and discovery is complete you can list the devices that your Raspberry Pi has discovered via all 1-Wire busses (by default BCM4), like so: ls /sys/bus/w1/devices/

n.b. Using w1-gpio on the Raspberry Pi typically needs a 4.7 kΩ pull-up resistor connected between the GPIO pin and a 3.3v supply (e.g. header pin 1 or 17). Other means of connecting 1-Wire devices to the Raspberry Pi are also possible, such as using i2c to 1-Wire bridge chips.

2.12 DPI - Display Parallel Interface

One of the alternate functions selectable on bank 0 of the Raspbery Pi GPIO is DPI. DPI (Display Parallel Interface) is a 24-bit parallel interface with 28 clock and synchronisation signals.

DPI Pins:

BCM (GPIO) 2 VSYNC

BCM (GPIO) 3 HSYNC

BCM (GPIO) 4 Blue 0 D0

  • BCM (GPIO) 17 Green 5 D13
  • BCM (GPIO) 27 Red 7 D23
  • BCM (GPIO) 22 Red 2 D18
  • BCM (GPIO) 10 Blue 6 D6
  • BCM (GPIO) 9 Blue 5 D5
  • BCM (GPIO) 11 Blue 7 D7
  • BCM (GPIO) 0 CLK
  • BCM (GPIO) 5 Blue 1 D1
  • BCM (GPIO) 6 Blue 2 D2
  • BCM (GPIO) 13 Green 1 D9
  • BCM (GPIO) 19 Green 7 D15
  • BCM (GPIO) 26 Red 6 D22
  • BCM (GPIO) 14 Green 2 D10
  • BCM (GPIO) 15 Green 3 D11
  • BCM (GPIO) 18 Green 6 D14
  • BCM (GPIO) 23 Red 3 D19
  • BCM (GPIO) 24 Red 4 D20
  • BCM (GPIO) 25 Red 5 D21
  • BCM (GPIO) 8 Blue 4 D4
  • BCM (GPIO) 7 Blue 3 D3
  • BCM (GPIO) 1 DEN
  • BCM (GPIO) 12 Green 0 D8
  • BCM (GPIO) 16 Green 4 D12
  • BCM (GPIO) 20 Red 0 D16
  • BCM (GPIO) 21 Red 1 D17

This interface allows parallel RGB displays to be attached to the Raspberry Pi GPIO either in RGB24 (8 bits for red, green and blue) or RGB666 (6 bits per colour) or RGB565 (5 bits red, 6 green, and 5 blue). It is available as alternate function 2 (ALT2) on GPIO bank 0.

The pinout presented here is for the RGB24 mode, see url below for documentation of the RGB666 and RGB565 modes. More information about that you can find here.

2.13 JTAG - Joint Test Action Group

JTAG is generally refers to on-chip debugging interfaces that follow the IEEE 1149.x standard. The standard doesn’t mandate a certain connection – it just dictates a standard for communicating with chips in a device. It uses 5 pins: TCK, TMS, TDI, TDO and (options) TRST; which are (Test) Clock, Mode Select, Data In, Data Out, and Reset.

JTAG Pins:

  • BCM (GPIO) 4 (JTAG TDI)
  • BCM (GPIO) 27 (JTAG TMS)
  • BCM (GPIO) 22 (JTAG TRST)
  • BCM (GPIO) 5 (JTAG TDO)
  • BCM (GPIO) 6 (JTAG RTCK)
  • BCM (GPIO) 13 (JTAG TCK)
  • BCM (GPIO) 26 (JTAG TDI)
  • BCM (GPIO) 23 (JTAG RTCK)
  • BCM (GPIO) 24 (JTAG TDO)
  • BCM (GPIO) 25 (JTAG TCK)
  • BCM (GPIO) 12 (JTAG TMS)

It can be useful to hardware hackers in various ways, such as extracting device IDs, extracting firmware, overwriting memory.

3. BOARD or BCM? Which one to use?

Each pin has two numbers attached to it. Its BOARD number (the numbers in the circle) and its BCM (Broadcom SOC channel) number. You can choose which convention to use when you write your Python code:

  • 1. GPIO/BCM numbering: GPIO.setmode(GPIO.BCM)
  • 2. Board numbering: GPIO.setmode(GPIO.BOARD)

You can only use one convention in each DIY project, so select a one which makes most sense to you (the output is the same). It is worth noting however, that certain peripherals rely on GPIO/BCM numbering (RPi.GPIO and GPIO Zero).

The easiest way to control the GPIO pins is using the RPi.GPIO Python library.

4. Pinout command

A handy reference can be accessed on the Raspberry Pi by opening a Terminal window and running the command: pinout. This tool is provided by the GPIO Zero Python library, which it is installed by default on the Raspbian desktop image, but not on Raspbian Lite.


WiringPi - Wiring Pi pin number (shown as a tooltip), for Gordon Henderson's Wiring Pi library

Physical - Number corresponding to the pin's physical location on the header

Rev 1 Pi - Alternate BCM numbers for the original, 26-pin model "A" and "B" Pi

Understanding the Raspberry PI Zero W board

You can read more about it here.

Signals and connections of the Raspberry PI Zero W board


Step by Step instruction

1. Setup and preparation

We assume that you have Windows 10 installed on your PC and Raspbian OS installed on your Raspberry Pi Zero W board. 

  1. Do wiring.
  2. Insert your micro SD card with Raspbian OS into the TF card slot on the Raspberry Pi Zero W board. It will only fit one way.
  3. Connect Raspberry PI Zero W board mini HDMI port to your TV or Monitor HDMI (DVI) port (use HDMI cable and mini HDMI to HDMI adapter and/or HDMI to DVI adapter).
  4. Make sure that your monitor or TV is turned on, and that you have selected the right input (e.g. HDMI/DVI,  etc).
  5. Plug in micro USB 2.0 OTG Cable to USB data port of Pi Zero and 4-Port USB 2.0 Hub to micro USB 2.0 OTG Cable. 
  6. Plug in your USB mouse and USB keyboard to 4-Port USB 2.0 Hub.
  7. If you intend to connect your Raspberry Pi Zero vers 1.2 or vers 1.3 to the internet, connect a WiFi dongle to one of the 4-Port USB 2.0 Hub ports.
  8. Connect Micro USB power supply to Raspberry PI Zero board micro USB input.
  9. The Raspberry PI desktop will start up.
  10. Go to Raspberry icon-> Preferences -> Raspberry PI configuration
  11. Select Interfaces and make sure that I2C and SPI are enabled. If it’s not enabled, enable it and reboot your Raspberry PI 3 board to begin. We suggest to enable all settings and you will not need to come back to these configuration tool again.
  12. Open Terminal window and type the command: sudo apt-get update
  13. Then type the command: sudo apt-get upgrade -y
  14. Adding -y to the end of apt-get commands instructs the program to automatically answer yes to any questions rather than waiting for you to type Y or N.

2. How to install the rpi.gpio package manually

The RPi.GPIO Python library allows you to easily configure and read-write the input/output pins on the Raspberry Pi’s GPIO header within a Python script. This library is now included in the standard Raspbian image.

If you are using an up to date image you don’t need to install it but we keep these instructions in case you want to try a manually installation.

There are two methods to do it:

Method 1:

If the package exists in the Raspbian repository is can be installed using apt-get.

  1. Go to Terminal. Type the command to install the rpi.gpio package: sudo apt-get install rpi.gpio

Method 2:

The package is available from http://pypi.python.org/pypi/RPi.GPIO and the current version is 0.6.5 (November 2018). If this version is updated you will need to make appropriate changes to the version number in the commands below.

  1. Go to the Terminal. Type the command to download the library: wget https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.6.5.tar.gz
  2. Type the command to extract the archive to a new folder: tar -xvf RPi.GPIO-0.6.5.tar.gz
  3. Type the command to browse to the new directory: cd RPi.GPIO-0.6.5
  4. Type the command to install the library: sudo python setup.py install
  5. Type the command to remove the directory and archive file: cd ~
  6. sudo rm -rf RPi.GPIO-0.*

Example of script:

import RPi.GPIO as GPIO

# to use Raspberry Pi board pin numbers

GPIO.setmode(GPIO.BOARD)

# set up the GPIO channels - one input and one output

GPIO.setup(11, GPIO.IN)

GPIO.setup(12, GPIO.OUT)

# input from pin 11

input_value = GPIO.input(11)

# output to pin 12

GPIO.output(12, GPIO.HIGH)

# the same script as above but using BCM GPIO 00..nn numbers

GPIO.setmode(GPIO.BCM)

GPIO.setup(17, GPIO.IN)

GPIO.setup(18, GPIO.OUT)

input_value = GPIO.input(17)

GPIO.output(18, GPIO.HIGH)

3. Another option to enable I2C and SPI

  1. You can also enable I2C and SPI in raspi-config tool. Just type the command in Terminalsudo raspi-config then go to 5 Interfacing Options and enable I2C and SPI in each submenu.

Summary

We have learnt everything we need to know about the Raspberry Pi’s GPIO pins: what they can do, how to use them, and mistakes to avoid while using them.

Libraries in use

  • None

Script

  • None


Other projects of Acoptex.com

« Go back to category
Is this project fake? Report it!   
Recommend to a friend
Published at 19-04-2019
Viewed: 399 times