Projects / Virtual pet keyring

Virtual pet keyring

Intermediate5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. Wire the screen

    I2C to PB0 and PB2.

  2. Show a face

    Print one line of text.

  3. Add three numbers

    Hunger, joy and mess.

  4. Add the buttons

    Feed, play and clean.

  5. Add the slow clock

    Wake from sleep every 0.25 s.

  6. ++

    Add screen-off

    Save the coin cell.

  7. Save to EEPROM

    Survive a battery change.

In short

How do you build a virtual pet keyring?

A tiny OLED pet gets hungry and sleepy, cared for with three buttons, running for weeks on a coin cell. You need ATtiny85-20PU microcontroller, 0.49 inch micro OLED (64x32), Tactile push button 6 mm × 3, CR2032 coin cell and CR2032 coin cell holder. It takes 7 steps and about 300 minutes.

Level
Intermediate
Time
About 300 min
Parts
5

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
ATtiny85 pin 8 (VCC)Coin cell holder plusPower
ATtiny85 pin 4 (GND)Coin cell holder minusGround
OLED SDAATtiny85 PB0 (pin 5)I2C data
OLED SCLATtiny85 PB2 (pin 7)I2C clock
OLED VCC and GNDCell plus and minusPower (3 V is fine for most 0.49 inch modules)
Button FEEDPB1 (pin 6) to GNDUses the internal pull-up
Button PLAYPB3 (pin 2) to GNDUses the internal pull-up
Button CLEANPB4 (pin 3) to GNDUses the internal pull-up

Code

The pet is a handful of numbers and a few rules, which is exactly what a state machine is: hunger rises, joy falls and mess builds slowly, and the face on the screen is chosen from the numbers. The chip sleeps almost all of the time and wakes four times a second to look at the buttons, then switches the screen off after ten seconds, because the screen is what drains a coin cell. The ten-minute and one-hour steps are short on purpose so you can see behaviour in an evening; stretch them for a pet that lives for days. State is saved once an hour so it survives a battery change. If it shows nothing, check the I2C wiring first, then try the NONAME screen type. Programming needs ArduinoISP or a USBasp programmer; the ATtiny has no USB. A CR2032 is not for children to chew on, so keep the finished keyring away from toddlers and use a holder that closes.

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).

// Virtual pet on an ATtiny85. Core: ATTinyCore, chip ATtiny85, clock 1 MHz internal. Library: U8g2 (U8x8 text mode, low RAM).
// Program it with a second Arduino running ArduinoISP, or a USBasp.
#include <U8x8lib.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/power.h>
#include <EEPROM.h>
U8X8_SSD1306_64X32_1F_HW_I2C oled(U8X8_PIN_NONE);   // try 64X32_NONAME if the picture is shifted
const uint8_t FEED = 1, PLAY = 3, CLEAN = 4;
uint8_t hunger = 3, joy = 3, mess = 0, age = 0;     // 0..9 for each
bool asleepScreen = false;
unsigned long ticks = 0;                             // one tick = about 0.25 second
unsigned long lastInput = 0;
ISR(WDT_vect) {}
void saveState() { EEPROM.update(0, hunger); EEPROM.update(1, joy); EEPROM.update(2, mess); EEPROM.update(3, age); }
void loadState() { if (EEPROM.read(4) == 0x42) { hunger = EEPROM.read(0) % 10; joy = EEPROM.read(1) % 10; mess = EEPROM.read(2) % 10; age = EEPROM.read(3); } else EEPROM.update(4, 0x42); }
void napQuarterSecond() {
  MCUSR = 0; WDTCR = _BV(WDCE) | _BV(WDE); WDTCR = _BV(WDIE) | _BV(WDP2);   // interrupt every 0.25 s
  set_sleep_mode(SLEEP_MODE_PWR_DOWN); ADCSRA = 0; sleep_enable(); sleep_cpu(); sleep_disable();
}
void draw() {
  oled.setPowerSave(0);
  oled.clear();
  oled.setCursor(0, 0);
  if (hunger >= 8 || joy <= 1) oled.print("(T_T) sad");
  else if (mess >= 7) oled.print("(>_<) yuck");
  else if (joy >= 7 && hunger <= 4) oled.print("(^_^) happy");
  else oled.print("(-_-) ok");
  oled.setCursor(0, 2); oled.print("F"); oled.print(9 - hunger); oled.print(" J"); oled.print(joy);
  oled.setCursor(0, 3); oled.print("M"); oled.print(mess); oled.print(" A"); oled.print(age);
}
void setup() {
  pinMode(FEED, INPUT_PULLUP); pinMode(PLAY, INPUT_PULLUP); pinMode(CLEAN, INPUT_PULLUP);
  power_timer1_disable(); power_usi_disable();
  oled.begin(); oled.setFont(u8x8_font_chroma48medium8_r);
  loadState(); draw(); lastInput = 0;
}
void loop() {
  napQuarterSecond(); ticks++;
  bool pressed = false;
  if (!digitalRead(FEED))  { if (hunger > 0) hunger--; pressed = true; }
  if (!digitalRead(PLAY))  { if (joy < 9) joy++; if (hunger < 9 && ticks % 4 == 0) hunger++; pressed = true; }
  if (!digitalRead(CLEAN)) { mess = 0; pressed = true; }
  if (pressed) { lastInput = ticks; draw(); delay(150); }
  if (ticks % 2400 == 0) { if (hunger < 9) hunger++; if (joy > 0) joy--; if (random(3) == 0 && mess < 9) mess++; }   // every 10 minutes
  if (ticks % 14400 == 0) { if (age < 250) age++; saveState(); }                                                    // every hour
  if (ticks - lastInput > 40) { oled.setPowerSave(1); }          // screen off after about 10 s
  else if (!pressed && ticks % 4 == 0) draw();
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
BootFace and numbers appear
FEED pressedHunger falls
Wait 10 minutesHunger rises, joy falls
Remove and replace the cellNumbers are kept

If it does not work

Blank screen

Check SDA and SCL, then try the NONAME screen type.

Upload fails

Check the programmer wiring and the chip selection.

Cell dies in days

Check the screen switches off and no pin floats.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the virtual pet keyring?

ATtiny85-20PU microcontroller, 0.49 inch micro OLED (64x32), Tactile push button 6 mm × 3, CR2032 coin cell and CR2032 coin cell holder.

How long does the virtual pet keyring take to build?

About 300 minutes for a intermediate maker, in 7 steps.

How do I know the virtual pet keyring works?

Boot: Face and numbers appear. FEED pressed: Hunger falls. Wait 10 minutes: Hunger rises, joy falls.

Blank screen: what should I check?

Check SDA and SCL, then try the NONAME screen type.

Upload fails: what should I check?

Check the programmer wiring and the chip selection.

Cell dies in days: what should I check?

Check the screen switches off and no pin floats.