Bird
0
0
Arduinoprogramming~10 mins

Alarm system with sensor and buzzer in Arduino

Choose your learning style9 modes available
Introduction

An alarm system helps warn you when something happens, like detecting movement. Using a sensor and a buzzer, you can make a simple alert system.

You want to know if someone enters a room.
You want to detect if a door or window opens.
You want to create a simple security alert for your home.
You want to learn how sensors and buzzers work together.
You want to practice basic Arduino programming with input and output.
Syntax
Arduino
const int sensorPin = 2;  // Pin connected to sensor output
const int buzzerPin = 9;  // Pin connected to buzzer

void setup() {
  pinMode(sensorPin, INPUT);  // Set sensor pin as input
  pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
}

void loop() {
  int sensorState = digitalRead(sensorPin);  // Read sensor
  if (sensorState == HIGH) {  // If sensor detects something
    digitalWrite(buzzerPin, HIGH);  // Turn buzzer on
  } else {
    digitalWrite(buzzerPin, LOW);   // Turn buzzer off
  }
}

sensorPin is where the sensor is connected.

buzzerPin controls the buzzer sound.

Examples
This example uses different pins but works the same way.
Arduino
const int sensorPin = 3;
const int buzzerPin = 8;

void setup() {
  pinMode(sensorPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  if (digitalRead(sensorPin) == HIGH) {
    digitalWrite(buzzerPin, HIGH);
  } else {
    digitalWrite(buzzerPin, LOW);
  }
}
This example uses an internal pull-up resistor and assumes the sensor gives LOW when triggered.
Arduino
const int sensorPin = 2;
const int buzzerPin = 9;

void setup() {
  pinMode(sensorPin, INPUT_PULLUP);  // Use internal pull-up resistor
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  if (digitalRead(sensorPin) == LOW) {  // Sensor active low
    digitalWrite(buzzerPin, HIGH);
  } else {
    digitalWrite(buzzerPin, LOW);
  }
}
Sample Program

This program reads the sensor state every half second. If the sensor detects something (HIGH), it turns the buzzer on and prints "Alarm ON". Otherwise, it turns the buzzer off and prints "Alarm OFF". The serial monitor shows the sensor state and alarm status.

Arduino
const int sensorPin = 2;
const int buzzerPin = 9;

void setup() {
  pinMode(sensorPin, INPUT);
  pinMode(buzzerPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int sensorState = digitalRead(sensorPin);
  Serial.print("Sensor state: ");
  Serial.println(sensorState);
  if (sensorState == HIGH) {
    digitalWrite(buzzerPin, HIGH);
    Serial.println("Alarm ON");
  } else {
    digitalWrite(buzzerPin, LOW);
    Serial.println("Alarm OFF");
  }
  delay(500);
}
OutputSuccess
Important Notes

Make sure your sensor and buzzer are connected to the correct pins.

Use a resistor if your buzzer needs one to avoid damage.

Test the sensor separately to understand its output before connecting the buzzer.

Summary

An alarm system uses a sensor to detect events and a buzzer to alert you.

Use digitalRead() to check the sensor and digitalWrite() to control the buzzer.

Simple programs can help you learn how input and output work on Arduino.