Education

Education

Buzzer

What is Buzzer

A buzzer is a small device that makes a sound when electricity flows through it. Think of it like a tiny musical instrument!

How It Works

  • Electricity Makes It Move:
    When you send electricity into the buzzer, it makes a small metal part inside it move or vibrate very quickly.

  • Vibrations Create Sound:
    These fast movements push against the air around the buzzer, creating waves in the air. These waves are called sound waves.

  • You Hear the Buzz:
    Your ears pick up these sound waves as a buzzing sound, just like a beep!

How to turn on buzzer on Yellow Tyro Board

Code

// Buzzer: When its switch (A1) is pressed (reads LOW), beep the buzzer.
if (digitalRead(buzzerSwitchPin) == LOW) {
  digitalWrite(buzzerPin, HIGH);  // Turn the buzzer on
  delay(100);                     // Keep it on for 100 milliseconds (a short beep)
  digitalWrite(buzzerPin, LOW);   // Turn the buzzer off
  delay(200);                     // Wait 200 milliseconds to avoid accidental repeats (debouncing)
}

Checking the Button:
The code asks, "Is the buzzer button pressed?" When the button is pressed, it sends a special signal (LOW) to the Arduino.

  • Beeping the Buzzer:
    If the button is pressed, the code turns the buzzer on. This makes it beep.

  • Short Beep:
    The buzzer stays on for a very short time (100 milliseconds), so you hear a quick beep.

  • Turning Off:
    After 100 milliseconds, the code turns the buzzer off, so the beep stops.

  • Waiting a Little:
    Then the code waits 200 milliseconds. This pause makes sure the Arduino doesn't think you pressed the button many times quickly (this is called debouncing).

Fun Activity!

Now that you understand how the buzzer works and have seen the main code pre-programmed in the Yellow Tyro board, let's have some fun! Below is a sample Arduino code that plays a cheerful "Jungle Bells" tune whenever you press the button. Simply upload this code to your board, press the button, and enjoy the musical surprise!


/*
  ------------------------------------------------------------
                         JumpLabs®
  Follow us on Instagram: jumplabs.co
             LinkedIn: JumpLabs
  "Bringing Families Closer!"
  
  This code blinks the on-board LED every second.
  ------------------------------------------------------------
*/
// Define only the notes we need for the tune
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392

// Pin definitions
const int buzzerPin = 8;    // Buzzer connected to digital pin 8
const int buttonPin = A1;   // Button connected to analog pin A1

// Variable to track if the tune has been played for the current press
bool tunePlayed = false;

// "Jingle Bells" melody (a fun, simplified version)
int melody[] = {
  NOTE_E4, NOTE_E4, NOTE_E4,
  NOTE_E4, NOTE_E4, NOTE_E4,
  NOTE_E4, NOTE_G4, NOTE_C4, NOTE_D4, NOTE_E4,
  NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_E4, NOTE_E4,
  NOTE_D4, NOTE_D4, NOTE_E4,
  NOTE_D4, NOTE_G4
};

// Note durations: 4 means a quarter note, 2 means a half note, 1 means a whole note
int noteDurations[] = {
  4, 4, 2,
  4, 4, 2,
  4, 4, 4, 4, 1,
  4, 4, 4, 4, 4, 4, 4, 4,
  4, 4, 4,
  4, 1
};

void setup() {
  pinMode(buzzerPin, OUTPUT);   // Set the buzzer pin as output
  pinMode(buttonPin, INPUT);      // Set the button pin as input (assumes external pull-down)
}

void loop() {
  // If the button is pressed (reads HIGH) and the tune hasn't been played yet
  if (digitalRead(buttonPin) == HIGH && !tunePlayed) {
    playTune();         // Play the tune
    tunePlayed = true;  // Mark that the tune has been played for this press
  }
  // When the button is released (reads LOW), reset the flag to allow a new press to play the tune
  if (digitalRead(buttonPin) == LOW) {
    tunePlayed = false;
  }
}

void playTune() {
  int notesCount = sizeof(melody) / sizeof(melody[0]);
  for (int i = 0; i < notesCount; i++) {
    int noteDuration = 1000 / noteDurations[i];  // Calculate note duration in milliseconds
    tone(buzzerPin, melody[i], noteDuration);      // Play the note on the buzzer
    int pauseBetweenNotes = noteDuration * 1.30;     // Add a short pause between notes
    delay(pauseBetweenNotes);                        // Wait for the note to finish
    noTone(buzzerPin);                               // Stop the tone before playing the next note
  }
}

Explanation

  • Pin Setup:
    The buzzer is connected to digital pin 8, and the button is connected to analog pin A1. We set the buzzer as an output and the button as an input. (This sketch assumes you’re using an external pull-down resistor so that the button reads LOW when not pressed and HIGH when pressed.)

  • Button and Tune Logic:
    In the loop(), the code checks if the button reads HIGH and if the tune hasn't been played yet (using the tunePlayed flag). When both conditions are true, the playTune() function is called to play the melody.
    When the button is released (reads LOW), the tunePlayed flag is reset to allow the tune to be played again on the next press.

  • Playing the Tune:
    The playTune() function cycles through the melody array, calculates how long each note should sound (using the noteDurations array), and uses the tone() function to play each note on the buzzer. After each note, it pauses briefly before moving to the next note.

Thank you for exploring our buzzer page—a place where sound meets creativity! As you continue your Robo Explorer journey, may every project inspire you to think bigger and brighter. Now, you can proceed to the next chapter, where we dive into the fascinating world of potentiometers. Enjoy the adventure!

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+91 9910566229

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+91 9910566229

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us

Socials

info.jumplabs@gmail.com

Call Us

+4550142069

+91 9910566229

Damgade 82, 6400 Sønderborg

© Jumplabs Inc. 2023

Reach out to Us