Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Create an Alarme Sonoro Using Arduino

Creating an alarme sonoro (audible alarm) using an Arduino is a practical and rewarding project that can be applied in various scenarios, such as security systems, notification systems, or simple sound alerts. This article will guide you through the steps to build an audible alarm using an Arduino board, a piezo buzzer, and a few other components.

Materials Needed

  • Arduino Uno (or any compatible Arduino board)
  • Piezo buzzer
  • Breadboard
  • Jumper wires
  • Resistor (220 ohms)
  • Push button (optional, for manual triggering)

Circuit Diagram

Before diving into the code, let's set up the circuit:

  1. Connect the positive leg of the piezo buzzer to digital pin 8 on the Arduino.
  2. Connect the negative leg of the piezo buzzer to the ground (GND) on the Arduino.
  3. If using a push button, connect one leg of the push button to digital pin 2 on the Arduino and the other leg to the ground (GND).
  4. Use a 220-ohm resistor in series with the push button to limit the current.

Example Code

Below is a sample code to create an audible alarm that will sound when a button is pressed. If you don't have a button, you can modify the code to trigger the alarm based on other conditions (e.g., a sensor reading).

// Define pin numbers
const int buzzerPin = 8;
const int buttonPin = 2;

// Variable to store the button state
int buttonState = 0;

void setup() {
  // Initialize the buzzer pin as an output
  pinMode(buzzerPin, OUTPUT);

  // Initialize the button pin as an input
  pinMode(buttonPin, INPUT);
}

void loop() {
  // Read the state of the button
  buttonState = digitalRead(buttonPin);

  // Check if the button is pressed
  if (buttonState == HIGH) {
    // Sound the alarm
    tone(buzzerPin, 1000); // 1000 Hz frequency
  } else {
    // Turn off the alarm
    noTone(buzzerPin);
  }
}

Explanation

  • tone(buzzerPin, 1000); generates a 1000 Hz sound on the buzzer pin.
  • noTone(buzzerPin); stops the sound.
  • The digitalRead(buttonPin) function reads the state of the button (HIGH if pressed, LOW if not).

Customizing the Alarm

You can customize the alarm sound by changing the frequency and duration of the tone. For example, to create a beeping sound, you can modify the loop as follows:

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    // Sound the alarm with a beeping pattern
    tone(buzzerPin, 1000); // 1000 Hz frequency
    delay(500);            // Wait for 500 milliseconds
    noTone(buzzerPin);     // Turn off the alarm
    delay(500);            // Wait for 500 milliseconds
  } else {
    noTone(buzzerPin);
  }
}

This will create a beeping sound every second when the button is pressed.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.