Step 1: Make it
What is it?
Improve your micro:bit guitar by shifting the pitch up and down octaves.
How it works
- Connect your micro:bit to headphones or a speaker so you can hear sound.
- Connect metal foil pads to the micro:bit's pins like in the Guitar 1 and Guitar 2 projects.
- Instead of using musical notation to play notes, this program stores the frequency of each note in variables called F, A, C and E.
- When you touch pin 1 or pin 2 and GND it will play a broken chord, but now you can move the chord down an octave (lowering its pitch) by pressing button A, and move it up an octave (raising its pitch) by pressing button B.
- The pitch (frequency) of a note doubles when you move up one octave: middle A has a frequency of 440Hz (440 vibrations per second), high A has a frequency of 880Hz. This is why making the vibrating part of guitar strings different lengths with your fingers changes the pitch of the note being played.
- Buttons A and B halve and double the frequency number stored in each variable, which has the effect of lowering or raising each note played by one octave.
What you need
- micro:bit and optional battery pack
- headphones, buzzer or powered speaker
- 5 crocodile clip leads
- optional cardboard, tin foil, glue stick, scissors to make guitar or keyboard
Step 2: Code it
1from microbit import * 2import music 3F = 349 4A = 440 5C = 523 6E = 659 7 8while True: 9 if pin1.is_touched(): 10 music.pitch(int(F), 500) 11 music.pitch(int(A), 500) 12 music.pitch(int(C), 500) 13 if pin2.is_touched(): 14 music.pitch(int(A), 500) 15 music.pitch(int(C), 500) 16 music.pitch(int(E), 500) 17 if button_a.was_pressed(): 18 F = F / 2 19 A = A / 2 20 C = C / 2 21 E = E / 2 22 if button_b.was_pressed(): 23 F = F * 2 24 A = A * 2 25 C = C * 2 26 E = E * 2 27
Step 3: Improve it
- Make a guitar-shape cardboard cut-out and mount your micro:bit to it so you can perform standing up.
- If you shift the octaves too high or too low the program will stop working – and you can’t hear very low or very high frequency sounds. Modify the program to stop this happening.
- Increase or decrease the tempo depending on which way you tilt the micro:bit.
This content is published under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) licence.