Arduino Virtual Pet using Arduino Uno – OLED Interactive Game in Wokwi

 

Arduino Virtual Pet using Arduino Uno – OLED Interactive Game in Wokwi

Build your own Virtual Pet Game using Arduino Uno in the Wokwi simulator! This beginner-friendly and fun electronics project lets you create a digital Tamagotchi-style pet that reacts to button inputs. Feed it, play with it, and let it sleep while monitoring its status on a 128x64 OLED display.

This interactive Arduino project is perfect for students, STEM learners, robotics enthusiasts, and beginners who want hands-on experience with embedded systems, OLED display interfacing, and real-time game logic programming.


 Components Used (Wokwi Simulation)

  • Arduino Uno

  • SSD1306 128x64 OLED Display (I2C)

  • Push Buttons (Feed, Play, Sleep)

  • Buzzer (for sound effects)

  • Wokwi


 How the Arduino Virtual Pet Works

The Arduino Uno communicates with the SSD1306 OLED display via I2C (A4 – SDA, A5 – SCL). The virtual pet has real-time status parameters:

  •  Happiness Level

  •  Hunger Level

  •  Energy Level

  •  Age Counter

 Button Interactions

  • Feed Button (Pin 2): Reduces hunger and slightly increases happiness

  • Play Button (Pin 3): Boosts happiness but reduces energy

  • Sleep Button (Pin 4): Restores energy

If ignored, the pet’s stats gradually decrease every 5 seconds, making the system dynamic and interactive. If hunger reaches maximum or happiness drops to zero, the pet “dies” — adding a real game logic element.


 Sound Effects & Animations

The buzzer connected to Pin 8 produces:

  • Startup melody

  • Eating sound

  • Happy tone

  • Sleep melody

  • Warning beeps

  • Death sound

The OLED displays:

  • Animated facial expressions

  • Mood-based reactions (happy, sad, tired, hungry)

  • Bouncing animation

  • Live status bars

  • Age counter

  • R.I.P screen when stats reach critical levels


 Key Features

✔ Animated pet expressions on OLED
✔ Real-time status bar indicators
✔ Button-controlled interaction
✔ Sound feedback using buzzer
✔ Automatic stat decay system
✔ Beginner-friendly Arduino logic
✔ Fully testable in Wokwi (no hardware required)


 Learning Outcomes

This project helps learners understand:

  • OLED display interfacing with Arduino

  • I2C communication basics

  • Push button input handling using INPUT_PULLUP

  • Variables and conditional statements

  • State-based programming logic

  • Basic embedded game development

  • Real-time system updates using millis()


 Applications

  • STEM classroom activity

  • Arduino beginner coding project

  • Robotics club demonstration

  • DIY handheld game device concept

  • Embedded systems mini-game

Code:
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Button pins
const int BTN_FEED = 2;
const int BTN_PLAY = 3;
const int BTN_SLEEP = 4;

// Buzzer pin
const int BUZZER = 8;

// Pet stats
int hunger = 50;
int happiness = 70;
int energy = 80;
int age = 0;

// Animation
int frame = 0;
unsigned long lastUpdate = 0;
unsigned long lastAnim = 0;

// Sound enabled flag
bool soundEnabled = true;

void setup() {
  Serial.begin(9600);
  Serial.println("Starting Virtual Pet with Sounds...");
 
  // Setup buttons
  pinMode(BTN_FEED, INPUT_PULLUP);
  pinMode(BTN_PLAY, INPUT_PULLUP);
  pinMode(BTN_SLEEP, INPUT_PULLUP);
 
  // Setup buzzer
  pinMode(BUZZER, OUTPUT);
 
  // Initialize display
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("Display failed!");
    for(;;);
  }
 
  Serial.println("Display OK!");
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
 
  // Show welcome screen with startup sound
  playStartupSound();
  display.setTextSize(2);
  display.setCursor(10, 20);
  display.println("Virtual");
  display.setCursor(25, 40);
  display.println("Pet!");
  display.display();
  delay(2000);
}

