The Arduino version of the digital fidget spinner is a good example of performing peak detection with a signal.  In this case the signal is one of the axes of the accelerometer and a sudden peak, or tap/flick of the board, causes the LEDs around the board to animate with movement.  By applying a decay to the velocity of the LED animation the spinner will slow down as if it were affected by friction and air resistance.

To use this example you'll need to install the Adafruit Circuit Playground library in the Arduino IDE.  Follow the Circuit Playground guide page on installing the library.  If you've already installed the Circuit Playground library make sure to use the Arduino board manager to check for updates and install the latest version of the library.

Look for a FidgetSpinner example in the Adafruit Circuit Playground library (you can also get the sketch directly from GitHub here):

The sketch is ready to run however you might want to change one of the configuration values at the top.  Look for the AXIS define:

Download: file
// Configure which accelerometer axis to check:
// This should be one of the following values (uncomment only ONE):
//#define AXIS              CircuitPlayground.motionX()    // X axis, good for Circuit Playground express (SAMD21).
#define AXIS                CircuitPlayground.motionY()    // Y axis, good for Circuit Playground classic (32u4).
//#define AXIS              CircuitPlayground.motionZ()    // Z axis, wacky--try it!

Notice in the comments it mentions that you can change which accelerometer axis is used to detect a tap of the spinner.  For Circuit Playground Classic the sketch is setup to use the Y axis of the accelerometer which means you should tap the board from the left or right side of the USB & charging ports.  However if you're using Circuit Playground Express you'll want to change the AXIS define to use the X axis as the accelerometer is in a different position on the board.  Comment and uncomment the appropriate line for the board you're using (or experiment by trying the different axes!).

Now upload the sketch to your Circuit Playground board.  When the sketch finishes uploading it should start with a red dot on the board LEDs.  Try flicking or tapping a side of the board to see the spinner start moving the LED around the board.  You can press either of the push buttons to change the display of the spinner:

  • One of the buttons (left on Circuit Playground Classic, A on Circuit Playground Express) cycles through four different animations:
    • Single dot
    • Double dots
    • Single smooth pulse
    • Double smooth pulse
  • The other button (right on Circuit Playground Classic, B on Circuit Playground Express) cycles through six different color combinations:
    • Red & black
    • Green & black
    • Blue & black
    • Red & green
    • Red & blue
    • Green & blue

If you're curious there are a few other configuration values you can experiment with changing at the top of the sketch:

Download: file
// Configure how the axis direction compares to pixel movement direction:
#define INVERT_AXIS         true  // Set to true or 1 to invert the axis direction vs.
                                  // pixel movement direction, or false/0 to disable.
                                  // If the pixels spin in the opposite direction of your
                                  // flick all the time then try changing this value.

// Configure pixel brightness.
// Set this to a value from 0 to 255 (minimum to maximum brightness).
#define BRIGHTNESS          255

// Configure peak detector behavior:
#define LAG                 30     // Lag, how large is the buffer of filtered samples.
                                   // Must be an integer value!
#define THRESHOLD           20.0   // Number of standard deviations above average a
                                   // value needs to be to be considered a peak.
#define INFLUENCE            0.1   // Scale down peak values to this percent influence
                                   // when storing them back in the filtered values.
                                   // Should be a value from 0 to 1.0 where smaller
                                   // values mean peaks have less influence.

// Configure spinner decay, i.e. how much it slows down.  This should
// be a value from 0 to 1 where smaller values cause the spinner to
// slow down faster.
#define DECAY                0.66
  • The INVERT_AXIS option flips which direction the LEDs spin when the board is tapped.  Depending on how you hold the spinner and the orientation of the accelerometer you might need to set this to true or false to line up the tap direction and LED movement.
  • BRIGHTNESS controls how bright the LEDs are on the board.  The default of 255 is maximum brightness, but you can bump it down to a smaller positive value like 100 for dimmer LEDs.
  • LAG, THRESHOLD, and INFLUENCE control how the flick/peak detection works.  Read the comments and watch the video at the top of the page to see more about how these values change the behavior of the peak detection.  You can make the detection more or less sensitive by changing these values.
  • DECAY controls how quickly the spinner slows down after it starts spinning.  This is a value from 0 to 1.0 where higher values mean the spinner will spin for longer (i.e. less 'friction').

That's all there is to using the Arduino version of the digital fidget spinner!

This guide was first published on Jun 09, 2017. It was last updated on Jun 09, 2017. This page (Arduino) was last updated on Aug 13, 2019.