กลับไปหน้ารวมไฟล์
door-sensor-with-arduino-nano-every-520189-en.md

title: "Door Sensor Alert System with La Cucaracha Melody" description: "Create a small, humorous security innovation using Arduino and a Magnetic Reed Switch to detect door status."

In this project, we will bring a plain door to life by building a "Smart Door Sounder". We will program the system to monitor the door's status, and when the door "closes", our Arduino board will instruct a speaker to play the classic Mexican melody "La Cucaracha" using the powerful tone() function.

This learning experience is not just about creating music, but also about understanding the fundamentals of Digital Input, Logic States, and controlling audio frequencies in embedded systems.

Hardware Components

To complete this project, we will need the following electronic components:

  1. Arduino Board (Uno, Nano, or other models): Serves as the main processing unit.
  2. Magnetic Reed Switch: A switch that activates when a magnetic field approaches (used to detect door closure).
  3. Piezo Buzzer or small speaker: For converting electrical signals into sound waves.
  4. Resistor (220Ω - 1kΩ): To limit current for the Buzzer.
  5. Jumper wires and Breadboard: For circuit connections.

Engineering Principles

1. Detection with Magnetic Reed Switch

A magnetic sensor consists of two small metal plates enclosed in a vacuum glass tube. When a magnet (attached to the door frame) moves close, the magnetic force pulls the plates together, completing the circuit. In terms of software, we will read this value via digitalRead() to check if the state changes from HIGH to LOW (when using an Internal Pull-up).

2. Sound Generation with tone() Function

The core of sound generation in Arduino is the tone(pin, frequency, duration) function, which creates a Square Wave with a specific Frequency according to musical notes, such as:

  • Note C4: 262 Hz
  • Note F4: 349 Hz
  • Note A4: 440 Hz

When these frequencies are arranged correctly with proper Timing, we will get the complete "La Cucaracha" melody.


Code Logic

For the system to function accurately, our code will divide the operation into three main parts:

  1. Input Sensing: Continuously check the state of the Digital pin connected to the Reed Switch within loop().
  2. Edge Detection: The system will not play the song repeatedly, but will only play "when the door is closing" (Transition from Open to Closed).
  3. Melody Sequence: Once the conditions are met, the function will call the Array of La Cucaracha musical notes to play.
// Example code structure for La Cucaracha melody
#define BUZZER_PIN 8
#define REED_SWITCH_PIN 2

void setup() {
  pinMode(REED_SWITCH_PIN, INPUT_PULLUP); // Using internal Pull-up for convenience
  pinMode(BUZZER_PIN, OUTPUT);
}

void playLaCucaracha() {
  // La Cucaracha melody notes (C4, C4, C4, F4, A4...)
  int melody[] = {262, 262, 262, 349, 440}; 
  int durations[] = {200, 200, 200, 400, 400};
  
  for (int i = 0; i < 5; i++) {
    tone(BUZZER_PIN, melody[i], durations[i]);
    delay(durations[i] * 1.3); // Pause between notes
  }
}

void loop() {
  int doorState = digitalRead(REED_SWITCH_PIN);
  
  // If sensor detects magnet (door closed), value will be LOW
  if (doorState == LOW) {
    playLaCucaracha();
    while(digitalRead(REED_SWITCH_PIN) == LOW); // Wait until door opens again to prevent repeat playback
  }
}

Project Overview (Media)

(Retain original image and video positions)

So, in this proyect, we will make a sensor for your door. We will programe it so that when the door closes, It plays La Cucaracha. We will use a function called tone.


Conclusion of Experiment

From this experiment, we found that using tone() in conjunction with Timing management (delay()) is a simple and effective method for notifying various Hardware statuses. This project can be applied in various ways, such as a safe alert system, an anti-theft system, or even a welcome sound system in shops, by changing the melody according to your needs.

ข้อมูล Frontmatter ดั้งเดิม

title: "Door Sensor with Arduino Nano Every"
description: "Make it play \"La Cucaracha\" when your door closes"
author: "enriquekirkpatrickc"
category: "Audio & Sound"
tags:
  - "sensor"
  - "magnetism"
views: 14912
likes: 8
price: 870
difficulty: "Intermediate"
components:
  - "1x Buzzer, Piezo"
  - "1x Reed Switch, SPST-NO"
  - "1x Arduino Nano Every"
  - "1x SparkFun Solder-able Breadboard - Mini"
  - "1x 9V Battery Clip"
  - "1x Male/Female Jumper Wires"
  - "1x 9V battery (generic)"
tools: []
apps:
  - "1x Arduino IDE"
downloadableFiles: []
documentationLinks: []
passwordHash: "8716556b6958a9d2b48b6bdb6fe6d557a73a3993452e191ee4dbf12e8a1b38a4"
encryptedPayload: "U2FsdGVkX18hNBUxiNLwi1pBgkfLfFLZJ9NVlUjaT+bj+1DUF1eGmzv552MdtL7JMukZht5LlaJgYGZqggPvsuXgnZ8kYrxW362P6FMlK9I="
seoDescription: "Build a Door Sensor with Arduino Nano Every that plays La Cucaracha when the door closes. A fun and easy DIY electronics project."
videoLinks: []
heroImage: "https://cdn.jsdelivr.net/gh/bigboxthailand/arduino-assets@main/images/projects/door-sensor-with-arduino-nano-every-520189_cover.jpg"
lang: "en"