23. API - Pi Information

The GPIO Zero library also contains a database of information about the various revisions of the Raspberry Pi computer. This is used internally to raise warnings when non-physical pins are used, or to raise exceptions when pull-downs are requested on pins with physical pull-up resistors attached. The following functions and classes can be used to query this database:

23.1. pi_info

gpiozero.pi_info(revision=None)[source]

Returns a PiBoardInfo instance containing information about a revision of the Raspberry Pi.

Parameters:revision (str) – The revision of the Pi to return information about. If this is omitted or None (the default), then the library will attempt to determine the model of Pi it is running on and return information about that.

23.2. PiBoardInfo

class gpiozero.PiBoardInfo[source]

This class is a namedtuple() derivative used to represent information about a particular model of Raspberry Pi. While it is a tuple, it is strongly recommended that you use the following named attributes to access the data contained within. The object can be used in format strings with various custom format specifications:

from gpiozero import *

print('{0}'.format(pi_info()))
print('{0:full}'.format(pi_info()))
print('{0:board}'.format(pi_info()))
print('{0:specs}'.format(pi_info()))
print('{0:headers}'.format(pi_info()))

“color” and “mono” can be prefixed to format specifications to force the use of ANSI color codes. If neither is specified, ANSI codes will only be used if stdout is detected to be a tty:

print('{0:color board}'.format(pi_info())) # force use of ANSI codes
print('{0:mono board}'.format(pi_info())) # force plain ASCII
physical_pin(function)[source]

Return the physical pin supporting the specified function. If no pins support the desired function, this function raises PinNoPins. If multiple pins support the desired function, PinMultiplePins will be raised (use physical_pins() if you expect multiple pins in the result, such as for electrical ground).

Parameters:function (str) – The pin function you wish to search for. Usually this is something like “GPIO9” for Broadcom GPIO pin 9.
physical_pins(function)[source]

Return the physical pins supporting the specified function as tuples of (header, pin_number) where header is a string specifying the header containing the pin_number. Note that the return value is a set which is not indexable. Use physical_pin() if you are expecting a single return value.

Parameters:function (str) – The pin function you wish to search for. Usually this is something like “GPIO9” for Broadcom GPIO pin 9, or “GND” for all the pins connecting to electrical ground.
pprint(color=None)[source]

Pretty-print a representation of the board along with header diagrams.

If color is None (the default), the diagram will include ANSI color codes if stdout is a color-capable terminal. Otherwise color can be set to True or False to force color or monochrome output.

pulled_up(function)[source]

Returns a bool indicating whether a physical pull-up is attached to the pin supporting the specified function. Either PinNoPins or PinMultiplePins may be raised if the function is not associated with a single pin.

Parameters:function (str) – The pin function you wish to determine pull-up for. Usually this is something like “GPIO9” for Broadcom GPIO pin 9.
to_gpio(spec)[source]

Parses a pin spec, returning the equivalent Broadcom GPIO port number or raising a ValueError exception if the spec does not represent a GPIO port.

The spec may be given in any of the following forms:

  • An integer, which will be accepted as a GPIO number
  • ‘GPIOn’ where n is the GPIO number
  • ‘WPIn’ where n is the wiringPi pin number
  • ‘BCMn’ where n is the GPIO number (alias of GPIOn)
  • ‘BOARDn’ where n is the physical pin number on the main header
  • ‘h:n’ where h is the header name and n is the physical pin number (for example J8:5 is physical pin 5 on header J8, which is the main header on modern Raspberry Pis)
revision

A string indicating the revision of the Pi. This is unique to each revision and can be considered the “key” from which all other attributes are derived. However, in itself the string is fairly meaningless.

model

A string containing the model of the Pi (for example, “B”, “B+”, “A+”, “2B”, “CM” (for the Compute Module), or “Zero”).

pcb_revision

A string containing the PCB revision number which is silk-screened onto the Pi (on some models).

Note

This is primarily useful to distinguish between the model B revision 1.0 and 2.0 (not to be confused with the model 2B) which had slightly different pinouts on their 26-pin GPIO headers.

released

A string containing an approximate release date for this revision of the Pi (formatted as yyyyQq, e.g. 2012Q1 means the first quarter of 2012).

