It's easy to use the STEMMA Analog SPDT Switch with CircuitPython and the digitalio and analogio core modules. These modules allow you to easily write Python code for accessing basic digital or analog inputs and outputs.
CircuitPython Microcontroller Wiring
First wire up the switch to your board exactly as follows. The following is the switch wired to a Feather RP2040 (shown using a JST PH cable). You'll connect your two external analog inputs to NO and NC.
-
Board 3V to switch JST PH VIN (red wire)
-
Board GND to switch JST PH GND (black wire)
-
Board pin 5 to switch JST PH SIG (white wire)
- Board pin A1 to switch terminal block common (yellow wire)
- Analog input 1 to switch terminal block NO (blue wire)
- Analog input 2 to switch terminal block NC (green wire)
The following is the switch wired to a Feather RP2040 using a solderless breadboard:
-
Board 3V to switch VIN (red wire)
-
Board GND to switch GND (black wire)
-
Board pin 5 to switch SIG (white wire)
- Board pin A1 to switch common (yellow wire)
- Analog input 1 to switch NO (blue wire)
- Analog input 2 to switch NC (green wire)
CircuitPython Usage
To use with CircuitPython, you need to update code.py with the example script.
In the example below, click the Download Project Bundle button below to download the code.py file in a zip file. Extract the contents of the zip file, and copy the code.py file to your CIRCUITPY drive.

Example Code
If running CircuitPython: Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries # SPDX-License-Identifier: MIT import time import board from digitalio import DigitalInOut, Direction from analogio import AnalogIn analog_in = AnalogIn(board.A1) switch = DigitalInOut(board.D5) switch.direction = Direction.OUTPUT switch_time = 2 clock = time.monotonic() while True: if (time.monotonic() - clock) > switch_time: switch.value = not switch.value print(switch.value) clock = time.monotonic() print((analog_in.value,)) time.sleep(0.1)
Every 2 seconds, the analog output from the common pin will be switched by pin 5 toggling high or low. You can see this in action with Mu, by opening the Plotter window in the REPL.
Page last edited January 22, 2025
Text editor powered by tinymce.