Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Interface Buzzer with Arduino

Introductio

An active buzzer module is a simple sound-producing component that’s easy to integrate with Arduino. Unlike a passive buzzer, it generates sound when voltage is applied, making it ideal for alerts and alarms. This module typically has 3 pins (VCC, GND, and Signal), allowing for direct control of sound frequency and duration through Arduino. In this tutorial, we’ll explore how to program Arduino to play different tones on the buzzer using the tone() function.

Required Components

  • Arduino UNO, Nano
  • Buzzer module
  • Jumper wires
  • Breadboard (optional)

Pinout

Circuit Diagram / Wiring

  • BUZZER VCC → 5V (Arduino)
  • BUZZER GND → GND (Arduino)
  • BUZZER SIGNAL → Pin D9 (Arduino)

Arduino Code / Programming

The following code demonstrates how to generate different tones using the tone() function with a passive buzzer.

int buzzerPin = 9; // Pin connected to the buzzer

void setup() {
  // Nothing is required here
}

void loop() {
  // Play a tone at 1000 Hz for 500 milliseconds
  tone(buzzerPin, 1000, 500);
  delay(1000); // Wait for 1 second before playing the next tone

  // Play a tone at 1500 Hz for 500 milliseconds
  tone(buzzerPin, 1500, 500);
  delay(1000); // Wait for 1 second before playing the next tone

  // Stop the tone for 1 second
  noTone(buzzerPin);
  delay(1000);
}

Explanation of the Code

  • Pin Definitions: The pin connected to the buzzer is defined.
  • tone() Function: This function generates a square wave of a specified frequency on the buzzer pin, allowing for different sounds or tones. It takes three parameters: the pin number, the frequency in hertz, and the duration in milliseconds.
  • noTone() Function: Stops any tone being played on the specified pin.
  • Delays: Used to add pauses between tones for distinct sound patterns.

Testing and Troubleshooting

  • Verify Connections: Make sure the buzzer is connected correctly to the Arduino.
  • Monitor Output: The buzzer should produce sound as specified in the code.
  • Adjust Tone Frequency and Duration: Experiment with different values in the tone() function to produce various sounds.

Arduino Projects:

Arduino project 1 – Creating Proximity Alert System with Sonar, Buzzer, and LED

This project uses a sonar sensor, buzzer module, and LED with an Arduino to create a simple distance-based alert system. The sonar sensor measures the distance between the sensor and an object, and based on that distance, an LED lights up and a buzzer sounds to provide feedback. The closer the object gets, the more intense the alert becomes. This project demonstrates how to use ultrasonic sensors and basic output modules like LEDs and buzzers to interact with the environment.

Required Components

  • Arduino UNO, Nano
  • Buzzer module
  • LED
  • Ultrasonic Sensor
  • Jumper wires
  • Breadboard (optional)

Circuit Diagram / Wiring

  • Buzzer module with Arduino
    • BUZZER VCC → 5V (Arduino)
    • BUZZER GND → GND (Arduino)
    • BUZZER SIGNAL → Pin D11 (Arduino)
  • Ultrasonic Sensor with Arduino
    • Ultrasonic VCC → 5V (Arduino)
    • Ultrasonic GND → GND (Arduino)
    • Ultrasonic TRIG → Pin D9 (Arduino)
    • Ultrasonic ECHO → Pin D10 (Arduino)
  • LED wtih Arduino
    • Connect the longer leg (anode) of the LED to digital pin 13 through a 220-ohm resistor.
    • Connect the shorter leg (cathode) to the GND pin.

Arduino Code

// Define the pins
#define TRIG_PIN 9  // Pin connected to the TRIG pin of the ultrasonic sensor
#define ECHO_PIN 10 // Pin connected to the ECHO pin of the ultrasonic sensor
#define BUZZER_PIN 11 // Pin connected to the Buzzer
#define LED_PIN 13    // Pin connected to the LED

long duration;
int distance;

void setup() {
  // Initialize the pins
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  
  // Start serial communication
  Serial.begin(9600);
}

void loop() {
  // Send a pulse to the trigger pin
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Measure the duration of the pulse
  duration = pulseIn(ECHO_PIN, HIGH);

  // Calculate the distance (in cm)
  distance = duration * 0.0344 / 2;

  // Print the distance to the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // If the distance is less than a certain threshold, activate the buzzer and LED
  if (distance < 20) {
    digitalWrite(BUZZER_PIN, HIGH);  // Turn on the Buzzer
    digitalWrite(LED_PIN, HIGH);     // Turn on the LED
  } else {
    digitalWrite(BUZZER_PIN, LOW);   // Turn off the Buzzer
    digitalWrite(LED_PIN, LOW);      // Turn off the LED
  }

  // Small delay before the next measurement
  delay(500);
}

Explanation:

  • Ultrasonic Sensor: The TRIG pin sends out a pulse, and the ECHO pin receives the pulse back. The duration of the pulse is used to calculate the distance.
  • LED: Lights up when the object is detected within the threshold distance (20 cm in this case).
  • Buzzer: Emits a sound when the object is detected within the threshold distance.
  • Threshold Distance: You can change the value from 20 cm to any value that suits your needs.

How It Works:

  1. The ultrasonic sensor measures the distance to an object in front of it.
  2. If the object is within 20 cm, the buzzer and LED are turned on.
  3. If the object is farther than 20 cm, both the buzzer and LED are turned off.

Troubleshooting

  • Check wiring: Ensure correct connections for the ultrasonic sensor, buzzer, and LED.
  • Test components individually: Verify the ultrasonic sensor, buzzer, and LED work with simple code.
  • Verify power supply: Ensure the Arduino and all components are properly powered.
  • Use Serial Monitor: Check the distance readings and ensure correct serial output for debugging.

    Leave a Reply

    Your email address will not be published.

    Need Help?