CircuitPython Digital Input Example - Blinking an LED using the built-in button.
"""
import board
import digitalio
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)
while True:
if not button.value:
led.value = True
else:
led.value = False
Now, press the button. The LED lights up! Let go of the button and the LED turns off.
Note that the code is a little less "Pythonic" than it could be. It could also be written as
led.value = not button.value
new to programming, so the example is a bit longer than it needed to be to make it
easier to read.
First you
import
available for use in your code. Both are built-in to CircuitPython, so you don't need to
download anything to get started.
Next, you set up the LED. To interact with hardware in CircuitPython, your code must
let the board know where to look for the hardware and what to do with it. So, you
create a
digitalio.DigitalInOut()
module, and save it to the variable
d
.
OUTPUT
You include setup for the button as well. It is similar to the LED setup, except the
button is an
INPUT
©Adafruit Industries
. That way is more difficult to understand if you're
two modules:
board
, and requires a pull up.
and
. This makes these modules
digitalio
object, provide it the LED pin using the
. Then, you tell the pin to act as an
led
boar
Page 130 of 263
Need help?
Do you have a question about the ESP32-S3 and is the answer not in the manual?