Build it step by step
0 of 7 doneWire the Charlieplex
Three nodes, six LEDs.
Light each LED in turn
Check the direction.
Add the fade
Software PWM.
Add the random timing
Pick LED and wait.
Add the light sensor
Dark means blink.
Add sleep
Wake every 8 seconds.
Hang them in the jar
Thin wire.
How do you build a firefly jar?
Tiny green LEDs blink at random like fireflies in a jar, with a light sensor so they only wake at night. You need ATtiny85-20PU microcontroller, LED 5 mm green × 6, Resistor 330 Ω × 3, LDR Photoresistor, Resistor 100 kΩ, Battery holder 2 × AA and AA alkaline battery × 2. It takes 7 steps and about 90 minutes.
- Level
- Beginner
- Time
- About 90 min
- Parts
- 7
Parts list
- Integrated CircuitsBuy
- SemiconductorsBuyLED 5 mm green × 6
- PassivesBuyResistor 330 Ω × 3
- SensorsBuy
- PassivesBuyResistor 100 kΩ × 1
- PowerBuy
- BatteriesBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Battery holder plus | ATtiny85 pin 8 (VCC) | Power (2 AA, about 3 V) |
| Battery holder minus | ATtiny85 pin 4 (GND) | Ground |
| PB0, PB1, PB2 (pins 5, 6, 7) | 330 ohm resistor each, then the Charlieplex nodes A, B, C | LED drive |
| LEDs | Six green LEDs between each pair of nodes, one in each direction | Charlieplex: A to B, B to A, B to C, C to B, A to C, C to A |
| LDR | ATtiny85 VCC to PB3 (pin 2) | Light sensor top |
| 100 k resistor | PB3 (pin 2) to GND | Light sensor bottom |
Code
Fireflies flash at random and fade softly, and both are easy to copy. Charlieplexing lets three pins drive six LEDs by turning only two pins into outputs at a time, so the LED connected the right way round between them lights. The sketch fades each firefly in and out by switching it on for longer and longer fractions of a very short cycle, and waits a random time before the next. In daylight the chip sleeps and wakes every eight seconds to check the light sensor, which keeps two AA cells going for weeks. Hang the LEDs on thin wire or glue them to the inside of a jar, leave air gaps, and keep the battery holder outside the glass, where you can change the cells. Use a jar with a wide mouth and a lid, so that nothing is hard to remove, and keep the lamp away from very small children, who might open the jar. The LEDs are small and dim, which is the point, since firefly light is gentle. Programming needs ArduinoISP or a USBasp, because the chip has no USB.
Runs on a virtual ATtiny85 in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Firefly jar: six LEDs on three pins (Charlieplexing), random blinks with a soft fade, asleep in daylight.
// Core: ATTinyCore, chip ATtiny85, clock 1 MHz internal. LDR on PB3 with 100 k to ground: dark gives a LOW reading.
#include <avr/sleep.h>
#include <avr/wdt.h>
const byte PINS[3] = {0, 1, 2};
// anode, cathode for each of the six LEDs
const byte LEDS[6][2] = {{0,1},{1,0},{1,2},{2,1},{0,2},{2,0}};
ISR(WDT_vect) {}
void allOff() { for (byte i = 0; i < 3; i++) pinMode(PINS[i], INPUT); }
void lightFor(byte led, unsigned int on_us, unsigned int off_us) {
byte a = PINS[LEDS[led][0]], c = PINS[LEDS[led][1]];
allOff();
pinMode(a, OUTPUT); pinMode(c, OUTPUT);
digitalWrite(c, LOW); digitalWrite(a, HIGH);
delayMicroseconds(on_us);
allOff();
delayMicroseconds(off_us);
}
void blinkFirefly(byte led) {
for (int s = 0; s <= 40; s++) { // fade in then out using software PWM
int level = s <= 20 ? s : 40 - s; // 0..20
for (int rep = 0; rep < 6; rep++) lightFor(led, level * 20, 420 - level * 20);
}
}
bool isDark() { return analogRead(3) < 400; } // about 1.2 V at 3 V supply
void sleepSeconds8() {
allOff();
MCUSR = 0; WDTCR = _BV(WDCE) | _BV(WDE); WDTCR = _BV(WDIE) | _BV(WDP3) | _BV(WDP0); // 8 s
ADCSRA &= ~_BV(ADEN);
set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sleep_cpu(); sleep_disable();
ADCSRA |= _BV(ADEN);
}
void setup() { allOff(); randomSeed(analogRead(3) + analogRead(2)); }
void loop() {
if (!isDark()) { sleepSeconds8(); return; } // daytime: check every 8 seconds
blinkFirefly(random(6));
unsigned long wait = random(300, 2500);
unsigned long t0 = millis();
while (millis() - t0 < wait) delay(10);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Dark room | Random fireflies blink and fade |
| Cover or switch on a light | Blinking stops |
| Each LED | Lights in turn during a test |
| Battery check | A few microamps while asleep |
If it does not work
Two LEDs light together
One LED is in backwards.
Never dark
Raise the threshold or use a smaller resistor.
Blinks in daylight
Check the 100 k resistor to ground.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the firefly jar?
ATtiny85-20PU microcontroller, LED 5 mm green × 6, Resistor 330 Ω × 3, LDR Photoresistor, Resistor 100 kΩ, Battery holder 2 × AA and AA alkaline battery × 2.
How long does the firefly jar take to build?
About 90 minutes for a beginner maker, in 7 steps.
How do I know the firefly jar works?
Dark room: Random fireflies blink and fade. Cover or switch on a light: Blinking stops. Each LED: Lights in turn during a test.
Two LEDs light together: what should I check?
One LED is in backwards.
Never dark: what should I check?
Raise the threshold or use a smaller resistor.
Blinks in daylight: what should I check?
Check the 100 k resistor to ground.
