|
| 1 | +# Adding a Buzzer |
| 2 | + |
| 3 | +Adding a buzzer to the Shark Rover allows it to communicate status updates, low battery warnings, or just play fun "disco" sounds. |
| 4 | + |
| 5 | +## Parts Required |
| 6 | + |
| 7 | +* **Passive Buzzer Module:** Ensure it is "Passive" (requires a PWM signal to generate tone), not "Active". |
| 8 | +* **Jumper Wires:** Female-to-Female (or matching your header type). |
| 9 | + |
| 10 | +## Wiring |
| 11 | + |
| 12 | +Connect the buzzer to the Raspberry Pi Pico. We will use **GP15** for this example, located next to the Laser pin (GP16). |
| 13 | + |
| 14 | +| Buzzer Pin | Pico Pin | |
| 15 | +| :--- | :--- | |
| 16 | +| **Signal / (+) / I/O** | **GP15** | |
| 17 | +| **GND / (-) / Minus** | **GND** | |
| 18 | +| **VCC** (If present) | **3.3V** | |
| 19 | + |
| 20 | +> *Note: Simple 2-pin buzzers just need Signal and Ground. 3-pin modules may require VCC.* |
| 21 | +
|
| 22 | +--- |
| 23 | + |
| 24 | +## Technical Concept: How PWM Works |
| 25 | + |
| 26 | +The Raspberry Pi Pico is a digital device, meaning it usually only sends "On" (3.3V) or "Off" (0V) signals. To make different sounds (or dim LEDs), we use **Pulse Width Modulation (PWM)**. |
| 27 | + |
| 28 | + |
| 29 | +*Figure: PWM creates an analog-like signal by switching on and off rapidly.* |
| 30 | + |
| 31 | +PWM turns the digital pin on and off extremely fast—thousands of times per second. This creates a "square wave" that the buzzer vibrates to, creating sound. |
| 32 | + |
| 33 | +### 1. Frequency = Pitch |
| 34 | +**Frequency** is how many times per second the wave repeats (measured in Hertz, Hz). |
| 35 | +* **Low Frequency (e.g., 100 Hz):** The buzzer diaphragm vibrates slowly, creating a deep, low sound. |
| 36 | +* **High Frequency (e.g., 2000 Hz):** The diaphragm vibrates quickly, creating a high-pitched beep. |
| 37 | + |
| 38 | +### 2. Duty Cycle = Volume (and Power) |
| 39 | +**Duty Cycle** is the percentage of time the signal is "High" (On) versus "Low" (Off) during one cycle. |
| 40 | +* In MicroPython, this is set using a 16-bit integer, where **0** is 0% and **65535** is 100%. |
| 41 | +* For a buzzer, a **50% duty cycle** (around 32768) is typically the loudest, as the diaphragm pushes and pulls equally. |
| 42 | + |
| 43 | +--- |
| 44 | + |
| 45 | +## Software Installation |
| 46 | + |
| 47 | +### 1. The Buzzer Library |
| 48 | +The buzzer class is already available in the repository. |
| 49 | +* **File Location:** `code/buzzer.py` |
| 50 | + |
| 51 | +Ensure this file is uploaded to your Pico. This class uses `asyncio` for non-blocking beeps (so the shark can keep driving while beeping) but also includes standard blocking functions for complex sound patterns. |
| 52 | + |
| 53 | +### 2. Integration into Main |
| 54 | +To use the buzzer in your main rover code (`main.py`): |
| 55 | + |
| 56 | +1. **Import:** Import the `Buzzer` class from the `buzzer` file. |
| 57 | +2. **Initialize:** Create a variable (e.g., `shark_sound`) and initialize the `Buzzer` on **pin 15**. |
| 58 | +3. **Usage:** |
| 59 | + * **Startup Sound:** Call `buzzDisco()` to play a sequence on startup (note: this may block execution for a few seconds). |
| 60 | + * **Status Beep:** Call `buzzTimeNFreq(time, frequency)` for non-blocking beeps while driving. |
| 61 | + * **Warning Tone:** Call `buzzLow()` for a continuous hum, and `stop()` to silence it. |
| 62 | + |
| 63 | +## Troubleshooting |
| 64 | +* **No Sound:** Check that you are connected to the correct GPIO pin and Ground. |
| 65 | +* **Clicking/Static:** Ensure you are using a **Passive** buzzer. Active buzzers (which generate their own tone) do not work well with PWM signals. |
| 66 | +* **Volume:** Passive buzzers driven directly by the Pico GPIO are generally quiet. Ensure the sticker is removed from the top of the buzzer. |
0 commit comments