06 test sound
# Testing the Robot Speaker
This Python code is designed to make a small speaker or buzzer play different sounds! It's written for a microcontroller (like a Raspberry Pi Pico) that can control electronic components.
What the Code Does
Think of this code like a simple music player that can play beeps and tones through a speaker. Here's how it works:
The Setup
1 2 3 4 5 |
|
This part imports the tools needed to control the hardware and sets up the speaker on pin 22 of the microcontroller. PWM stands for "Pulse Width Modulation" - it's a way to control how much power goes to the speaker, which affects the volume and tone.
The Main Functions
playnote(frequency, time) - This is like pressing a key on a piano. The frequency
determines how high or low the sound is (like different piano keys), and time
determines how long the note plays.
setfreq(frequency) - This changes the pitch of the sound. Higher numbers make higher-pitched sounds, lower numbers make lower-pitched sounds.
sound_off() - This turns the speaker off completely, like lifting your finger off a piano key.
rest(time) - This creates silence for a specific amount of time, like a pause between musical notes.
The Different Sound Effects
The code creates three different sound patterns:
play_no_signal() - Plays a very low, short beep (100 Hz for 0.1 seconds). This might be used to indicate something isn't working.
play_turn() - Plays a medium-pitched, quick beep (500 Hz for 0.1 seconds). This could be used when someone takes a turn in a game.
play_startup() - Plays a little melody: three quick medium beeps followed by a longer, higher beep. This sounds like something you'd hear when a device turns on.
What Happens When You Run It
When you run this code, it automatically plays the startup sound - three quick beeps followed by a longer, higher-pitched tone. It's like the "power on" sound for whatever device this code is controlling.
Real-World Comparison
This is similar to how your phone makes different notification sounds, or how a microwave beeps when it's done cooking. The code is creating these simple electronic sounds by rapidly turning the speaker on and off at different speeds (frequencies).
Full Sample Code
```python from machine import Pin, PWM from utime import sleep
SPEAKER_PIN = 22
speaker = PWM(SPEAKER_PIN)
def playnote(frequency, time): speaker.duty_u16(1000) setfreq(frequency) sleep(time)
def play_no_signal(): playnote(100, 0.1) sound_off()
def play_turn(): playnote(500, .1) sound_off()
def setfreq(frequency): speaker.freq(frequency)
def sound_off(): speaker.duty_u16(0)
def rest(time): speaker.duty_u16(0) sleep(time)
def play_startup(): playnote(600, .1) rest(.05) playnote(600, .1) rest(.05) playnote(600, .1) rest(.1) playnote(800, .5) sound_off()
play_startup() ```