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

Magnetic Door Sensor

The Importance and Utility of Magnetic Door Sensors

Magnetic door sensors are essential components in various security and automation systems. These sensors detect the opening and closing of doors or windows by utilizing the principles of magnetism. They are widely used in home security systems, access control systems, and even in smart home automation. By understanding how magnetic door sensors work and how to integrate them into projects, engineers can enhance the security and functionality of their designs.

Project: Creating a Magnetic Door Sensor System

In this example project, we will create a simple magnetic door sensor system using an Arduino board. The objective is to detect the opening and closing of a door and trigger an action, such as turning on a light or sending a notification. This project can serve as a foundation for more complex security or automation systems.

List of Components:

  • Arduino Uno board x1
  • Magnetic door sensor x1
  • Breadboard x1
  • Jumper wires x5

You can purchase these components from the following links:

  • Arduino Uno board: [link]
  • Magnetic door sensor: [link]
  • Breadboard: [link]
  • Jumper wires: [link]

Examples:

Example 1: Basic Magnetic Door Sensor Setup

const int sensorPin = 2; // Connect the sensor to digital pin 2
const int ledPin = 13; // Connect an LED to digital pin 13

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

void loop() {
  int sensorValue = digitalRead(sensorPin);

  if (sensorValue == HIGH) {
    digitalWrite(ledPin, HIGH);
    Serial.println("Door opened");
  } else {
    digitalWrite(ledPin, LOW);
    Serial.println("Door closed");
  }

  delay(1000);
}

Explanation:

  • We define the sensorPin as the digital pin to which the magnetic door sensor is connected and the ledPin as the digital pin to which an LED is connected for visual feedback.
  • In the setup function, we set the sensorPin as an input pin and the ledPin as an output pin. We also initialize the serial communication for debugging purposes.
  • In the loop function, we continuously read the sensor value using the digitalRead function. If the sensor value is HIGH, indicating that the door is open, we turn on the LED and print "Door opened" to the serial monitor. Otherwise, we turn off the LED and print "Door closed".
  • We add a delay of 1 second between each iteration to avoid rapid toggling of the LED.

Example 2: Enhanced Magnetic Door Sensor Setup with Notification

const int sensorPin = 2; // Connect the sensor to digital pin 2

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

void loop() {
  int sensorValue = digitalRead(sensorPin);

  if (sensorValue == HIGH) {
    Serial.println("Door opened");
    sendNotification("Door opened"); // Custom function to send a notification
  }

  delay(1000);
}

void sendNotification(String message) {
  // Code to send a notification, e.g., via Wi-Fi or SMS
}

Explanation:

  • In this example, we remove the LED feedback and focus on triggering a notification when the door is opened.
  • We define the sensorPin and set it as an input pin in the setup function.
  • In the loop function, we read the sensor value and check if it is HIGH. If it is, we print "Door opened" to the serial monitor and call the sendNotification function with the message "Door opened".
  • The sendNotification function is a custom function where you can implement the code to send a notification. This can be done using Wi-Fi, GSM, or any other communication method supported by the Arduino board.

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.