EasyManuals Logo

Adafruit Feather M4 Express User Manual

Adafruit Feather M4 Express
178 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #100 background imageLoading...
Page #100 background image
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.
Note that we made the code a little less "Pythonic" than necessary. The if/else block could be replaced with a simple
led.value = not switch.value but we wanted to make it super clear how to test the inputs. The interpreter will read the
digital input when it evaluates switch.value .
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.
For Feather M0 Express and Feather M4 Express, comment out switch = DigitalInOut(board.D2) (and/or switch =
DigitalInOut(board.D7) depending on what changes you already made), and uncomment switch =
DigitalInOut(board.D5) .
For Circuit Playground Express, you'll need to comment out switch = DigitalInOut(board.D2) (and/or switch =
DigitalInOut(board.D5) depending on what changes you already made), and uncomment switch =
DigitalInOut(board.D7) .
# 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) # For Feather M0 Express, Feather M4 Express
# switch = DigitalInOut(board.D7) # For Circuit Playground Express
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) # debounce delay
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.
© Adafruit Industries https://learn.adafruit.com/adafruit-feather-m4-express-atsamd51 Page 105 of 183

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

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

Adafruit Feather M4 Express Specifications

General IconGeneral
BrandAdafruit
ModelFeather M4 Express
CategoryMicrocontrollers
LanguageEnglish

Related product manuals