mpr121_simpletest.py
1 # Simple test of the MPR121 capacitive touch sensor library. 2 # Will print out a message when any of the 12 capacitive touch inputs of the 3 # board are touched. Open the serial REPL after running to see the output. 4 # Author: Tony DiCola 5 import time 6 import board 7 import busio 8 9 # Import MPR121 module. 10 import adafruit_mpr121 11 12 # Create I2C bus. 13 i2c = busio.I2C(board.SCL, board.SDA) 14 15 # Create MPR121 object. 16 mpr121 = adafruit_mpr121.MPR121(i2c) 17 18 # Note you can optionally change the address of the device: 19 # mpr121 = adafruit_mpr121.MPR121(i2c, address=0x91) 20 21 # Loop forever testing each input and printing when they're touched. 22 while True: 23 # Loop through all 12 inputs (0-11). 24 for i in range(12): 25 # Call is_touched and pass it then number of the input. If it's touched 26 # it will return True, otherwise it will return False. 27 if mpr121[i].value: 28 print("Input {} touched!".format(i)) 29 time.sleep(0.25) # Small delay to keep from spamming output messages.