Teacher notes for the Fifteen Pack from microbit...

20
Teacher notes for the Fifteen Pack from microbit-accessories.co.uk Fifteen physical computing activities for the BBC micro:bit. Rather than splurge on lots of the same accessory which limits the number & variety of different activities that can be undertaken, we have created a matrix of activities and accessories. Many of the activities can use a variety of output devices, giving some flexibility in resource allocation. The activities range in levels of difficulty however the majority start out simple enough which can then be built up in to a more complex project. I write software for a living, some of which is used in micro controllers modules not dissimilar to the micro:bit. These modules are used in security alarm systems, a non-trivial safety-critical environment. In every one of these activities I see applications or techniques I use in my work. Whilst there may be academic abstract topics in these notes, these lead directly to real world applications. We have many other activities but all of these were selected as being ‘real’. There are many possibilities for using this pack - either one pack with students working in pairs with a smaller class size around 20 students. This will allow enough entry level projects to be available that no pair is suddenly in at the deep end. Two packs should allow individual work. Three packs should cover a larger class. Once a number of projects have been completed, the student should have enough knowledge and practise to turn their hand to creating something of their own design, however impractical or unreal. Each pack includes some accessories not in the activity matrix that can be used in these inventions - some crocodile clip leads, a push button, a soil moisture sensor, a water pump, an accessory battery pack (to run the pump & servo) and a switchable micro:bit battery pack for mobile accessories. As an engineer, I am very keen for students to understand that a project is rarely successful by leaping straight to the keyboard & mouse and is almost always takes longer. Key project headings to consider are: Understanding the (clients) brief. Project plan Overall design - hardware & software Obtaining the correct resources Project overspend (don’t take accessories you don’t need) Documentation - what is connected to where Calibration of analog sensors - obtaining typical values Block / flow diagram No magic numbers - use variables to explain what values used are for Actual coding - code a little, test a little Final testing Final documentation & notes Project review - what worked, issues, stumbling blocks, lessons learned 10 th Sept 2016 Page of 20 1 v1.0

Transcript of Teacher notes for the Fifteen Pack from microbit...

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

Fifteen physical computing activitiesfor the BBC micro:bit.

Rather than splurge on lots of the same accessory which limits the number & variety of different activities that can be undertaken, we have created a matrix of activities and accessories. Many of the activities can use a variety of output devices, giving some flexibility in resource allocation. The activities range in levels of difficulty however the majority start out simple enough which can then be built up in to a more complex project.

I write software for a living, some of which is used in micro controllers modules not dissimilar to the micro:bit. These modules are used in security alarm systems, a non-trivial safety-critical environment. In every one of these activities I see applications or techniques I use in my work. Whilst there may be academic abstract topics in these notes, these lead directly to real world applications. We have many other activities but all of these were selected as being ‘real’.

There are many possibilities for using this pack - either one pack with students working in pairs with a smaller class size around 20 students. This will allow enough entry level projects to be available that no pair is suddenly in at the deep end. Two packs should allow individual work. Three packs should cover a larger class.

Once a number of projects have been completed, the student should have enough knowledge and practise to turn their hand to creating something of their own design, however impractical or unreal. Each pack includes some accessories not in the activity matrix that can be used in these inventions - some crocodile clip leads, a push button, a soil moisture sensor, a water pump, an accessory battery pack (to run the pump & servo) and a switchable micro:bit battery pack for mobile accessories.

As an engineer, I am very keen for students to understand that a project is rarely successful by leaping straight to the keyboard & mouse and is almost always takes longer. Key project headings to consider are:

• Understanding the (clients) brief.• Project plan• Overall design - hardware & software• Obtaining the correct resources• Project overspend (don’t take accessories you don’t need)• Documentation - what is connected to where• Calibration of analog sensors - obtaining typical values• Block / flow diagram• No magic numbers - use variables to explain what values used are for• Actual coding - code a little, test a little• Final testing• Final documentation & notes• Project review - what worked, issues, stumbling blocks, lessons learned

10th Sept 2016 Page � of 201 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

Contents

