piezo speakers!

nam doan
4 min readOct 3, 2023

--

This project allows the intersection of both music and technological integrations of the Arduino. Using the Theremin methodology of being an instructment that plays without physically touched, the use of the photocell and piezzo buzzer together enhances this technology.

Code:

const int buzzer = 7; //buzzer to arduino pin 9

const int yellowPin = 10;

int yellowVal = 255;

void setup(){

pinMode(buzzer, OUTPUT);

pinMode (yellowPin, OUTPUT);// Set buzzer — pin 9 as an output

}

void loop(){

analogWrite(yellowPin, yellowVal);

tone(buzzer, 0.00); // Send 1KHz sound signal…

delay(100000); // …for 1 sec

noTone(buzzer); // Stop sound…

delay(100000); // …for 1sec

}

pitches.h:

// — — — — — — — — — — — — — — — — — — — — — — — — -

// Copyright © 2022 HiBit <https://www.hibit.dev>

// — — — — — — — — — — — — — — — — — — — — — — — — -

#include “pitches.h”

#define BUZZER_PIN 7

int melody[] = {

NOTE_E5, NOTE_E5, REST, NOTE_E5, REST, NOTE_C5, NOTE_E5,

NOTE_G5, REST, NOTE_G4, REST,

NOTE_C5, NOTE_G4, REST, NOTE_E4,

NOTE_A4, NOTE_B4, NOTE_AS4, NOTE_A4,

NOTE_G4, NOTE_E5, NOTE_G5, NOTE_A5, NOTE_F5, NOTE_G5,

REST, NOTE_E5,NOTE_C5, NOTE_D5, NOTE_B4,

NOTE_C5, NOTE_G4, REST, NOTE_E4,

NOTE_A4, NOTE_B4, NOTE_AS4, NOTE_A4,

NOTE_G4, NOTE_E5, NOTE_G5, NOTE_A5, NOTE_F5, NOTE_G5,

REST, NOTE_E5,NOTE_C5, NOTE_D5, NOTE_B4,

REST, NOTE_G5, NOTE_FS5, NOTE_F5, NOTE_DS5, NOTE_E5,

REST, NOTE_GS4, NOTE_A4, NOTE_C4, REST, NOTE_A4, NOTE_C5, NOTE_D5,

REST, NOTE_DS5, REST, NOTE_D5,

NOTE_C5, REST,

REST, NOTE_G5, NOTE_FS5, NOTE_F5, NOTE_DS5, NOTE_E5,

REST, NOTE_GS4, NOTE_A4, NOTE_C4, REST, NOTE_A4, NOTE_C5, NOTE_D5,

REST, NOTE_DS5, REST, NOTE_D5,

NOTE_C5, REST,

NOTE_C5, NOTE_C5, NOTE_C5, REST, NOTE_C5, NOTE_D5,

NOTE_E5, NOTE_C5, NOTE_A4, NOTE_G4,

NOTE_C5, NOTE_C5, NOTE_C5, REST, NOTE_C5, NOTE_D5, NOTE_E5,

REST,

NOTE_C5, NOTE_C5, NOTE_C5, REST, NOTE_C5, NOTE_D5,

NOTE_E5, NOTE_C5, NOTE_A4, NOTE_G4,

NOTE_E5, NOTE_E5, REST, NOTE_E5, REST, NOTE_C5, NOTE_E5,

NOTE_G5, REST, NOTE_G4, REST,

NOTE_C5, NOTE_G4, REST, NOTE_E4,

NOTE_A4, NOTE_B4, NOTE_AS4, NOTE_A4,

NOTE_G4, NOTE_E5, NOTE_G5, NOTE_A5, NOTE_F5, NOTE_G5,

REST, NOTE_E5, NOTE_C5, NOTE_D5, NOTE_B4,

NOTE_C5, NOTE_G4, REST, NOTE_E4,

NOTE_A4, NOTE_B4, NOTE_AS4, NOTE_A4,

NOTE_G4, NOTE_E5, NOTE_G5, NOTE_A5, NOTE_F5, NOTE_G5,

REST, NOTE_E5, NOTE_C5, NOTE_D5, NOTE_B4,

NOTE_E5, NOTE_C5, NOTE_G4, REST, NOTE_GS4,

NOTE_A4, NOTE_F5, NOTE_F5, NOTE_A4,

NOTE_D5, NOTE_A5, NOTE_A5, NOTE_A5, NOTE_G5, NOTE_F5,

NOTE_E5, NOTE_C5, NOTE_A4, NOTE_G4,

NOTE_E5, NOTE_C5, NOTE_G4, REST, NOTE_GS4,

NOTE_A4, NOTE_F5, NOTE_F5, NOTE_A4,

NOTE_B4, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_E5, NOTE_D5,

NOTE_C5, NOTE_E4, NOTE_E4, NOTE_C4,

NOTE_E5, NOTE_C5, NOTE_G4, REST, NOTE_GS4,

NOTE_A4, NOTE_F5, NOTE_F5, NOTE_A4,

NOTE_D5, NOTE_A5, NOTE_A5, NOTE_A5, NOTE_G5, NOTE_F5,

NOTE_E5, NOTE_C5, NOTE_A4, NOTE_G4,

NOTE_E5, NOTE_C5, NOTE_G4, REST, NOTE_GS4,

NOTE_A4, NOTE_F5, NOTE_F5, NOTE_A4,

NOTE_B4, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_E5, NOTE_D5,

NOTE_C5, NOTE_E4, NOTE_E4, NOTE_C4,

NOTE_C5, NOTE_C5, NOTE_C5, REST, NOTE_C5, NOTE_D5, NOTE_E5,

REST,

NOTE_C5, NOTE_C5, NOTE_C5, REST, NOTE_C5, NOTE_D5,

NOTE_E5, NOTE_C5, NOTE_A4, NOTE_G4,

NOTE_E5, NOTE_E5, REST, NOTE_E5, REST, NOTE_C5, NOTE_E5,

NOTE_G5, REST, NOTE_G4, REST,

NOTE_E5, NOTE_C5, NOTE_G4, REST, NOTE_GS4,

NOTE_A4, NOTE_F5, NOTE_F5, NOTE_A4,

NOTE_D5, NOTE_A5, NOTE_A5, NOTE_A5, NOTE_G5, NOTE_F5,

NOTE_E5, NOTE_C5, NOTE_A4, NOTE_G4,

NOTE_E5, NOTE_C5, NOTE_G4, REST, NOTE_GS4,

NOTE_A4, NOTE_F5, NOTE_F5, NOTE_A4,

NOTE_B4, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_E5, NOTE_D5,

NOTE_C5, NOTE_E4, NOTE_E4, NOTE_C4,

// Game over sound

NOTE_C5, NOTE_G4, NOTE_E4,

NOTE_A4, NOTE_B4, NOTE_A4, NOTE_GS4, NOTE_AS4, NOTE_GS4,

NOTE_G4, NOTE_D4, NOTE_E4

};

