Buzzer Sound Generation Using Arduino Uno in Wokwi – Beginner Guide with Code

 

Introduction

In this project, we will learn how to generate sound using a buzzer with Arduino Uno in the Wokwi simulator. Buzzers are commonly used in alarms, timers, security systems, and IoT alert devices.

This beginner-friendly Arduino project helps you understand:

  • Digital output control

  • Tone generation using tone() function

  • Frequency control

  • Basic sound patterns

You can simulate the entire circuit online using Wokwi without physical components.


Components Required (Wokwi Simulation)

  • Arduino Uno

  • Piezo Buzzer

  • Jumper wires


Circuit Connections

Diagram.json:
{
  "version": 1,
  "author": "Claude",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-uno", "id": "uno", "top": 211.8, "left": -144.6, "attrs": {} },
    {
      "type": "wokwi-buzzer",
      "id": "buzzer1",
      "top": 127.2,
      "left": 117,
      "attrs": { "volume": "0.8" }
    },
    { "type": "wokwi-led", "id": "led1", "top": 100, "left": 250, "attrs": { "color": "red" } },
    { "type": "wokwi-resistor", "id": "r1", "top": 110, "left": 180, "attrs": { "value": "220" } },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": 200,
      "left": 50,
      "attrs": { "color": "green" }
    }
  ],
  "connections": [
    [ "uno:8", "buzzer1:1", "green", [ "v0" ] ],
    [ "buzzer1:2", "uno:GND.1", "black", [ "v0" ] ],
    [ "uno:13", "r1:1", "red", [ "v0" ] ],
    [ "r1:2", "led1:A", "red", [ "v0" ] ],
    [ "led1:C", "uno:GND.2", "black", [ "v0" ] ],
    [ "btn1:1.l", "uno:2", "blue", [ "v0" ] ],
    [ "btn1:2.r", "uno:GND.3", "black", [ "v0" ] ]
  ],
  "dependencies": {}
}

Buzzer PinArduino Connection
Positive (+)Pin 8
Negative (–)GND

That’s it! The wiring is very simple.

How the Code Works

Code:
/*
 * Buzzer Sound Generation - IMPROVED VERSION
 * Better button response and clear mode indicators
 * Press button ANYTIME to change modes!
 */

// Pin Definitions
const int buzzerPin = 8;
const int buttonPin = 2;
const int ledPin = 13;

// Variables
volatile int soundMode = 0;  // Use volatile for interrupt
int lastMode = -1;           // Track mode changes
bool modeChanged = true;     // Flag for mode change

// Musical note frequencies
#define NOTE_C4  262
#define NOTE_D4  294
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_G4  392
#define NOTE_A4  440
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_D5  587
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_G5  784
#define NOTE_A5  880
#define NOTE_B5  988
#define REST 0

