🖥️ Display LCD 16×2 (I²C)
Apresentar texto e valores num LCD via interface I²C com o módulo PCF8574. Apenas 4 fios — VCC, GND, SDA e SCL. Biblioteca RPLCD.
Ativar I²C no RPi
Terminal
# Ativar I²C via raspi-config sudo raspi-config # → Interfacing Options → I2C → Enable # Instalar biblioteca pip3 install RPLCD smbus2 # Verificar endereço I²C do LCD (normalmente 0x27 ou 0x3F) i2cdetect -y 1
Circuito
LCD VCC → 5V (pino 2)
LCD GND → GND (pino 6)
LCD SDA → GPIO 2 / SDA (pino 3)
LCD SCL → GPIO 3 / SCL (pino 5)
Exercício 1 — "Olá Mundo"
Python · lcd_ola.py
from RPLCD.i2c import CharLCD # Inicializar LCD no endereço 0x27, porta I2C 1 lcd = CharLCD('PCF8574', 0x27) lcd.clear() lcd.write_string('Ola, RPi!') lcd.cursor_pos = (1, 0) # linha 2, coluna 0 lcd.write_string('Kit RPi LED PT')
Exercício 2 — Mostrar temperatura do DHT11
Python · lcd_temp.py
from RPLCD.i2c import CharLCD import adafruit_dht, board, time lcd = CharLCD('PCF8574', 0x27) sensor = adafruit_dht.DHT11(board.D4) try: while True: try: t = sensor.temperature h = sensor.humidity lcd.clear() lcd.write_string(f'Temp: {t:.1f} \xdfC') lcd.cursor_pos = (1, 0) lcd.write_string(f'Humid: {h:.0f} %') except RuntimeError: pass time.sleep(2) except KeyboardInterrupt: lcd.clear() sensor.exit()
Endereço I²C: Se o
i2cdetect -y 1 mostrar 0x3F em vez de 0x27, altera o endereço na inicialização do LCD. Alguns módulos PCF8574A usam 0x3F.