Simple test¶
Ensure your device works with this simple test.
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
9import board
10import busio
11
12# Import MPR121 module.
13import adafruit_mpr121
14
15# Create I2C bus.
16i2c = busio.I2C(board.SCL, board.SDA)
17
18# Create MPR121 object.
19mpr121 = adafruit_mpr121.MPR121(i2c)
20
21# Note you can optionally change the address of the device:
22# mpr121 = adafruit_mpr121.MPR121(i2c, address=0x91)
23
24# Loop forever testing each input and printing when they're touched.
25while True:
26 # Loop through all 12 inputs (0-11).
27 for i in range(12):
28 # Call is_touched and pass it then number of the input. If it's touched
29 # it will return True, otherwise it will return False.
30 if mpr121[i].value:
31 print("Input {} touched!".format(i))
32 time.sleep(0.25) # Small delay to keep from spamming output messages.
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
18# Configure PWM buzzer and other state:
19BUZZER_PIN = board.D9
20TONE_ON_DUTY = 2**15 # Duty cycle of tone when turned on, a square wave.
21TONE_OFF_DUTY = 0 # Duty cycle of tone when turned off, 0 or no signal.
22NOTE_FREQS = [
23 261, # Input 0 = 261 hz = middle C
24 294, # Input 1 = middle D
25 329, # Input 2 = middle E
26 349, # Input 3 = middle F
27 392, # Input 4 = middle G
28 440, # Input 5 = middle A
29 493, # Input 6 = middle B
30 0, # Input 7 = nothing (set to a frequency in hertz!)
31 0, # Input 8
32 0, # Input 9
33 0, # Input 10
34 0,
35] # Input 11
36
37
38# Create I2C bus.
39i2c = busio.I2C(board.SCL, board.SDA)
40
41# Create MPR121 class.
42mpr121 = adafruit_mpr121.MPR121(i2c)
43# Note you can optionally change the address of the device:
44# mpr121 = adafruit_mpr121.MPR121(i2c, address=0x91)
45
46# pylint: disable-msg=no-member
47# Setup buzzer PWM output.
48buzzer = pwmio.PWMOut(
49 BUZZER_PIN, duty_cycle=TONE_OFF_DUTY, frequency=440, variable_frequency=True
50)
51# pylint: disable-msg=no-member
52
53last_note = None
54while True:
55 # Get touched state for all pins
56 touched = mpr121.touched_pins
57 # If no pins are touched, be quiet
58 if True not in touched:
59 last_note = None
60 buzzer.duty_cycle = TONE_OFF_DUTY
61 continue
62 # Get index of touched pin
63 note = touched.index(True)
64 # Play note if pin is different and has a defined note
65 if note != last_note and NOTE_FREQS[note] != 0:
66 last_note = note
67 buzzer.frequency = NOTE_FREQS[note]
68 buzzer.duty_cycle = TONE_ON_DUTY