void loop() {
  unsigned long now = millis();
 
  // Update stats every 5 seconds
  if (now - lastUpdate > 5000) {
    hunger = min(100, hunger + 3);
    happiness = max(0, happiness - 2);
    energy = max(0, energy - 2);
    age++;
    lastUpdate = now;
   
    // Warning beep if stats getting critical
    if (hunger > 80 || happiness < 20 || energy < 20) {
      playWarningSound();
    }
   
    Serial.print("H:"); Serial.print(hunger);
    Serial.print(" E:"); Serial.print(happiness);
    Serial.print(" T:"); Serial.println(energy);
  }
 
  // Animation frame
  if (now - lastAnim > 500) {
    frame = (frame + 1) % 2;
    lastAnim = now;
  }
 
  // Check buttons
  if (digitalRead(BTN_FEED) == LOW) {
    hunger = max(0, hunger - 30);
    happiness = min(100, happiness + 5);
    playEatSound();
    showMessage("Fed!");
    delay(500);
  }
 
  if (digitalRead(BTN_PLAY) == LOW) {
    if (energy > 20) {
      happiness = min(100, happiness + 20);
      energy = max(0, energy - 15);
      playHappySound();
      showMessage("Fun!");
      delay(500);
    } else {
      playSadSound();
      showMessage("Too tired!");
      delay(500);
    }
  }
 
  if (digitalRead(BTN_SLEEP) == LOW) {
    energy = min(100, energy + 40);
    playSleepSound();
    showMessage("Zzz...");
    delay(1000);
  }
 
  // Draw screen
  display.clearDisplay();
 
  // Check if dead
  if (hunger >= 100 || happiness <= 0) {
    playDeathSound();
    drawDead();
  } else {
    drawPet();
    drawStats();
    drawButtons();
  }
 
  display.display();
  delay(100);
}

// ============ SOUND FUNCTIONS ============

void playStartupSound() {
  // Happy startup melody
  tone(BUZZER, 523, 150); // C
  delay(150);
  tone(BUZZER, 659, 150); // E
  delay(150);
  tone(BUZZER, 784, 200); // G
  delay(200);
}

void playEatSound() {
  // Eating sound - quick ascending notes
  tone(BUZZER, 400, 100);
  delay(120);
  tone(BUZZER, 500, 100);
  delay(120);
  tone(BUZZER, 600, 100);
  delay(120);
}

void playHappySound() {
  // Happy playful sound
  tone(BUZZER, 800, 100);
  delay(120);
  tone(BUZZER, 1000, 100);
  delay(120);
  tone(BUZZER, 1200, 150);
  delay(170);
}

void playSleepSound() {
  // Sleepy descending melody
  tone(BUZZER, 700, 200);
  delay(220);
  tone(BUZZER, 600, 200);
  delay(220);
  tone(BUZZER, 500, 300);
  delay(320);
}

void playSadSound() {
  // Sad descending notes
  tone(BUZZER, 400, 200);
  delay(220);
  tone(BUZZER, 300, 300);
  delay(320);
}

void playWarningSound() {
  // Warning beep
  tone(BUZZER, 800, 100);
  delay(150);
  tone(BUZZER, 800, 100);
  delay(150);
}

void playDeathSound() {
  // Sad death sound (only plays once)
  static bool deathSoundPlayed = false;
  if (!deathSoundPlayed) {
    tone(BUZZER, 500, 300);
    delay(320);
    tone(BUZZER, 400, 300);
    delay(320);
    tone(BUZZER, 300, 500);
    delay(520);
    deathSoundPlayed = true;
  }
}

// ============ DRAWING FUNCTIONS ============

