Getting Code Onto Trinket

In this portion of the guide, we'll get code uploaded to the Adafruit Trinket micro-controller. 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 page by clicking the button below. 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!

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 Adafruit Trinket (if you're using the 3V Adafruit Trinket version use Trinket 8Mhz. If you're using the 5V Trinket, select Trinket 12Mhz). 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). As soon as you plug it in, you'll see a red LED blink on the Adaruit Trinket - This let's you know the board is ready to except code. While the LED is blinking, click on the Upload button (It's a right arrow icon, next to the chekc mark). The Arduino IDE will notify you if the upload is successful and completed.

#include <Adafruit_NeoPixel.h>

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

#define PIXEL_PIN    0  // Pin connected to neo pixels
#define FIREPIN      2  // Fire button
#define PIXEL_COUNT  1  // Count of neo pixels

int buttonState = 0;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

void setup() {

  #if defined (__AVR_ATtiny85__)
    if (F_CPU == 16000000) clock_prescale_set(clock_div_1);



// Linear interpolation of y value given min/max x, min/max y, and x position.
float lerp(float x, float x0, float x1, float y0, float y1)
  // Clamp x within x0 and x1 bounds.
  x = x > x1 ? x1 : x;
  x = x < x0 ? x0 : x;
  // Calculate linear interpolation of y value.
  return y0 + (y1-y0)*((x-x0)/(x1-x0));

// Get the color of a pixel within a smooth gradient of two colors.
uint32_t color_gradient(uint8_t start_r, // Starting R,G,B color
                        uint8_t start_g,
                        uint8_t start_b, 
                        uint8_t end_r,   // Ending R,G,B color
                        uint8_t end_g,
                        uint8_t end_b,
                        float pos)       // Position along gradient, should be a value 0 to 1.0
  // Interpolate R,G,B values and return them as a color.  
  uint8_t red   = (uint8_t) lerp(pos, 0.0, 1.0, start_r, end_r);
  uint8_t green = (uint8_t) lerp(pos, 0.0, 1.0, start_g, end_g);
  uint8_t blue  = (uint8_t) lerp(pos, 0.0, 1.0, start_b, end_b);
  return Adafruit_NeoPixel::Color(red, green, blue);

// Set all pixels to the specified color.
void fill_pixels(Adafruit_NeoPixel& pixels, uint32_t color)
  for (int i=0; i < pixels.numPixels(); ++i) {
    pixels.setPixelColor(i, color);

void animate_gradient_fill(Adafruit_NeoPixel& pixels, // NeoPixel strip/loop/etc.
                           uint8_t start_r,           // Starting R,G,B color
                           uint8_t start_g,
                           uint8_t start_b, 
                           uint8_t end_r,             // Ending R,G,B color
                           uint8_t end_g,
                           uint8_t end_b,
                           int duration_ms)           // Total duration of animation, in milliseconds
  unsigned long start = millis();
  // Display start color.
  fill_pixels(pixels, Adafruit_NeoPixel::Color(start_r, start_g, start_b));
  // Main animation loop.
  unsigned long delta = millis() - start;
  while (delta < duration_ms) {
    // Calculate how far along we are in the duration as a position 0...1.0
    float pos = (float)delta / (float)duration_ms;
    // Get the gradient color and fill all the pixels with it.
    uint32_t color = color_gradient(start_r, start_g, start_b, end_r, end_g, end_b, pos);
    fill_pixels(pixels, color);
    // Update delta and repeat.
    delta = millis() - start;
  // Display end color.
  fill_pixels(pixels, Adafruit_NeoPixel::Color(end_r, end_g, end_b));

void loop() { 
  uint8_t  i;
  //Button switch
  buttonState = digitalRead(FIREPIN);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is LOW:
  if (buttonState == LOW) {     
  // Run It:
  // Nice flash and fade out over about 3/4 of a second:
    animate_gradient_fill(strip, 255, 255, 255, 0, 0, 255,150);
  // Then flash from purple to nothing over a longer period.
    animate_gradient_fill(strip, 0, 100, 50, 0, 0, 50, 150);
    animate_gradient_fill(strip, 0, 0, 20, 0, 0, 0, 150);
  else {
     strip.setPixelColor(i, 0,0,0); //Button not pressed, turn off pixels; //Show no pixels

This guide was first published on Nov 09, 2016. It was last updated on Nov 09, 2016.

This page (Software) was last updated on Oct 31, 2016.

Text editor powered by tinymce.