Simple Buzzer

Basics: Lights & Sound

Project Setup

What You Need And How To Connect It All

A simple buzzer, two wires and the Micro:Bit.

A Micro:Bit and buzzer connected with two wires (female connector to crocodile clip).  pin0 goes to the right connector on the buzzer (marked S) and the Ground pin goes to the left connector (marked -).

Project Details

The What, Why And How Of It

Connect the Micro:Bit and buzzer with two wires.

Of the three connectors on this type of buzzer only two are used:

The right connector (marked S) goes to pin0.
The left connector (marked -) goes to the GND pin.

The following MicroPython code shows three things you can do with this setup, from playing a single sound on a button press to playing a tune (of sorts)...

Project Code

The Code That Makes It Happen (Hopefully)


###################################
#
#  MB Buzzer
#    make a sound on button press
#
###################################

# import MB module
from microbit import *

# import the music module
import music

while True:

    # if you press button A (left)
    if button_a.is_pressed():
        # make a sound
        music.pitch(300, 500)

# END main loop
          

###################################
#
#  MB Buzzer
#    make different sounds
#    depending on the button
#
###################################

# import MB module
from microbit import *

# import the music module
import music

while True:

    # if you press button A (left)
    if button_a.is_pressed():
        # make one sound
        music.pitch(440, 1000)
    # else if you press button B (right)
    elif button_b.is_pressed():
        # make another sound
        music.pitch(660, 500)
    # else you don't press either
    else:
        music.stop()
    # END series of if...elif...else

# END main loop
          

###################################
#
#  MB Buzzer
#    play a series of notes
#    to make a (sort of) tune.
#
#  via http://www.microbitlearning.com/
#
###################################

# import MB module
from microbit import *

# import the music module
import music

while True:

    # play a series of sounds
    music.pitch(440, 1000)
    sleep(1000)
    music.pitch(660, 500)
    sleep(1000)
    music.pitch(880, 750)
    sleep(1000)
    music.stop()
    sleep(1000)

# END main loop