How to create an alarm for your home or business with the Arduino

Arduino UNO

If you are concerned about the security of your home and can't afford to pay for an expensive professional alarm system, you can create your own. cheap and simple alarm thanks to Arduino. In this tutorial we will explain how to create a very simple alarm that is able to detect when a door or window is opened and emit a sound.

If you are not very familiar with Arduino, we suggest you take a look at last week's post in which we did an introduction to this system that you will surely love.


  • Arduino UNO board.

Arduino UNO

  • The Arduino IDE software.
  • Magnetic door or window alarm sensor (you can find them in Chinese shops or electronics shops).
  • Batteries for the magnetic sensor.
  • Resistance of 10K ohms.
  • Rigid single-wire copper cable.
  • Red LED.


To mount our alarm, we will follow the following steps. But first I would like to explain how the sensor works. We see that the magnetic sensor consists of two parts, a large one where the speaker, batteries and circuit that is detonated in case of opening the door or window and another smaller part which is a magnet. These parts must be placed together in the door or window where we want to place the alarm, the manufacturer will give the appropriate assembly instructionsThe basic idea is to place the larger part in such a way that it is in contact with the smaller part and the smaller part is attached to the door or window so that when the door or window is opened, the two parts separate and detonate the alarm.

Magnetic sensor

Once we understand how the sensor works, we are going to connect two wires (single wire with the bare ends of the insulating material to make contact) to the positive and negative terminals of the box where the batteries of the magnetic sensor are housed. To do this we will have to remove the screws from the largest part of the sensor and connect it in a simple way.

These wires are jumpered with the 10k resistor, i.e. their ends are joined using the resistor. And from the negative side of the resistor (where we have connected the cable coming from the negative end of the sensor batteries) we take a connection to connect it to the GND pin of the Arduino board. In the image I have represented this part of the sensor with the figure of a loudspeaker to simplify it, but you must know that this is the larger part of the magnetic sensor assembly.

Arduino Assembly

As you have the magnetic sensor housing open, you will see that in the circuit there is a kind of elongated element similar to a silver resistor (it is a reed switch). It is unmistakable, as the sensor circuit is quite simple. It is also easy to verify that it is the correct element as it has one of its ends connected to the negative terminal of the batteries. Well, we will make a connection with another single wire cable to the end that is not connected to the negative terminal of the batteries and we will join it to the Arduino digital pin number 2.

Now we will place the red LED with its anode (longest terminal) to pin number 13 of the Arduino board (digital I/O). The cathode of the LED will go to the GND connection close to pin 13. If instead of the LED we want to connect any other device, you can use your imagination (camera to take a picture, light, GSM module to make a call, etc).

Finally, all that remains is to create the sketch for programming Arduino. To do this, once the Arduino IDE is installed, write this code and once finished and with the Arduino board connected to the PC via USB, run the sketch by clicking on the right arrow button (Load). We will then be able to check its operation.

//Scheme for our simple alarm with Arduino
const int sensorpin = 2; //Specify that the sensor is to be attached to pin 2
const int ledpin = 13; //Pin 13 will be for the LED
int sensorstatus = 0; //Specify the initial state of the sensor (off)
void setup()
pinMode(ledpin, OUTPUT); //Mark LED pin as output
pinMode(sensorpin, INPUT); //The sensor pin must be an input.
void loop(){
statesensor = digitalRead(sensorpin); //Read sensor status
//If the status is high, i.e. activated, the alarm LED will light up.
if (statesensor == HIGH) {
digitalWrite(ledpin, HIGH);
else { //While the sensor is off the pin will be off
digitalWrite(ledpin, LOW);

In order not to depend on the PC, we can connect the Arduino board to an external power source or use batteries. This means that the prototype will continue to operate independently without the need for a USB connection.

Buy - Arduino UNO rev. 3

More information - Introduction to Arduino: an electronic universe on a single board

8 thoughts on “Cómo crear una alarma para tu vivienda o comercio con Arduino”

  1. Patricio Cajas

    Hello, can you help me with this programming?
    A house requires an alarm system, so in this exercise we propose that we emulate one. When everyone leaves the house, the last person to leave is responsible for activating the alarm with a 4-digit number, at which point the system begins to receive information from the magnetic detectors (reed switch) located in each window and each door. The alarm must be triggered when a magnetic field alters the state of the reed switch, when the alarm is activated a sound is emitted and a visual alarm is generated. The only way to deactivate the alarm is by means of a 4-digit numerical code that must be different from the activation code, the steps are described below:
    A. A 4-digit integer number is received by serial console, if this number is equal to the number stored by the system the alarm system is activated, for this purpose the console must display Alarm activated.
    B If the reed switch is in the presence of a magnetic field, the serial console should display. Presence of foreign object, system tripped.
    C. As the alarm is triggered the buzzer must be switched on to generate sound (handled as a digital output) and in addition the yellow and red LED must be switched on/off and emulate the movements of a fire siren at 1s intervals.
    D. If the alarm is to be deactivated, a 4-digit numerical code must be entered. If it is correct, the serial console will display Alarm deactivated, otherwise step C must be repeated until the correct code is entered.

    1. Good,

      There are shields or add-ons to add Bluetooth functionality to Arduino, these elements usually have code options and libraries to work with and generate the necessary code to be able to manage the board via Bluetooth as you want. There are many of these elements on the market and their price varies, but they are not usually expensive.


  2. Ricardo Ante Lezama

    I can add more sensors, I would like a couple that were motion (go) and as the magnetic add more and another thing as is done to sound the siren? and how many pins can be used for sensors?

    1. Hello,

      Yes, of course you can add more sensors. As many as you want, there are many types as you know. You just have to make the appropriate connections and modify the code to your liking. As for the sound you mention, the speaker will sound when one of the sensors is activated when it detects something anomalous...

      Greetings and thank you for following us.

    1. Hello,

      In principle, anyone can be used, as long as there is no physical impediment...

      Best regards!

Leave a Comment

Your email address will not be published.

Scroll to Top