Lesson 09 Buzzer


Buzzer is a kind of electronic sound receiver with integrated structure. It is widely used as a sound device in electronic products like computers, printers, copying machines, alarm apparatus, electronic toys, auto electronic devices, telephones, etc. In this experiment, we are going to use the micro:bit to drive a buzzer and make its sound circulate between high frequency and low frequency just like an alarm. We will present its sound frequency on the micro:bit display with bar chart format.

What you need

  • micro:bit board
  • Micro USB Cable
  • micro:bit breadboard adaptor
  • Breadboard
  • Buzzer
  • 100 ohm resistor
  • TIP120 NPN Transistor
  • Male to male jumper wires


Buzzer is a kind of sound device. It is made of vibration device and resonance device. According to the difference of control method, we can divide buzzer into active type and passive type.

Here’s the working principle of active buzzer:
Because the active buzzer has integrated amplify sampling circuit and resonance system, when DC power input pass through the active buzzer, it will make resonance device generate sound signal. We can see the schematic diagram below for the working principle of active buzzer:

The working principle of passive buzzer is: When square wave signal pass through the buzzer, its resonance device will transform the square wave signal input into sound signal output. Below is the schematic diagram for the working principle of passive buzzer:

Note: In this experiment, we use passive buzzer only.


Transistor is a kind of semi-conductor component for current control in a circuit. It is used to amplify the weak signal to signal with larger frequency.

If we input PWM signal produced by micro:bit into buzzer directly, the buzzer will send out feeble sound. This is because the drive current of I/O port is usually too weak to directly drive components like buzzer. At this time, we have to use transistor to amplify the current of PMW signal so that the buzzer can sound properly. Here is the circuit diagram for a typical application of using transistor to drive buzzer:

Hardware Connection

Please complete connection according to the breadboard diagram below.

After connection, you will see:


Please open Microsoft Makecode, write your code in the edit area. I would like to suggest you to program by yourself first.

Of course, you can see the whole program in the link below. Just click “Edit” on the right top corner of the interface to edit your program, and then click “Download” to download your code into micro:bit directly.

Here is the link to the whole program: https://makecode.microbit.org/_E6iRg83xD649

Code Explanation

Analog Set Period

Configure the period of Pulse Width Modulation (PWM) on the specified analog pin. Before you call this function, you should set the specified pin as analog.

Under block “on start”, set two variables: variable “f” is for audio frequency, variable “item” for frequency change intervals.

Under brick “forever”, “analog write pin P0 to 512” indicates to make “P0” output square wave.

Variable “T” is for square wave period. We all know that “ period=1s/frequency” and time unit in “Analog Set Period” is “us”. So we get “T=1000000/f”.

Every circulation makes “f” change “item”, “f” changes among 20Hz to 6000Hz.


The sound sent out by buzzer changes between high frequency and low frequency. And we can see the bar chart of frequency on Micro:bit screen.

Taking it further

If we want to make a high temperature alarming device with temperature sensor and buzzer, then how can we design circuit and program? We look forward to your feedback and further discussion.

micro:bit Starter Kit Lessons

  • Lesson 01 – LED
  • Lesson 02 – Button
  • Lesson 03 – Trimpot
  • Lesson 04 – Photocell
  • Lesson 05 – RGB LED
  • Lesson 06 – Self-lock Switch
  • Lesson 07 – Temperature Sensor
  • Lesson 08 – Servo
  • Lesson 09 – Buzzer
  • Lesson 10 – Motor
  • Lesson 11 – Rainbow LED
  • Lesson 12 – Accelerometer
  • Lesson 13 – Compass
  • Lesson 14 – Ambient Light
First published at 2:37pm on July 9, 2019
Last updated at 2:37pm on July 9, 2019