Lesson 2: NeoPixels

The Circuit Playground Express has two buttons. Button A is on the left and button B is on the right. These buttons can be used as inputs, which means you can use them to tell your board to do something when you press them.

In this example, we'll use the buttons of the CPX to change the colors of the NeoPixels. Button A will turn them red, and B will turn them blue.

The NeoPixel and Buttons Program

Library Import and NeoPixel Set Up

  • First import digitalio, board, and neopixel.
  • Next from the NeoPixel category, drag in an np =  neopixel.NeoPixel() block and set it equal to board.NEOPIXEL, 10, brightness = 0.2. This sets up the NeoPixels and determines brightness (can be between 0.0 and 1.0).
  • Now drag in an np = fill() block and set it to (0,0,0). This sets the NeoPixels to the color to black or off.

Creating Variables

  • In order to control the buttons, we need to create some variables to assign to button Aa and button B.
  • To do this, drag in a var block in Basic.
  • In the drop down menu click Rename variable
  • Next name the variable as "button_a" and hit ok.
  • Now you have a variable for button_a!
  • Repeat for button_b

Setting Up the Buttons

  • To set up the buttons, drag in a DigitalInOut() block from the Digital block category. Choose the variable from the drop down menu as button_a. Set the block equal to board.BUTTON_A.
  • Now drag in a .direction = block and set it equal to Direction.INPUT choosing button_a from the drop down menu.
  • Then drag in a .pull = block and set it equal to Pull.DOWN.
  • Repeat the above steps for button_b.

The While Loop:

  • Drag in a while: True loop.
  • Next drag in an ifstatement with the condition set as button_a.value (this block can be found in Digital)
  • From Neopixel drag in np.fill() and set it to (255,0,0) which is Red. (R,G,B)
  • Then drag in an np.show() block.
  • Now put an elif(meaning else if) block setting the condition to button_b.
  • Repeat steps above but set the color to (0,0,255) for blue.

Download the file and drag to the CPX.

Try it out!

When button A is pressed, NeoPixels should turn red.

When button B is pressed they should turn blue!

Here are the code files if you want to upload directly to EduBlocks or the CPX.

EduBlocks file:

CircuitPython Code:

Download: file
np = None
button_a = None
button_b = None


from digitalio import *
import board
import neopixel
np = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness = 0.2)
np.fill((0,0,0))
np.show()
button_a = DigitalInOut(board.BUTTON_A)
button_a.direction = Direction.INPUT
button_a.pull = Pull.DOWN
button_b = DigitalInOut(board.BUTTON_B)
button_b.direction = Direction.INPUT
button_b.pull = Pull.DOWN
while True:
  if button_a.value:
    np.fill((255,0,0))
    np.show()
  elif button_b.value:
    np.fill((0,0,255))
    np.show()
This guide was first published on Feb 05, 2019. It was last updated on Feb 05, 2019. This page (Lesson 2: NeoPixels) was last updated on Jul 16, 2019.