PEIZO Buzzer With BBC Micro - Bit

By the end of this tutorial, you will be able to connect your micro: bit to the buzzer and play sound in buzzer.

This article is all about connecting Piezo Buzzer and playing a sound on a buzzer using micro: bit. So let's have a quick introduction about the buzzer.
 
What is Piezo Buzzer? 

Piezo buzzer is an electronic device which produces sound based on the reverse of the piezoelectric effect. The generation of pressure variation or strain by the application of an electric potential across a piezoelectric material is the underlying principle.Basically, buzzers are used in an alert system corresponding to a switching action, counter signal or sensor input and are also used in the alarm circuit.

It produces a noisy sound based on the voltage applied to it. Most buzzers produce sound in the range of 2 to 4khz.

And this is how a buzzer looks.
 
 
 
And the connection of the buzzer is like this:

Red to Input

Black to GND 

So now let us see how we can connect this buzzer to micro: bit and play some sound. Let's dive into it. 
 
Tools you need :
  1.  micro: bit (1 pcs)
  2. USB (1 pcs)
  3. AA battery (2 pcs)
  4. Battery Box (1 pcs)
  5. Piezo Buzzer (1 pcs)
  6. Crocodile connector (jumpers) (2 pcs)
Let's have a look at the connection of micro: bit and buzzer.
 
 
As I have discussed above, the red terminal is connected to PIN 0 and the black terminal is connected to GND.
 
 Buzzer micro:bit
 Red PIN0
 Black GND
 
And if you don't have a black and red terminal at the buzzer then there will be two terminals called +ve and -ve or just for generalization, big and small, then connect your big pin to PIN0 of micro: bit and small to GND of micro:bit.like this
 
 
 
I hope there is no confusion in the connection part; if you have any questions you can ask me in the comment section.
Let us see the coding part of this tutorial. So, go to makecode.microbit.org and create a new project.
 
After this just go to Input and choose on Button A pressed.
 
 
 
We're using this because if we will use start or forever then it will play the sound on the buzzer,and  start once and forever respectively.
That's why we're using this, so that we can play this whenever we want.

Now next what we need to do is: 

Go to music block and grab start melody block and leave it to default.
 
 
 
Now go to basic and choose pause just to provide some delay to play another melody. And after that place one more melody and change the melody to another type.
 
 
 
 
That's all now your final code should look like this 
 
 
Get the code here,
 
You can see a demo Here.