Though the following example uses the Circuit Playground Express to demonstrate, the code works exactly the same way with the Circuit Playground Bluefruit. Simply copy the code and follow along with your Circuit Playground Bluefruit!

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

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 Introducing_CircuitPlaygroundExpress/CircuitPlaygroundExpress_DigitalIO/ 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:

CIRCUITPY
# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries
#
# SPDX-License-Identifier: MIT

# Circuit Playground digitalio example

import time
import board
import digitalio

led = digitalio.DigitalInOut(board.D13)
led.switch_to_output()

button = digitalio.DigitalInOut(board.BUTTON_A)
button.switch_to_input(pull=digitalio.Pull.DOWN)

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

    time.sleep(0.01)
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 simply include button.switch_to_input().

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 = digitalio.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.

Hints: 

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.

Hints:

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

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

This guide was first published on Oct 12, 2017. It was last updated on Oct 12, 2017.

This page (CircuitPython Digital In & Out) was last updated on Sep 19, 2023.

Text editor powered by tinymce.