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!

Guides

If you haven’t already worked through the Adafruit Trinket M0 or Introducing Trinket guide, do that first. 

Getting Code Onto Trinket

Before we start disassembling or building the circuit, it's a good idea to get code uploaded to the micro-controller first. If you don't write / understand code, don't to worry! You don't need to be a programmer to be able to upload prewritten code :-) 

We'll walk you through the whole process. 

First, visit the Trinket tutorial pages above. Follow the instructions to download & setup the Arduino IDE and install drivers.

Make sure you are able to get sketches compiled and uploaded, especially the blink example in the tutorial. Once you are comfortable with using the Trinket, you can continue!

Install Adafruit NeoPixel Library

Next, we need to add support for NeoPixels.

Visit the Adafruit NeoPixel tutorial to install the NeoPixel library!

We've had issues with uploading code to the original (non-M0) Trinket on a Mac with El Capitan – If you're running this setup be sure to use a USB 2.0 Hub. The issue is due to USB 3.0 ports on Mac hardware.

Uploading Code to Board

Now that we have the Adafruit boards & NeoPixel library installed, we can get our code ready to upload onto the board. Select all of the code listed below in the black box and copy it to your clip board. Then, in Arduino IDE, paste it in the sketch window (making sure to overwrite anything currently there). Next, goto the Tools menu > Board and select either Adafruit Trinket M0 or Adafruit Trinket (if you're using the 3V Adafruit Trinket version use Trinket 8Mhz. If you're using the 5V Trinket, select Trinket 16Mhz).

Now you can click on the "check mark" icon to verify the code. If it's all good, we can continue to upload the code to the board.

Connect USB Data Cable to Trinket

Be sure to use a micro USB cable that can transfer data - A USB cable that ONLY charges devices will simply not work. Plug it into the microUSB port on the Adafruit Trinket board and the USB port on your computer (try to avoid connecting to a USB hub).

Follow the upload procedure documented in the Getting Started guides at the top of this page to upload. The Arduino IDE will notify you if the upload is successful and completed.

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            4

// Color Segments
#define APIXELS      14 // number of first orange pixels 
#define BPIXELS      84 // number of blue pixels
#define CPIXELS      93 // second orange pixels

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(93, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 10; // delay for half a second

void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

  pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {

  // For the first 14 pixels, make them orange, starting from pixel number 0.
  for(int i=0;i<APIXELS;i++){
    pixels.setPixelColor(i, pixels.Color(255,50,0)); // Set Pixels to Orange Color
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
  }

  // Fill up 84 pixels with blue, starting with pixel number 14.
  for(int i=14;i<BPIXELS;i++){
    pixels.setPixelColor(i, pixels.Color(0,250,200)); // Set Pixels to Blue Color
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).

  }
  
  // Fill up 9 pixels with orange, starting from pixel number 84.
  for(int i=84;i<CPIXELS;i++){
    pixels.setPixelColor(i, pixels.Color(250,50,0)); //Set Pixels to Orange Color
    pixels.show(); // This sends the updated pixel color to the hardware.
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
}
This guide was first published on May 10, 2017. It was last updated on Oct 19, 2018. This page (Arduino Code) was last updated on Jan 05, 2018.