Look after a micro:bit pet

A pocket pet that gets hungry and bored over time: A feeds it, B plays with it and a stroke of the gold logo makes it happy.

Go to the code ↓

Lesson at a glance

45 to 60 minutes · Grades 4 to 7

maze.education/microbit/lessons/microbit-pet

Grades
Grades 4 to 7
Time
45 to 60 minutes
The CAPS gives Coding and Robotics 2 hours a week (Coding and Robotics CAPS, Grades 4 to 6, page 22; Grades 7 to 9, page 22).
Level
Some experience
For classes that have used the micro:bit and MakeCode once or twice.
Board
micro:bit V2 only
This lesson needs a micro:bit V2, for its touch logo.
Code with
MakeCode blocks, with Python as an extension
CAPS
Coding and Robotics, Life Skills, Life Orientation
Aligned: Grade 6, Term 2 · Where it fits

Learning intentions

Learners will learn to:

  1. Use number variables to keep track of a pet's food and happiness.
  2. Change a variable from more than one place: buttons, the touch logo and a timer.
  3. Use if, else if and else to choose the pet's face from its variables.

Success criteria

Each learner can say:

  • I can use two number variables, food and happy, to keep track of the pet.
  • I can change a variable from a button, the touch logo and a timer, and keep it between 0 and 9.
  • I can use if, else if and else to choose the pet's face from its two variables.
  • Grade 7: I can trace food and happy over time and say which face the pet shows.

Competencies

From the Coding and Robotics CAPS places this lesson cites.

  • C.2Present a simple coding solution
  • C.3Interpret and execute a given symbolic or written set of commands
  • R.6Mimic the operations of a robot

Key words

variable
A named store for a value that can change, like food or happy.
timer
Code that runs again and again after a set time, here every six seconds.
touch logo
The gold logo on the front of the micro:bit V2. It works like a button when you touch it.
limit
The highest or lowest a value may go. Our pet's values stay between 0 and 9.
condition
A question with a yes or no answer, such as: is food 0?
trace table
A table that follows the value of each variable, step by step, as a program runs.

What you need

  • A micro:bit V2 for each pair (the touch logo is only on the V2), with a USB cable and a battery holder
  • A computer with Chrome or Edge for each pair
  • A pet care card for each pair to fill in: what does a real pet need every day?

From our shop: BBC micro:bit V2 Go, BBC micro:bit V2.21 Next Gen Classroom Pack

Safety

  • Use the battery holder with two AAA batteries. Never mix old and new batteries, and take the batteries out before the kits are packed away.
  • Unplug the battery holder by holding its plug, never by pulling the wires.
  • Keep the micro:bit dry, and hold it by its edges rather than the parts on the back.

The lesson plan

One lesson of 45 to 60 minutes in three parts: introduction, lesson development and consolidation. The CAPS gives Coding and Robotics 2 hours a week, so it fits one of the week's lessons. Times are a guide.

  1. Introduction

    5 to 10 minutes
    Retrieval practice

    What does an if, then, else block do? (It checks a condition, then runs one set of blocks when the answer is yes and the other set when it is no.)

    Problem statement

    Before a family adopts a puppy or a kitten from the SPCA, everyone needs to understand that a pet needs food and attention every day, not only in the first week. Program a virtual pet whose food and happiness drop by themselves, and find out how much care it really takes.

    1. Ask: what does a pet need every day? Collect food, water, play, attention and sleep on the board.
    2. Read the problem statement. Explain that our micro:bit pet keeps two numbers, food and happy. They go down by themselves, and we push them back up.
  2. Lesson development

    35 to 40 minutes
    1. Unplugged first. Play pet keeper in pairs: the pet holds up fingers for food, starting at six, and the keeper writes happy on paper, also starting at six. Each time the teacher claps, both drop by one. The keeper may feed (food up by three) or play (happy up by three), but never above nine. After ten claps, ask: what happened when only one need was met?
    2. Plan on paper first. Pairs fill in the IPO table, with a row for the timer, each button, the logo and the face, then write the algorithm. Grade 7 learners add the trace table for the first 24 seconds.
    3. Predict. Ask: food is 8 and happy is 3. Which face will the pet show? If nobody touches the pet, how long until it looks sad? (36 seconds: six drops of 1, one every 6 seconds.)
    4. Build in blocks. Pairs share one computer. The driver makes the variables, the timer and feeding from the Blocks tab; the navigator checks each block against the IPO table. Swap roles halfway, after step 3, so the new driver adds play, the logo and the faces.
    5. Test and debug. Test in the simulator (click the logo to touch it). Leave the pet alone and time how long it takes to look sad. Did it match your prediction? If the numbers go below 0 or above 9, check the min and max blocks. Then download it to a micro:bit V2.
    6. Give the pet a name and look after it for five minutes. Can you keep it happy without overfeeding it? Note what you had to do, and how often.

    Plan on paper: a model answer

    In pairs, fill in the IPO table, with a row for each input and one for the face, then write the algorithm. Grade 7 learners also trace food and happy over the first 24 seconds.

    Algorithm
    1. Set food and happy to 6.
    2. Every 6 seconds, take 1 off each, but never below 0.
    3. When A is pressed, add 3 to food. When B is pressed, add 3 to happy. When the logo is touched, add 1 to happy. Never go above 9.
    4. Over and over: if food or happy is 0, show the sad face.
    5. Otherwise, if food or happy is below 4, show the confused face.
    6. Otherwise, show the happy face.
    IPO table
    InputProcessingOutput
    Every 6 seconds (the timer)Take 1 off food and 1 off happy, but not below 0The face may change
    Button A is pressedAdd 3 to food, but not above 9The surprised face: the pet eats
    Button B is pressedAdd 3 to happy, but not above 9The rabbit: the pet jumps about
    The logo is touchedAdd 1 to happy, but not above 9A heart
    The values of food and happy, every half secondIs either 0? If not, is either below 4?The sad face, the confused face or the happy face
    Trace table
    Grade 7: food and happy over the first 24 seconds. A is pressed at 20 seconds and B at 22. The face is the one that forever shows.
    Time (s)EventfoodhappyFace
    0Start66Happy
    6Timer55Happy
    12Timer44Happy
    18Timer33Confused
    20A pressed63Confused
    22B pressed66Happy
    24Timer55Happy
  3. Consolidation

    5 to 10 minutes
    1. Ask: what makes the pet's numbers go down? What makes them go up?
    2. Ask: why do we need the min and max blocks? What would happen without them?
    3. Back to the problem: how often did your pet need you in five minutes? How is this like caring for a real pet, and how is it different? What should a family think about before adopting one? Learners check themselves against the success criteria.

