Lesson at a glance
maze.education/microbit/lessons/microbit-pet
- Grades
- Grades 4 to 7
- Time
- 45 to 60 minutesThe 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 experienceFor classes that have used the micro:bit and MakeCode once or twice.
- Board
- micro:bit V2 onlyThis 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 OrientationAligned: Grade 6, Term 2 · Where it fits
Learning intentions
Learners will learn to:
- Use number variables to keep track of a pet's food and happiness.
- Change a variable from more than one place: buttons, the touch logo and a timer.
- 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.
Introduction
5 to 10 minutesRetrieval practiceWhat 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 statementBefore 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.
- Ask: what does a pet need every day? Collect food, water, play, attention and sleep on the board.
- 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.
Lesson development
35 to 40 minutes- 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?
- 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.
- 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.)
- 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.
- 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.
- 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
- Set food and happy to 6.
- Every 6 seconds, take 1 off each, but never below 0.
- 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.
- Over and over: if food or happy is 0, show the sad face.
- Otherwise, if food or happy is below 4, show the confused face.
- Otherwise, show the happy face.
IPO table
Input Processing Output Every 6 seconds (the timer) Take 1 off food and 1 off happy, but not below 0 The face may change Button A is pressed Add 3 to food, but not above 9 The surprised face: the pet eats Button B is pressed Add 3 to happy, but not above 9 The rabbit: the pet jumps about The logo is touched Add 1 to happy, but not above 9 A heart The values of food and happy, every half second Is 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) Event food happy Face 0 Start 6 6 Happy 6 Timer 5 5 Happy 12 Timer 4 4 Happy 18 Timer 3 3 Confused 20 A pressed 6 3 Confused 22 B pressed 6 6 Happy 24 Timer 5 5 Happy Consolidation
5 to 10 minutes- Ask: what makes the pet's numbers go down? What makes them go up?
- Ask: why do we need the min and max blocks? What would happen without them?
- 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.
These are the MakeCode steps. The other routes are on the lesson's web page.
Build it in MakeCode blocks
- Start a New Project called Pet. In Variables, make food and happy, and in on start set both to 6.
- 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.
- 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).
- Add on button B pressed the same way for happy, with the rabbit icon, as if the pet is jumping.
- From Input, add on logo pressed: set happy to the min of happy plus 1 and 9, and show icon with the heart.
- 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.
- Test in the simulator (click the logo to touch it), then Download to a micro:bit V2.
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.
No Purple Mash route for this lesson
We could not find a Purple Mash unit or project that matches this lesson, so it is MakeCode blocks, with Python as an extension. These lessons have a Purple Mash route:
Extension: text-based coding, beyond the CAPS
The same program in MicroPython, for the micro:bit Python Editor. It suits Grade 7 to 9 learners who are ready for text-based code. The CAPS itself follows a block-based approach (Coding and Robotics CAPS, Grades 7 to 9, page 24).
# Look after a micro:bit pet: A feeds, B plays, the logo strokes, and both numbers drop every six seconds (micro:bit V2).
from microbit import *
food = 6
happy = 6
last_drop = running_time()
while True:
if button_a.was_pressed():
food = min(food + 3, 9)
display.show(Image.SURPRISED)
sleep(500)
if button_b.was_pressed():
happy = min(happy + 3, 9)
display.show(Image.RABBIT)
sleep(500)
if pin_logo.is_touched():
happy = min(happy + 1, 9)
display.show(Image.HEART)
sleep(500)
if running_time() - last_drop > 6000:
food = max(food - 1, 0)
happy = max(happy - 1, 0)
last_drop = running_time()
if food == 0 or happy == 0:
display.show(Image.SAD)
elif food < 4 or happy < 4:
display.show(Image.CONFUSED)
else:
display.show(Image.HAPPY)
sleep(100)Python has no every 6000 ms block, so the program checks running_time() on each pass of the loop and drops the numbers when six seconds have gone by.
Open the Python editor, replace the code that is there with this program (or open the downloaded file), then click Send to micro:bit. MakeCode also has a Python view, which is written a little differently.
Extension or homework
Challenges for learners who finish early, or to take further in a club or as homework.
- Challenge 1
Give the pet a voice: on a micro:bit V2, play a happy sound from Music when it is fed.
- Challenge 2
Add exercise: a shake counts as a walk and makes the pet happier, but also hungrier.
- 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.
- Name the two variables and say what changes each one.
- What does the min block stop from happening?
- Why does the pet need a micro:bit V2?
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 for | Beginning | Developing | Accomplished | Exemplary |
|---|---|---|---|---|
| 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
- The pet is always sad.Check that food and happy are set to 6 in on start, not 0.
- The numbers go below 0 or above 9.Use max of (number minus 1) and 0 in the timer, and min of (number plus 3) and 9 in the buttons.
- Touching the logo does nothing.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, 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, 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, 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, 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.
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.

