Save a copy Lesson 3 Connections Year 9

  • Slides: 24
Download presentation
Save a copy Lesson 3: Connections Year 9 – Physical computing

Save a copy Lesson 3: Connections Year 9 – Physical computing

Starter activity Light it up Question. How can you light up the LED?

Starter activity Light it up Question. How can you light up the LED?

Starter activity Light it up Question. How can you light up the LED? Answer.

Starter activity Light it up Question. How can you light up the LED? Answer. Form a closed circuit.

Starter activity GPIO pins The micro: bit can be connected to other components through

Starter activity GPIO pins The micro: bit can be connected to other components through GPIO pins, becoming part of a circuit. GPIO means General-Purpose Input Output

Objectives In this lesson, you will: ● Write programs that use GPIO pins to

Objectives In this 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

Activity 1 GPIO pins General-Purpose Input Output Through the pins, your programs can control

Activity 1 GPIO pins General-Purpose Input Output Through the pins, your programs can control the power in a circuit (output). Through the pins, your programs can detect power in a circuit (input). ● GPIO pins pin 0, pin 1, pin 2

Activity 1 Pairings please! You will be using pair programming , with each member

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 Connecting through pins Follow your worksheet to find out how you can

Activity 1 Connecting through pins Follow your worksheet to find out how you can use the pins to: ● Sense touch ● Create a makeshift switch ● Connect speakers to the micro: bit and play music Get your crocodile clips ready!

Activity 1 Create a makeshift switch ● Connect a wire to the “power” pin

Activity 1 Create a makeshift switch ● Connect a wire to the “power” pin (3 V). ● Connect another wire to the input pin. In the example, pin 1 is the input pin. It could also be pins 0 or 2. ● Any contact between the wires will allow a circuit to be formed.

Activity 1 Pins: Solutions 1 2 3 4 5 6 7 from microbit import

Activity 1 Pins: Solutions 1 2 3 4 5 6 7 from microbit import * while True: in_value = pin 1. read_digital() if in_value == 1: display. show(Image. YES) else: display. show(Image. NO) The read_digital method returns either 0 or 1, depending on the voltage detected on the pin. The input is usually assigned to a variable (in_value in this case), for further processing. Syntax checklist. ✔ Use == to compare if two values are equal. The = is only used for assignments.

Activity 1 Pins: Solutions 1 2 3 4 5 6 from microbit import *

Activity 1 Pins: Solutions 1 2 3 4 5 6 from microbit import * reactions = [Image. NO, Image. YES] while True: in_value = pin 1. read_digital() reaction = reactions[in_value] display. show(reaction) An alternative solution: The reactions list contains the images that may be displayed. reactions[0] is displayed when the in_values is equal to 0. reactions[1] is displayed when the in_values is equal to 1.

Activity 1 Connecting speakers through pins ● Connect pin 0 to one end of

Activity 1 Connecting speakers through pins ● Connect pin 0 to one end of the speaker jack. ● Connect the “ground” pin (GND) to the other end of the speaker jack.

Activity 1 Pins: Solutions 1 2 3 4 5 from microbit import * import

Activity 1 Pins: Solutions 1 2 3 4 5 from microbit import * import music while True: if pin 2. is_touched(): music. play(music. BADDY) You can use input from any sensor here. Syntax checklist. ✔ You need to import the music module in order to access and play the melodies. ✔ Don’t forget to prepend music. to the melodies.

Activity 1 Light an LED using the pins ● Connect the output pin to

Activity 1 Light an LED using the pins ● Connect the output pin to the long leg of the LED (the anode). In the example, pin 0 is the output pin. It could also be pins 1 or 2. ● Connect the “ground” pin (GND) to short leg of the LED (the cathode).

Activity 1 Pins: Solutions 1 2 3 4 5 from microbit import * while

