Arduino Code

The Arduino code presented below works equally well on all versions of GEMMA: v1, v2 and M0. But if you have an M0 board, consider using the CircuitPython code on the next page of this guide!

Install Arduino IDE

If you're new to GEMMA and the Arduino IDE, we have a great introduction tutorial to get you started with the program and configuration. 

You'll need to download our special flavor of the Arduino IDE that includes profiles for Adafruit micro-controllers. Walk through the guide and configure the GEMMA.

Arduino Sketch

Below is the code used in the project that animated the NeoPixel rings. Once you have installed the Adafruit Arduino IDE and configured the GEMMA. Copy the code below and paste it into a new sketch.

#include <Adafruit_NeoPixel.h>

#define PIN       0
#define NUM_LEDS 24

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_LEDS, PIN);

uint8_t  mode   = 0,        // Current animation effect
         offset = 0;        // Position of spinner animation
uint32_t color  = 0xFF0000; // Starting color = red
uint32_t prevTime;          // Time of last animation mode switch

void setup() {
  pixels.begin();
  pixels.setBrightness(85); // 1/3 brightness
  prevTime = millis();      // Starting time
}

void loop() {
  uint8_t  i;
  uint32_t t;

  switch(mode) {

   case 0: // Random sparkles - just one LED on at a time!
    i = random(NUM_LEDS);           // Choose a random pixel
    pixels.setPixelColor(i, color); // Set it to current color
    pixels.show();                  // Refresh LED states
    // Set same pixel to "off" color now but DON'T refresh...
    // it stays on for now...both this and the next random
    // pixel will be refreshed on the next pass.
    pixels.setPixelColor(i, 0);
    delay(10);                      // 10 millisecond delay
    break;
 
   case 1: // Spinny wheel
    // A little trick here: pixels are processed in groups of 8
    // (with 2 of 8 on at a time), NeoPixel rings are 24 pixels
    // (8*3) and 16 pixels (8*2), so we can issue the same data
    // to both rings and it appears correct and contiguous
    // (also, the pixel order is different between the two ring
    // types, so we get the reversed motion on #2 for free).
    for(i=0; i<NUM_LEDS; i++) {    // For each LED...
      uint32_t c = 0;              // Assume pixel will be "off" color
      if(((offset + i) & 7) < 2) { // For each 8 pixels, 2 will be...
        c = color;                 // ...assigned the current color
      }
      pixels.setPixelColor(i, c);  // Set color of pixel 'i'
    }
    pixels.show();                 // Refresh LED states
    delay(50);                     // 50 millisecond delay
    offset++;                      // Shift animation by 1 pixel on next frame
    if(offset >= 8) offset = 0;    // Reset offset every 8 pixels
    break;

    // More animation modes could be added here!
  }

  t = millis();                    // Current time in milliseconds
  if((t - prevTime) > 8000) {      // Every 8 seconds...
    mode++;                        // Advance to next animation mode
    if(mode > 1) {                 // End of modes?
      mode = 0;                    // Start over from beginning
      color >>= 8;                 // Next color R->G->B
      if(!color) color = 0xFF0000; // Preiodically reset to red
    }
    pixels.clear();                // Set all pixels to 'off' state
    prevTime = t;                  // Record the time of the last mode change
  }
}

Installing Arduino libraries is a frequent stumbling block. If this is your first time, or simply needing a refresher, please read the All About Arduino Libraries tutorial.If the NeoPixel library is correctly installed (and the Arduino IDE is restarted), you should be able to navigate through the “File” rollover menus as follows:

File→Sketchbook→Libraries→Adafruit_NeoPixel→strandtest

From the Tools→Board menu, select the device you are using: 

  • Adafruit Gemma M0
  • Adafruit Gemma 8 MHz 
  • Connect the USB cable between the computer and your device. The original Gemma (8 MHz) need the reset button pressed on the board, then click the upload button (right arrow icon) in the Arduino IDE. You do not need to press the reset on the newer Gemma M0 or Trinket M0.

When the battery is connected, you should get a light show from the LEDs. Refer to the NeoPixel Uberguide for more info.

This guide was first published on Oct 02, 2014. It was last updated on Oct 02, 2014. This page (Arduino Code) was last updated on Dec 12, 2018.