soc

A string indicating the SoC (system on a chip) that this revision of the Pi is based upon.

manufacturer

A string indicating the name of the manufacturer (usually “Sony” but a few others exist).

memory

An integer indicating the amount of memory (in Mb) connected to the SoC.

Note

This can differ substantially from the amount of RAM available to the operating system as the GPU’s memory is shared with the CPU. When the camera module is activated, at least 128Mb of RAM is typically reserved for the GPU.

storage

A string indicating the type of bootable storage used with this revision of Pi, e.g. “SD”, “MicroSD”, or “eMMC” (for the Compute Module).

usb

An integer indicating how many USB ports are physically present on this revision of the Pi, of any type.

Note

This does not include the micro-USB or USB-C port used to power the Pi.

usb3

An integer indicating how many of the USB ports are USB3 ports on this revision of the Pi.

ethernet

An integer indicating how many Ethernet ports are physically present on this revision of the Pi.

eth_speed

An integer indicating the maximum speed (in Mbps) of the Ethernet ports (if any). If no Ethernet ports are present, this is 0.

wifi

A bool indicating whether this revision of the Pi has wifi built-in.

bluetooth

A bool indicating whether this revision of the Pi has bluetooth built-in.

csi

An integer indicating the number of CSI (camera) ports available on this revision of the Pi.

dsi

An integer indicating the number of DSI (display) ports available on this revision of the Pi.

headers

A dictionary which maps header labels to HeaderInfo tuples. For example, to obtain information about header P1 you would query headers['P1']. To obtain information about pin 12 on header J8 you would query headers['J8'].pins[12].

A rendered version of this data can be obtained by using the PiBoardInfo object in a format string:

from gpiozero import *
print('{0:headers}'.format(pi_info()))
board

An ASCII art rendition of the board, primarily intended for console pretty-print usage. A more usefully rendered version of this data can be obtained by using the PiBoardInfo object in a format string. For example:

from gpiozero import *
print('{0:board}'.format(pi_info()))

23.3. HeaderInfo

class gpiozero.HeaderInfo[source]

This class is a namedtuple() derivative used to represent information about a pin header on a board. The object can be used in a format string with various custom specifications:

from gpiozero import *

print('{0}'.format(pi_info().headers['J8']))
print('{0:full}'.format(pi_info().headers['J8']))
print('{0:col2}'.format(pi_info().headers['P1']))
print('{0:row1}'.format(pi_info().headers['P1']))

“color” and “mono” can be prefixed to format specifications to force the use of ANSI color codes. If neither is specified, ANSI codes will only be used if stdout is detected to be a tty:

print('{0:color row2}'.format(pi_info().headers['J8'])) # force use of ANSI codes
print('{0:mono row2}'.format(pi_info().headers['P1'])) # force plain ASCII

The following attributes are defined:

pprint(color=None)[source]

Pretty-print a diagram of the header pins.

If color is None (the default, the diagram will include ANSI color codes if stdout is a color-capable terminal). Otherwise color can be set to True or False to force color or monochrome output.

name

The name of the header, typically as it appears silk-screened on the board (e.g. “P1” or “J8”).

rows

The number of rows on the header.

columns

The number of columns on the header.

pins

A dictionary mapping physical pin numbers to PinInfo tuples.

23.4. PinInfo

class gpiozero.PinInfo[source]

This class is a namedtuple() derivative used to represent information about a pin present on a GPIO header. The following attributes are defined:

number

An integer containing the physical pin number on the header (starting from 1 in accordance with convention).

function

A string describing the function of the pin. Some common examples include “GND” (for pins connecting to ground), “3V3” (for pins which output 3.3 volts), “GPIO9” (for GPIO9 in the Broadcom numbering scheme), etc.

pull_up

A bool indicating whether the pin has a physical pull-up resistor permanently attached (this is usually False but GPIO2 and GPIO3 are usually True). This is used internally by gpiozero to raise errors when pull-down is requested on a pin with a physical pull-up resistor.

row

An integer indicating on which row the pin is physically located in the header (1-based)

col

An integer indicating in which column the pin is physically located in the header (1-based)