Digital inputs let you interface with the board. This example adds a button input to the Arduino RP2040 Connect. When you press the button, the onboard LED will turn on. Additionally, while the button is pressed, the variable n
increases in value by 1
. That value is printed to the REPL.
In the example below, click the Download Project Bundle button below to download the necessary files in a zip file. Extract the contents of the zip file, open the directory Arduino_Nano_RP2040_Connect/arduino_nano_rp2040_connect_button/ and then click on the directory that matches the version of CircuitPython you're using and copy code.py to your CIRCUITPY drive.
Your CIRCUITPY drive should now look similar to the following image:

# SPDX-FileCopyrightText: 2021 Liz Clark for Adafruit Industries # # SPDX-License-Identifier: MIT import time import board from digitalio import DigitalInOut, Direction, Pull # LED setup for onboard LED led = DigitalInOut(board.LED) led.direction = Direction.OUTPUT # button setup switch = DigitalInOut(board.D4) switch.direction = Direction.INPUT switch.pull = Pull.UP # variable for number count n = 0 while True: # when switch is NOT pressed... if switch.value: # LED is off led.value = False # when switch is pressed... else: # LED is on led.value = True # value of n increases by 1 n += 1 # n is printed to the REPL print("led ON %i" % n) # delay time.sleep(0.01)
Page last edited January 22, 2025
Text editor powered by tinymce.