void drawPet() {
  int x = 64;
  int y = 30;
 
  // Body
  display.fillCircle(x, y, 12, SSD1306_WHITE);
 
  // Determine face based on stats
  if (hunger > 70) {
    // Hungry face
    display.fillCircle(x-4, y-2, 2, SSD1306_BLACK);
    display.fillCircle(x+4, y-2, 2, SSD1306_BLACK);
    display.fillCircle(x, y+4, 3, SSD1306_BLACK); // Open mouth
  } else if (energy < 30) {
    // Tired face
    display.drawLine(x-5, y-2, x-2, y-2, SSD1306_BLACK);
    display.drawLine(x+2, y-2, x+5, y-2, SSD1306_BLACK);
    if (frame == 0) {
      display.setCursor(x+12, y-8);
      display.setTextSize(1);
      display.print("Z");
    }
  } else if (happiness < 40) {
    // Sad face
    display.fillCircle(x-4, y-2, 2, SSD1306_BLACK);
    display.fillCircle(x+4, y-2, 2, SSD1306_BLACK);
    display.drawLine(x-3, y+6, x, y+4, SSD1306_BLACK);
    display.drawLine(x, y+4, x+3, y+6, SSD1306_BLACK);
  } else {
    // Happy face
    display.fillCircle(x-4, y-2, 2, SSD1306_BLACK);
    display.fillCircle(x+4, y-2, 2, SSD1306_BLACK);
    display.drawLine(x-5, y+3, x-2, y+5, SSD1306_BLACK);
    display.drawLine(x-2, y+5, x+2, y+5, SSD1306_BLACK);
    display.drawLine(x+2, y+5, x+5, y+3, SSD1306_BLACK);
   
    // Bounce animation
    if (frame == 0) y -= 2;
  }
 
  // Feet
  display.fillCircle(x-8, y+13, 3, SSD1306_WHITE);
  display.fillCircle(x+8, y+13, 3, SSD1306_WHITE);
 
  // Draw musical note if happy
  if (happiness > 70 && frame == 0) {
    display.setCursor(x+15, y-5);
    display.setTextSize(1);
    display.print((char)3); // Musical note character
  }
}

void drawStats() {
  display.setTextSize(1);
 
  // Hunger (inverted - lower is better)
  display.setCursor(0, 0);
  display.print("H:");
  int h = map(100-hunger, 0, 100, 0, 20);
  display.drawRect(12, 0, 22, 6, SSD1306_WHITE);
  display.fillRect(13, 1, h, 4, SSD1306_WHITE);
 
  // Happiness
  display.setCursor(38, 0);
  display.print("E:");
  int e = map(happiness, 0, 100, 0, 20);
  display.drawRect(50, 0, 22, 6, SSD1306_WHITE);
  display.fillRect(51, 1, e, 4, SSD1306_WHITE);
 
  // Energy
  display.setCursor(76, 0);
  display.print("T:");
  int t = map(energy, 0, 100, 0, 20);
  display.drawRect(88, 0, 22, 6, SSD1306_WHITE);
  display.fillRect(89, 1, t, 4, SSD1306_WHITE);
 
  // Age
  display.setCursor(114, 0);
  display.print(age);
}

void drawButtons() {
  display.setTextSize(1);
  display.setCursor(0, 56);
  display.print("Feed");
  display.setCursor(44, 56);
  display.print("Play");
  display.setCursor(88, 56);
  display.print("Sleep");
}

void drawDead() {
  display.setTextSize(2);
  display.setCursor(20, 15);
  display.println("R.I.P.");
  display.setTextSize(1);
  display.setCursor(10, 35);
  display.println("Pet has died");
  display.setCursor(5, 50);
  display.println("Reset to restart");
}

void showMessage(const char* msg) {
  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(20, 25);
  display.println(msg);
  display.display();
}


 Features of the Virtual Pet Project

  • Animated face expressions on OLED

  • Button-controlled interaction

  • Real-time status updates

  • Beginner-friendly Arduino logic

  • Fully testable in Wokwi simulator

  • Great for robotics and STEM learning

