With CircuitPython and Circuit Playground Express you can create a similar digital fidget spinner that's powered entirely by Python code!  This spinner has the same functionality as the Arduino version but it's written in easier to understand Python code.  If you aren't familiar with MicroPython & CircuitPython be sure to read more about it here first.

To create the spinner with Python a slightly different approach is taken compared to the Arduino version.  With this Python version the spinner relies on built-in tap detection of the LIS3DH accelerometer on the Circuit Playground Express board.  This means the spinner code doesn't need to be as fast as the Arduino code because it isn't looking for a small impulse of acceleration.  Instead the spinner code waits for the LIS3DH to detect this small impulse or tap on the X axis (left/right sides of the board) and then starts spinning.  In addition the CircuitPython version uses an advanced FIFO buffer built-in to the LIS3DH to go back and detect how strong the flick or tap was against the board.  Watch the video at the top of this guide for a deeper exploration of how the Python fidget spinner works and how it compares to the Arduino version.

To use the spinner first make sure your Circuit Playground Express board is loaded with the latest version of CircuitPython.  See the similar Feather M0 Express board guide for instructions how to load CircuitPython on your board.  The instructions are the same for the Feather M0 Express and Circuit Playground Express boards, but be sure to download the circuitplayground_express .uf2 or .bin file from the CircuitPython releases page:

Once you download the .uf2 file double tap the Circuit Playground Express board's reset button and drag the .uf2 file to the CPLAYBOOT drive.  After a moment the CircuitPython firmware will be flashed to the board.

Next you'll need to copy a few libraries to the board's filesystem.  For each of these libraries go to their releases page on GitHub and grab the .zip or .mpy file from the latest release.  If the library has a .zip file you'll need to open it and the folder inside is the library, or if it's just a .mpy file that's all you need for the library.

Remember you can just drag and drop copy these libraries to the CIRCUITPY drive your Circuit Playground Express board running CircuitPython will show up as on your computer:

Finally grab the actual code for the fidget spinner from the examples of the CircuitPython LIS3DH library.  There are two files you can use:

  • spinner.py - This is a basic spinner that only spins with one color and animation type.  You can see this spinner built in the video at the top of this page.
  • spinner_advanced.py - This is an advanced spinner that changes color and animation type with A and B button presses (just like the Arduino-based spinner).  Try this one first if you just want to play with the spinner project.

To run the spinner copy one of the above files to your board and rename it to main.py  This will cause CircuitPython to run the code as soon as the board 'boots' up.  You should see the board reset itself automatically after copying and renaming the file, but if not press the board's reset button.  You should see the spinner start with one red dot.  Try flicking or tapping the side of the board (the side with the push buttons) to watch the spinner spin the LED around the board.  If you're using the advanced spinner example press either of the A or B buttons to cycle through different colors and animation types (just like the Arduino-based spinner on the previous page).

There are a few configuration values you can change in the spinner code.  Open the main.py in a text editor and look for these lines near the top (assuming the spinner_advanced.py above):

# Configuration:
ACCEL_RANGE   = adafruit_lis3dh.RANGE_16_G  # Accelerometer range.
TAP_THRESHOLD = 20              # Accelerometer tap threshold.  Higher values
                                # mean you need to tap harder to start a spin.
SPINNER_DECAY = 0.5             # Decay rate for the spinner.  Set to a value
                                # from 0 to 1.0 where lower values mean the
                                # spinner slows down faster.
# Define list of color combinations.  Pressing button A will cycle through
# these combos.  Each tuple entry (line) should be a 2-tuple of 3-tuple RGB
# values (0-255).
    ((255, 0, 0), (0, 0, 0)),      # Red to black
    ((0, 255, 0), (0, 0, 0)),      # Green to black
    ((0, 0, 255), (0, 0, 0)),      # Blue to black
    ((255, 0, 0), (0, 255, 0)),    # Red to green
    ((255, 0, 0), (0, 0, 255)),    # Red to blue
    ((0, 255, 0), (0, 0, 255))     # Green to blue
  • ACCEL_RANGE allows you to change the range of the accelerometer.  You probably want to stick with the default +/-16G range, but you can experiment with smaller ranges by using values like adafruit_lis3dh.RANGE_8_G, adafruit_lis3dh.RANGE_4_G, or adafruit_lis3dh.RANGE_2_G.
  • TAP_THRESHOLD controls the sensitivity of the spinner.  Set to higher values to make the spinner less sensitive.
  • SPINNER_DECAY controls how quickly the spinner slows down after it starts spinning.  Set to a value from 0 to 1.0 where higher values mean the spinner will keep spinning for longer (i.e. less 'friction').
  • COLORS is a tuple of 2-tuples that specify primary and secondary color combinations.

That's all there is to building the digital fidget spinner with CircuitPython and Circuit Playground express!

This guide was first published on Jun 09, 2017. It was last updated on Mar 08, 2024.

This page (CircuitPython) was last updated on Jun 05, 2017.

Text editor powered by tinymce.