Simulator
The ||music:play tone||
block works on the micro:bit board. It might not work in the simulator on every browser.
Play a musical tone on the speaker or at a sound pin of the micro:bit for as long as you say.
music.playTone(440, 120)
The frequency of the tone is set as a number of cycle per second, or Hertz. The note frequency block will allow you to use a musical note for the tone instead of a number of Hertz.
The duration of the tone is set as a number of milliseconds. It’s typical though to use a number of beats or a beat fraction for the tone duration instead. The ||music:beat||
block is used to convert beats to milliseconds. You can also make a custom duration by just setting the tone duration to certain amount of milliseconds.
The ||music:play tone||
block works on the micro:bit board. It might not work in the simulator on every browser.
Play a Middle C
for 1 beat
.
music.playTone(music.noteFrequency(Note.C), music.beat(BeatFraction.Whole))
Play a 250
Hertz tone for 1000
milliseconds.
music.playTone(250, 1000)
Use analogSetPitchPin to change the pin used to generate music.
pins.analogSetPitchPin(AnalogPin.P1)