Skip to content

Instantly share code, notes, and snippets.

@k0d
Last active May 15, 2020 09:44
Show Gist options
  • Save k0d/fccd52347b89e702dc7223264b1a7c14 to your computer and use it in GitHub Desktop.
Save k0d/fccd52347b89e702dc7223264b1a7c14 to your computer and use it in GitHub Desktop.
mpr121-threshold
# Simple test of the MPR121 capacitive touch sensor library.
# Will print out a message when any of the 12 capacitive touch inputs of the
# board are touched. Open the serial REPL after running to see the output.
# Author: Tony DiCola
import time
import board
import busio
# Import MPR121 module.
import adafruit_mpr121
# Create I2C bus.
i2c = busio.I2C(board.SCL, board.SDA)
# Create MPR121 object.
mpr121 = adafruit_mpr121.MPR121(i2c)
# Note you can optionally change the address of the device:
# mpr121 = adafruit_mpr121.MPR121(i2c, address=0x91)
for i in range(12):
mpr121[i].threshold = 1 # I don't know what the value should be!
mpr121[i].release_threshold = 1 # I don't know what the value should be!
# Loop forever testing each input and printing when they're touched.
while True:
# Loop through all 12 inputs (0-11).
for i in range(12):
# Call is_touched and pass it then number of the input. If it's touched
# it will return True, otherwise it will return False.
if mpr121[i].value:
print("Input {} touched!".format(i))
time.sleep(0.25) # Small delay to keep from spamming output messages.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment