Adafruit Feather M4 Express Manual page 100

Table of Contents

Advertisement

CircuitPython Digital In & Out
The first part of interfacing with hardware is being able to manage digital inputs and outputs. With CircuitPython, it's
super easy!
This example shows how to use both a digital input and output. You can use a switch input with pullup resistor (built in)
to control a digital output - the built in red LED.
Copy and paste the code into code.py using your favorite editor, and save the file to run the demo.
# CircuitPython IO demo #1 - General Purpose I/O
import time
import board
from digitalio import DigitalInOut, Direction, Pull
led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT
# For Gemma M0, Trinket M0, Metro M0 Express, ItsyBitsy M0 Express, Itsy M4 Express
switch = DigitalInOut(board.D2)
# switch = DigitalInOut(board.D5)
# switch = DigitalInOut(board.D7)
switch.direction = Direction.INPUT
switch.pull = Pull.UP
while True:
# We could also do "led.value = not switch.value"!
if switch.value:
led.value = False
else:
led.value = True
time.sleep(0.01)
Note that we made the code a little less "Pythonic" than necessary. The
led.value = not switch.value
digital input when it evaluates
For Gemma M0, Trinket M0, Metro M0 Express, Metro M4 Express, ItsyBitsy M0 Express, ItsyBitsy M4 Express, no
changes to the initial example are needed.
Note: To "comment out" a line, put a # and a space before it. To "uncomment" a line, remove the # + space
from the beginning of the line.
For Feather M0 Express and Feather M4 Express, comment out
depending on what changes you already made), and uncomment
DigitalInOut(board.D7)
.
DigitalInOut(board.D5)
For Circuit Playground Express, you'll need to comment out
depending on what changes you already made), and uncomment
DigitalInOut(board.D5)
.
DigitalInOut(board.D7)
© Adafruit Industries
# For Feather M0 Express, Feather M4 Express
# For Circuit Playground Express
# debounce delay
but we wanted to make it super clear how to test the inputs. The interpreter will read the
.
switch.value
https://learn.adafruit.com/adafruit-feather-m4-express-atsamd51
block could be replaced with a simple
if/else
switch = DigitalInOut(board.D2)
switch = DigitalInOut(board.D2)
(and/or
switch =
switch =
(and/or
switch =
switch =
Page 105 of 183

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the Feather M4 Express and is the answer not in the manual?

Related Products for Adafruit Feather M4 Express

Table of Contents

Save PDF