Egg hunt finder on an Arduino Uno
A hot and cold finder for glowing eggs hidden in a dim room: a light sensor measures the change, five WS2812B LEDs warm from blue to red and a piezo beeps faster as you get close. The whole build, an Arduino Uno and 4 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 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
What you'll learn
- Measuring a change against a stored baseline
- Reading a light sensor with analogRead
- Addressable LEDs with Adafruit_NeoPixel
- Playing tones on a piezo with tone
- Mapping a number to a small set of levels
- Difficulty: Beginner
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 6
How it works
At start-up the finder reads the light sensor 20 times over two seconds and averages them. That number is the room's normal light, the "cold" level. The module reads lower as light gets brighter.
In the loop it subtracts the live reading from that room level. The bigger the drop, the closer the glowing egg. Every 60 counts of drop lights one more LED on the strip of five, from one blue LED up to five red ones.
The piezo beeps faster and higher as the level rises, from about one beep a second to five. At the top level it plays one steady note.
Parts list
6 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Photoresistor sensor moduleIn Desk Buddy Kit, Escape Box Kit, First Inventions Kit.
- 1 × Resistor, 330 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Addressable RGB LEDsIn Claw Machine Kit, Halloween Prop Kit, Christmas Lights Kit.
- 1 × Piezo speakerIn Starter Kit, Claw Machine Kit, Vending Machine Kit.
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 |
|---|---|
| Piezo speaker pin 1 | Arduino Uno R3 pin 8 |
| Resistor, 330 Ω pin 1 | Arduino Uno R3 pin 6 |
| Photoresistor sensor module pin VCC | Arduino Uno R3 pin 5V |
| Addressable RGB LEDs pin VDD | Arduino Uno R3 pin 5V |
| Photoresistor sensor module pin GND | Arduino Uno R3 pin GND |
| Addressable RGB LEDs pin VSS | Arduino Uno R3 pin GND |
| Piezo speaker pin 2 | Arduino Uno R3 pin GND |
| Photoresistor sensor module pin AO | Arduino Uno R3 pin A0 |
| Addressable RGB LEDs pin DIN | Resistor, 330 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Egg hunt finder: a hot and cold beeper for an egg hunt in a dim room. Each
// hidden egg has a small light inside, and the finder beeps faster and glows
// warmer the closer you point it to one.
//
// light sensor module AO -> A0, VCC -> 5 V, GND -> GND (in a short tube)
// piezo pin 8 -> piezo -> GND
// meter 5 WS2812B LEDs, DIN <- 330 ohms <- pin 6
//
// The eggs are plastic eggs with a coin-cell LED tea light inside, hidden
// around a room with the curtains drawn. Put the light sensor at the end of a
// short cardboard tube, so it sees where it points and not the whole room.
//
// When it starts, the finder reads the room for two seconds and keeps that as
// "cold". The module's reading drops as the light grows, so the further the
// reading falls below the room's, the closer the egg. The meter goes from one
// blue LED (cold) through green and yellow to five red ones (hot!), and the
// beeps come faster as it warms, until it sings one steady note.
//
// It works in daylight too, pointed at a bright window, which makes it a fun
// way to show how a light sensor sees the room.
//
// Things to try. Drag the light sensor's slider up toward an egg. Turn the
// room darker first, then start: the finder only measures the change.
#include <Adafruit_NeoPixel.h>
const int PIEZO_PIN = 8;
const int LEDS = 5;
Adafruit_NeoPixel meter(LEDS, 6, NEO_GRB + NEO_KHZ800);
const uint32_t WARMTH[LEDS] = {0x0040FF, 0x00C080, 0xC0C000, 0xFF6000, 0xFF0000};
const char *WORDS[LEDS + 1] = {"Cold", "Cool", "Warm", "Warmer", "Hot", "Hot! You found it!"};
int room = 0;
int lastLevel = -1;
unsigned long nextBeep = 0;
void setup() {
Serial.begin(9600);
meter.begin();
meter.setBrightness(120);
long sum = 0;
for (int i = 0; i < 20; i++) {
sum += analogRead(A0);
delay(100);
}
room = sum / 20;
Serial.print("Egg finder ready. The room reads ");
Serial.println(room);
}
void loop() {
int closer = room - analogRead(A0); // brighter light reads lower
int level = constrain(closer / 60, 0, LEDS);
meter.clear();
for (int i = 0; i < (level == 0 ? 1 : level); i++) meter.setPixelColor(i, WARMTH[level == 0 ? 0 : level - 1]);
meter.show();
if (level != lastLevel) {
lastLevel = level;
Serial.println(WORDS[level]);
}
unsigned long now = millis();
if (level == LEDS) {
tone(PIEZO_PIN, 1568); // found it: one steady note
} else if (now >= nextBeep) {
// From a beep every second when cold to five a second when warm.
tone(PIEZO_PIN, 880 + level * 150, 40);
nextBeep = now + 1000 - level * 180;
}
delay(20);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Starts the LED strip, then takes 20 readings from A0, 100 ms apart, and stores their average in room. Do not change the light during these two seconds, because this is the baseline the whole hunt is measured against.
void setup() {
Serial.begin(9600);
meter.begin();
meter.setBrightness(120);
long sum = 0;
for (int i = 0; i < 20; i++) {
sum += analogRead(A0);
delay(100);
}
room = sum / 20;
Serial.print("Egg finder ready. The room reads ");
Serial.println(room);
}loop sketch.ino
Works out closer as the room reading minus the current reading, and turns it into a level from 0 to 5 by dividing by 60. It draws that many LEDs in the matching color. At level 5 it plays a steady 1568 Hz note. Below that it schedules short beeps, with the pitch going up and the gap shrinking as the level rises.
void loop() {
int closer = room - analogRead(A0); // brighter light reads lower
int level = constrain(closer / 60, 0, LEDS);
meter.clear();
for (int i = 0; i < (level == 0 ? 1 : level); i++) meter.setPixelColor(i, WARMTH[level == 0 ? 0 : level - 1]);
meter.show();
if (level != lastLevel) {
lastLevel = level;
Serial.println(WORDS[level]);
}
unsigned long now = millis();
if (level == LEDS) {
tone(PIEZO_PIN, 1568); // found it: one steady note
} else if (now >= nextBeep) {
// From a beep every second when cold to five a second when warm.
tone(PIEZO_PIN, 880 + level * 150, 40);
nextBeep = now + 1000 - level * 180;
}
delay(20);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- It always shows cold, or jumps straight to hot This project
The baseline was taken in the wrong light. Set the room's light first, then start the sketch so the two-second calibration sees it. - The LEDs show the wrong colors or nothing This project
The strip's data goes from pin 6 through the 330 Ω resistor to DIN. Check that resistor and the strip's 5 V and ground. - The digital output never changes, or is always on, no matter the light. Photoresistor sensor module
Turn the trimmer on the module until DO flips at the light level you want. The digital output is set by that threshold, and which direction means bright depends on the board. - Analog readings go down when it gets brighter, and the code expects the opposite. Photoresistor sensor module
The LDR sits in a divider, so the direction depends on how the module is built. Print the raw value in the light and dark, then flip it in code with 1023 minus the reading if needed. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The first LED flickers, shows wrong colors, or the whole strip glitches. Addressable RGB LEDs
Join the grounds of the strip and the controller. Add a 300 to 500 ohm resistor in series with the data line. Put a 1000 uF capacitor across the strip's 5 V and GND. - The strip works at low brightness but dims, turns yellow, or resets when many LEDs go white. Addressable RGB LEDs
Each LED can draw about 60 mA at full white. Use a 5 V supply sized for the count, feed power in at several points, and lower the brightness in code.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.