Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
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.
Before diving into the code, let's set up the circuit:
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);
}
}
tone(buzzerPin, 1000);
generates a 1000 Hz sound on the buzzer pin.noTone(buzzerPin);
stops the sound.digitalRead(buttonPin)
function reads the state of the button (HIGH if pressed, LOW if not).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.