Skip to main content

Buzzer

The Buzzer class controls Eliobot's passive buzzer. It allows you to play tones, musical notes and predefined sounds.

Initialization

import board
import pwmio
from elio import Buzzer

buzzer_pin = pwmio.PWMOut(board.IO17, variable_frequency=True)
buzzer = Buzzer(buzzer_pin)

Basic methods

play_tone(frequency, duration, volume)

Plays a tone at a given frequency.

ParamètreTypeDescription
frequencyfloatFréquence en Hz
durationfloatDurée en secondes
volumeintVolume (0–100)
buzzer.play_tone(440, 0.5, 80)  # La4 pendant 0.5 seconde

play_note(note, duration, NOTES_FREQUENCIES, volume)

Plays a note from a frequency dictionary.

NOTES = {
"Do": 262,
"Re": 294,
"Mi": 330,
"Fa": 349,
"Sol": 392,
"La": 440,
"Si": 494,
"silence": 0.1,
}

buzzer.play_note("Do", 0.5, NOTES, 80)
buzzer.play_note("silence", 0.25, NOTES, 0) # pause
tip

Use the value 0.1 to represent a rest in the note dictionary.

sweep(start, end, dur, steps=10, pause=0.01)

Scans a frequency range from start to end in steps steps over dur seconds.

buzzer.sweep(500, 1500, 0.3, steps=12)

Preset sounds

The class includes a collection of ready-to-use sounds:

MéthodeDescription
sound_startup()Son de démarrage
sound_hello()Salutation
sound_happy()Son joyeux
sound_win()Victoire
sound_sad() (non disponible)
sound_jump()Saut
sound_land()Atterrissage
sound_laser()Effet laser
sound_explosion()Explosion
sound_bump()Collision
sound_error()Erreur
sound_alert()Alerte (bip répété)
sound_question()Interrogation
sound_blink()Petit bip court
sound_music() (non disponible)
buzzer.sound_startup()
buzzer.sound_hello()
buzzer.sound_happy()
buzzer.sound_win()
buzzer.sound_jump()
buzzer.sound_land()
buzzer.sound_laser()
buzzer.sound_explosion()
buzzer.sound_bump()
buzzer.sound_error()
buzzer.sound_alert()
buzzer.sound_question()
buzzer.sound_blink()

Complete example — playing a melody

import board
import pwmio
from elio import Buzzer

buzzer_pin = pwmio.PWMOut(board.IO17, variable_frequency=True)
buzzer = Buzzer(buzzer_pin)

NOTES = {
"Do": 262,
"Re": 294,
"Mi": 330,
"Fa": 349,
"Sol": 392,
"La": 440,
"Si": 494,
"silence": 0.1,
}

melodie = [
("Do", 0.3),
("Re", 0.3),
("Mi", 0.3),
("Sol", 0.6),
("silence", 0.2),
("La", 0.5),
]

for note, duree in melodie:
buzzer.play_note(note, duree, NOTES, 80)