Activity 1 Pins: Solutions 1 2 3 4 5 from microbit import * while True: out_value = int(button_a. is_pressed()) pin 0. write_digital(out_value) display. show(out_value) The write_digital method turns the voltage on the pin on or off, depending on the value of its argument, which can be either 0 or 1.

Activity 2 Wireless communication micro: bits have a built-in component for wireless communication over

Activity 2 Wireless communication micro: bits have a built-in component for wireless communication over radio. They can broadcast and receive messages. ● Radio transceiver radio (the radio module needs to be imported)

Activity 2 Wireless communication: Demonstration 1 2 3 4 5 6 7 from microbit

Activity 2 Wireless communication: Demonstration 1 2 3 4 5 6 7 from microbit import * import radio. config(group=0) radio. on() while True: radio. send("ping") sleep(1000) The radio module must be imported. Communication can be restricted to a specific group, so that messages are not mixed up. The radio must be turned on before it is used. It is off by default, to save power. The send method broadcasts a message to the group. Messages can only be strings (text). Other data can be converted to strings.

Activity 2 Wireless communication: Demonstration 1 2 3 4 5 6 7 8 9

Activity 2 Wireless communication: Demonstration 1 2 3 4 5 6 7 8 9 10 from microbit import * import radio. config(group=0) radio. on() The receive method returns the next incoming message on the message queue. It returns None if there are no pending messages. while True: message = radio. receive() if message == "ping": display. show(Image. HEART) sleep(200) display. clear() The message is usually assigned to a variable, for further processing.

Activity 2 Radio Ga Ga Follow your worksheet to get your micro: bits to

Activity 2 Radio Ga Ga Follow your worksheet to get your micro: bits to communicate wirelessly with each other. Make sure you know what your group number is.

Activity 2 Radio Ga Ga: Solutions 1 2 3 4 5 6 7 8

Activity 2 Radio Ga Ga: Solutions 1 2 3 4 5 6 7 8 9 10 11 from microbit import * import radio. config(group=0) radio. on() while True: if accelerometer. was_gesture("shake"): radio. send("pass") display. clear() message = radio. receive() if message == "pass": display. show(Image. HEART) Outgoing: Sends "pass" message, if the accelerometer detects a shake. Incoming: Retrieves any received message and displays an image if that message equals "pass".

Activity 2 Radio Ga Ga: Solutions 5 6 7 8 9 10 11 12

Activity 2 Radio Ga Ga: Solutions 5 6 7 8 9 10 11 12 13 14 15 16 answer = 1 while True: if button_a. was_pressed() and answer > 1: answer = answer - 1 if button_b. was_pressed() and answer < 4: answer = answer + 1 display. show(answer) if accelerometer. was_gesture("shake"): message = str(answer) radio. send(message) display. show(Image. SQUARE_SMALL) sleep(1000) Outgoing: Broadcasts the message, i. e. the user’s answer converted to a string, if the accelerometer detects a shake.

Plenary What can you do with it? Did you have any project ideas while

Plenary What can you do with it? Did you have any project ideas while you were exploring the micro: bit’s GPIO pins and communication capabilities?

Homework: Project ideas Think. Suggest two or three ideas for physical computing projects you

Homework: Project ideas Think. Suggest two or three ideas for physical computing projects you could build with the micro: bit. Does it solve a problem? This can range from social or environmental issues, to artistic expression, to simply having fun. Who will use it? Due: Next lesson Is it ambitious while feasible? Can a non-trivial prototype be built by two people in about two hours? Are the components and materials available? 24

Summary In this lesson, you. . . ● Wrote programs that use GPIO pins

Summary In this lesson, you. . . ● Wrote programs that use GPIO pins to generate output and receive input ● Wrote programs that communicate with other devices by sending and receiving messages wirelessly In the next lesson, you will. . . ● Draft a proposal for a physical computing project, keeping in mind the problem at hand, the needs of the audience involved, and the available resources ● Start working on a prototype for the project