02 - Lab Using Active Buzzer

Video

# Lab: Active Buzzer Sound Patterns ## **Objective** Learn how to use an active buzzer to create various sound patterns, musical notes, and interactive audio feedback. This lab covers basic buzzer control, musical note generation, alarm patterns, and sound effects. ## **Required Components** ![](https://cdn.shopify.com/s/files/1/0331/9994/7908/files/Pasted_image_20250723133430.png?v=1753857539) 1. **Lonely Binary UNO R3** - Main Arduino board 2. **TinkerBlock UNO R3 Shield** - Expansion shield that plugs onto the UNO R3 3. **TinkerBlock TK36** - Active Buzzer Module ## **Theory** ### **Active Buzzer Technology** - **Piezoelectric Effect**: Crystal material vibrates when voltage is applied - **Built-in Oscillator**: Generates its own frequency when powered - **Simple Control**: Just needs HIGH/LOW signal to turn on/off - **Fixed Frequency**: Typically operates at a specific frequency (e.g., 2.5kHz) ### **Sound Characteristics** - **Frequency**: Determines pitch (higher = higher pitch) - **Duration**: How long the sound plays - **Pattern**: Timing of on/off cycles - **Volume**: Cannot be easily controlled (fixed amplitude) ### **Applications** - **Alarm Systems**: Security and safety alerts - **User Feedback**: Button presses, notifications - **Musical Instruments**: Simple melodies and tunes - **Game Sounds**: Interactive audio feedback - **Status Indicators**: System state announcements ## **Wiring Instructions** ### **TK36 Active Buzzer Pinout** - **GND** → Arduino GND - **VCC** → Arduino 5V - **NC** → No Connection - **Signal** → Arduino Digital Pin 9 ### **Connection Diagram** ![](https://cdn.shopify.com/s/files/1/0331/9994/7908/files/Pasted_image_20250723133514.png?v=1753857544) ``` UNO R3 + Shield └── Digital Pin 9 ───→ TK36 Signal ``` ## **Basic Buzzer Control Code** ```cpp // Basic Active Buzzer Control // Pin definitions #define BUZZER_PIN 9 // Digital pin connected to TK36 buzzer // Timing definitions #define SHORT_BEEP 100 // Short beep duration (ms) #define LONG_BEEP 500 // Long beep duration (ms) #define BEEP_PAUSE 200 // Pause between beeps (ms) void setup() { // Initialize serial communication Serial.begin(9600); Serial.println("Active Buzzer Basic Control"); Serial.println("==========================="); // Initialize buzzer pin pinMode(BUZZER_PIN, OUTPUT); digitalWrite(BUZZER_PIN, LOW); // Start with buzzer off // Wait for system to stabilize delay(1000); Serial.println("Buzzer initialized. Starting sound patterns..."); Serial.println(); } void loop() { // Pattern 1: Single beep Serial.println("Pattern 1: Single Short Beep"); singleBeep(SHORT_BEEP); delay(1000); // Pattern 2: Double beep Serial.println("Pattern 2: Double Beep"); doubleBeep(); delay(1000); // Pattern 3: Triple beep Serial.println("Pattern 3: Triple Beep"); tripleBeep(); delay(1000); // Pattern 4: Long beep Serial.println("Pattern 4: Long Beep"); singleBeep(LONG_BEEP); delay(1000); // Pattern 5: Rapid beeps Serial.println("Pattern 5: Rapid Beeps"); rapidBeeps(10); delay(1000); // Pattern 6: Morse code SOS Serial.println("Pattern 6: Morse Code SOS"); morseSOS(); delay(2000); } // Function to create a single beep void singleBeep(int duration) { digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); } // Function to create a double beep void doubleBeep() { singleBeep(SHORT_BEEP); delay(BEEP_PAUSE); singleBeep(SHORT_BEEP); } // Function to create a triple beep void tripleBeep() { singleBeep(SHORT_BEEP); delay(BEEP_PAUSE); singleBeep(SHORT_BEEP); delay(BEEP_PAUSE); singleBeep(SHORT_BEEP); } // Function to create rapid beeps void rapidBeeps(int count) { for (int i = 0; i < count; i++) { singleBeep(50); delay(50); } } // Function to play Morse code SOS void morseSOS() { // S: ... (3 short) for (int i = 0; i < 3; i++) { singleBeep(200); delay(200); } delay(400); // O: --- (3 long) for (int i = 0; i < 3; i++) { singleBeep(600); delay(200); } delay(400); // S: ... (3 short) for (int i = 0; i < 3; i++) { singleBeep(200); delay(200); } } ``` ## **Code Explanation** ### **Pin Configuration and Constants** ```cpp #define BUZZER_PIN 9 // Digital pin connected to TK36 buzzer #define SHORT_BEEP 100 // Short beep duration (ms) #define LONG_BEEP 500 // Long beep duration (ms) #define BEEP_PAUSE 200 // Pause between beeps (ms) ``` - **BUZZER_PIN**: Defines the digital pin (D9) connected to the TK36 buzzer - **SHORT_BEEP**: 100ms duration for quick notification sounds - **LONG_BEEP**: 500ms duration for attention-grabbing sounds - **BEEP_PAUSE**: 200ms pause between consecutive beeps for clear separation ### **Setup Function** ```cpp void setup() { Serial.begin(9600); pinMode(BUZZER_PIN, OUTPUT); digitalWrite(BUZZER_PIN, LOW); delay(1000); } ``` - **Serial Communication**: Initializes serial monitor for debugging and status messages - **Pin Mode**: Sets BUZZER_PIN as OUTPUT to control the buzzer - **Initial State**: Ensures buzzer starts in OFF state (LOW) - **Stabilization Delay**: Allows system to settle before starting ### **Main Loop Structure** ```cpp void loop() { // Pattern 1: Single beep Serial.println("Pattern 1: Single Short Beep"); singleBeep(SHORT_BEEP); delay(1000); // Pattern 2: Double beep Serial.println("Pattern 2: Double Beep"); doubleBeep(); delay(1000); // ... continues with other patterns } ``` - **Sequential Execution**: Each pattern plays in order with 1-second delays - **Status Messages**: Serial output shows which pattern is currently playing - **Pattern Variety**: Demonstrates different sound combinations and timings ### **Core Buzzer Control Function** ```cpp void singleBeep(int duration) { digitalWrite(BUZZER_PIN, HIGH); delay(duration); digitalWrite(BUZZER_PIN, LOW); } ``` - **HIGH Signal**: Activates the buzzer by setting pin to HIGH (5V) - **Duration Control**: Uses delay() to maintain the sound for specified milliseconds - **LOW Signal**: Deactivates buzzer by setting pin to LOW (0V) - **Parameter Flexibility**: Accepts any duration value for custom beep lengths ### **Pattern Building Functions** #### **Double Beep Pattern** ```cpp void doubleBeep() { singleBeep(SHORT_BEEP); delay(BEEP_PAUSE); singleBeep(SHORT_BEEP); } ``` - **First Beep**: Creates initial sound using singleBeep() - **Pause**: Waits 200ms between beeps for clear separation - **Second Beep**: Creates identical follow-up sound - **Reusability**: Uses the core singleBeep() function for consistency #### **Triple Beep Pattern** ```cpp void tripleBeep() { singleBeep(SHORT_BEEP); delay(BEEP_PAUSE); singleBeep(SHORT_BEEP); delay(BEEP_PAUSE); singleBeep(SHORT_BEEP); } ``` - **Three Beeps**: Creates a sequence of three identical sounds - **Consistent Timing**: Uses same SHORT_BEEP and BEEP_PAUSE values - **Clear Rhythm**: Establishes a recognizable three-beat pattern #### **Rapid Beep Function** ```cpp void rapidBeeps(int count) { for (int i = 0; i < count; i++) { singleBeep(50); delay(50); } } ``` - **Loop Control**: Uses for loop to repeat beeps specified number of times - **Quick Timing**: 50ms beeps with 50ms pauses create rapid-fire effect - **Parameter Control**: Count parameter allows customizing number of beeps - **Efficient Code**: Reuses singleBeep() function instead of duplicating logic ### **Morse Code Implementation** ```cpp void morseSOS() { // S: ... (3 short) for (int i = 0; i < 3; i++) { singleBeep(200); delay(200); } delay(400); // O: --- (3 long) for (int i = 0; i < 3; i++) { singleBeep(600); delay(200); } delay(400); // S: ... (3 short) for (int i = 0; i < 3; i++) { singleBeep(200); delay(200); } } ``` - **Morse Code Structure**: Implements international distress signal SOS - **Letter S**: Three short beeps (200ms each) with 200ms pauses - **Letter O**: Three long beeps (600ms each) with 200ms pauses - **Word Separation**: 400ms delays between letters for clear distinction - **Emergency Pattern**: Recognizable pattern for emergency situations ### **Key Programming Concepts** #### **Function Modularity** - **Reusable Functions**: singleBeep() serves as building block for all patterns - **Code Efficiency**: Avoids code duplication by reusing core function - **Maintainability**: Changes to beep logic only need to be made in one place - **Scalability**: Easy to add new patterns using existing functions #### **Timing Control** - **Precise Delays**: Uses delay() for accurate timing control - **Pattern Recognition**: Consistent timing creates recognizable sound patterns - **User Experience**: Appropriate pauses prevent overwhelming or confusing sounds - **Professional Quality**: Proper timing makes sounds sound intentional and polished #### **Parameter Flexibility** - **Customizable Durations**: Functions accept duration parameters for flexibility - **Configurable Counts**: rapidBeeps() accepts count parameter for variable length - **Easy Modification**: Constants can be adjusted to change timing without code changes - **Adaptable Patterns**: Same functions can create different effects with different parameters ### **Active Buzzer Characteristics** - **Simple Control**: Only requires HIGH/LOW digital signals - **Fixed Frequency**: Built-in oscillator provides consistent pitch - **Immediate Response**: No warm-up time required - **Reliable Operation**: Works consistently across different Arduino boards - **Low Power**: Minimal current draw for battery-powered applications ### **Common Applications** - **User Interface Feedback**: Button press confirmation, menu navigation - **Alarm Systems**: Security alerts, timer notifications, error conditions - **Game Development**: Sound effects, scoring feedback, level completion - **Educational Projects**: Learning Morse code, sound pattern recognition - **Prototyping**: Quick audio feedback during development and testing