# Trinket / Gemma Space Invader Pendant

## Overview

_Pac Man_ is my best friend.  
  
Okay, not really. But as a child of the 80’s, video arcade games were a huge cultural phenomenon of my formative years.  
  
Tiny affordable microcontrollers are a cultural phenomenon of _today’s_ generation.  
  
This project bridges the generations to create an animated LED necklace or charm that <u>you</u> can customize to create a retro-style personal “totem video game creature.” It’s a small project, good for electronics novices and group workshops. And you’ll have something eye-catching to wear and show off afterward.

![](https://cdn-learn.adafruit.com/assets/assets/000/011/401/medium800thumb/led_matrix_pendant-anim.jpg?1448311221)

Info: 

# Tools Needed
**This is a soldering project,** albeit a small one. You will need the common soldering paraphernalia of a soldering iron, solder, wire (20 to 26 gauge, either stranded or solid) and tools for cutting and stripping wire.  
# Parts needed

- **Adafruit&nbsp;** [**Trinket Mini**](http://www.adafruit.com/category/261) or [**Gemma v2**](http://www.adafruit.com/products/1222)&nbsp;microcontroller board (if Trinket, either the [3.3V](http://www.adafruit.com/products/1500) or [5V](http://www.adafruit.com/products/1501) type works). The newer “M0” boards are _not_ supported.
- **[Mini 8x8 LED Matrix](http://www.adafruit.com/products/870)****[w/Backpack](http://www.adafruit.com/products/870 "Link: http://www.adafruit.com/products/870")** (any color — we have several! Green for Space Invaders, yellow for Pac Man, etc.)
- [**3.7V 150mAh Lithium-Ion Polymer Battery**](http://www.adafruit.com/products/1317)
- [**LiPo battery charger**](http://www.adafruit.com/products/1304)
- [**JST Battery Extension Cable**](http://www.adafruit.com/products/1131) (Trinket only — not required if using Gemma)
- **Lanyard** to create a necklace. This should be non-conductive — plastic lace (the sort used for weaving bracelets), rubber necklace cord or heavy fishing line all work. A pin back (such as our [magnetic](http://www.adafruit.com/products/1170) variety) is another possibility.
- **Optional:** momentary pushbutton to activate the animation. You could substitute a [vibration sensor switch](https://www.adafruit.com/product/2384) to make it shake-activated. Or&nbsp;just use the tiny reset button that’s built into the Trinket or Gemma to restart the animation code.
- **Optional:** If using Trinket, a bit of [heat-shrink tube](http://www.adafruit.com/products/344) is best for covering some connections; it’s cleaner than alternatives like tape.

### 

Sure can! Just be **super extra careful** to **follow the assembly directions** in the [LED Backpack Guide](http://learn.adafruit.com/adafruit-led-backpack/1-2-8x8-matrix) and **install the matrix the right way on the board.** This is a common mistake! The “mini” matrix is recommended for this project because it’s less troublesome for beginners.

### 

Yes! You can use **any** color of our 8x8 matrices: red, green, blue, white...

### 

_Potentially_ yes,&nbsp;but you will have to adjust the bitmaps and code to handle the extra rows (it appears like a 16x8 matrix to the driver chip). We don’t have a code or tutorial for this variation, but the wiring is the same if you’re inclined to try.

# Trinket / Gemma Space Invader Pendant

## Wiring and Soldering

Here’s the schematic when using a Gemma board. Simplicity itself! The battery isn’t shown here…it simply plugs into the JST connector.

_The battery charger isn’t part of the circuit. Unplug the battery from the board and use the USB charger to top it off._

![](https://cdn-learn.adafruit.com/assets/assets/000/011/398/medium800/led_matrix_gemma-circuit.jpg?1380659963)

A couple extra steps are required if using a Trinket board (3.3V or 5V doesn’t make a difference, the connections are the same):

![](https://cdn-learn.adafruit.com/assets/assets/000/011/399/medium800/led_matrix_trinket-circuit.jpg?1380660028)

Since there’s no JST battery connector on the Trinket, we’ll need to make one using the battery extension cable. Cut it a couple inches from the socket end (where the battery plugs in), separate the red and black wires about halfway, and strip about 1/4" of insulation from the ends.  
  
The red and black wires each need to connect to two places in the circuit: red goes to BAT+ on Trinket and + on the backpack, while black goes to GND and –. These connections require a _split inline splice._ Even a regular 1-to-1 inline splice can be tricky for a novice, 2-way is an extra challenge. It’s a little easier with narrow-gauge wire (e.g. 26 ga.).

This lets us route battery power to both the Trinket board and the LED matrix.

![](https://cdn-learn.adafruit.com/assets/assets/000/011/311/medium800/led_matrix_splice.jpg?1380308052)

_An example of a three-way inline splice_ (this is from another project — we’ll be doing just a 2-way splice, but the technique is the same):  
  
Line up the 2 wires on one side and twist them together. Slide heat-shrink tube as far down as possible. Then bring in the power wire from the opposite direction and twist around the others. Solder and heat-shrink the connection.  
  
Work slowly and methodically, remember to slide the heat-shrink tube on FIRST before joining the wires, and use proper soldering technique: heat the wires and apply the solder there; <u>don’t</u> move a glop of solder from the iron to the connection.  
# Trinket / Gemma Space Invader Pendant

## Arduino Code

If this is your first time using Trinket or Gemma, work through the [Introducing Trinket](http://learn.adafruit.com/introducing-trinket "Link: http://learn.adafruit.com/introducing-trinket") or [Introducing Gemma](http://learn.adafruit.com/introducing-gemma) guide first; you need to customize some settings in the Arduino IDE. Once you have it up and running (test the “blink” sketch), then&nbsp;continue…

In the Arduino IDE, create a new sketch ( **File→New** ), then copy and paste the following code (click the “copy code” link at the top right, switch to the Arduino IDE and select **Edit→Paste** ).

Alternately, you can use the “Download Project Bundle” button below. This will download a ZIP file containing all the code for both Arduino _and_ CircuitPython — so you’ll need to pick through for just the “Trinket\_Gemma\_Space\_Invader\_Pendant.ino” and “anim.h” files.

The program is fairly small but uses some advanced techniques, so don’t be alarmed if a lot of it is unfamiliar. The important stuff you’ll actually be editing is further ahead.

_ **Two source files are required for the project. The second is explained a bit further down.** _

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Gemma/Trinket_Gemma_Space_Invader_Pendant/Trinket_Gemma_Space_Invader_Pendant.ino

# We’re not done yet!

Now we'll create the graphics for some alien creatures. You can scroll down if you just want to finish the pendant with our sample graphics, or read on for the geeky code details…  
  
Normally when using these matrices we recommend using the [Adafruit LED Backpack Library](https://github.com/adafruit/Adafruit-LED-Backpack-Library). Because library installation is often a trouble spot, this code minimizes extra library use and instead needs to do a few things “raw,” and it’s a bit intimidating as a result. So it’s okay just to copy and paste the code and proceed to the next step if you prefer.  
  
The code starts by disabling the chip’s Timer1 and analog-to-digital converter to save a little power and extend battery life; they’re not used by this program. Then it initializes the HT16K33 matrix driver chip using the Arduino “Wire” library for the I²C protocol, clearing the image memory, setting the brightness and enabling the display (brightness is set with a #define near the top of the code…lower numbers are dimmer, but improve battery life).  
  
The program then loops one or more times, reading animation frames from flash memory (we’ll explain that on the next page), issuing the bitmap data to the matrix driver and displaying each image for a short period. The big table lookup is because the matrix columns aren’t wired in-order on the Backpack board; this re-orders the bits in memory to match the column order.  
  
At the end of the sequence, both the LED matrix driver and the CPU are put into a low-power state to help preserve battery life. A pin-change interrupt is enabled on pin #1 that will wake the CPU from sleep and restart the animation. This button is optional; you can use the onboard reset button as well (though it will have a brief delay as the bootloader starts).

## Second File

Animation data goes in a separate file. This way it can be edited or replaced without having to rummage through the code; it’s a bit less intimidating.

At the right side of the editor window, click the triangle, select “New Tab” and type anim.h as the filename. Then copy and paste this next chunk of code:

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Gemma/Trinket_Gemma_Space_Invader_Pendant/anim.h

From the **Tools→Board** menu, select **Adafruit Trinket 8 MHz** or **Adafruit Gemma** as appropriate. Connect the USB cable between the computer and board, press the reset button, then click the upload button (right arrow icon) in the Arduino IDE. In a moment you should get a light show from the LEDs. (If it doesn’t, check your wiring against the schematics. If the code refuses to compile, most likely the anim.h file is mis-named.  
  
You’ll see an animation sequence of four Space Invaders-style aliens that repeats three times, then shuts off. To see it again, tap the reset button. (If the USB cable is still connected, there’s a long delay before it starts again — this is normal, the delay is <u>much</u> shorter when running off the battery or using the optional replay button.)

To change the animation in the future, you only need to edit or replace the contents of anim.h; it’s rare that you’ll need to edit the main source code. Ambitious programmers could write a program to convert an animated GIF into a replacement anim.h file, but for now it’s necessary to edit this file manually.  
  
There are 9 lines for each frame of animation; 8 of these are bitmap data, and the 9th line is the delay time. Each bitmap line consists of the letter ‘B’ followed by 8 binary digits (0 or 1), where ‘0’ (zero, not uppercase ‘o’) represents an “off” pixel, ‘1’ (one) an “on” pixel, and ends with a comma. The delay is given in 1/100ths of a second; 100 = 1 second, 25 = 1/4 second and so forth. The delay range is from 0 to 255; if you need longer delays, make duplicate frames.  
  
You can almost see the bitmap image in the text:

![](https://cdn-learn.adafruit.com/assets/assets/000/053/320/medium800/led_matrices_led_matrix_bitmap-diagram.png?1524713876)

You don’t _have_ to represent every row on its own line like this, but it makes the image much easier to visualize. You can see a few places in the file above where we’ve smooshed all 9 lines together to save space in the vertical direction. These frames are copies of others; we already know what they look like.  
  
After editing, press reset on the board and upload as you did before.  
  
If the program refuses to compile after editing anim.h, it’s most likely one of the following:

- Missing comma at end of line
- Missing B (upper case) at start of line
- Too many or too few digits on a line, or characters other than 0 (zero) and 1 (one)
- Spaces between characters

There’s enough room in the chip for about 320 frames of animation; anything less is fine, of course.

# Trinket / Gemma Space Invader Pendant

## Finishing Up

Connect the LiPo battery to make sure that everything runs, then stack the components and fold the wires around so that nothing is protruding. If using the onboard reset button, make sure it’s on the back side where you can reach it, not blocked by the LED backpack or battery. Other than your wires, there should be no conductive parts touching between the microcontroller board and LED backpack.  
  
The layers can be held together with a couple small pieces of foam tape, dabs of hot-melt adhesive, epoxy, etc. If you added a replay button you can encase the whole thing in a plastic bubble (such as toy vending machines dispense) with just the button protruding.  
  
The mounting holes on the LED backpack can be used for attaching a lanyard. Plastic lace from a craft store is one option. Do not use a metal chain for this…it’s conductive and could cause an electrical short as the pendant shifts around. Another option is a pin back…we have a [magnetic type](http://www.adafruit.com/products/1170) in the shop, or you can find the regular variety at most sewing or craft shops…again, be careful with this bridging electrical contacts.

![](https://cdn-learn.adafruit.com/assets/assets/000/011/403/medium800/led_matrix_sandwich.jpg?1380672550)

The little battery should last all day, depending how often you activate it. To recharge, unplug the LiPo cell from the Gemma (or JST plug if using a Trinket) and connect it to the Micro Lipo USB charger. The battery does <u>not</u> charge when the microcontroller is connected to USB.

![](https://cdn-learn.adafruit.com/assets/assets/000/011/402/medium800/led_matrix_corded.jpg?1380671580)


## Featured Products

### Adafruit GEMMA v2 - Miniature wearable electronic platform

[Adafruit GEMMA v2 - Miniature wearable electronic platform](https://www.adafruit.com/product/1222)
 **Deprecation Warning: The Gemma bit-bang USB technique it uses doesn't work as well as it did in 2014, many modern computers won't work well. So while we still carry the Gemma so that people can maintain some older projects, we no longer recommend it.** <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/1222)
[Related Guides to the Product](https://learn.adafruit.com/products/1222/guides)
### Adafruit Trinket - Mini Microcontroller - 3.3V Logic

[Adafruit Trinket - Mini Microcontroller - 3.3V Logic](https://www.adafruit.com/product/1500)
 **Deprecation Warning: The Trinket bit-bang USB technique it uses doesn't work as well as it did in 2014, many modern computers won't work well. So while we still carry the Trinket so that people can maintain some older projects, we no longer recommend it.** <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/1500)
[Related Guides to the Product](https://learn.adafruit.com/products/1500/guides)
### Adafruit Trinket - Mini Microcontroller - 5V Logic

[Adafruit Trinket - Mini Microcontroller - 5V Logic](https://www.adafruit.com/product/1501)
 **Deprecation Warning: The Trinket bit-bang USB technique it uses doesn't work as well as it did in 2014, many modern computers won't work well. So while we still carry the Trinket so that people can maintain some older projects, we no longer recommend it.** <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/1501)
[Related Guides to the Product](https://learn.adafruit.com/products/1501/guides)
### Adafruit Mini 8x8 LED Matrix w/I2C Backpack - Red

[Adafruit Mini 8x8 LED Matrix w/I2C Backpack - Red](https://www.adafruit.com/product/870)
What's better than a single LED? Lots of LEDs! A fun way to make a small display is to use an [8x8 matrix](https://www.adafruit.com/category/37_88) or a [4-digit 7-segment display](https://www.adafruit.com/category/37_103). Matrices like these are...

In Stock
[Buy Now](https://www.adafruit.com/product/870)
[Related Guides to the Product](https://learn.adafruit.com/products/870/guides)
### Adafruit Mini 8x8 LED Matrix w/I2C Backpack - Yellow

[Adafruit Mini 8x8 LED Matrix w/I2C Backpack - Yellow](https://www.adafruit.com/product/871)
What's better than a single LED? Lots of LEDs! A fun way to make a small display is to use an [8x8 matrix](https://www.adafruit.com/category/37_88) or a [4-digit 7-segment display](https://www.adafruit.com/category/37_103). Matrices like these are...

In Stock
[Buy Now](https://www.adafruit.com/product/871)
[Related Guides to the Product](https://learn.adafruit.com/products/871/guides)
### Adafruit Mini 0.8" 8x8 LED Matrix w/I2C Backpack - Yellow-Green

[Adafruit Mini 0.8" 8x8 LED Matrix w/I2C Backpack - Yellow-Green](https://www.adafruit.com/product/872)
What's better than a single LED? Lots of LEDs! A fun way to make a small display is to use an [8x8 matrix](https://www.adafruit.com/category/37_88) or a [4-digit 7-segment display](https://www.adafruit.com/category/37_103). Matrices like these are...

In Stock
[Buy Now](https://www.adafruit.com/product/872)
[Related Guides to the Product](https://learn.adafruit.com/products/872/guides)
### Adafruit Mini 8x8 LED Matrix w/I2C Backpack - Blue

[Adafruit Mini 8x8 LED Matrix w/I2C Backpack - Blue](https://www.adafruit.com/product/959)
What's better than a single LED? Lots of LEDs! A fun way to make a small display is to use an [8x8 matrix](https://www.adafruit.com/category/37_88) or a [4-digit 7-segment display](https://www.adafruit.com/category/37_103). Matrices like these are...

In Stock
[Buy Now](https://www.adafruit.com/product/959)
[Related Guides to the Product](https://learn.adafruit.com/products/959/guides)
### Adafruit Mini 8x8 LED Matrix w/I2C Backpack - Ultra Bright White

[Adafruit Mini 8x8 LED Matrix w/I2C Backpack - Ultra Bright White](https://www.adafruit.com/product/1080)
What's better than a single LED? Lots of LEDs! A fun way to make a small display is to use an [8x8 matrix](https://www.adafruit.com/category/37_88) or a [4-digit 7-segment display](https://www.adafruit.com/category/37_103). Matrices like these are...

In Stock
[Buy Now](https://www.adafruit.com/product/1080)
[Related Guides to the Product](https://learn.adafruit.com/products/1080/guides)

## Related Guides

- [Adafruit LED Backpacks](https://learn.adafruit.com/adafruit-led-backpack.md)
- [Ursula's Seashell Necklace](https://learn.adafruit.com/ursulas-seashell-necklace.md)
- [Serial Debugging with GEMMA](https://learn.adafruit.com/serial-debugging-with-gemma.md)
- [3D Printed Flora Band](https://learn.adafruit.com/3d-printed-flora-band.md)
- [Trash-Built Robotic Fish](https://learn.adafruit.com/trash-robo-fish.md)
- [Sparkle Skirt](https://learn.adafruit.com/sparkle-skirt.md)
- [Wind Blowing Emoji Prop](https://learn.adafruit.com/wind-face-emoji.md)
- [LED candles: simple, easy, cheap](https://learn.adafruit.com/led-candles-simple-easy-cheap.md)
- [Animating Multiple LED Backpacks](https://learn.adafruit.com/animating-multiple-led-backpacks.md)
- [Lie Ren's Stormflower Gun Blade](https://learn.adafruit.com/stormflower-gun-blade.md)
- [Close Encounters Hat](https://learn.adafruit.com/close-encounters-hat.md)
- [3D Printed NeoPixel LED Gas Mask](https://learn.adafruit.com/3d-printed-el-wire-laser-neopixel-led-gas-mask.md)
- [NeoPixie Dust Bag](https://learn.adafruit.com/neopixel-pixie-dust-bag.md)
- [3D Printed Unicorn Horn](https://learn.adafruit.com/3d-printed-unicorn-horn.md)
- [SNES EZ Key Bluefruit Game Pad](https://learn.adafruit.com/snes-ez-key-bluefruit-game-pad.md)
- [Free-Wired 3x3x3 NeoPixel Cube](https://learn.adafruit.com/free-wire-3x3x3-neopixel-cube.md)
