code.py
 1  # SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
 2  # SPDX-License-Identifier: MIT
 3  """
 4  CircuitPython Touch-Compatible Pin Identification Script
 5  
 6  Depending on the order of the pins in the CircuitPython pin definition, some inaccessible pins
 7  may be returned in the script results. Consult the board schematic and use your best judgement.
 8  
 9  In some cases, such as LED, the associated pin, such as D13, may be accessible. The LED pin
10  name is first in the list in the pin definition, and is therefore printed in the results. The
11  pin name "LED" will work in code, but "D13" may be more obvious. Use the schematic to verify.
12  """
13  import board
14  import touchio
15  from microcontroller import Pin
16  
17  
18  def get_pin_names():
19      """
20      Gets all unique pin names available in the board module, excluding a defined list.
21      This list is not exhaustive, and depending on the order of the pins in the CircuitPython
22      pin definition, some of the pins in the list may still show up in the script results.
23      """
24      exclude = [
25          "NEOPIXEL",
26          "APA102_MOSI",
27          "APA102_SCK",
28          "LED",
29          "NEOPIXEL_POWER",
30          "BUTTON",
31          "BUTTON_UP",
32          "BUTTON_DOWN",
33          "BUTTON_SELECT",
34          "DOTSTAR_CLOCK",
35          "DOTSTAR_DATA",
36          "IR_PROXIMITY",
37          "SPEAKER_ENABLE",
38          "BUTTON_A",
39          "BUTTON_B",
40          "POWER_SWITCH",
41          "SLIDE_SWITCH",
42          "TEMPERATURE",
43          "ACCELEROMETER_INTERRUPT",
44          "ACCELEROMETER_SDA",
45          "ACCELEROMETER_SCL",
46          "MICROPHONE_CLOCK",
47          "MICROPHONE_DATA",
48      ]
49      pins = [
50          pin
51          for pin in [getattr(board, p) for p in dir(board) if p not in exclude]
52          if isinstance(pin, Pin)
53      ]
54      pin_names = []
55      for pin_object in pins:
56          if pin_object not in pin_names:
57              pin_names.append(pin_object)
58      return pin_names
59  
60  
61  for possible_touch_pin in get_pin_names():  # Get the pin name.
62      try:
63          touch_pin_object = touchio.TouchIn(
64              possible_touch_pin
65          )  # Attempt to create the touch object on each pin.
66          # Print the touch-capable pins that do not need, or already have, an external pulldown.
67          print("Touch on:", str(possible_touch_pin).replace("board.", ""))
68      except ValueError as error:  # A ValueError is raised when a pin is invalid or needs a pulldown.
69          # Obtain the message associated with the ValueError.
70          error_message = getattr(error, "message", str(error))
71          if (
72              "pulldown" in error_message  # If the ValueError is regarding needing a pulldown...
73          ):
74              print(
75                  "Touch (no pulldown) on:", str(possible_touch_pin).replace("board.", "")
76              )
77          else:
78              print("No touch on:", str(possible_touch_pin).replace("board.", ""))
79      except TypeError:  # Error returned when checking a non-pin object in dir(board).
80          pass  # Passes over non-pin objects in dir(board).