Build it, step by step

Pairs build the program in blocks, following the numbered steps, with the same program as text beside them to check against or paste. Test in the simulator before sending it to a board.

Button A adds 3 to food, B adds 3 to happy and the logo adds 1 to happy, each with its own picture; every 6 seconds both drop by 1. The face is sad if either is 0, confused if either is below 4, otherwise happy.
Two variables, food and happy, change from several places, and if, else if and else turn them into one face.

These are the MakeCode steps. The other routes are on the lesson's web page.

Build it in MakeCode blocks

  1. Start a New Project called Pet. In Variables, make food and happy, and in on start set both to 6.
  2. From Loops, add every 500 ms and change it to 6000. Inside, set food to the max of food minus 1 and 0 (max is in Math). Do the same for happy.
  3. From Input, add on button A pressed: set food to the min of food plus 3 and 9, then show icon with the surprised face (an open mouth).
  4. Add on button B pressed the same way for happy, with the rabbit icon, as if the pet is jumping.
  5. From Input, add on logo pressed: set happy to the min of happy plus 1 and 9, and show icon with the heart.
  6. In forever, add if then else if then else from Logic: if food = 0 or happy = 0, show the sad face; else if food < 4 or happy < 4, show the confused face; else show the happy face. Add pause (ms) 500.
  7. Test in the simulator (click the logo to touch it), then Download to a micro:bit V2.
The same blocks as text (to paste into MakeCode)
let food = 6
let happy = 6
input.onButtonPressed(Button.A, function () {
    food = Math.min(food + 3, 9)
    basic.showIcon(IconNames.Surprised)
})
input.onButtonPressed(Button.B, function () {
    happy = Math.min(happy + 3, 9)
    basic.showIcon(IconNames.Rabbit)
})
input.onLogoEvent(TouchButtonEvent.Pressed, function () {
    happy = Math.min(happy + 1, 9)
    basic.showIcon(IconNames.Heart)
})
loops.everyInterval(6000, function () {
    food = Math.max(food - 1, 0)
    happy = Math.max(happy - 1, 0)
})
basic.forever(function () {
    if (food == 0 || happy == 0) {
        basic.showIcon(IconNames.Sad)
    } else if (food < 4 || happy < 4) {
        basic.showIcon(IconNames.Confused)
    } else {
        basic.showIcon(IconNames.Happy)
    }
    basic.pause(500)
})

To use the text instead: open MakeCode, start a new project, click JavaScript at the top, replace what is there with this code, then click Blocks to see it as blocks.

To send it to the board, click Download. In Chrome or Edge, MakeCode can connect to the micro:bit by USB so the program goes straight onto it. Otherwise a .hex file downloads: drag it onto the MICROBIT drive.

Extension or homework

Challenges for learners who finish early, or to take further in a club or as homework.

  1. Challenge 1

    Give the pet a voice: on a micro:bit V2, play a happy sound from Music when it is fed.

  2. Challenge 2

    Add exercise: a shake counts as a walk and makes the pet happier, but also hungrier.

  3. Challenge 3

    Make the pet grow up: count how many minutes it has stayed happy, and show a bigger face after five.

Support and extension

Support

For learners who need more help

  • Start with food only: one variable, button A and the timer. Add happy once the pet eats and gets hungry.
  • Give pairs a printed card with the three faces and the rule for each.

Extension

For learners who are ready for more

  • Add sleep: if the room is dark (light level below 20), the pet shows the asleep face and its numbers stop dropping.
  • Show the pet's numbers as two columns of lights, food on the left and happy on the right, with plot bar graph or plot x y.

