MicroPython 101

A project-based introduction to micro:bit programming using MicroPython with instructions to build 16 hands-on micro:bit projects. Request Quote

  • Grades 6 - 8
  • 14 lessons
    • Web
  • Python
  • Advanced Course
Course includes
  • 14 lessons
  • 86 activities
  • Enhanced Creativity Tools
  • Automatic Assessment
  • Tutorials and Reviews
  • Coding Puzzles
  • DIY Projects
  • Quizzes
  • Teacher Guides
  • Answer Keys
Prerequisites
No previous coding experience required.

MicroPython 101 Lesson Plan

Lesson: High Rollers
Time: 45+ mins

Introduction

In this lesson, students will learn how to program their micro:bit into fun games that require dice (minimum of 3). First, they'll get more practice with functions and image strings as they create a Higher Roller game where they'll need to roll dice against the micro:bit. The highest roll wins! Next, they'll created a Loaded Dice prank game where the micro:bit will roll only high numbers (5 or 6) for the user, but rolls low numbers (1-4) for whomever the user is playing against. The user always wins!

New Code

  • pin0.is_touched() : This is a pin function that detects if the user is touching a specified pin (e.g., pin0, pin1, or pin2) on the bottom of the micro:bit. The function returns True if the pin is being touched. Otherwise, it returns False.

Vocabulary

  • None

Objectives

Students will...
  • Use the micro:bit to run coding examples
  • Apply coding concepts to create two games: High Rollers and Loaded Dice

Materials

  • micro:bit device (recommended 1 per student)
  • USB cable
  • Computers, laptops, or Chromebooks (1 per student) with student account access to Tynker.com
  • Dice (3 or more)

Warm-Up (5 minutes)

  • Tell students that they're going to create a Loaded Die game that they can use to prank friends! Next, discuss the "dos" and "don'ts" of pranks. For example:
    • Before doing a prank in school (e.g., flash mob, micro:bit game prank), you should ask your teacher and/or principal for permission.
    • Never do a prank that can cause harm to you or others.

Activities (45 minutes)

Facilitate as students complete all High Rollers modules on their own:
1. High Rollers (Document)
  • Students will read a short document that introduces the lesson.
  • Students will learn that they're going to build two different games:
    • High Roller: In this game, students will test their luck as they roll die against the micro:bit's CPU (central processing unit). The highest number wins.
    • Loaded Die: Students will create a prank game by programming their micro:bit to roll a 5 or 6 when it's their turn to roll the dice. When it's the other player's turn, the micro:bit will only roll a 1-4.
2. Project 1: High Rollers Game (DIY)
  • Students will begin programming their High Rollers game. Explain that they'll continue adding onto their game in the upcoming modules.
  • Coding Challenge, Step 1: Creating the roll_die() function:
    • To solve this challenge, students need to program their micro:bit to display a random number from 1 to 6.
    • Make sure they read the requirements carefully.
    • Give a hint: Ask students…
      After we name our variable, what do we need to assign it to? (Answer: We need to assign it to random.randint(1, 6) so we can get a random number from 1 to 6.)
      What function should we use to display the number? (Answer: show.display() )
      What function should we use to pause our program for a specified amount of milliseconds? (Answer: sleep() )
3. Rolling Again and Again (DIY)
  • Coding Challenge, Step 2: Rolling the Dice Three Times
    • Students will expand on their previous project by using a for loop that will simulate rolling a die three times.
    • Remind students that in Python, the += operator adds to the variable. They'll need to know this in order to add the die value to the score.
    • Give a hint: Tell students to create a variable called player_score and assign it a value of 0. They'll also need to use button_a.was_pressed() and button_b.was_pressed() functions as part of their while loop.
4. Computer-Player's Dice Roll (DIY)
  • This is the last step for the dice rolling game.
  • Coding Challenge, Step 3: Computer-Player's Dice Rolls
    • Make sure students read the criteria carefully.
    • Inform students that CPU is short for central processing unit.
    • Give a hint: Tell students to create a variable called cpu_score and assign it a value of 0. Also ask students…
      What function can we use to clear the screen? (Answer: display.clear() )
      What operator should we use to indicate thatplayer_score is greater than cpu_score? (Answer: The greater than [>] symbol.)
      What function can we use to display a message? (Answer: display.scroll() )
