fbpx

Most orders for IN STOCK items placed by 12PM CST M-F ship SAME DAY. Orders for custom items and prints may take additional time to process.

Arduino: Use a Motion Sensor to Sound a Piezo Buzzer

In this Arduino tutorial we’re going to learn how to use a motion sensor to sound a piezo buzzer. This is great for an intrusion detection circuit or burglar alarm circuit! Any time the motion sensor senses movement the piezo buzzer will make an audible sound. In this tutorial we will use an active piezo buzzer that does not need a PWM signal to operate.

RELATED: Piezo Buzzers: Active vs Passive

Parts List for this Project

Here’s a handy parts list for this project. Some of these links may be affiliate links. If you use them, they cost you nothing, but we may get a small commission that helps us keep building awesome content like this.

QTYPART/LINK
1XArduino Uno
1XUSB Type B Cable
1XSolderless Breadboard
1XJumper Wire Kit
1XMotion Sensor
1XPiezo Buzzer
Get a Geek Pub Uno!

Wiring Diagram: Use a Motion Sensor to Sound a Piezo Buzzer

This wiring diagram will teach you how to wire your breadboard for using a motion sensor to sound a piezo buzzer! Since there are no resistors or capacitors needed, this is one of those projects where you could completely skip the breadboard and wire it direct to the Arduino should you choose. However, the breadboard definitely makes it easier to visualize. The choice is yours!

Use a motion sensor to control a piezo buzzer wiring diagram

Arduino Code for Motion Sensor

This Arduino sketch will control your piezo buzzer based on the motion sensor’s status. Any time the motion sensor detects movement or a change in the environment, this will trigger the piezo buzzer to sound.

This code could be adapted to perform many other functions and is a great basis for a burglar alarm circuit! You could expand this with other sensors such as door/window sensors and have a complete Arduino alarm system!

const int MOTION_SENSOR_PIN = 7;   // Arduino pin connected to the OUTPUT pin of motion sensor
const int BUZZER_PIN        = 3;   // Arduino pin connected to Buzzer's pin
int motionStateCurrent      = LOW; // current  state of motion sensor's pin
int motionStatePrevious     = LOW; // previous state of motion sensor's pin

void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode
  pinMode(BUZZER_PIN, OUTPUT);          // set arduino pin to output mode
}

void loop() {
  motionStatePrevious = motionStateCurrent;            // store old state
  motionStateCurrent  = digitalRead(MOTION_SENSOR_PIN); // read new state

  if (motionStatePrevious == LOW && motionStateCurrent == HIGH) { // pin state change: LOW -> HIGH
    Serial.println("Motion detected!");
    digitalWrite(BUZZER_PIN, HIGH); // turn on
  }
  else
  if (motionStatePrevious == HIGH && motionStateCurrent == LOW) { // pin state change: HIGH -> LOW
    Serial.println("Motion stopped!");
    digitalWrite(BUZZER_PIN, LOW);  // turn off
  }
}

Next Steps

Now you can move on to the next tutorial or return to the Arduino Tutorial Index.

Upgrade to Premium

If you like our content maybe consider upgrading to Premium. You’ll get access to

  • Free access to all plans
  • Member only videos
  • Early access to content
  • Ad free Experience
  • Discounts on store merch
  • Direct hotline contact form

Leave a Reply