NeoPixel matrix smiley
Draw a colour-changing smiley face on an 8×8 NeoPixel matrix from a bitmap.
Displays · Intermediate
In a real product: LED wearable badge (Wearables): A colour matrix shows animations.
An 8×8 NeoPixel matrix is 64 addressable LEDs on one data wire. To draw a picture you work out which pixel number each row and column is, then colour the pixels a bitmap says are on.
What you'll learn
- Storing a picture as a bitmap: one byte per row, one bit per pixel
- Turning a row and column into a pixel index
- Animating colour over time
How it works
DIN goes to pin 6. This matrix is wired row by row: pixel 0 is the top-left, 7 the top-right, 8 the start of the second row, so pixel = row × 8 + column. (Some real matrices zig-zag; then odd rows run backwards.)
SMILEY[] has one byte per row, with the leftmost pixel in the highest bit. For each pixel the loop tests its bit with bitRead(SMILEY[row], 7 - col) and colours it, or turns it off. The hue moves on each frame, so the face cycles through the rainbow.
Try this
- Draw your own 8×8 icon: a heart, an arrow, an invader.
- Scroll a letter across the matrix by shifting the columns each frame.
- Swap between two bitmaps to make the face wink.
sketch.ino
// An 8x8 NeoPixel matrix on pin 6: a smiley face that cycles through the colours.
#include <Adafruit_NeoPixel.h>
const int PIN = 6;
const int SIZE = 8;
Adafruit_NeoPixel matrix(SIZE * SIZE, PIN, NEO_GRB + NEO_KHZ800);
// One byte a row; the leftmost pixel is the highest bit.
const byte SMILEY[SIZE] = {
0b00111100,
0b01000010,
0b10100101,
0b10000001,
0b10100101,
0b10011001,
0b01000010,
0b00111100,
};
uint16_t hue = 0;
void setup() {
matrix.begin();
matrix.setBrightness(60);
}
void loop() {
uint32_t color = matrix.gamma32(matrix.ColorHSV(hue));
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
bool on = bitRead(SMILEY[row], 7 - col);
matrix.setPixelColor(row * SIZE + col, on ? color : 0);
}
}
matrix.show();
hue += 256;
delay(30);
}