Skip to content

Instantly share code, notes, and snippets.

@idriszmy
Last active September 10, 2021 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idriszmy/67ee18da693ac754e7fdb4719cd9e21f to your computer and use it in GitHub Desktop.
Save idriszmy/67ee18da693ac754e7fdb4719cd9e21f to your computer and use it in GitHub Desktop.
Demo code for TTP229L 16-Channel Touch HAT using CircuitPython/Blinka libraries on Raspberry Pi
"""
Hardware
- Raspberry Pi 4 Model B
https://my.cytron.io/p-raspberry-pi-4-model-b?tracking=idris
- Raspberry Pi Approved MakerDisk microSD Card with RPi OS
https://my.cytron.io/p-raspberry-pi-approved-makerdisk-microsd-with-rpi-os??tracking=idris
- TTP229L 16-Channel Touch HAT for Raspberry Pi
https://my.cytron.io/p-ttp229l-16-channel-touch-hat-for-raspberry-pi?tracking=idris
- Official RPi 15W (5V/3A) PSU USB C UK Plug
https://my.cytron.io/c-power-for-rpi-4/p-official-rpi-15w-5v-3a-psu-usb-c-uk-plug-white?tracking=idris
Step 1:
- Install CircuitPython Libraries on Raspberry Pi
https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi
Step 2:
- Install Adafruit CircuitPython BusDevice
pip3 install adafruit-circuitpython-busdevice
Step 3:
- Run the code
Last update on 10 Sept 2021, by Idris
"""
import time
import board
import busio
from adafruit_bus_device.i2c_device import I2CDevice
i2c = board.I2C()
ADDRESS = 0x57
ttp229l = I2CDevice(i2c, ADDRESS)
touchpad = [
"Right", "Up", "Left", "Plus", "Power", "Raspberry Pi", "keyestudio",
"Triangle", "Square", "Circle", "Cross", "R", "Down", "L", "Minus",
"Not detected"
]
def read_touch_hat():
bytes_read = bytearray(2)
ttp229l.readinto(bytes_read)
data = bytes_read[1] << 8 | bytes_read[0]
#print("TTP229: {:016b}".format(data))
for i in range(16):
if data >> i == 1:
break
return i
while True:
pad_index = read_touch_hat()
if touchpad[pad_index] != "Not detected":
print(touchpad[pad_index])
time.sleep(0.2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment