Horn

A buzzer, also known as a horn, is integrated on the mainboard of the Joy-Car. This buzzer makes it possible to play melodies and simple sounds, such as a honking noise.

The following code example shows how the buzzer of the Joy-Car can be used to play either horn noises or melodies. Different frequencies and duration values are used to generate the desired sounds or tone sequences. This example illustrates how the buzzer can be used in a variety of ways for acoustic signals.

Horn

Play a selected note using the buzzer for a specific time.

Melody

Play a predefined melody using the buzzer. You can also choose whether the melody should only be played once or whether it should be repeated continuously.

Code example

JoyCar.initJoyCar(RevisionMainboard.OnepThree)
JoyCar.buzzer_sound(Note.C, 500)
JoyCar.buzzer_sound(Note.D3, 500)
JoyCar.buzzer_melody(Melodies.Dadadadum, MelodyOptions.Once)

Using the horn

To use the Joy-Car's horn, you can use the pitch method from the micro:bit's music library. With this method, the pitch of the sound can be defined by specifying a certain frequency in Hertz. A pause is then inserted, the duration of which determines how long the horn should sound.
This procedure makes it easy to control the buzzer to generate both short horn sounds and longer acoustic signals or melodies.

# Play sound with frequency 440 for 500 ms
music.pitch(440, 500, pin=pin16)
sleep(1000)

Play melody

Melodies can also be played using the play method from the micro:bit's music library.

# Play song on buzzer
music.play(music.DADADADUM, pin=pin16)

Code example

# Import necessary libraries
from microbit import *
import music

# Define your Joy-Car mainboard revision
joycar_rev = 1.3

# Play sound with frequency 440 for 500 ms
music.pitch(440, 500, pin=pin16)
sleep(1000)

# Play sound with frequency 660 for 500 ms
music.pitch(660, 500, pin=pin16)
sleep(1000)

# Play song on buzzer
music.play(music.DADADADUM, pin=pin16)