Arduino Buzzer Tone With LED

Christian Jaya
2 min readApr 24, 2021

--

A buzzer or beeper is an audio signalling device, which may be mechanical, electromechanical, or piezoelectric (piezo for short). Typical uses of buzzers and beepers include alarm devices, timers, and confirmation of user input such as a mouse click or keystroke.

Buzzer Piezo

In this tutorial you will learn how to sound a buzzer or piezo speaker and also turn on the LED light according to the buzzer tone sounded on the Arduino.

1. WHAT YOU WILL NEED?

  • Arduino Uno.
  • Breadboard.
  • Buzzer/Piezo speaker.
  • 220 Ohm resistor.

2. THE CIRCUIT

The connections are pretty easy, see the image above with breadboard circuit schematic.

3. THE CODE

#define led0 HIGH

int ledPins[] = {9};
int pinCount = 1;

#define B3 246.94
#define C4 261.63
#define D4 293.67
#define E4 329.63
#define F4 349.43
#define G4 392.00
#define A4 440.00
#define B4 493.88
#define C5 523.25

int melody[] =
{
C4, D4, E4, F4, G4, E4, C4,
A4, C5, B4, A4, G4,
F4, A4, G4, F4, E4, C4,
D4, F4, E4, D4, C4,

F4, E4, F4, A4, G4, A4, G4, E4, C4,
E4, D4, E4, F4, G4, E4,
F4, E4, F4, A4, G4, A4, G4, E4, C4,
E4, D4, F4, B3, D4, C4
};

int noteDurations[] =
{
8, 16, 12, 12, 8, 16, 6,
8, 16, 12, 12, 3,
8, 16, 12, 12, 6, 6,
8, 16, 12, 12, 3,

8, 16, 12, 12, 24, 24, 24, 24, 12,
10, 12, 12, 12, 12, 3,
8, 16, 12, 12, 24, 24, 24, 24, 12,
10, 12, 12, 12, 12, 3
};

byte LEDs[] =
{
HIGH
};

void setup()
{
for(int i = 0; i < pinCount; i++)
{
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW);
}

for(int thisNote = 0; thisNote < 53; thisNote++)
{
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);

LEDon(thisNote);
delay(noteDuration);
LEDoff(thisNote);

int pauseBetweenNotes = noteDuration * 4;
delay(pauseBetweenNotes);
noTone(8);
}
}

void loop()
{
}

void LEDon(int thisNote)
{
if((LEDs[thisNote] & led0) != 0) digitalWrite(ledPins[0], HIGH);
}

void LEDoff(int thisNote)
{
if((LEDs[thisNote] & led0) != 0) digitalWrite(ledPins[0], LOW);
}

4. WELL DONE

You can see the results in the following video :

--

--