void setup() {
  pinMode(buzzerPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
 
  // Attach interrupt for instant button response
  attachInterrupt(digitalPinToInterrupt(buttonPin), buttonPressed, FALLING);
 
  Serial.begin(9600);
  Serial.println("\n========================================");
  Serial.println("   BUZZER SOUND SYSTEM - IMPROVED");
  Serial.println("========================================");
  Serial.println("Press button ANYTIME to change modes!");
  Serial.println("Watch the LED blink pattern!");
  Serial.println("========================================");
  Serial.println("Mode 0: Simple Beeps (1 blink)");
  Serial.println("Mode 1: Alarm (2 blinks)");
  Serial.println("Mode 2: Police Siren (3 blinks)");
  Serial.println("Mode 3: Ambulance (4 blinks)");
  Serial.println("Mode 4: Door Bell (5 blinks)");
  Serial.println("Mode 5: Happy Birthday (6 blinks)");
  Serial.println("Mode 6: Twinkle Star (7 blinks)");
  Serial.println("Mode 7: Mario (8 blinks)");
  Serial.println("Mode 8: Star Wars (9 blinks)");
  Serial.println("Mode 9: Nokia (10 blinks)");
  Serial.println("========================================\n");
 
  playStartupSound();
}

void loop() {
  // Check if mode changed
  if (modeChanged) {
    noTone(buzzerPin);  // Stop current sound
    showMode();         // Show mode with LED blinks
    modeChanged = false;
    lastMode = soundMode;
  }
 
  // Play current mode
  switch(soundMode) {
    case 0: simpleBeeps(); break;
    case 1: alarmSound(); break;
    case 2: policeSiren(); break;
    case 3: ambulanceSiren(); break;
    case 4: doorBell(); break;
    case 5: playHappyBirthday(); break;
    case 6: playTwinkleTwinkle(); break;
    case 7: playMarioTheme(); break;
    case 8: playStarWars(); break;
    case 9: playNokiaRingtone(); break;
  }
 
  delay(100);  // Small delay
}

// Interrupt service routine
void buttonPressed() {
  static unsigned long lastInterruptTime = 0;
  unsigned long interruptTime = millis();
 
  // Debounce - ignore if pressed within 200ms
  if (interruptTime - lastInterruptTime > 200) {
    soundMode++;
    if (soundMode > 9) soundMode = 0;
    modeChanged = true;
  }
  lastInterruptTime = interruptTime;
}

// Show current mode with LED blinks
void showMode() {
  Serial.print("\n>>> MODE ");
  Serial.print(soundMode);
  Serial.print(": ");
 
  switch(soundMode) {
    case 0: Serial.println("Simple Beeps"); break;
    case 1: Serial.println("Alarm Sound"); break;
    case 2: Serial.println("Police Siren"); break;
    case 3: Serial.println("Ambulance Siren"); break;
    case 4: Serial.println("Door Bell"); break;
    case 5: Serial.println("Happy Birthday"); break;
    case 6: Serial.println("Twinkle Twinkle"); break;
    case 7: Serial.println("Mario Theme"); break;
    case 8: Serial.println("Star Wars"); break;
    case 9: Serial.println("Nokia Ringtone"); break;
  }
 
  // Blink LED (mode + 1) times
  delay(500);
  for(int i = 0; i <= soundMode; i++) {
    digitalWrite(ledPin, HIGH);
    tone(buzzerPin, 1000, 50);  // Short beep
    delay(150);
    digitalWrite(ledPin, LOW);
    delay(150);
  }
  delay(500);
}

// ============================================
// MODE 0: Simple Beeps
// ============================================
void simpleBeeps() {
  digitalWrite(ledPin, HIGH);
  tone(buzzerPin, 1000, 200);
  delay(200);
  digitalWrite(ledPin, LOW);
  delay(800);
}

// ============================================
// MODE 1: Alarm Sound
// ============================================
void alarmSound() {
  for(int i = 0; i < 2; i++) {
    digitalWrite(ledPin, HIGH);
    tone(buzzerPin, 1000, 300);
    delay(300);
    digitalWrite(ledPin, LOW);
    tone(buzzerPin, 500, 300);
    delay(300);
  }
  delay(400);
}

// ============================================
// MODE 2: Police Siren
// ============================================
void policeSiren() {
  // Faster siren
  for(int freq = 500; freq < 1500; freq += 100) {
    tone(buzzerPin, freq, 20);
    digitalWrite(ledPin, HIGH);
    delay(20);
  }
  for(int freq = 1500; freq > 500; freq -= 100) {
    tone(buzzerPin, freq, 20);
    digitalWrite(ledPin, LOW);
    delay(20);
  }
  delay(200);
}

// ============================================
// MODE 3: Ambulance Siren
// ============================================
void ambulanceSiren() {
  for(int i = 0; i < 3; i++) {
    digitalWrite(ledPin, HIGH);
    tone(buzzerPin, 600, 150);
    delay(150);
    digitalWrite(ledPin, LOW);
    tone(buzzerPin, 800, 150);
    delay(150);
  }
  delay(300);
}

// ============================================
// MODE 4: Door Bell
// ============================================
void doorBell() {
  digitalWrite(ledPin, HIGH);
  tone(buzzerPin, 1000, 200);
  delay(200);
  tone(buzzerPin, 800, 400);
  delay(400);
  digitalWrite(ledPin, LOW);
  delay(1000);
}

// ============================================
// MODE 5: Happy Birthday (SHORT VERSION)
// ============================================
void playHappyBirthday() {
  int melody[] = {
    NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4,
    NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4
  };
 
  int durations[] = {8, 8, 4, 4, 4, 2, 8, 8, 4, 4, 4, 2};
 
  for (int i = 0; i < 12; i++) {
    int duration = 1000 / durations[i];
    digitalWrite(ledPin, HIGH);
    tone(buzzerPin, melody[i], duration);
    delay(duration * 1.2);
    digitalWrite(ledPin, LOW);
    noTone(buzzerPin);
    delay(50);
  }
  delay(1000);
}

// ============================================
// MODE 6: Twinkle Twinkle (SHORT VERSION)
// ============================================
void playTwinkleTwinkle() {
  int melody[] = {
    NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4,
    NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_C4
  };
 
  int durations[] = {4, 4, 4, 4, 4, 4, 2, 4, 4, 4, 4, 4, 4, 2};
 
  for (int i = 0; i < 14; i++) {
    int duration = 1000 / durations[i];
    digitalWrite(ledPin, HIGH);
    tone(buzzerPin, melody[i], duration);
    delay(duration * 1.2);
    digitalWrite(ledPin, LOW);
    noTone(buzzerPin);
    delay(50);
  }
  delay(1000);
}

// ============================================
// MODE 7: Mario Theme
// ============================================
void playMarioTheme() {
  int melody[] = {
    NOTE_E5, NOTE_E5, REST, NOTE_E5, REST, NOTE_C5, NOTE_E5, NOTE_G5
  };
 
  int durations[] = {8, 8, 8, 8, 8, 8, 8, 4};
 
  for (int i = 0; i < 8; i++) {
    int duration = 1000 / durations[i];
    if (melody[i] == REST) {
      delay(duration);
    } else {
      digitalWrite(ledPin, HIGH);
      tone(buzzerPin, melody[i], duration);
      delay(duration * 1.2);
      digitalWrite(ledPin, LOW);
      noTone(buzzerPin);
    }
    delay(50);
  }
  delay(1000);
}

// ============================================
// MODE 8: Star Wars
// ============================================
void playStarWars() {
  int melody[] = {
    NOTE_A4, NOTE_A4, NOTE_A4, NOTE_F4, NOTE_C5,
    NOTE_A4, NOTE_F4, NOTE_C5, NOTE_A4
  };
 
  int durations[] = {8, 8, 8, 6, 16, 8, 6, 16, 4};
 
  for (int i = 0; i < 9; i++) {
    int duration = 1000 / durations[i];
    digitalWrite(ledPin, HIGH);
    tone(buzzerPin, melody[i], duration);
    delay(duration * 1.2);
    digitalWrite(ledPin, LOW);
    noTone(buzzerPin);
    delay(50);
  }
  delay(1000);
}

// ============================================
// MODE 9: Nokia Ringtone
// ============================================
void playNokiaRingtone() {
  int melody[] = {
    NOTE_E5, NOTE_D5, NOTE_F4, NOTE_G4,
    NOTE_C5, NOTE_B4, NOTE_D4, NOTE_E4
  };
 
  int durations[] = {8, 8, 4, 4, 8, 8, 4, 4};
 
  for (int i = 0; i < 8; i++) {
    int duration = 1000 / durations[i];
    digitalWrite(ledPin, HIGH);
    tone(buzzerPin, melody[i], duration);
    delay(duration * 1.2);
    digitalWrite(ledPin, LOW);
    noTone(buzzerPin);
    delay(50);
  }
  delay(1000);
}

// ============================================
// Startup Sound
// ============================================
void playStartupSound() {
  Serial.println("Starting up...");
  for(int i = 200; i < 1000; i += 100) {
    tone(buzzerPin, i, 50);
    digitalWrite(ledPin, HIGH);
    delay(50);
    digitalWrite(ledPin, LOW);
    delay(50);
  }
  Serial.println("Ready!\n");
}

  • tone(pin, frequency) generates sound at a specific frequency (in Hz).

  • noTone(pin) stops the sound.

  • delay() controls how long the buzzer stays ON or OFF.

  • 1000 Hz produces a medium-pitch sound.

Applications of Buzzer with Arduino

  • Alarm systems

  • Fire detection alerts

  • Timer notification systems

  • Smart home alerts

  • Security systems

  • School exhibition Arduino projects


Why Use Wokwi Simulator?

  • No hardware required

  • Easy to test sound logic

  • Beginner-friendly interface

  • Perfect for Arduino learners

Wokwi allows you to simulate buzzer sound generation instantly.


FAQ

Q1: What is the tone() function?

It generates a square wave of a specific frequency on a digital pin.

Q2: Can I play music using a buzzer?

Yes, by programming different frequencies in sequence.

Q3: Is this project suitable for beginners?

Yes, this is one of the easiest Arduino output projects.


Conclusion

The Buzzer Sound Generation using Arduino Uno in Wokwi is a simple yet powerful beginner project. It teaches frequency control, sound generation, and digital output concepts in embedded systems.

This project is perfect for robotics students, electronics beginners, and IoT enthusiasts.


Comments