The Circuit Playground Express and Bluefruit have two buttons. Button A is on the left and button B is on the right. Though the images are of the Circuit Playground Express, the buttons are in the same location on the Bluefruit. These buttons can be used as inputs, which means you can use them to tell your board to do something when you press them.

Let's start with button A. Add the following code to your code.py. Remember, if you need help with this, check here.

"""This example turns on the little red LED when button A is pressed."""
from adafruit_circuitplayground import cp

while True:
    if cp.button_a:
        print("Button A pressed!")
        cp.red_led = True

Now, press button A. Red LED!

Let's look at the code. First, we import cp.

Inside our loop, we check to see if button A is pressed with if cp.button_a:. Then, if it is, we print Button A pressed! to the serial console and we turn on the red LED!

Notice the LED stays on once button A is pressed. This is because we didn't tell the code to turn it off. So, let's try something a little different.

Add the following code to your code.py:

"""This example turns the little red LED on only while button B is currently being pressed."""
from adafruit_circuitplayground import cp

# This code is written to be readable versus being Pylint compliant.
# pylint: disable=simplifiable-if-statement

while True:
    if cp.button_b:
        cp.red_led = True
    else:
        cp.red_led = False

# Can also be written as:
#    cp.red_led = cp.button_b

Now press button B. Red LED! But only while it's pressed. Nice!

Let's take a look at the code. First we import cp.

Inside our loop, we check to see if button B is pressed with if cp.button_b:. If it is, we turn on the red LED. Then, with our else:, we're telling the code, "otherwise, turn off the red LED." So, when the button is not being pressed, the LED turns off!

You can use both buttons in the same program. Let's change things up.

Add the following code to your code.py:

"""This example lights up the third NeoPixel while button A is being pressed, and lights up the
eighth NeoPixel while button B is being pressed."""
from adafruit_circuitplayground import cp

cp.pixels.brightness = 0.3
cp.pixels.fill((0, 0, 0))  # Turn off the NeoPixels if they're on!

while True:
    if cp.button_a:
        cp.pixels[2] = (0, 255, 0)
    else:
        cp.pixels[2] = (0, 0, 0)

    if cp.button_b:
        cp.pixels[7] = (0, 0, 255)
    else:
        cp.pixels[7] = (0, 0, 0)

Now press button A or B. Or press them both at the same time. Green and blue NeoPixels!

Our code is checking to see if each button is pressed. If it is, it turns on the LED next to the button to the specified color. Button A turns the LED next to it green. Button B turns the LED next to it blue. And, if the buttons are not being pressed, the LEDs are otherwise turned off by cp.pixels.fill((0, 0, 0)).

Half and Half

Let's get a little fancier. Add the following code to your code.py:

"""This example lights up half the NeoPixels red while button A is being pressed, and half the
NeoPixels green while button B is being pressed."""
from adafruit_circuitplayground import cp

cp.pixels.brightness = 0.3
cp.pixels.fill((0, 0, 0))  # Turn off the NeoPixels if they're on!

while True:
    if cp.button_a:
        cp.pixels[0:5] = [(255, 0, 0)] * 5
    else:
        cp.pixels[0:5] = [(0, 0, 0)] * 5

    if cp.button_b:
        cp.pixels[5:10] = [(0, 255, 0)] * 5
    else:
        cp.pixels[5:10] = [(0, 0, 0)] * 5

Now press button A or button B. Neopixels half and half, split down the middle, matching the sides the buttons are on!

Here we're using a concept called slicing. Slicing allows you to specify a start point and an end point and enables us to tell the code to light up everything in between. So, instead of specifying a single LED with [0], we tell the board to light up the first half of the LEDs on pressing button A with cp.pixels[0:5] = [(255, 0, 0)] * 5. The [0:5] is the start and end point, and the * 5 is the slice size (5 out of 10 LEDs). We do the same with button B and the second half of the LEDs with cp.pixels[5:10]. And we tell the LEDs to otherwise be off if no buttons are pressed.

Note that the end points are 1 higher than the normal LED numbering - slice math is a little bit different than CircuitPython counting. Try playing with it a little bit. Change the first set to cp.pixels[1:4] = [(255, 0, 0)] * 3. See which LEDs light up!

If you try to specify a set of LEDs that's different from the slice size, you code won't run and an error will be printed to the serial console. For example, cp.pixels[1:4] = [(255, 0, 0)] * 4 will fail because your slice size should be 3. So be sure to match them up!

This guide was first published on Jun 05, 2018. It was last updated on Jun 05, 2018. This page (Buttons) was last updated on May 31, 2020.