Save a copy Lesson 2 Bare bones Year
Save a copy Lesson 2: Bare bones Year 9 – Physical computing
Starter activity Know your tools: Recap Can you remember the input, output, and communication devices you can use on the micro: bit?
Starter activity Know your tools: Recap 1 Buttons: input Capture user input to make things happen 2 LED display: output 2 1 3 Show pictures, words, and numbers 3 Light sensor: input Measure how much light is falling on the micro: bit 4 GPIO pins: input and output Connect headphones, sense touch, and add other electronics 4 4 4
Starter activity Know your tools: Recap 5 Temperature sensor: input 8 Measure how warm the environment is 6 Compass: input Find magnetic north or measure the strength of magnetic fields 7 Accelerometer: input Detect gestures and measure movement in 3 dimensions 8 5 Radio: communication i/o Communicate with micro: bits and other devices 6 7
Objectives In this lesson, you will: ● Write programs that use the micro: bit’s built-in input and output devices
Activity 1 Built-in output components ● 5� 5 LED display Additional output devices can be connected through the General-Purpose Input/Output pins (GPIO). You'll learn more about this in the next lesson.
Activity 1 Pairings please! You will be using pair programming , with each member in a pair taking on a specific role: Driver Control the keyboard and mouse. Navigator Provide support and instructions. You will alternate between roles.
Activity 1 Output Follow your worksheet to explore different ways of using the 5� 5 LED display for providing output.
Activity 1 Output: Solutions 1 2 3 4 5 from microbit import * images = [Image. DIAMOND_SMALL, Image. DIAMOND, Image. SQUARE] 7 for image in images: display. show(image) sleep(1000) 8 display. clear() 6 images is a list of built-in images. A for loop is used to iterate over each image in the list and display it. sleep introduces a delay between successive images. Syntax checklist. ✔ Python is case-sensitive: Upper case and lower case characters are different.
Activity 1 Output: Solutions 1 from microbit import * 2 6 star = Image("03530: " "35753: " "57975: " "35753: " "03530") 7 display. show(star) 3 4 5 After an image like star has been created, it can be used just like the builtin images. Syntax checklist. ✔ There are no commas between the lines in the star image. This is one string, laid out in different lines for readability.
Activity 1 Output: Solutions 1 2 3 4 5 6 7 8 9 10 from microbit import * delay = 100 # top right display. set_pixel(4, 0, 9) sleep(delay) display. clear() # bottom right display. set_pixel(4, 4, 9) sleep(delay) display. clear() 0 0 1 Y 2 3 4 1 X 2 3 4
Activity 1 Output: Solutions 1 2 3 4 5 6 7 8 from microbit import * from random import randint while True: x = randint(0, 4) y = randint(0, 4) brightness = randint(0, 9) display. set_pixel(x, y, brightness) sleep(5) The randint function, imported from the random module, returns a random integer within a specified range. The condition in the while loop is always True: the statements in the while block with be repeated forever. Syntax checklist. ✔ Indentation matters: Spaces before a statement mean that it belongs inside a nested block.
Activity 2 Built-in input components ● Buttons button_a, button_b ● Light sensor display ● Compass compass ● Accelerometer accelerometer ● Temperature sensor
Activity 2 Built-in input components ● Buttons button_a, button_b ● Light sensor display ● Compass compass ● Accelerometer accelerometer ● Temperature sensor Additional input devices can be connected through the GPIO pins.
Activity 2 Input Follow your worksheet to explore different ways of obtaining input from the micro: bit buttons and sensors.
Activity 2 Input: Solutions 1 2 3 4 5 6 7 8 9 from microbit import * while True: light = display. read_light_level() delay = 5 * light # blink display. show(Image. SQUARE) sleep(delay) display. clear() sleep(delay) read_light_level returns a value that is assigned to the light variable. The delay between blinks depends on the light level: the lower the light level, the faster the blinks.
Activity 2 Input: Solutions 1 2 from microbit import * x = 0 3 4 5 6 7 8 9 10 15 16 while True: if button_a. was_pressed(): if x > 0: x = x - 1 else: x = 4 if button_b. was_pressed(): . . . display. clear() display. set_pixel(x, 2, 9) The x variable is initialised to 0 and reduced by 1 with each press of button_a. The inner if statements make sure the value of x stays within the bounds. In each cycle, the display is cleared and the dot displayed at (x, 2).
Activity 2 Input: Solutions 1 2 3 4 5 6 7 8 9 10 from microbit import * x = 0 y = 0 while True: if accelerometer. was_gesture("left"): if x > 0: x = x - 1 else: x = 4 if button_b. was_pressed(): . . . display. clear() display. set_pixel(x, y, 9)
Activity 2 Input: Solutions 1 2 3 4 5 6 7 8 from microbit import * while True: decimal = 0 if button_a. is_pressed(): decimal = decimal + 2 if button_b. is_pressed(): decimal = decimal + 1 display. show(decimal) The place value corresponding to each button is added to decimal, if the button is pressed. Both buttons can be pressed at the same time, so the two if statements must be independent (not mutually exclusive): there is no elif or else.
Plenary What can you do with it? Did you have any project ideas while you were exploring the micro: bit’s built-in input and output components?
Plenary What can you do with it? Virtual pets Data logger Sensor networks Musical instruments Security alarm Timer gate Games Text messaging Electronic dice and coins Interactive art Robots
Homework: Input and output Practise your micro: bit coding skills with the tasks in the homework sheet. Due: Next lesson 22
Summary In this lesson, you. . . ● Wrote programs that use the micro: bit’s built-in input and output devices In the next lesson, you will. . . ● Write programs that use GPIO pins to generate output and receive input ● Write programs that communicate with other devices by sending and receiving messages wirelessly
Summary Additional resources ● Get creative (videos and project ideas) microbit. org/get-started/first-steps/get-creative ● Features in depth (videos and project ideas) microbit. org/get-started/user-guide/features-in-depth ● Python guide (code snippets and project ideas) microbit. org/get-started/user-guide/python ● Beginner Python projects microbit. org/projects/make-it-code-it/? filters=python, beginner ● Introduction to Micro. Python (video series by Arm Education) ncce. io/mb-arm ● Micro: bit projects from the Raspberry Pi Foundation ncce. io/mb-rpf
- Slides: 24