Cross reference grid of activities & accessories on last page

Theremin 3Hearing test 4Movement sensor 5Door Open Alarm 7Persistence of vision 8Visual Analog Reading 9Touch Sensor 10LED brightness by Pulse Width Modulation (PWM) 11Inactivity alarm 12Sun Exposure Alarm 13Clap On - Off 14Music Notation 15Combination lock 17Animated Line Graph 18Temperature Sensor 19

10th Sept 2016 Page � of 202 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

ThereminMore fun than useful and certainly hard on the ears, this is definitely a crowd pleaser for the younger student. It varies the tone of the output of the speaker or headphones depending on the input, which can be by a light or touch sensor.

Input: LDR &/or Touch/Water sensorOutput: Speaker, headphone or piezo

The input will provide an analog input between 0 and 1023. There is a lower limit to the frequency that the output can run at and the upper values can be much higher for younger ears. Consequently, some maths can be employed to widen the range and provide a base value.

Example code:

�This is the code at it’s simplest.

Using variables to store the reading and calculate the output frequency. Other intermediate steps include displaying the input value, breaking the calculation down in to two steps and showing the values. Of values are shown, the length of the play tone will need to be increased.

Going further: Use the Temperature sensor - this will require some tuning of the calculations as the input range is much narrower.

10th Sept 2016 Page � of 203 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

Hearing testA more practical use of Theramin type code. It varies the tone of the output of the speaker or headphones depending on the a potentiometer, a variable voltage control.

Input: PotentiometerOutput: Headphone

The potentiometer will provide an analog input between 0 and 1023. There is a lower limit to the frequency that the output can run at and the upper values can be much higher for younger ears. Some brief research on expected frequency response against age is useful. Maths can be employed to set an appropriate range and provide a base value.

Example code:

Concept: Concealed testing - the tester shouldn’t let the subject know if a sound has actually played to prevent subjects saying they can hear something that they actually can’t.

Going further: Use the B button to increase the duration of the tone, so when the limits of someones hearing range is being explored, the tone can sound for longer. If the duration reaches a sensible limit (say, 2 seconds), reset back to the initial level.

10th Sept 2016 Page � of 204 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

Movement sensorNot a very hard piece of coding, but an interesting input device that needs to be setup and consideration given to the way that it works. Using a passive infra-red sensor, identical to the sensors used on intruder alarms, once the student has got to grips with it, all sorts of possibilities arise.

Input: PIR sensorOutput: Optional - buzzer, speaker, piezo

The PIR sensor takes about 30 seconds from power up for it to be able to detect. There is a range sensor pot that can alter it’s sensitivity. The challenge will be all the movement around the sensor that will keep triggering it so getting a stable testing environment can take a while before everyone starts co-operating.

Example code:

Add a buzzer or speaker to get some audio indication:

This will buzz for half a second and then wait 10 seconds before checking again.

Going further to something closer to a real world requirement, buzz 4 times upon activation, wait for de-activation and then automatically reset.

10th Sept 2016 Page � of 205 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

The ‘stray’ set triggered block initialises the variable at startup - good practice. Whilst there appear to be two redundant if statements here, it shows that the programmer has considered the different combinations of inputs.

10th Sept 2016 Page � of 206 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

Door Open AlarmThe basis of many a fledgling hackers first foray in to electronics, putting an alarm on their bedroom door (or draw of secrets in the bed side cabinet). A basic activity that can be extended with various output devices as imagination allows.

Input: MicroswitchOutput: None, buzzer, speaker, LED

Example code:

This is using a speaker or piezo for output.

This is using a buzzer with a loop to turn it on & off 4 times.

10th Sept 2016 Page � of 207 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

Persistence of visionUsing an analog input to control the timing of flashing on a LED output. The transition to appearing on all the time is exactly how TV & film works/

Input: LDROutput: LED

Example code:

Additions include displaying the speed when a button is pressed and/or performing calculations on the analog read.

10th Sept 2016 Page � of 208 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

Visual Analog ReadingProviding some visual feedback on an analog input, this also demonstrates the power of small utility building blocks.