Diagram.json:
{
  "version": 1,
  "author": "Wokwi",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} },
    { "type": "wokwi-ssd1306", "id": "oled", "top": -100, "left": 100, "attrs": {} },
    { "type": "wokwi-pushbutton", "id": "btn1", "top": 150, "left": -50, "attrs": { "color": "green" } },
    { "type": "wokwi-pushbutton", "id": "btn2", "top": 150, "left": 20, "attrs": { "color": "blue" } },
    { "type": "wokwi-pushbutton", "id": "btn3", "top": 150, "left": 90, "attrs": { "color": "yellow" } },
    { "type": "wokwi-buzzer", "id": "buzzer", "top": 100, "left": -100, "attrs": {} }
  ],
  "connections": [
    [ "oled:GND", "uno:GND.1", "black", [ "v0" ] ],
    [ "oled:VCC", "uno:5V", "red", [ "v0" ] ],
    [ "oled:SDA", "uno:A4", "blue", [ "v0" ] ],
    [ "oled:SCL", "uno:A5", "green", [ "v0" ] ],
    [ "btn1:1.l", "uno:2", "green", [ "v0" ] ],
    [ "btn1:2.l", "uno:GND.2", "black", [ "v0" ] ],
    [ "btn2:1.l", "uno:3", "blue", [ "v0" ] ],
    [ "btn2:2.l", "uno:GND.2", "black", [ "v0" ] ],
    [ "btn3:1.l", "uno:4", "yellow", [ "v0" ] ],
    [ "btn3:2.l", "uno:GND.2", "black", [ "v0" ] ],
    [ "buzzer:1", "uno:8", "red", [ "v0" ] ],
    [ "buzzer:2", "uno:GND.3", "black", [ "v0" ] ]
  ]
}


  

IOT & SMART SYSTEM PROJECTS

  1. IoT Weather Monitoring System (NodeMCU ESP8266 + DHT11 + Rain Sensor)
  2. ESP8266 NodeMCU Smart Health & Environment Monitoring System with Pulse, Temperature and Motion Sensors
  3. ESP32 Wi-Fi Weight Sensor with HX711
  4. Smart RFID Access Control System Using ESP32 Dev Board and UHF RFID Reader Module
  5. Smart IoT Motor Control System Using ESP32 Dev Board and L298N Motor Driver Module
  6. Smart Waste Management System Using Arduino Nano, Ultrasonic Sensor & GSM Module – Solar Powered IoT Solution
  7. Raspberry Pi Zero W and GSM SIM900 Based Ultrasonic Distance Measurement System
  8. Arduino UNO Smart Surveillance System with ESP8266 WiFi, PIR Motion Sensor & Camera Module
  9. Arduino UNO Environmental Monitoring System with OLED & 16x2 I2C LCD Display
  10. Arduino UNO-Based Smart Home Automation System with Flame and IR Sensors 
  11. Arduino Nano-Based Landslide Detection System with GSM Alerts – Smart Disaster Monitoring Project
  12. Arduino Nano Rain-Sensing Stepper Motor System
  13. Arduino Based Automatic Tire Inflator Using Pressure Sensor, Relay Module and LCD Display
  14. Arduino-Based Automatic Cooker Using Servo Motors, DC Stirrer Motor, Temperature Sensor and Relay-Controlled Heater
  15. Arduino Sketch for Plastic Bottle and Can Reverse Vending Machine

 TRAFFIC & SMART CITY PROJECTS
  1. RFID-Based Smart Traffic Control System (Arduino Mega)
  2. Arduino UNO Traffic Light Control System – Smart LED Signal Project
  3.  Arduino UNO Controlled Traffic Light System with Joystick Interface

ROBOTICS PROJECTS
  1. Arduino UNO Smart Obstacle Avoiding Robot (Ultrasonic + IR + GSM)
  2. Arduino-Powered Autonomous Obstacle Avoidance Robot with Servo Control
  3. Arduino Nano Bluetooth Controlled Line Follower Robot Using L298N Motor Driver
  4. Arduino UNO Bluetooth Controlled 4WD Robot Car Using L298N Motor Driver
  5. Arduino UNO Multi-Sensor Obstacle Avoidance & Bluetooth Controlled Robot Car Using L298N
  6. Raspberry Pi Motor Control Robot (L298N + Li-ion)
  7. RC Car Simulation with L298N Motor Driver and Joystick Control using Arduino (CirkitDesign Simulation)
  8. Raspberry Pi Robotic Arm Control System with Camera Module and Motor Driver – Smart Automation & Vision-Based Robotics Project
  9. ESP32-Based 4WD Robot Car Using Dual L298N Motor Drivers – Circuit Diagram and IoT Control Project