int durations[] = {

8, 4, 8, 8, 8, 4, 8,

4, 4, 8, 4,

4, 2,1,1,

4, 8, 8, 4,

8, 8, 8, 4, 8, 8,

1,1,1,8, 8, 4,

4, 8, 4, 4,

4, 4, 8, 4,

8, 8, 8, 4, 8, 8,

8, 4,8, 8, 4,

4,1,1,1, 4, 8,

8, 8, 8, 8, 8, 8, 8, 8,

4, 4, 8, 4,

2, 2,

4, 8, 8, 8, 4, 8,

8, 8, 8, 8, 8, 8, 8, 8,

4, 4, 8, 4,

2, 2,

8, 4, 8, 8, 8, 4,

8, 4, 8, 2,

8, 4, 8, 8, 8, 8, 8,

1,

8, 4, 8, 8, 8, 4,

8, 4, 8, 2,

8, 8, 8, 8, 8, 8, 4,

4, 4, 4, 4,

4, 8, 4, 4,

4, 4, 8, 4,

8, 8, 8, 4, 8, 8,

8, 4, 8, 8, 4,

4, 8, 4, 4,

4, 4, 8, 4,

8, 8, 8, 4, 8, 8,

8, 4, 8, 8, 4,

8, 4, 8, 4, 4,

8, 4, 8, 2,

8, 8, 8, 8, 8, 8,

8, 4, 8, 2,

8, 4, 8, 4, 4,

8, 4, 8, 2,

8, 4, 8, 8, 8, 8,

8, 4, 8, 2,

8, 4, 8, 4, 4,

8, 4, 8, 2,

8, 8, 8, 8, 8, 8,

8, 4, 8, 2,

8, 4, 8, 4, 4,

8, 4, 8, 2,

8, 4, 8, 8, 8, 8,

8, 4, 8, 2,

8, 4, 8, 8, 8, 8, 8,

1,

8, 4, 8, 8, 8, 4,

8, 4, 8, 2,

8, 8, 8, 8, 8, 8, 4,

4, 4, 4, 4,

8, 4, 8, 4, 4,

8, 4, 8, 2,

8, 8, 8, 8, 8, 8,

8, 4, 8, 2,

8, 4, 8, 4, 4,

8, 4, 8, 2,

8, 4, 8, 8, 8, 8,

8, 4, 8, 2,

//game over sound

4, 4, 4,

8, 8, 8, 8, 8, 8,

8, 8, 2

};

void setup()

{

pinMode(BUZZER_PIN, OUTPUT);

}

void loop()

{

int size = sizeof(durations) / sizeof(int);

for (int note = 0; note < size; note++) {

//to calculate the note duration, take one second divided by the note type.

//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.

int duration = 1000 / durations[note];

tone(BUZZER_PIN, melody[note], duration);

//to distinguish the notes, set a minimum time between them.

//the note’s duration + 30% seems to work well:

int pauseBetweenNotes = duration * 5;

delay(pauseBetweenNotes);

//stop the tone playing:

noTone(BUZZER_PIN);

}

Reflection:

After manipulating the different puaseBetweenNotes and duration, it was a lot of fun to see how the resulting sound output changes. Furthermore, by testing out different modes of contact for the piezzo buzzer to touch such as the cotton and the sock, I found that the closer it’s to an enclosed object, the lower the sound gets and vice versa.

--

--

nam doan

hi everyone! i am nam, a Media Studies undegraduate student! i am really look forward to meeting everyone and grow in this class together! :)