Source: GPIOs Inputs and Outputs

Overview

The Raspberry Pi (and reTerminal) expose 40 GPIO pins that can be configured as input or output in software.

  • 2× 5 V pins, 2× 3.3 V pins, multiple GND pins (not configurable)
  • All remaining GPIO pins: 3.3 V logic (outputs set to 3.3 V, inputs are 3.3 V-tolerant)

Run pinout in terminal to see the GPIO reference map (provided by GPIO Zero).

Outputs

  • Set high (3.3 V) or low (0 V)

Inputs

  • Read as high (3.3 V) or low (0 V)

No Native Analog (ADC)

The Raspberry Pi does NOT have a built-in ADC.

To read analog signals, use an external ADC — in this course: Grove Base Hat for Raspberry Pi.

Grove Base Hat

  • 4 analog (ADC) connectors — each with 12-bit resolution (0–4095)
  • Also exposes: 6 digital, 3 I2C, 1 PWM, 1 UART connectors
  • Adds-on (HAT) to the Raspberry Pi 40-pin header

Digital Communication Protocols on GPIO

Available on GPIO pins (some on all pins, some on specific pins):

ProtocolTypeNotes
PWMDigital (simulated analog)Pulse-width modulation
SPISynchronous serialController/peripheral, separate clock
I2CSynchronous serial2 wires, multiple devices
Serial (UART)Asynchronous serialTX/RX, point-to-point
PCMPulse-code modulationAudio

Permissions

User must be in the gpio group to use GPIO:

sudo usermod -a -G gpio <username>

(default user is already in the group)

GPIO Zero Python Library

from gpiozero import LED, Button, PWMLED, Servo
from time import sleep

LED (GPIO 17)

led = LED(17)
led.on()
led.off()
led.toggle()
led.blink()   # blinks indefinitely

Button (GPIO 2)

button = Button(2)
if button.is_pressed:
    print("Pressed")
 
# Callbacks
button.when_pressed = led.on
button.when_released = led.off
 
# Blocking
button.wait_for_press()
button.wait_for_release()

Button + LED combined

from gpiozero import LED, Button
led = LED(17)
button = Button(2)
while True:
    if button.is_pressed:
        led.on()
    else:
        led.off()

See Also