0% found this document useful (0 votes)
195 views5 pages

Project 5 PIEZO BUZZER

This circuit uses a piezo buzzer connected to a microcontroller to play simple songs by generating tones of different frequencies. It introduces the tone() function to generate tones on a pin and arrays to store note frequencies. The code includes arrays with musical notes and their corresponding frequencies. It then loops through a series of notes, looking up the frequency for each from the arrays and using tone() to play the notes for a set duration, creating a simple "Happy Birthday" song.

Uploaded by

Darwin Vargas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
195 views5 pages

Project 5 PIEZO BUZZER

This circuit uses a piezo buzzer connected to a microcontroller to play simple songs by generating tones of different frequencies. It introduces the tone() function to generate tones on a pin and arrays to store note frequencies. The code includes arrays with musical notes and their corresponding frequencies. It then loops through a series of notes, looking up the frequency for each from the arrays and using tone() to play the notes for a set duration, creating a simple "Happy Birthday" song.

Uploaded by

Darwin Vargas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Circuit 2A: PIEZO BUZZER

In this circuit, you’ll use the MICROCONTROLLER and a small buzzer to make music, and you’ll
learn how to program your own songs using arrays.
Page | 1
Parts Needed
Grab the following quantities of each part listed to build this circuit:

NEW COMPONENTS

BUZZER: The buzzer uses a small magnetic coil to vibrate a metal disc inside a plastic
housing. By pulsing electricity through the coil at different rates, different frequencies
(pitches) of sound can be produced. Attaching a potentiometer to the output allows you
to limit the amount of current moving through the buzzer and lower its volume.

NEW CONCEPTS
RESET BUTTON: The MICROCONTROLLER has a built-in reset button. This button will reset
the board and start the code over from the beginning, running setup()then loop().

TONE FUNCTION: To control the buzzer, you will


use the tone() function. This function is similar to
PWM in that it generates a wave that is of a certain frequency on the specified pin. The frequency and
duration can both be passed to the tone() function when calling it. To turn the tone off, you need to call
noTone() or pass a duration of time for it to play and then stop. Unlike PWM, tone() can be used on any
digital pin.

ARRAYS are used like variables, but they can store multiple values. The simplest array is just a list.
Imagine that you want to store the frequency for each note of the C major scale. We could make seven
variables and assign a frequency to each one, or we could use an array and store all seven in the same
list. To refer to a specific value in the array, an index number is used. Arrays are indexed from 0. For
example, to call the first element in the array, use array_name[0]; to call the second element, use
array_name[1]; and so on.
HOOKUP GUIDE

Page | 2
SOURCE CODE:

int speakerPin = 10; //the pin that buzzer is connected to

void setup()
Page | 3
{
pinMode(speakerPin, OUTPUT); //set the output pin for the speaker
}

void loop()
{

play('g', 2); //ha


play('g', 1); //ppy
play('a', 4); //birth
play('g', 4); //day
play('C', 4); //to
play('b', 4); //you

play(' ', 2); //pause for 2 beats

play('g', 2); //ha


play('g', 1); //ppy
play('a', 4); //birth
play('g', 4); //day
play('D', 4); //to
play('C', 4); //you

play(' ', 2); //pause for 2 beats

play('g', 2); //ha


play('g', 1); //ppy
play('G', 4); //birth
play('E', 4); //day
play('C', 4); //dear
play('b', 4); //your
play('a', 6); //name

play(' ', 2); //pause for 2 beats

play('F', 2); //ha


play('F', 1); //ppy
play('E', 4); //birth
play('C', 4); //day
play('D', 4); //to
play('C', 6); //you
while (true) {} //get stuck in this loop forever so that the song only plays once
}

void play( char note, int beats)


{ Page | 4
int numNotes = 14; // number of notes in our note and frequency array (there are 15 values, but arrays
start at 0)

//Note: these notes are C major (there are no sharps or flats)

//this array is used to look up the notes


char notes[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B', ' '};
//this array matches frequencies with each letter (e.g. the 4th note is 'f', the 4th frequency is 175)
int frequencies[] = {131, 147, 165, 175, 196, 220, 247, 262, 294, 330, 349, 392, 440, 494, 0};

int currentFrequency = 0; //the frequency that we find when we look up a frequency in the arrays
int beatLength = 150; //the length of one beat (changing this will speed up or slow down the tempo of
the song)

//look up the frequency that corresponds to the note


for (int i = 0; i < numNotes; i++) // check each value in notes from 0 to 14
{
if (notes[i] == note) // does the letter passed to the play function match the letter in the array?
{
currentFrequency = frequencies[i]; // Yes! Set the current frequency to match that note
}
}

//play the frequency that matched our letter for the number of beats passed to the play function
tone(speakerPin, currentFrequency, beats * beatLength);
delay(beats * beatLength); //wait for the length of the tone so that it has time to play
delay(50); //a little delay between the notes makes the song sound more natural

/* CHART OF FREQUENCIES FOR NOTES IN C MAJOR


Note Frequency (Hz)
c 131
d 147
e 165
f 175
g 196
a 220
b 247
C 262
D 294
E 330
F 349
G 392
A 440
B 494
*/
Page | 5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy