The Codecraft "Buzzer pin" block plays a musical note for a given duration on the buzzer. You can use this to play a melody.
Build this sketch ➡
Or, to save some time, click to download this file and save it to your computer (remember where you saved it!)
buzzer_melody_button.cdc
Then, in Codecraft, use "Files ... Open local projects" to open the file you saved.
This is the first few notes of a well-known song. What is it?
Modify the sketch:
Change the melody. Maybe add add the rest of the notes of this song?
Or create your own melody?
Use the rotary potentiometer and button to select a pitch and play the sound
Your ideas?
The Codecraft "Buzzer pin" block is limited to playing tones that are specific musical notes. And, the block does not accept a parameter for the note value. (Hope this will be fixed in a future release!)
However, the Arduino tone( ) function accepts a parameter for any frequency. The noTone( ) stops the sound.
You can use tone( ) and noTone( ) to make all kinds of sounds.
Start the Arduino IDE and copy and paste this sketch:
// make an annoying chirp sound with the buzzer/speaker
#define buzzerPin 5
#define buttonPin 6
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
if (digitalRead(buttonPin) == HIGH){
for (int frequency = 2000; frequency < 4000; frequency += 25) {
tone(buzzerPin, frequency);
delay(1);
}
noTone(buzzerPin);
delay(500);
}
}
Compile and upload the sketch. Press the button.
Change the sketch to experiment with different sounds:
Use two for( ) loops to increase and decrease the frequency to make a siren sound
Use the random( ) to generate random tones
Your ideas?
Start the Arduino IDE and open the "toneMelody" example sketch:
File ... Examples ... Built-in Examples ... 0.2 Digital ... toneMelody
This sketch uses the Arduino array data type to store the melody notes and durations:
"An array is a collection of variables that are accessed with an index number. "
// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};
An element of the array is accessed with an index number, for example:
melody[thisNote]
melody[0] // the first note in the melody
The note names (NOTE_C4, etc.) are defined in the file pitches.h, which is part of the example sketch .
(There are two source code files in the sketch, displayed in two tabs, toneMelody, pitches.h.
Change these lines to match the buzzer pin number on your Grove Kit:
tone(8, melody[thisNote], noteDuration);
noTone(8);
Change the melody (different notes, durations, more notes, etc.)
Be sure to change this line if you change the number of notes in the melody[ ] array:
for (int thisNote = 0; thisNote < 8; thisNote++) {
Your ideas?
You can add a module with an amplifier and speaker.
Example:
Grove-Speaker Plus
Or, if you're OK with some easy soldering and adding any old speaker:
3 watt 5 volt PAM8403 Stereo Amplifier with Potentiometer
(Under $1 on eBay from China if you're not in a hurry.
Just need to connect the Power +/- pins to the 5V and GND pins on the Arduino board, "L" to pin 5 (same as the buzzer), and connect a speaker to the "Lout" pins on the amplifier board.
Sounds like this: