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 quick-start example shows how you can use one of the Circuit Playground Express buttons as an input  to control another digital output - the built in LED

Copy and paste the code block into using your favorite text editor, and save the file, to run the demo

# CircuitPlaygroundExpress_DigitalIO

import time

import board
from digitalio import DigitalInOut, Direction, Pull

led = DigitalInOut(board.D13)
led.direction = Direction.OUTPUT

button = DigitalInOut(board.BUTTON_A)
button.direction = Direction.INPUT
button.pull = Pull.DOWN

while True:
    if button.value:  # button is pushed
        led.value = True
        led.value = False

We're using the built-in pushbuttons in this example because it's very easy to get started, but you can use ALL KINDS of different buttons and switches, even homemade ones such as tinfoil or pennies, as digital inputs connected to the Digital IO pads!

Note that we made the code a little less 'pythonic' than necessary, the if/then could be replaced with a simple led.value = not button.value but I wanted to make it super clear how to test the inputs. When the interpreter gets to evaluating button.value that is when it will read the digital input.

Press Button A (the one on the left), and the onboard red LED will turn on!

Note that on the M0/SAMD based CircuitPython boards, at least, you can also have internal pullups with Pull.UP when using external buttons, but the built in buttons require Pull.DOWN.

Maybe you're setting up your own external button with pullup or pulldown resistor. If you want to turn off the internal pullup/pulldown just assign button.pull = None

Going Beyond the Lesson!

It's time to flex your new learnings and try something different!!

Experiment 1

See if you can adjust your code so that you use Button B instead of Button A.

It only takes a small change to switch buttons. If you get stuck, click on the blurry text below to reveal a hint and then the answer:

You need to change one single, solitary letter!

button = DigitalInOut(board.BUTTON_B) 

Experiment 2

Perhaps you want to be sure there are no accidental illuminations of the red LED! Make it so that BOTH buttons must be pressed in order to light the red LED.


You'll need to declare a variable for the second button, just as you did with the first. You'll also need to set it up as an input, with pull down resistance.

It's a good idea to rename the original button variable to buttonA, and the new set to buttonB.

To check both buttons in the 'if' statement, you'll use an 'and' to string together both value checks. 

  if buttonA.value == True and buttonB.value == True:

Experiment 3

Try testing the slide switch instead of the buttons. For the slide switch you need to use Pull.UP instead of Pull.DOWN.


switch = DigitalInOut(board.SLIDE_SWITCH)
switch.direction = Direction.INPUT
switch.pull = Pull.UP

if switch.value is True: # switch is slid to the left 

Last updated on Apr 24, 2018 Published on Oct 12, 2017