Hatching chick on an Arduino Uno
An egg on an 8x8 MAX7219 matrix wobbles, cracks three times and hatches into a chick that blinks and peeps on a piezo. A button taps the shell to help it along. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $33. On Mokxi they're free.
What you'll learn
- Drawing pictures as eight bytes, one per matrix row
- Driving a MAX7219 matrix with the LedControl library
- Staging an animation with millis() and a random wait
- Reading a button with INPUT_PULLUP
- Making sound with tone()
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 5
How it works
An 8x8 LED matrix shows an egg. The sketch moves it through stages: resting, wobbling, three cracks, then hatched. Each stage waits a random 1.5 to 3.5 seconds before the next one begins.
Pressing the button skips straight to the next stage. When the egg hatches, a chick appears for 6 seconds, blinks, and the piezo on pin 8 peeps. Then a new egg starts.
Parts list
5 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Piezo speakerIn Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × LED matrix, 8x8In Falling Blocks Kit.
Kits that can build it
This starter kit has every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| LED matrix, 8x8 pin CLK | Arduino Uno R3 pin 13 |
| LED matrix, 8x8 pin DIN | Arduino Uno R3 pin 11 |
| LED matrix, 8x8 pin CS | Arduino Uno R3 pin 10 |
| Piezo speaker pin 1 | Arduino Uno R3 pin 8 |
| Pushbutton pin 1a | Arduino Uno R3 pin 2 |
| LED matrix, 8x8 pin VCC | Arduino Uno R3 pin 5V |
| Pushbutton pin 2a | Arduino Uno R3 pin GND |
| Piezo speaker pin 2 | Arduino Uno R3 pin GND |
| LED matrix, 8x8 pin GND | Arduino Uno R3 pin GND |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Hatching chick: an egg on an 8x8 LED matrix wobbles, cracks and hatches
// into a chick that blinks and peeps. Tap the button to help it along.
//
// MAX7219 8x8 matrix module: DIN -> pin 11, CLK -> pin 13, CS -> pin 10,
// VCC -> 5 V, GND -> GND
// button pin 2 -> button -> GND (INPUT_PULLUP): a tap on the shell
// piezo pin 8 -> piezo -> GND
//
// A spring build for Easter, or for any classroom that has watched real eggs
// in an incubator. The story goes in stages: the egg rests, wobbles, shows one
// crack, then two, then three, and then the top of the shell opens and the
// chick looks out. Each stage waits a random few seconds, so it never plays
// out quite the same way twice, and a press of the button moves straight on
// to the next stage.
//
// The pictures are eight bytes each, one per row. A crack is drawn by
// clearing a few dots of the egg, and a wobble by shifting every row one
// place left or right, so the whole animation is three small tables.
//
// Things to try. Press the button to tap the shell. Draw a different hatchling
// in CHICK. Make the chick peep a tune.
#include <LedControl.h>
const int BUTTON_PIN = 2;
const int PIEZO_PIN = 8;
LedControl matrix(11, 13, 10, 1); // DIN, CLK, CS, one module
const uint8_t EGG[8] = {0b00011000, 0b00111100, 0b01111110, 0b01111110,
0b01111110, 0b01111110, 0b00111100, 0b00000000};
// The dots each crack clears from the egg.
const uint8_t CRACKS[3][8] = {
{0, 0, 0, 0b00010000, 0, 0, 0, 0},
{0, 0, 0b00001000, 0b00010100, 0, 0, 0, 0},
{0, 0, 0b00101000, 0b01010100, 0b00000010, 0, 0, 0},
};
const uint8_t CHICK[8] = {0b00011000, 0b00111100, 0b00101110, 0b00111100,
0b01011010, 0b01111110, 0b00111100, 0b00000000};
int stage = 0; // 0 resting, 1 wobbling, 2 to 4 cracks, 5 hatched
unsigned long nextStage = 0;
int hatched = 0;
void draw(const uint8_t rows[8], int shift, int cracks) {
for (int r = 0; r < 8; r++) {
uint8_t row = rows[r];
for (int c = 0; c < cracks; c++) row &= ~CRACKS[c][r];
if (shift > 0) row >>= 1;
if (shift < 0) row <<= 1;
matrix.setRow(0, r, row);
}
}
void peep() {
for (int f = 2000; f < 3200; f += 200) {
tone(PIEZO_PIN, f, 15);
delay(15);
}
}
void advance() {
stage++;
if (stage > 5) {
stage = 0;
Serial.println("A new egg");
} else if (stage == 1) {
Serial.println("The egg is wobbling");
} else if (stage <= 4) {
Serial.print("Crack ");
Serial.println(stage - 1);
tone(PIEZO_PIN, 300, 30);
} else {
hatched++;
Serial.print("Peep! Chick ");
Serial.print(hatched);
Serial.println(" has hatched");
peep();
}
nextStage = millis() + (stage == 5 ? 6000 : random(1500, 3500));
}
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
matrix.shutdown(0, false);
matrix.setIntensity(0, 6);
matrix.clearDisplay(0);
randomSeed(analogRead(A0));
nextStage = millis() + 2000;
Serial.println("An egg, keeping warm");
}
void loop() {
unsigned long now = millis();
if (digitalRead(BUTTON_PIN) == LOW) {
advance();
while (digitalRead(BUTTON_PIN) == LOW) delay(10);
} else if (now >= nextStage) {
advance();
}
if (stage == 5) {
// The chick blinks every so often and peeps now and then.
bool blink = (now / 150) % 20 == 0;
uint8_t rows[8];
for (int r = 0; r < 8; r++) rows[r] = CHICK[r];
if (blink) rows[2] |= 0b00010000;
draw(rows, 0, 0);
if (random(0, 400) == 0) peep();
} else {
// From the first wobble on, the egg rocks a little now and then.
int shift = 0;
if (stage >= 1 && (now / 200) % 5 == 0) shift = (now / 1000) % 2 ? 1 : -1;
draw(EGG, shift, stage >= 2 ? stage - 1 : 0);
}
delay(20);
}
Code walkthrough
The key functions of the sketch, and what each one does.
draw sketch.ino
Sends one picture to the matrix, row by row. It clears the dots of each crack that has happened so far, then shifts every row one place left or right to make the egg wobble. Cracks and wobbles are just bit tricks on the same EGG table.
void draw(const uint8_t rows[8], int shift, int cracks) {
for (int r = 0; r < 8; r++) {
uint8_t row = rows[r];
for (int c = 0; c < cracks; c++) row &= ~CRACKS[c][r];
if (shift > 0) row >>= 1;
if (shift < 0) row <<= 1;
matrix.setRow(0, r, row);
}
}advance sketch.ino
Moves the story to the next stage and prints a message about it. Cracks play a low 300 Hz tick, and the last stage counts the hatched chick and calls peep. It also picks when the next stage will start: 6000 ms for the chick, a random 1500 to 3500 ms otherwise.
void advance() {
stage++;
if (stage > 5) {
stage = 0;
Serial.println("A new egg");
} else if (stage == 1) {
Serial.println("The egg is wobbling");
} else if (stage <= 4) {
Serial.print("Crack ");
Serial.println(stage - 1);
tone(PIEZO_PIN, 300, 30);
} else {
hatched++;
Serial.print("Peep! Chick ");
Serial.print(hatched);
Serial.println(" has hatched");
peep();
}
nextStage = millis() + (stage == 5 ? 6000 : random(1500, 3500));
}loop sketch.ino
Checks the button first. A press calls advance and waits for release. Otherwise it advances when the timer runs out. Then it draws: the chick blinks and sometimes peeps, or the egg rocks a little now and then.
void loop() {
unsigned long now = millis();
if (digitalRead(BUTTON_PIN) == LOW) {
advance();
while (digitalRead(BUTTON_PIN) == LOW) delay(10);
} else if (now >= nextStage) {
advance();
}
if (stage == 5) {
// The chick blinks every so often and peeps now and then.
bool blink = (now / 150) % 20 == 0;
uint8_t rows[8];
for (int r = 0; r < 8; r++) rows[r] = CHICK[r];
if (blink) rows[2] |= 0b00010000;
draw(rows, 0, 0);
if (random(0, 400) == 0) peep();
} else {
// From the first wobble on, the egg rocks a little now and then.
int shift = 0;
if (stage >= 1 && (now / 200) % 5 == 0) shift = (now / 1000) % 2 ? 1 : -1;
draw(EGG, shift, stage >= 2 ? stage - 1 : 0);
}
delay(20);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The matrix stays dark This project
Check that CLK is on pin 13, DIN on pin 11 and CS on pin 10, and that VCC and GND are connected. TheLedControlpin order is DIN, CLK, CS. - The button does nothing This project
Wire it between pin 2 and GND. The sketch usesINPUT_PULLUP, so a press reads LOW. - The input reads random HIGH and LOW when the button is not pressed. Pushbutton
A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW. - The button seems always pressed, or never changes. Pushbutton
A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected. - A passive piezo stays silent when you just set the pin HIGH. Piezo speaker
A passive piezo needs a changing signal. Use tone(pin, frequency) or a PWM wave. Around 2 to 4 kHz is loudest. A buzzer with a built in oscillator makes sound from plain DC instead. - Sound is very quiet or the pin seems overloaded. Piezo speaker
Drive the piezo from a pin directly, or through a 100 ohm resistor. For a bigger buzzer, switch it with a transistor. Check that you wired the right two legs and joined grounds. - The matrix is blank after upload. LED matrix
On power up the MAX7219 is in shutdown mode with intensity 0. Wake it, set intensity, set scan limit to all 8 digits, and clear the display. - Dim, flickering, or wrong rows. LED matrix
Set the ISET resistor right for your LEDs (about 10 kohm for 40 mA). Power from 5 V with a 0.1 uF capacitor, and chain DOUT to the next DIN.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.