Connect speaker to snap:bit

Shows how to connect a speaker to snap:bit and play music from the micro:bit.
Connect speaker to snap:bit
Build the circuit shown on the diagram above.

Download the code shown below to your micro:bit. Use the "Download" link at the bottom of the embedded code editor.
After downloading the code, disconnect all cables from your micro:bit. Both the USB and the battery pack must be disconnected from the micro:bit.

The micro:bit plays music through pin P0. So the Speaker (SP) snaps must be connected across snap 0 and either the 3V or GND snap of the snap:bit.

When you close the slide switch (S1), the Battery Holder (B1) powers the snap:bit through the 3V snap and the micro:bit turns on. The “on start” event triggers and the micro:bit plays the melody. The sound is modulated as a current through snap 0, which goes through the speaker and makes it play the sound.

Incorrect connection of the speaker

You must be careful to ground the speaker properly to the micro:bit by connecting it to either the 3V or GND snap of the snap:bit.

Below is an example of an incorrectly connected speaker.

incorrectly connected speaker

Although the speaker is connected to the batter (-) and the circuit is closed (current can flow from snap 0 of the snap:bit, through the speaker, back to the battery), the melody sounds distorted, because the speaker is not grounded properly to the micro:bit.

Minimal speaker connection

You can connect the speaker directly across the 0 and 3V snaps on top of the snap:bit without any additional wires.

minimal speaker connection

This minimal setup is very useful when experimenting with the code and the micro:bit is connected to the computer. This way you can have just the snap:bit with the speaker next to your computer without any additional components.