Input: PotentiometerOutput: None

Example code:

What are the threshold / boundary values that cause the bar graph to display another level?

Going further, IF statements can be added to display or output for low / high or middle values.

10th Sept 2016 Page � of 209 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

Touch SensorA simple project using the water sensor as a touch sensor. In terms of control systems, this is a great opportunity to introduce the concept of calibration as well as reinforce the analog concept.

Input: Water sensorOutput: None

Example code:

The on button A pressed shows the current sensor reading. This can be used to get the values for different levels of damp finger or depth of insertion in to water. We use a variable to store the chosen threshold value so that the if statement doesn’t have a ‘magic number’ (an undocumented value).

10th Sept 2016 Page � of 2010 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

LED brightness by Pulse Width Modulation (PWM)A common control technique for lights & motors to control the amount of power being sent to them is to vary the duration of the on time. In theory, the voltage supplied can be reduced. However for LEDs there is a minimum voltage for them to reach to turn on at all and motors need a certain amount of voltage to get moving.

So a PWM output sends a regular square wave pulse but varies the on to off ratio.

Input: PotentiometerOutput: LED

Example code:

10th Sept 2016 Page � of 2011 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

Inactivity alarmSoftware developers particularly, but many desk workers suffer from health issues due to being at a desk for too long. This project is a reversal on a burglar alarm - it sounds an alert if it does not sense any movement OR sound within a time interval.

Input: Sound detector and PIROutput: Buzzer, piezo, speaker

Example code:

The loop keeps checking the sensors and resets the last time any activity was detected when either the Sound OR the PIR are active.

The time check multiplies out the Alarm interval from minutes to milliseconds, adds it to the last time there was any activity and compares it with the current running time. If the time has been exceeded, a short alarm output is triggered. This will repeat until some activity is detected.

A second alarm condition could be added to escalate the alarm.

10th Sept 2016 Page � of 2012 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

Sun Exposure AlarmA variation on our larger group activity, there are multiple variables that need to be considered, calibrated and configured - as much engineering & product development as programming.

Input: LDROutput: None, buzzer, speaker, LED

Example code:

The top block initialises the settings variables. The on button A pressed block resets the unit.

10th Sept 2016 Page � of 2013 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

Clap On - OffThis uses the sound detector to create one of the signature gadgets of the 80’s - the clap on, clap off switch. It is a great introduction to the idea of storing the state of the program which can be separate from the state of the sensors.

Input: Sound detectorOutput: None, buzzer, speaker, LED

Example code:

�Every time the input is detecting a sound, with the main level being set on the sensor itself, the if statement checks the state and swaps it over.

10th Sept 2016 Page � of 2014 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

Music NotationComputers spend a lot of time representing binary in a whole host of formats - numbers, text, graphics, pictures, sound and so forth. Encoding & decoding data and the different ways that information is represented is a common engineering issue with writing a program.

One example that will be familiar to some but not to most, is music notation. Take a short tune and transcribe it from stave to screen.

Input: NoneOutput: Speaker, Headphone or Piezo

Example code:

10th Sept 2016 Page � of 2015 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

This is the first few notes from:

There are plenty of pieces of music available from classics through to the latest hits. The Block Editor provides the notes from middle-C (aka C4) through to C5. Other notes have to be entered using their frequency.

Here are the notes on the staves:

� �

The nemonic for the line notes is Every Good Boy Deserves Football.

Durations are:

Here are the frequencies for the standard notes. Middle C is C4.

