Source: Serial Protocols — Connected Objects

Overview

GPIO pins on the reTerminal support specialized digital communication protocols beyond simple input/output.

Serial Communication Basics

Serial interfaces stream data one bit at a time, using 1–4 wires.

TypeClock wireDevices
SynchronousYes (shared clock)SPI, I2C, USB
AsynchronousNoSerial (UART) — “Serial” usually means this

Asynchronous Serial (UART)

  • No clock wire — both devices must agree on baud rate
  • Typically point-to-point (2 devices only)
  • Raspberry Pi built-in UART: GPIO14 (TX), GPIO15 (RX)
  • Device: /dev/ttyS0 or /dev/serial0

pySerial (Python)

import serial
 
ser = serial.Serial('/dev/ttyS0')       # open serial port (default 9600 baud)
print(ser.name)                          # check port name
ser.write(b'hello')                      # write bytes
ser.close()
 
# Context manager pattern:
with serial.Serial('/dev/serial0', 9600) as ser:
    while True:
        line = ser.readline()            # read until '\n'
        print(f'Line: {line}')
        if line == b'hello\n':
            break
    ser.write(b'there\n')

Pico → reTerminal Serial Demo (MicroPython on Pico side)

from machine import UART, Pin
uart0 = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))
uart0.write('hello\n')

SPI (Serial Peripheral Interface)

  • Synchronous — has a clock line (SCK)
  • Controller selects peripheral via CS line (active low)
  • Full-duplex: data flows both ways simultaneously

Signal Names (current)

SignalMeaning
SCKClock (generated by controller)
COPIController-Out Peripheral-In
CIPOController-In Peripheral-Out
CSChip Select (one per peripheral, active low)

(Old names: MOSI/MISO/SS — deprecated)

  • Multiple peripherals: each needs its own CS line; controller keeps non-selected CS high

I2C (Inter-Integrated Circuit)

  • Synchronous, only 2 wires (SDA data + SCL clock)
  • Supports up to 1008 peripheral devices on the same bus
  • Each device has a unique hexadecimal address
  • Slower than SPI but simpler wiring

Detect I2C devices

i2cdetect -y 1
# Shows hex addresses of connected devices (UU = in use by kernel driver)

Communication frames

  1. Address frame: controller announces target device hex address
  2. Data frames: 8-bit data messages (controller ↔ peripheral)

I2C devices in this course

  • AHT20 temperature & humidity sensor
  • reTerminal accelerometer
  • reTerminal light sensor
  • reTerminal LCD driver

smbus2 Python Library

from smbus2 import SMBus
 
with SMBus(1) as bus:
    b = bus.read_byte_data(80, 0)     # read 1 byte from address 80, offset 0
    print(b)
    bus.write_byte_data(80, 0, 45)    # write byte 45 to address 80, offset 0

In practice, use device-specific libraries (e.g. grove_temperature_humidity_aht20.py)


PWM (Pulse Width Modulation)

  • Digital signal that simulates analog by varying the duty cycle
  • Signal is always high (3.3 V / 5 V) or low (GND); never in between
  • Duty cycle = % of time signal is HIGH in one period

Common applications

  • Servo motors: pulse width determines arm position
  • LED dimming: rapid on/off fools the eye into seeing lower brightness

GPIO Zero PWM

from gpiozero import PWMLED, Servo
from time import sleep
 
# LED brightness (0 = off, 1 = full)
led = PWMLED(17)
led.value = 0     # off
led.value = 0.5   # half brightness
led.value = 1     # full brightness
 
# Servo position
servo = Servo(17)
servo.min()   # sleep(2)
servo.mid()   # sleep(2)
servo.max()   # sleep(2)

Protocol Comparison

PropertySerial (UART)SPII2CPWM
Wires2 (TX, RX)4+ (SCK, COPI, CIPO, CS×n)2 (SDA, SCL)1
ClockNo (async)Yes (sync)Yes (sync)N/A
Max devices2Many (1 CS per device)10081
SpeedSlowFastMediumN/A
ComplexityLowMediumMediumLow

See Also