LORA & WIRELESS COMMUNICATION PROJECTS
  1. Arduino LoRa Communication Project Using Adafruit RFM95W LoRa RadioArduino Nano with RFM95 SX1276 LoRa
  2. Arduino Nano with RFM95 LoRa SX1276 Module – Long Range Wireless Communication Project
  3. Arduino Nano Digital Clock Using DS3231 RTC and TM1637 4-Digit Display – Circuit Diagram and Project Guide

 LED, LIGHTING & DISPLAY PROJECTS
  1. Arduino UNO Controlled NeoPixel Ring Light Show
  2. Wi-Fi Controlled NeoPixel Ring (ESP8266)
  3. Chained NeoPixel Rings with Arduino – Addressable RGB LED Control Project
  4. Arduino Nano-Controlled Lighting System with Gesture and Sound Interaction
  5. Raspberry Pi GPIO Multi-LED Control System – Beginner-Friendly Embedded Electronics Project
  6. 4 Channel Relay Module with Arduino UNO

 SENSOR & DETECTION PROJECTS
  1. Arduino UNO Color Sensor + Proximity System (TCS3200 + Inductive)
  2. Arduino Color Detection Project Using Adafruit TCS34725 RGB Color Sensor
  3. Arduino Gas Leakage Detection and Safety Alert System Using MQ-2 Gas Sensor
  4. MQ-135 Air Quality Detector Using Arduino | Cirkit Designer Simulation Project
  5. Pulse Sensor Using Arduino – Complete Guide with Simulation 
  6. HX711 Load Sensor Demo Using Arduino | Digital Weight Measurement Project
  7. Track Time with DS1307 RTC and Display on Arduino Uno with 16x2 LCD | Cirkit Designer Project
  8. Parking Sensor Simulator using Arduino Uno and HC-SR04 Ultrasonic Sensor

 FUN & INTERACTIVE PROJECTS
  1. Pong Game with Arduino UNO and OLED Display – Project Explanation
  2.   Arduino UNO Bluetooth-Controlled Servo Motor System
  3. Arduino UNO-Based Interactive Touch and Distance Sensing System with LED Indicators and Servo Control

 INDUSTRIAL / AUTOMATION PROJECTS
  1. Arduino UNO Smart Waste Sorting System Using Ultrasonic Sensor, Moisture Sensor, Servo, Stepper Motor and LCD
  2. Arduino-Based Smart Waste Segregation System Using Metal, Plastic and Moisture Sensors with Stepper and Servo Control
  3. ESP32-Based Digital Weighing Scale Using 50kg Load Cell, HX711 Module and 16x2 LCD Display
  4. Arduino-Based Smart Toll Gate Automation System with RFID, GSM, Ultrasonic Sensor and LCD Display

  5. Arduino-Based Automatic Pill Dispenser Machine with LCD Display, Servo Motor and Buzzer Reminder

  6. Arduino UNO Smart Water Quality Monitoring System with pH Sensor, Turbidity Sensor and LCD Display

  7. Arduino-Based Ocean Cleaning Boat Robot with Dual IBT-2 Motor Drivers and Conveyor Belt System

  8. IoT-Based Accident Detection and Health Monitoring System Using Raspberry Pi with GSM, GPS and Camera Integration

  9. Raspberry Pi RFID and Keypad Based Smart Door Lock System with LCD Display and L298N Motor Driver

  10. Smart Shopping Trolley Using Arduino UNO & RFID | Automatic Billing System

  11. Arduino UNO Based Automatic Liquid Hand Sanitizer & Soap Dispenser System

  12. Arduino Based Robotic Weeding Machine with Ultrasonic Obstacle Detection and L298N Motor Driver

  13. Arduino UNO Based Biometric Electronic Voting System with LCD Display and Fingerprint Authentication

  14. Arduino UNO Based Electronic Voting System with ILI9341 TFT Display

Comments