Project 1.5: "Musical Buzzer"
🚀 Project 1.5 – Musical Buzzer
🎯 What You’ll Learn
- ✅ Goal 1: Play tones with a passive buzzer.
- ✅ Goal 2: Perform scales and simple melodies.
- ✅ Goal 3: Control frequency by code and create personalized songs.
Key Ideas
- music.MIDI: Special block for buzzer control.
- pitch_time: Generates a tone with frequency and duration.
- play: Plays predefined melodies.
🧱 Blocks Glossary (used in this project)
- music.MIDI(pin): Initializes buzzer on the given pin.
- midi.pitch_time(freq, ms): Plays a tone at frequency (Hz) for duration (ms).
- midi.play(melody): Plays a predefined melody.
- Delay: Controls note duration.
🧰 What You Need
| Part | How many? | Pin connection |
|---|---|---|
| D1 R32 | 1 | USB cable |
| Passive Buzzer | 1 | Pin 26 → GND |
🔌 Wiring tip: Connect buzzer long leg to Pin 26, short leg to GND.
📍 Pin map snapshot: Pin 26 = buzzer control.
✅ Before You Start
- USB cable connected
- Buzzer wired correctly
- Test print:
print("Ready!") # Confirm serial is working
🎮 Microprojects (5 Mini Missions)
🎮 Microproject 1.5.1 – Single tone
Goal: Play one tone.
Blocks used: music.MIDI, pitch_time
Block sequence:
- Initialize buzzer on pin 26
- Play tone 440 Hz for 1000 ms
MicroPython Code:
import music # Import special library for buzzer
midi = music.MIDI(26) # Initialize buzzer on pin 26
midi.pitch_time(440, 1000) # Play tone at 440 Hz for 1000 ms
print("Tone: 440 Hz for 1s") # Serial: show current tone
🎮 Microproject 1.5.2 – Basic musical scale
Goal: Play C major scale.
Blocks used: music.MIDI, pitch_time, for loop
Block sequence:
- Define list of frequencies
- Play each tone for 500 ms
MicroPython Code:
import music
midi = music.MIDI(26) # Initialize buzzer on pin 26
scale = [262, 294, 330, 349, 392, 440, 494, 523] # Frequencies for C major scale
for freq in scale: # Loop through each frequency
midi.pitch_time(freq, 500) # Play tone for 500 ms
print("Tone:", freq, "Hz") # Serial: show current tone
🎮 Microproject 1.5.3 – Melody “Happy Birthday”
Goal: Play a simple melody.
Blocks used: music.MIDI, pitch_time, for loop
Block sequence:
- Define notes and durations
- Play sequence
MicroPython Code:
import music
midi = music.MIDI(26) # Initialize buzzer on pin 26
melody = [(264,300),(264,300),(297,600),(264,600),(352,600),(330,1200)] # Simplified notes
for note, dur in melody: # Loop through each note
midi.pitch_time(note, dur) # Play note with duration
print("Note:", note, "Hz for", dur, "ms") # Serial: show current note
🎮 Microproject 1.5.4 – Frequency control by code
Goal: Change tone frequency dynamically.
Blocks used: music.MIDI, pitch_time, for loop
Block sequence:
- Loop from 200 Hz to 800 Hz
- Increase step by step
MicroPython Code:
import music
midi = music.MIDI(26) # Initialize buzzer on pin 26
for freq in range(200, 801, 100): # Frequencies from 200 to 800 Hz
midi.pitch_time(freq, 500) # Play tone for 500 ms
print("Tone:", freq, "Hz") # Serial: show current tone
🎮 Microproject 1.5.5 – Personalized melody
Goal: Create your own melody.
Blocks used: music.MIDI, pitch_time, for loop
Block sequence:
- Define custom list of notes
- Play sequence
MicroPython Code:
import music
midi = music.MIDI(26) # Initialize buzzer on pin 26
my_song = [(392,400),(440,400),(494,400),(523,800)] # Custom melody
for note, dur in my_song: # Loop through each note
midi.pitch_time(note, dur) # Play note with duration
print("Note:", note, "Hz for", dur, "ms") # Serial: show current note
✨ Main Project – Musical Buzzer
🔧 Blocks Steps (with glossary)
- music.MIDI: Initialize buzzer.
- pitch_time: Generate tones with frequency and duration.
- Loop: Play sequence of notes.
Block sequence:
- Setup buzzer on pin 26
- Define melody
- Loop through notes
🐍 MicroPython Code (mirroring blocks)
# Project 1.5 – Musical Buzzer
import music # Import special library
midi = music.MIDI(26) # Initialize buzzer on pin 26
melody = [(264,300),(264,300),(297,600),(264,600),(352,600),(330,1200)] # Happy Birthday
for note, dur in melody: # Loop through each note
midi.pitch_time(note, dur) # Play note with duration
print("Note:", note, "Hz for", dur, "ms") # Serial: show current note
📖 External Explanation
- What it teaches: How to generate music with a buzzer.
- Why it works:
music.MIDIcontrols the buzzer,pitch_timechanges frequency and duration. - Key concept: Frequency + duration + repetition = melody.
✨ Story Time
Your robot is now a musician 🎶. It can play scales, famous songs, and even your own compositions.
🕵️ Debugging (2 Common Problems)
🐞 Debugging 1.5.A – No sound (incorrect pin)
Problem: Buzzer is silent.
Clues: Wrong pin used.
Broken code:
midi = music.MIDI(2) # Wrong pin
Fixed code:
midi = music.MIDI(26) # Correct pin
Why it works: The buzzer is wired to pin 26.
Avoid next time: Always check wiring and pin number.
🐞 Debugging 1.5.B – Distorted tone
Problem: Strange or noisy sound.
Clues: Frequency or duration set incorrectly.
Broken code:
midi.pitch_time(50, 10) # Very low frequency and too short duration
Fixed code:
midi.pitch_time(440, 500) # Standard frequency and proper duration
Why it works: Correct values produce a clear tone.
Avoid next time: Use common musical frequencies (200–1000 Hz).
✅ Final Checklist
- Single tone played.
- Basic scale performed.
- Happy Birthday melody worked.
- Frequency controlled by code.
- Personalized melody created.
📚 Extras
- 🧠 Student tip: Try writing your favorite song with notes and durations.
- 🧑🏫 Instructor tip: Verify buzzer wiring before running.
- 📖 Glossary: music.MIDI, pitch_time, frequency, duration.
- 💡 Mini tips: Always test with a single tone before building a melody.