5. Project 2: Loaded Die (Document)
  • Students will read a short document that introduces the next project: Loaded Die.
  • Point out that the Loaded Die will only roll high numbers (i.e., 5, 6) when they use it. This is because they'll program their micro:bit to detect touch.
  • Explain that in order for the Loaded Die to work, they'll need to touch both the specified pin (e.g., pin0, pin1, pin2) at the bottom of the micro:bit and the GND pin.
  • Show students where the pins are located on their micro:bit:

6. Detecting Pin Touching (DIY)
  • Students are introduced to the pin0.is_touched() pin function, which detects if the user is touching a specified pin (e.g., pin0, pin1, pin2). The function returns True if the pin is being touched. Otherwise, it returns False
  • Coding Challenge, Step 1: Showing if the Pin is Touched
    • This section is mostly blank. Make sure students read the comments, coding tips, and criteria carefully.
    • Give a hint: Tell students to use a while not loop. Also make sure they're using an if-else statement.
    • Emphasize to students that in order for their program to work, they'll need to have one hand touching the GND pin and their other hand touching the pin they've selected (e.g., pin0, pin1, or pin2)
    • Tell students that they'll finish this project in the next module.
7. Finishing the Dice (DIY)
  • Coding Challenge, Step 2: Rolling the Die
    • Students will expand on their previous project by programming the micro:bit to simulate rolling a die.
    • If students want to program their game to detect if their micro:bit is shaken, part of their solution might look similar to this:

    • Are students struggling? Give students a piece of paper and ask them to write pseudocode or a flow chart for what they want their program to do. Next, ask them to type their solution using MicroPython.
    • Make sure students are using the correct parameters for random.randint().
8. Playing the Game and Rolling the Dice (DIY)
  • This page instructs students to experiment with their code as they test and debug their High Rollers and Loaded Die programs.
    • Is the Loaded Die game not working as expected? Check the code for syntax errors. Next, make sure students are touching both the specified pin (e.g., pin0) and the GND pin.
    • Are the numbers flashing too quickly? Ask them to add more time to the sleep() parameter.
    • If students finish early, ask them to try the bonus tasks.
    • Optional: Ask students to try programming their Loaded Die game to work when a different pin is touched. For example, if they originally programmed their game to work when pin0 is touched, ask them to modify their code by changing pin0 to pin1. Did their program work when they touched pin1 and the GND pin?
9. Review (Document)
  • This page reviews the following:
    • The pin0.is_touched() pin function returns a value of 1 if the user is touching the specified pin (e.g., pin0, pin1, or pin2) on the bottom of the micro:bit.
    • In the High Roller project, students used a for loop.
    • The user can use the module's randint function to return a random number in a project/game.
10. Quiz (Multiple-choice)
  • Students will be asked 5 quiz questions to review concepts from this lesson.

Extended Activities (10 minutes)

Lead a discussion with your students:
  • Did anyone try the bonus tasks? How else did you modify your game?
  • Who do you plan to prank using your Loaded Die game?
  • What does the following code do: random.randint(1, 4)? (Answer: Gets a random number from 1 to 4.

U.S. Standards

  • CCSS-Math:MP.1
  • CCSS-ELA: 6-8.RST.3, 6-8.RST.4, 6-8.RST.7
  • CSTA: 2-AP-11, 2-AP-13, 2-AP-15, 2-AP-17
  • CS CA: 6-8.AP.11, 6-8.AP.13, 6-8.AP.15, 6-8.AP.16, 6-8.AP.17
  • ISTE: 1.c, 1.d, 4.d, 5.c, 5.d, 6.b

U.K. Standards

Key Stage 2 (Years 4-6)
  • Design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts.
  • Use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs.
  • Understand computer networks, including the internet; how they can provide multiple services, such as the World Wide Web, and the opportunities they offer for communication and collaboration.
  • Use technology safely, respectfully and responsibly; recognise acceptable/unacceptable behaviour; identify a range of ways to report concerns about content and contact.
Key Stage 3 (Years 7-9)
  • Understand several key algorithms that reflect computational thinking [for example, ones for sorting and searching]; use logical reasoning to compare the utility of alternative algorithms for the same problem.
  • Create, re-use, revise and re-purpose digital artefacts for a given audience, with attention to trustworthiness, design and usability.
  • Understand a range of ways to use technology safely, respectfully, responsibly and securely, including protecting their online identity and privacy; recognise inappropriate content, contact and conduct and know how to report concerns.