Fun With Arduino and Buzzer: Basic, Siren, Morse, Melody

Arduino is a popular open-source platform that can be used for a wide range of DIY projects. In this blog post, we’ll have fun with Arduino and Buzzer, and show you how to interface an Arduino with a buzzer and play some awesome music.

Components Required:

To interface Arduino with a buzzer, you’ll need the following components:

Wiring:

Buzzer Arduino Wiring
  1. Connect the positive pin of the buzzer to the digital pin 8 of the Arduino board.

2. Connect the negative pin of the buzzer to the ground (GND) pin of the Arduino board.

Buzzer Arduino Wiring
(Negative Wire-Buzzer)

Code 1 (Basic):

Here is a basic code to make a piezo work with an Arduino board:

int buzzerPin = 8;

void setup() {
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  tone(buzzerPin, 1000); // set the buzzer to a tone of 1000 Hz
  delay(1000); // wait for 1 second
  noTone(buzzerPin); // stop the tone
  delay(1000); // wait for 1 second
}

  • This code sets pin 8 as an output pin for the buzzer and generates a tone of 1000 Hz using the tone() function.
  • The tone is played for 1 second using the delay() function and then stopped using the noTone() function.
  • The program then waits for another second using the delay() function before starting again.
  • This creates a continuous beep sound with a frequency of 1000 Hz.

You can modify the frequency and duration of the tone to create different sounds or melodies. You can also use the tone() function to play different notes by specifying the frequency of each note.

Code1: Simulation

Below is the simulation for Basic interfacing code

Note:- If you get an error please restart the simulation, it will fix itself.


Code 2 (Siren):

Here is the code to play the siren with the buzzer and Arduino

int buzzerPin = 8;

void setup() {
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  for (int i = 1000; i < 2000; i++) { // increase the frequency of the tone
    tone(buzzerPin, i);
    delay(1);
  }
  for (int i = 2000; i > 1000; i--) { // decrease the frequency of the tone
    tone(buzzerPin, i);
    delay(1);
  }
}
  • This code uses a for loop to gradually increase and decrease the frequency of the tone, creating the sound of an ambulance siren.
  • The tone() function is used to generate the tone at each frequency.
  • The delay() function is used to control the speed of the frequency change.

You can modify the frequency range and the delay time to adjust the sound of the siren to your liking.

Code 2: Simulation

Below is the simulation for the siren code

Note:- If you get an error please restart the simulation, it will fix itself.

Read More: What Is Arduino: An Overview

Code 3 (Morse Code):

Here is the code to play Morse Code with Buzzer especially ( Hello World)

int buzzerPin = 8;
int dotDuration = 200; // duration of a dot in milliseconds
int dashDuration = 3 * dotDuration; // duration of a dash in milliseconds
int interCharDuration = 3 * dotDuration; // duration of space between characters in milliseconds
int interWordDuration = 7 * dotDuration; // duration of space between words in milliseconds

void setup() {
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  String message = "HELLO WORLD"; // message to transmit in Morse code
  for (int i = 0; i < message.length(); i++) {
    char c = toupper(message.charAt(i));
    if (c == ' ') { // space between words
      delay(interWordDuration);
    } else {
      String code = getMorseCode(c);
      for (int j = 0; j < code.length(); j++) {
        if (code.charAt(j) == '.') {
          tone(buzzerPin, 1000, dotDuration);
        } else if (code.charAt(j) == '-') {
          tone(buzzerPin, 1000, dashDuration);
        }
        delay(interCharDuration); // space between dots and dashes
        noTone(buzzerPin);
        delay(dotDuration); // space between each sound
      }
    }
  }
}

