Raspberry Pi Pico
Touch pHAT CAP1176

The Touch pHAT is a breakout board intended for the Raspberry Pi. It has 6 capacitive touch sensors and 6 LEDs. The LEDs are white and mounted underneath the board. You see a yellowy green glow when you light them up. The board uses a CAP1166 integrated circuit to do the sensing and to to control the lights. This is done using the i2c protocol.

The Touch pHAT is made by the UK electronics company, Pimoroni. At the time of writing, it is sold for £8.40.

In the photograph I am using an additional accessory to simplify the process of connecting to the touch pHAT. The one I am using is made by Waveshare and has a set of male headers for attaching Raspberry Pi accessories, another set of male headers for connecting Pico accessories and a few other features. You can always connect to the touch pHAT by poking in some jumpers to the correct pins or via a Raspberry Pi GPIO extender.

Pico Circuit

My connections are GP2 and GP3 to pins 2 and 3 on the pHAT. You also need to connect to the 5V, 3V3 and GND pins of the pHAT. You can see a diagram showing the pHAT pins at Gadgetoid's superb Pinout site, https://pinout.xyz/pinout/touch_phat.

I wrote a separate module for working with the touch pHAT and saved it on the Pico as touch.py.

from machine import Pin, I2C
from time import sleep_ms
from micropython import const

ADDRESS = const(0x2c)


class touchphat:
    def __init__(self, i, d, c):
        self.i2c = I2C(i,sda=Pin(d), scl = Pin(c))
        self.LEDTRACK = True
        self.i2c.writeto_mem(ADDRESS, 0x72, b'\x00')
        sleep_ms(1)
        self.i2c.writeto_mem(ADDRESS, 0x41, b'\x30')
        sleep_ms(1)
    
    def set_led_tracking(self, led_follow):
        self.LEDTRACK = led_follow
    
    def set_leds(self, led_byte):
        self.i2c.writeto(ADDRESS, bytes([0x74,led_byte]))
    
    def read_pads(self):
        self.i2c.writeto_mem(ADDRESS, 0 , b'\x00')
        data = self.i2c.readfrom_mem(ADDRESS, 0x03, 1)
        if self.LEDTRACK:
            pattern = sum(1<<(5-i) for i in range(6) if data[0]>>i&1)
            led_state = self.i2c.readfrom_mem(ADDRESS, 0x74, 1)
            self.set_leds(pattern)
        return data[0]

I saved this as my main.py program to test things out,

from touch import touchphat  
    
last = 0            
tphat = touchphat(1,2,3)
btns = ["<", "A", "B", "C", "D", ">"]
while True:
    reading = tphat.read_pads()
    if reading!=0 and reading!=last:
        bits = [reading >> i & 1 for i in range(5,-1,-1)][::-1]
        print(btns[bits.index(1)], "pressed.")
    last = reading