B0 31, C1 33, CS1 35, D1 37, DS1 39, E1 41, F1 44, FS1 46, G1 49, GS1 52, A1 55, AS1 58, B1 62, C2 65, CS2 69, D2 73, DS2 78, E2 82, F2 87, FS2 93, G2 98, GS2 104, A2 110, AS2 117, B2 123, C3 131, CS3 139, D3 147, DS3 156, E3 165, F3 175, FS3 185, G3 196, GS3 208, A3 220, AS3 233, B3 247, C4 262, CS4 277, D4 294, DS4 311, E4 330, F4 349, FS4 370, G4 392, GS4 415, A4 440, AS4 466, B4 494, C5 523, CS5 554, D5 587, DS5 622, E5 659, F5 698, FS5 740, G5 784, GS5 831, A5 880, AS5 932, B5 988, C6 1047, CS6 1109, D6 1175, DS6 1245, E6 1319, F6 1397, FS6 1480, G6 1568, GS6 1661, A6 1760, AS6 1865, B6 1976, C7 2093, CS7 2217, D7 2349, DS7 2489, E7 2637, F7 2794, FS7 2960, G7 3136, GS7 3322, A7 3520, AS7 3729, B7 3951, C8 4186, CS8 4435, D8 4699, DS8 4978

10th Sept 2016 Page � of 2016 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

Combination lockDespite there being only two input buttons, the power of binary means that an 8 button press sequence has 256 possible combinations. This code requires the concept of a state machine. Store the state in a variable. If the correct button is pressed, advance the state. If the wrong button is pressed, clear back to the start.

When the correct combination is reached, we command the servo to turn - unlocking the safe.

We’d grade this project as hard to create without any hints. Tracing how the code works requires some systematic thinking, concentrating on what the state variable is set to.

Input: NoneOutput: Servo

Example code:

The forever block is checking for when the State reaches 5 when it shows a key, turns the arm on the servo, waits and then turns the servo back and resets everything.

10th Sept 2016 Page � of 2017 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

Animated Line GraphA side scrolling Vu meter. A good example of building up a solution in segments and requiring a little ingenuity in getting the graph orientation the user expected way up and cascading the previous values across the display.

Input: Potentiometer, LDROutput: None

Example code:

The cascade of values occurs at the top and then we get the reading. We then have five loops (limitations of the Block Editor not allowing functions) that display a bar for each appropriate value. The y 0 point is top left, which is normal for computer displays, but graphs have y 0 bottom right - hence the 4 - i calculation which inverts the value.

10th Sept 2016 Page � of 2018 v1.0

Teacher notes for the Fifteen Pack from microbit-accessories.co.uk

Temperature SensorA calibrated temperature probe that reads 750mV at 25°C, with a base of -50°C and 10mV per °C - seems simple enough until you realise that you can only do integer maths with the Block Editor. This is not an artificial situation, many small microcontrollers don’t do floating point maths as a matter of course. So all the reading to voltage conversion has to be multiplied by a suitable power of 10 to keep the precision required, and then divided by a corresponding power of 10 to display the correct result.

Input: Temperature sensorOutput: None

Example code:

The micro:bit runs on 3.3V and the analog range is 0 to 1023, so the voltage reading is:

V = (Reading / 1023) x 3.3

Then we take the voltage, divide by 10mV aka 0.01 and as the base is -50, take away 50.

C = (V / 0.01) - 50

As we can’t have any decimal places in our maths, we change the formulas like so:

Va = Reading x 100000 / 1023 x 33

Which is effectively multiplying the answer by 1,000,000.

As we then have to divide the voltage by 10,000 which ends up being the equivalent to the original 0.01 division (10,000 / 1,000,000 = 0.01) and then taking away 50.

If more accuracy is required, we can calculate the left & right elements of the decimal point as shown.

10th Sept 2016 Page � of 2019 v1.0

Task LDR Pot Water Sensor

Sound Detector PIR Temp Servo Buzzer Headphone Speaker Piezo Micro

switch LED

Theramin 1 Alt Alt 1 Alt

Hearing Test 1 1

Movement Sensor 1 1 Alt Alt Alt

Door Open Alarm 1 Alt Alt 1 Alt

Persistence of vision 1 1

Visual Analog Reading 1 1

Touch Sensor 1

LED brightness 1 1

Inactivity alarm 1 1

Sun Exposure Timer 1 1 Alt Alt Alt

Clap On/Off 1 Alt

Music Notation Alt 1

Combination Lock 1 Alt

Animated Line Graph 1

Temperature sensor 1 1 Alt Alt Alt Alt

10th Sept 2016 Page � of �20 20 v1.0