String getMorseCode(char c) {
  switch (c) {
    case 'A':
      return ".-";
    case 'B':
      return "-...";
    case 'C':
      return "-.-.";
    case 'D':
      return "-..";
    case 'E':
      return ".";
    case 'F':
      return "..-.";
    case 'G':
      return "--.";
    case 'H':
      return "....";
    case 'I':
      return "..";
    case 'J':
      return ".---";
    case 'K':
      return "-.-";
    case 'L':
      return ".-..";
    case 'M':
      return "--";
    case 'N':
      return "-.";
    case 'O':
      return "---";
    case 'P':
      return ".--.";
    case 'Q':
      return "--.-";
    case 'R':
      return ".-.";
    case 'S':
      return "...";
    case 'T':
      return "-";
    case 'U':
      return "..-";
    case 'V':
      return "...-";
    case 'W':
      return ".--";
    case 'X':
      return "-..-";
    case 'Y':
      return "-.--";
    case 'Z':
      return "--..";
    case '0':
      return "-----";
    case '1':
      return ".----";
    case '2':
      return "..---";
    case '3':
      return "...--";
    case '4':
      return "....-";
    case '5':
      return ".....";
    case '6':
      return "-....";
    case '7':
      return "--...";
    case '8':
      return "---..";
    case '9':
      return "----.";
    default:
      return "";
  }
}
  • This code will transmit the message “HELLO WORLD” in Morse code using the buzzer.
  • The dotDuration, dashDuration, interCharDuration, and interWordDuration variables specify the duration of each element in Morse code.
  • The getMorseCode() function converts each letter and number to its Morse code representation.
  • The for loop plays the Morse code sound for each character in the message.
Code 3: Simulation

Below is the simulation for morse code with buzzer (Hello World)

Note:- If you get an error please restart the simulation, it will fix itself.


Code 4 (Twinkle-Twinkle):

Here is code to play Twinkle Twinkle with Arduino

int buzzerPin = 8;
int melody[] = {
  262, 262, 392, 392, 440, 440, 392,
  349, 349, 330, 330, 294, 294, 262,
  392, 392, 349, 349, 330, 330, 294,
  392, 392, 349, 349, 330, 330, 294,
  262, 262, 392, 392, 440, 440, 392,
  349, 349, 330, 330, 294, 294, 262
};
int noteDuration[] = {
  4, 4, 4, 4, 4, 4, 2,
  4, 4, 4, 4, 4, 4, 2,
  4, 4, 4, 4, 4, 4, 2,
  4, 4, 4, 4, 4, 4, 2,
  4, 4, 4, 4, 4, 4, 2,
  4, 4, 4, 4, 4, 4, 2
};

void setup() {
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  for (int i = 0; i < sizeof(melody) / sizeof(int); i++) {
    tone(buzzerPin, melody[i], 1000/noteDuration[i]);
    delay(1000/noteDuration[i]);
    noTone(buzzerPin);
  }
  delay(5000);
}
  • This code will play the melody of “Twinkle, Twinkle, Little Star” on the buzzer.
  • The melody array contains the frequency of each note, and the noteDuration array contains the duration of each note in milliseconds.
  • The for loop plays each note in the melody, using the tone() function to set the frequency and duration of each note.
  • The noTone() function is used to stop the sound after each note.
Code 4: Simulation

Below is the simulation playing “Twinkle Twinkle” with above-provided code

We tried to cover a few of the interfacing options to interface the buzzer with Arduino and have “Fun with Arduino and Buzzer” Our motto is to make learning Arduino easy and fun and make this topic more interesting for newbies as well as give experts a chance to have a creative approach to build quirky and awesome stuff.

Summary

This blog is focused on Arduino interfacing with Buzzer and inbuilt functions of Arduino like tone( ), noTone( ) and the use of general loops and arrays in Arduino to reduce code length and how can they benefit to build more complex melodies with Arduino, those same codes can be used to interface with the speaker, will provide a detailed explanation in upcoming blogs until then keep exploring and tinkering!

For any queries and suggestion do comment below, eager to help you out and work on your suggestions.

Spread The Knowledge 👨‍💻

One comment

Leave a Reply