Informal assessment

Ask these as learners work. Informal assessment happens every day and is not recorded.

  1. Name the two variables and say what changes each one.
  2. What does the min block stop from happening?
  3. Why does the pet need a micro:bit V2?
Exit question

Food is 2 and happy is 7. Which face does the pet show, and why?

Observation checklist

Watch pairs as they plan, build and test. Write each pair's initials under the level you see.

Watch forBeginningDevelopingAccomplishedExemplary
Names what changes each variable, and by how much.
Uses min and max to keep the values between 0 and 9.
Puts the conditions in the right order: sad first, then confused, then happy.
Predicts the face for given values, then checks it in the simulator.
Beginning:
not yet, even with help
Developing:
with help
Accomplished:
on their own
Exemplary:
on their own, and explains it or takes it further

The four levels are the CAPS rubric levels (Coding and Robotics CAPS, Grades 4 to 6, page 176; Grades 7 to 9, page 163). The short descriptions are ours.

This lesson is not a formal task, but it is good practice for the term's practical task in Grades 4 to 6 (Coding and Robotics CAPS, Grades 4 to 6, page 170), and for the mini-PATs in Grades 7 to 9 (Coding and Robotics CAPS, Grades 7 to 9, page 157).

Common mistakes and fixes

  • Problem: The pet is always sad.Fix: Check that food and happy are set to 6 in on start, not 0.
  • Problem: The numbers go below 0 or above 9.Fix: Use max of (number minus 1) and 0 in the timer, and min of (number plus 3) and 9 in the buttons.
  • Problem: Touching the logo does nothing.Fix: You need a micro:bit V2 with the gold logo. Touch the logo with a fingertip and hold the board by its edges.

Where it sits in CAPS

  • Coding and RoboticsAlignedGrade 6, Term 2: variables store values that the program can change, such as a score.Source: Coding and Robotics CAPS, Grades 4 to 6 (opens in a new tab), Grade 6, Term 2, Coding, C.2 Present a simple coding solution, page 137: Variables allow us to store, change and access this information as
  • Coding and RoboticsAlignedGrade 6, Term 2: two variables and IF, THEN and ELSE in one program, the most the Grade 6 CAPS asks for.Source: Coding and Robotics CAPS, Grades 4 to 6 (opens in a new tab), Grade 6, Term 2, Coding, C.3 Interpret and execute a given symbolic or written set of commands, page 139: Two variables with IF…THEN…ELSE
  • Coding and RoboticsFitsGrade 6, Term 3: a micro:bit pet that shows its mood (the CAPS pet mood detector).Source: Coding and Robotics CAPS, Grades 4 to 6 (opens in a new tab), Grade 6, Term 3, Robotics, R.6 Mimic the operations of a robot, page 155: Microcontroller pet mood detector
  • Coding and RoboticsFitsGrade 4, Term 2: IF…THEN to choose what happens (the CAPS teaches it with on-screen sprites in Grade 4; this lesson goes on to else if and else).Source: Coding and Robotics CAPS, Grades 4 to 6 (opens in a new tab), Grade 4, Term 2, Coding, C.2 Present a simple coding solution using symbolic or written statements representing sequences of commands, single repetition, and conditional constructs, page 67: Introduce simple IF…THEN statement formally
  • Life SkillsFits Life Skills (Life Orientation in Grade 7): caring for living things, and what a pet needs to stay healthy and happy.

Coding and Robotics quotes come from the Department of Basic Education's final CAPS, with the page number of the 2024 printed edition (the Department's October 2025 library copies number their pages differently). Schools may teach a topic in another term: the CAPS leaves the order to each school's annual teaching plan.

Ready to teach it?

Get a micro:bit for every learner or pair, and let us train your staff to run lessons like this one.

More lessons

All lessons →
  • Grades 4 to 7

    Grades 4 to 745 to 60 minLevel: Getting started

    Step counter

    Every step adds one: learners use a variable to count steps, show the total and reset it, then test how accurate it is.

    Subjects: Coding and Robotics · Mathematics · Life Skills · Life Orientation

    Code with: MakeCodePurple MashPython

  • Grades 4 to 7

    Grades 4 to 745 to 60 minLevel: Some experience

    Music maker

    Buttons play notes, A and B together play a scale and a shake starts a tune: a pocket instrument learners compose for.

    Subjects: Creative Arts · Coding and Robotics

    Code with: MakeCodePurple MashPython

  • Grades 4 to 7

    Grades 4 to 745 to 60 minLevel: Some experience

    30-second fitness challenge

    Press A, count down, and the micro:bit counts your star jumps for 30 seconds using its motion sensor.

    Subjects: Life Skills · Life Orientation · Coding and Robotics · Mathematics

    Code with: MakeCodePython

micro:bit is a trade mark of the Micro:bit Educational Foundation, and BBC is a trade mark of the British Broadcasting Corporation. Maze Education is an independent reseller of genuine BBC micro:bit products. Our teacher training and our other products and services are our own, and are not endorsed by the Foundation or the BBC.

WhatsApp