Built-in project · Arduino Uno R3

Motion alarm siren on an Arduino Uno

A PIR on pin 2 trips a 12 V alarm siren on a D4184 MOSFET module for ten seconds, then the alarm waits for quiet and re-arms. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $39. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Using a PIR sensor as a trigger
  • Switching a 12 V load with a MOSFET module
  • Sharing ground between the Uno and the switch module
  • Timing with millis() instead of delay()
  • A simple armed and sounding state
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

A PIR motion sensor on pin 2 goes HIGH when it sees someone move. The Uno then raises pin 8, which triggers a D4184 MOSFET module. The module switches a 12 V siren from a separate adapter, so the Uno only sends a small signal.

Once tripped, the siren sounds for ten seconds. Then the sketch turns it off, waits until the PIR goes quiet, and arms itself again. The serial monitor reports armed, motion and siren messages.

Parts list

5 parts, plus the jumper wires. They retail for about $39; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
D4184 dual MOSFET switch module pin GNDArduino Uno R3 pin GND
D4184 dual MOSFET switch module pin TRIGArduino Uno R3 pin 8
Motion sensor pin OUTArduino Uno R3 pin 2
Motion sensor pin VCCArduino Uno R3 pin 5V
Motion sensor pin GNDGround
DC jack and adapter pin V+D4184 dual MOSFET switch module pin VIN+
DC jack and adapter pin V-D4184 dual MOSFET switch module pin VIN-
Alarm siren, 12 V pin +D4184 dual MOSFET switch module pin OUT+
Alarm siren, 12 V pin -D4184 dual MOSFET switch module pin OUT-

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Motion alarm siren: an HC-SR501 PIR on pin 2 trips a 12 V siren, which a
// D4184 MOSFET module on pin 8 switches from a 12 V adapter.
//
//   2       the PIR's OUT (its VCC on 5V, GND on GND)
//   8       the module's TRIG, its GND shared with the Uno
//   12 V    the adapter into the module's VIN+ and VIN-
//   siren   red wire on OUT+, black on OUT-
//
// The siren has its own oscillator: 12 V across it and it wails, at about
// 300 mA, so the pin only switches the MOSFETs' gates. Once tripped it sounds
// for ten seconds, then the alarm waits for the PIR to go quiet and re-arms.
//
// Press Run, then press the PIR's motion button (or drag someone in front of it).

const int PIR_PIN = 2;
const int SIREN_PIN = 8;
const unsigned long SOUND_MS = 10000;

bool sounding = false;
unsigned long since = 0;

void setup() {
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: motion alarm siren");
  pinMode(PIR_PIN, INPUT);
  pinMode(SIREN_PIN, OUTPUT);
  digitalWrite(SIREN_PIN, LOW);
  Serial.println("armed");
}

void loop() {
  unsigned long now = millis();
  if (!sounding && digitalRead(PIR_PIN) == HIGH) {
    sounding = true;
    since = now;
    digitalWrite(SIREN_PIN, HIGH);
    Serial.println("motion: siren on");
  }
  if (sounding && now - since >= SOUND_MS) {
    digitalWrite(SIREN_PIN, LOW);
    Serial.println("siren off, waiting for quiet");
    while (digitalRead(PIR_PIN) == HIGH) {
      delay(50);
    }
    sounding = false;
    Serial.println("armed");
  }
  delay(20);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Opens the serial port, sets the PIR as an input and the siren pin as an output, and makes sure the siren is off. Then it prints armed.

void setup() {
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: motion alarm siren");
  pinMode(PIR_PIN, INPUT);
  pinMode(SIREN_PIN, OUTPUT);
  digitalWrite(SIREN_PIN, LOW);
  Serial.println("armed");
}

loop sketch.ino

If it is not sounding and the PIR is HIGH, it starts the siren and notes the time. Once SOUND_MS (10 seconds) has passed, it turns the siren off and waits in a while loop until the PIR goes LOW. Then it sets sounding back to false and prints armed.

void loop() {
  unsigned long now = millis();
  if (!sounding && digitalRead(PIR_PIN) == HIGH) {
    sounding = true;
    since = now;
    digitalWrite(SIREN_PIN, HIGH);
    Serial.println("motion: siren on");
  }
  if (sounding && now - since >= SOUND_MS) {
    digitalWrite(SIREN_PIN, LOW);
    Serial.println("siren off, waiting for quiet");
    while (digitalRead(PIR_PIN) == HIGH) {
      delay(50);
    }
    sounding = false;
    Serial.println("armed");
  }
  delay(20);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The siren does not sound This project
    The module's GND must be joined to the Uno's GND, and TRIG goes to pin 8. The 12 V adapter powers VIN+ and VIN-.
  • It re-arms very slowly This project
    The sketch waits for the PIR to go LOW. A PIR keeps its output high for a few seconds after motion.
  • The sensor reads high for ages after power up. Motion sensor
    A real PIR needs 30 to 60 seconds to settle. Ignore its output during that time, and keep still in front of it until the delay passes.
  • Wrong output or it never triggers. Motion sensor
    Power it from 5 V, join GND, and read OUT as a digital input (3.3 V logic high). Check the trimmers for delay and sensitivity.
  • The load never turns on, or it is only dim. D4184 dual MOSFET switch module
    Check the load's own supply is wired to the screw terminals and the control pin gets 3.3 V or 5 V. Join the grounds of the controller and the load supply together.
  • Parts get hot or wires melt at high current. D4184 dual MOSFET switch module
    The board can carry high current only with thick wire and good connections. Keep the load supply within the module's rated voltage, and use a flyback diode across motors and other coils.
  • The siren is quiet or does not sound. Alarm siren
    It needs 6 to 12 V and enough current, often a hundred mA or more. Power it from a proper supply, and respect the polarity, with red positive and black negative.
  • Wiring it to a microcontroller pin does not work or burns the pin. Alarm siren
    Switch it with a MOSFET or transistor, with the siren on the supply side and the grounds joined. A logic-level MOSFET gate on the pin gives on and off control.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.