-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64320c2
commit cd96a58
Showing
13 changed files
with
491 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Beep | ||
|
||
In this exercise, we will generate a beeping sound with a 50% duty cycle, followed by a 0% duty cycle, creating a looping pattern of sound and silence. We will use the A4 note (440Hz frequency) for this. (If you're not familiar with the A4 note, please look up more information on musical notes.) | ||
|
||
### Get Top function | ||
In the previous exercise (servo motor), we manually calculated and hardcoded the top value. In this exercise, we create a small function to calculate the top value based on the target frequency and div_int: | ||
```rust | ||
const fn get_top(freq: f64, div_int: u8) -> u16 { | ||
let result = 150_000_000. / (freq * div_int as f64); | ||
result as u16 - 1 | ||
} | ||
``` | ||
|
||
### `div_int` value | ||
We will be using 64 as `div_int`. | ||
```rust | ||
const PWM_DIV_INT: u8 = 64; | ||
``` | ||
|
||
### Configure the GPIO 15 pin | ||
Next, we need to configure the GPIO pin (GPIO 15) to output the PWM signal. | ||
```rust | ||
let pwm = &mut pwm_slices.pwm7; | ||
pwm.enable(); | ||
pwm.set_div_int(PWM_DIV_INT); | ||
pwm.channel_b.output_to(pins.gpio15); | ||
``` | ||
|
||
### To Set a frequency 440Hz(A4 Note) | ||
Now we calculate the top value required to generate the 440Hz frequency (A4 note) and set it for the PWM: | ||
```rust | ||
let top = get_top(440., PWM_DIV_INT); | ||
pwm.set_top(top); | ||
``` | ||
|
||
## Loop | ||
Finally, we create a loop to alternate between a 50% duty cycle (beep) and a 0% duty cycle (silence). The loop repeats with a delay of 500 milliseconds between each change: | ||
```rust | ||
loop { | ||
pwm.channel_b.set_duty_cycle_percent(50).unwrap(); | ||
timer.delay_ms(500); | ||
pwm.channel_b.set_duty_cycle(0).unwrap(); | ||
timer.delay_ms(500); | ||
} | ||
``` | ||
|
||
## Clone the existing project | ||
You can clone (or refer) project I created and navigate to the `beep` folder. | ||
|
||
```sh | ||
git clone https://github.com/ImplFerris/pico2-projects | ||
cd pico2-projects/beep | ||
``` | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
## Introduction to Buzzer | ||
|
||
A buzzer is an electronic device used to generate sound, beeps, or even melodies, and is commonly found in alarm systems, timers, computers, and for confirming user inputs, such as mouse clicks or keystrokes. Buzzers serve as audio signaling devices, providing audible feedback for various actions. | ||
|
||
## Active Buzzer vs Passive Buzzer | ||
### Active Buzzer: | ||
- **Built-in Oscillator**: An active buzzer has an internal oscillator that generates the tone automatically when power is applied. You can identify whether you have active buzzer or not by connecting the buzzer directly to the battery and it will make a sound. | ||
- **Simpler Usage**: No need to worry about generating specific frequencies since the buzzer does it internally. | ||
- **Tone**: Typically produces a single tone or a fixed frequency. | ||
<img style="display: block; margin: auto;" alt="pico2" src="./images/active-buzzer.png"/> | ||
|
||
- **How to identify**: Usually has a white covering on top and a black smooth finish at the bottom. It produces sound when connected directly to a battery. | ||
|
||
### Passive Buzzer: | ||
- **External Signal Required**: A passive buzzer requires an external signal (usually a square wave) to generate sound. It does not have an internal oscillator, so it relies on a microcontroller to provide a frequency. | ||
- **Flexible Tones**: You can control the frequency and create different tones, melodies, or alarms based on the input signal. | ||
|
||
<img style="display: block; margin: auto;" alt="pico2" src="./images/passive-buzzer.png"/> | ||
|
||
- **How to identify**: Typically has no covering on the top and looks like a PCB-style blue or green covering at the bottom. | ||
|
||
## Which one ? | ||
|
||
### Choose **Active Buzzer** if: | ||
- You need a simple, fixed tone or beep. It's ideal for basic alerts, alarms, or user input confirmation. | ||
|
||
### Choose **Passive Buzzer** if: | ||
- You want to generate different tones, melodies, or sound patterns. | ||
|
||
It is recommended to use a passive buzzer for our exercises. However, if you only have an active buzzer, don't worry; you can still use it. In fact, I personally used an active buzzer for this. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# GOT Buzzer? | ||
|
||
We are going to play the Game of Thrones (GOT) background music (BGM) on the buzzer, thanks to the awesome [arduino-songs](https://github.com/robsoncouto/arduino-songs) repository. It also has other bgms. | ||
|
||
If you're unsure about musical notes and sheet music, feel free to check out the quick theory I've provideded [here](./music-theory.md). | ||
|
||
I've splitted the code into rust module(you can do it in single file as we have done so far): [`music`](./music-module.md), [`got`](./got-module.md). | ||
|
||
|
||
### Reference | ||
- [arduino-songs repo](https://github.com/robsoncouto/arduino-songs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Melody Example: Game of Thrones Theme | ||
|
||
These section contains code snippets for the rust module `got`. | ||
|
||
### Tempo | ||
we declare the tempo for the song(you can also change and observe the result). | ||
|
||
```rust | ||
pub const TEMPO: u16 = 85; | ||
``` | ||
|
||
## Melody Array | ||
We define the melody of the Game of Thrones theme using the notes and durations in an array. The melody consists of tuple of note frequencies and their corresponding durations. The duration of each note is represented by an integer, where positive values represent normal notes and negative values represent dotted notes. | ||
|
||
```rust | ||
pub const MELODY: [(f64, i16); 92] = [ | ||
// Game of Thrones Theme | ||
(NOTE_G4, 8), | ||
(NOTE_C4, 8), | ||
(NOTE_DS4, 16), | ||
(NOTE_F4, 16), | ||
(NOTE_G4, 8), | ||
(NOTE_C4, 8), | ||
(NOTE_DS4, 16), | ||
(NOTE_F4, 16), | ||
(NOTE_G4, 8), | ||
(NOTE_C4, 8), | ||
(NOTE_DS4, 16), | ||
(NOTE_F4, 16), | ||
(NOTE_G4, 8), | ||
(NOTE_C4, 8), | ||
(NOTE_DS4, 16), | ||
(NOTE_F4, 16), | ||
(NOTE_G4, 8), | ||
(NOTE_C4, 8), | ||
(NOTE_E4, 16), | ||
(NOTE_F4, 16), | ||
(NOTE_G4, 8), | ||
(NOTE_C4, 8), | ||
(NOTE_E4, 16), | ||
(NOTE_F4, 16), | ||
(NOTE_G4, 8), | ||
(NOTE_C4, 8), | ||
(NOTE_E4, 16), | ||
(NOTE_F4, 16), | ||
(NOTE_G4, 8), | ||
(NOTE_C4, 8), | ||
(NOTE_E4, 16), | ||
(NOTE_F4, 16), | ||
(NOTE_G4, -4), | ||
(NOTE_C4, -4), | ||
(NOTE_DS4, 16), | ||
(NOTE_F4, 16), | ||
(NOTE_G4, 4), | ||
(NOTE_C4, 4), | ||
(NOTE_DS4, 16), | ||
(NOTE_F4, 16), | ||
(NOTE_D4, -1), | ||
(NOTE_F4, -4), | ||
(NOTE_AS3, -4), | ||
(NOTE_DS4, 16), | ||
(NOTE_D4, 16), | ||
(NOTE_F4, 4), | ||
(NOTE_AS3, -4), | ||
(NOTE_DS4, 16), | ||
(NOTE_D4, 16), | ||
(NOTE_C4, -1), | ||
// Repeat | ||
(NOTE_G4, -4), | ||
(NOTE_C4, -4), | ||
(NOTE_DS4, 16), | ||
(NOTE_F4, 16), | ||
(NOTE_G4, 4), | ||
(NOTE_C4, 4), | ||
(NOTE_DS4, 16), | ||
(NOTE_F4, 16), | ||
(NOTE_D4, -1), | ||
(NOTE_F4, -4), | ||
(NOTE_AS3, -4), | ||
(NOTE_DS4, 16), | ||
(NOTE_D4, 16), | ||
(NOTE_F4, 4), | ||
(NOTE_AS3, -4), | ||
(NOTE_DS4, 16), | ||
(NOTE_D4, 16), | ||
(NOTE_C4, -1), | ||
(NOTE_G4, -4), | ||
(NOTE_C4, -4), | ||
(NOTE_DS4, 16), | ||
(NOTE_F4, 16), | ||
(NOTE_G4, 4), | ||
(NOTE_C4, 4), | ||
(NOTE_DS4, 16), | ||
(NOTE_F4, 16), | ||
(NOTE_D4, -2), | ||
(NOTE_F4, -4), | ||
(NOTE_AS3, -4), | ||
(NOTE_D4, -8), | ||
(NOTE_DS4, -8), | ||
(NOTE_D4, -8), | ||
(NOTE_AS3, -8), | ||
(NOTE_C4, -1), | ||
(NOTE_C5, -2), | ||
(NOTE_AS4, -2), | ||
(NOTE_C4, -2), | ||
(NOTE_G4, -2), | ||
(NOTE_DS4, -2), | ||
(NOTE_DS4, -4), | ||
(NOTE_F4, -4), | ||
(NOTE_G4, -1), | ||
]; | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Buzzinga | ||
|
||
In this section, we'll explore some fun activities using the `buzzer`. I chose the title "Buzzinga" just for fun (a nod to Sheldon's "Bazinga" in *The Big Bang Theory*); it’s not a technical term. | ||
|
||
- **Passive Buzzer** | ||
- **Jumper Wires**: | ||
- **Female-to-Male** jumper wires for connecting the Pico 2 to the buzzer pins (Positive and Ground). | ||
|
||
The buzzer has two pins: | ||
| Pin | Function | Description | | ||
|-----------|------------|--------------------------------------------| | ||
| Positive | Signal | Connects to GPIO15 on the Pico 2 to control the buzzer. | | ||
| Ground | Ground (GND) | Connects to any ground pin on the Pico 2. | | ||
|
||
The positive side of the buzzer is typically marked with a **+** symbol and is the longer pin, while the negative side (ground) is the shorter pin, similar to an LED. However, some passive buzzers may allow for either pin to be connected to ground or signal, depending on the specific model. | ||
|
||
By the way, I used an active buzzer in my experiment. A passive buzzer is recommended if you plan to play different sounds, as it provides a better tone. | ||
|
||
## Connection Overview | ||
1. **Signal (Positive)**: Connect the buzzer's positive pin to **GPIO15** on the Pico 2. This pin will receive PWM signals to produce different sounds based on the signal's frequency. | ||
2. **Ground (GND)**: Connect the buzzer's ground pin to any ground pin on the Pico 2. | ||
|
||
<img style="display: block; margin: auto;" alt="pico2" src="./images/pico-buzzer-circuit.png"/> | ||
|
||
Before moving forward, make sure you've read the following sections and understood the concepts. | ||
- [PWM introduction](../blinky/pwm.md) in the Blink LED section | ||
- [More on PWM](../servo/pwm.md) in the servo section | ||
- [Calculating top](../servo/servo-pico.md) in the servo section | ||
|
||
|
||
## Reference | ||
- [Pico official guide on buzzer](https://projects.raspberrypi.org/en/projects/introduction-to-the-pico/9) |
Oops, something went wrong.