Simple test
Ensure your device works with this simple test.
examples/mpr121_simpletest.py
1# SPDX-FileCopyrightText: 2017 Tony DiCola for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# Simple test of the MPR121 capacitive touch sensor library.
5# Will print out a message when any of the 12 capacitive touch inputs of the
6# board are touched. Open the serial REPL after running to see the output.
7# Author: Tony DiCola
8import time
9
10import board
11import busio
12
13# Import MPR121 module.
14import adafruit_mpr121
15
16# Create I2C bus.
17i2c = busio.I2C(board.SCL, board.SDA)
18
19# Create MPR121 object.
20mpr121 = adafruit_mpr121.MPR121(i2c)
21
22# Note you can optionally change the address of the device:
23# mpr121 = adafruit_mpr121.MPR121(i2c, address=0x91)
24
25# Loop forever testing each input and printing when they're touched.
26while True:
27 # Loop through all 12 inputs (0-11).
28 for i in range(12):
29 # Call is_touched and pass it then number of the input. If it's touched
30 # it will return True, otherwise it will return False.
31 if mpr121[i].value:
32 print(f"Input {i} touched!")
33 time.sleep(0.25) # Small delay to keep from spamming output messages.
examples/mpr121_piano.py
1# SPDX-FileCopyrightText: 2017 Tony DiCola, Carter Nelson for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# MPR121 piano demo.
5# Listens to the first 7 inputs of the MPR121 and plays a middle scale note
6# when an input is touched. Note only one note is played at a time!
7# For use with microcontrollers or computers with PWM support only!
8
9
10import board
11import busio
12import pwmio
13
14# Import MPR121 module.
15import adafruit_mpr121
16
17# Configure PWM buzzer and other state:
18BUZZER_PIN = board.D9
19TONE_ON_DUTY = 2**15 # Duty cycle of tone when turned on, a square wave.
20TONE_OFF_DUTY = 0 # Duty cycle of tone when turned off, 0 or no signal.
21NOTE_FREQS = [
22 261, # Input 0 = 261 hz = middle C
23 294, # Input 1 = middle D
24 329, # Input 2 = middle E
25 349, # Input 3 = middle F
26 392, # Input 4 = middle G
27 440, # Input 5 = middle A
28 493, # Input 6 = middle B
29 0, # Input 7 = nothing (set to a frequency in hertz!)
30 0, # Input 8
31 0, # Input 9
32 0, # Input 10
33 0,
34] # Input 11
35
36
37# Create I2C bus.
38i2c = busio.I2C(board.SCL, board.SDA)
39
40# Create MPR121 class.
41mpr121 = adafruit_mpr121.MPR121(i2c)
42# Note you can optionally change the address of the device:
43# mpr121 = adafruit_mpr121.MPR121(i2c, address=0x91)
44
45# Setup buzzer PWM output.
46buzzer = pwmio.PWMOut(BUZZER_PIN, duty_cycle=TONE_OFF_DUTY, frequency=440, variable_frequency=True)
47
48last_note = None
49while True:
50 # Get touched state for all pins
51 touched = mpr121.touched_pins
52 # If no pins are touched, be quiet
53 if True not in touched:
54 last_note = None
55 buzzer.duty_cycle = TONE_OFF_DUTY
56 continue
57 # Get index of touched pin
58 note = touched.index(True)
59 # Play note if pin is different and has a defined note
60 if note != last_note and NOTE_FREQS[note] != 0:
61 last_note = note
62 buzzer.frequency = NOTE_FREQS[note]
63 buzzer.duty_cycle = TONE_ON_DUTY