In this tutorial, we will learn how to use a Buzzer or Piezo with Arduino. It is also called Piezo Buzzer.
The Arduino isn’t built to be a synthesizer, but it can certainly produce sound through an output device such as a speaker or Piezo.
Sound is produced by vibrating air. A sound has a distinctive pitch (frequency) if the vibration repeats regularly. The Arduino can create sound by driving a loudspeaker or piezo device, converting electronic vibrations into speaker pulses which vibrate the air.
The frequency of the sound is determined by the time it takes to pulse the speaker in and out; the shorter the amount of time, the higher the frequency.
The unit of frequency is measured in hertz, and it refers to the number of times the signal goes through a repeating cycle in one second. The range of human hearing is from around 20 hertz up to 20,000 hertz (although it varies by person and changes with age).
The Arduino software includes a tune function for producing sound. We are going to see how to use this function to make sound and tunes. The tone function uses hardware timers. On a standard Arduino board, we can produce only one tone at a time.
The sound that can be produced by pulsing a speaker is limited and does not sound very musical. The output is a square wave, which sounds harsh and like an antique computer game than a musical instrument.
It is difficult for Arduino to produce more musically complex sounds without external hardware. We can add a shield that extends Arduino’s capabilities.
Must See:
Components Needed
- Arduino.
- Piezo Buzzer.
- Resistors.
- LED.
- Jumper Wires.
Playing Tones
We want to produce audio tones through a speaker or other transducer. We want to specify the frequency and duration of the tone.
The solution uses the Arduino function. This sketch plays a tone with the frequency set by a variable resistor (or other sensors) connected to analog input 0.
Circuit Diagram for Buzzer with Arduino
Code for Buzzer with Arduino
The tone function can take up to three parameters: the pin attached to the speaker, the frequency to play (in hertz), and the length of time (in milliseconds) to play the note.
The third parameter is optional. If it is omitted, the note will continue until there is a value for audio frequencies in the following line:
int frequency = map(sensorReading, 0, 1023, 100, 5000);
Playing a Simple Melody
We want Arduino to play a simple melody.
We can use the tone function to play sounds corresponding to notes on a musical instrument. This sketch uses tone to play a string of notes, the “Hello world” of learning the piano, “Twinkle, Twinkle Little Star”.
Circuit Diagram
Code for Simple Melody Buzzer with Arduino
noteNames is an array of characters to identify notes in a score. Each entry in the array is associated with a frequency defined in the notes array. For example, note C (the first entry in the noteNames array) has a frequency of 262Hz (the first entry in the notes array).
The score is an array of notes representing the note names you want to play:
char score[] = “CCGGaaGFFEEDDC GGFFEEDGGFFEED CCGGaaGFFEEDDC “;
Each character in the score that matches a character in the noteNames array will make the note play. The space character is used as a rest, but any character not defined in noteNames will also produce a rest (no note playing).
The sketch calls playNote with each character in the score and duration for the notes of one-third of a second.
The playNote function does a lookup in the noteNames array to find a match and uses the corresponding entry in the frequencies array to get the frequency to sound.
Generating Audio Tones and Fading an LED
We want to produce sounds through a speaker or other audio transducer, and you need to generate the tone in software instead of with a timer; for example, if we need to use analogWrite on pin 9 or 10.
The tone function in earlier recipes is easier to use, but it requires a hardware timer, which may be needed for other tasks such as analogWrite.
This code does not use a timer, but it will not do anything else while the note is played. Unlike the Arduino tone function, the playTone function described here will block, it will not return until the note has finished.
Circuit Diagram
This sketch generates notes without a timer. It plays six notes, each one twice the frequency of (an octave higher than) the previous one. The playTone function generates a tone for a specified duration on a speaker or piezo device connected to a digital output pin and ground.
Code
Two values are used by playTone: period and duration. The variable represented the time of the one cycle tone to play.
The speaker is pulsed high and then low for the number of microseconds given by period. The for loop repeats the pulsing for the number of milliseconds given in the duration argument.
If we prefer to work in frequency rather than a period, we can use the reciprocal relationship between frequency and period; period is equal to 1 divided by frequency.
We need the period value in microseconds because there are 1 million microseconds in one second, the period is calculated as 1000000L / frequency (the “L” at the end of that number tells the compiler that it should calculate using long integer math to prevent the calculation from exceeding the range of a normal integer).
void playFrequency(int frequency, int duration) { int period = 1000000L / frequency; int pulse = period / 2; } The rest of the code is the same as playTone: for(long i = 0; i < duratoion * 1000L; i+= period) { digitalWrite(speakerPin, HIGH); delayMicroseconds(pulse); digitalWrite(speakerPin, LOW); delayMicroseconds(pulse); } }
The codes in this recipe stop and waits until a tone has completed before it can do any other processing. It is possible to produce the sound in the background (without waiting for the sound to finish) by putting the sound generation code in an interrupt handler.
The source code for tone function that comes with the Arduino distribution shows how this is done.
Some More Articles For You:
