Arduino Code

The Arduino code presented below works equally well on all versions of Trinket Mini and Trinket M0. But if you have an M0 board, consider using the CircuitPython code on the next page of this guide, no Arduino IDE required!

If you haven’t already worked through the Adafruit Trinket M0 or Introducing Trinket guide, do that first. Once you have the Arduino IDE up and running, then download and install the NeoPixel library:

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.

Create a new Arduino sketch (File→New), then copy and paste the following code:

// Low power NeoPixel goggles example.  Makes a nice blinky display
// with just a few LEDs on at any time.

#include <Adafruit_NeoPixel.h>
#ifdef __AVR_ATtiny85__ // Trinket, Gemma, etc.
 #include <avr/power.h>
#endif

#define PIN 0

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(32, PIN);

uint8_t  mode   = 0, // Current animation effect
         offset = 0; // Position of spinny eyes
uint32_t color  = 0xFF0000; // Start red
uint32_t prevTime;

void setup() {
#ifdef __AVR_ATtiny85__ // Trinket, Gemma, etc.
  if(F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  pixels.begin();
  pixels.setBrightness(85); // 1/3 brightness
  prevTime = millis();
}

void loop() {
  uint8_t  i;
  uint32_t t;

  switch(mode) {

   case 0: // Random sparks - just one LED on at a time!
    i = random(32);
    pixels.setPixelColor(i, color);
    pixels.show();
    delay(10);
    pixels.setPixelColor(i, 0);
    break;
 
   case 1: // Spinny wheels (8 LEDs on at a time)
    for(i=0; i<16; i++) {
      uint32_t c = 0;
      if(((offset + i) & 7) < 2) c = color; // 4 pixels on...
      pixels.setPixelColor(   i, c); // First eye
      pixels.setPixelColor(31-i, c); // Second eye (flipped)
    }
    pixels.show();
    offset++;
    delay(50);
    break;
  }

  t = millis();
  if((t - prevTime) > 8000) {      // Every 8 seconds...
    mode++;                        // Next mode
    if(mode > 1) {                 // End of modes?
      mode = 0;                    // Start modes over
      color >>= 8;                 // Next color R->G->B
      if(!color) color = 0xFF0000; // Reset to red
    }
    for(i=0; i<32; i++) pixels.setPixelColor(i, 0);
    prevTime = t;
  }
}

From the Tools→Board menu, select either Adafruit Trinket 8 MHz or Adafruit Trinket 16 MHz (either one will work). Also make sure to select USBTinyISP from the Tools:Programmer menu. Connect the USB cable between the computer and Trinket, press the reset button on the board, then click the upload button (right arrow icon) in the Arduino IDE.

Nothing will happen at first, this is normal. The LEDs only work on battery power! Unplug the USB cable, then connect the battery to the JST plug on the back of the Trinket. If all goes well, you should get a sparkly light show from the LEDs.

Success! Unplug the battery and we’ll move on to the final assembly…

 

(These rings are running a different sketch…more on that in a bit.)

If the code refuses to compile it’s usually one of the following:

  • The Trinket extensions to the Arduino IDE are not correctly installed (use the ready-made IDE, it’s easier).
  • The NeoPixel library is not correctly installed.
  • The wrong board type is selected (should be Adafruit Trinket M0,  Adafruit Trinket 8 MHz or 16 MHz, not any version of the Pro Trinket).

If the code compiles and uploads but nothing happens with the battery connected:

  • Compare your connections against the wiring diagram…for example, maybe you’ve accidentally connected Trinket pin #0 to the first ring’s OUT instead of IN?
  • An electrical short…a blob of solder or bit of wire making inadvertent contact with something nearby.
  • A cold solder joint…solder isn’t properly melted between the wire and board.

If you can’t identify the source of the problem, you can post in the Adafruit Forums for assistance. It’s very helpful if you can get a well-lit, in-focus photo (or several) of the project, showing all the wires and solder connections. We’ll look it over for any gremlins and recommend fixes.

Last updated on 2017-12-08 at 07.30.34 AM Published on 2013-09-13 at 12.20.42 PM