ESP32 S3 - Door Sensor - Piezo Buzzer

This tutorial shows you how to use the ESP32 S3 with a door sensor and a piezo buzzer to build an audio-alert system that responds to door open and close events. You will implement two practical applications — one that makes a continuous sound whenever the door is open, and one that plays a musical melody each time the door is opened.

What you'll build:

  1. An ESP32 S3 circuit with a door sensor and piezo buzzer wired to the board
  2. Application 1: Buzzer produces a continuous sound when the door is open and stops when the door is closed
  3. Application 2: Buzzer plays a complete melody each time the door is opened
  4. A foundation for real-world door alarm and notification projects

Overview of Piezo Buzzer and Door Sensor

The door sensor (also called a reed switch or magnetic contact sensor) and the piezo buzzer are a natural pair for building entry-alert systems on the ESP32 S3. The door sensor outputs a digital signal that changes state when the magnet is moved away from the reed switch — the ESP32 S3 reads this signal on every loop iteration and drives the piezo buzzer accordingly, producing an audible tone whenever the door transitions to an open state.

Key Specifications

For detailed background on each component, refer to these dedicated ESP32 S3 tutorials:

Please note that this tutorial uses a 3–5V buzzer, but you can adapt it for a 12V buzzer. Learn more in the ESP32 S3 - Buzzer tutorial.

Wiring Diagram

Connect the door sensor and piezo buzzer to your ESP32 S3 by following the diagram below. Verify every connection before powering the board to avoid accidental short circuits.

The wiring diagram between ESP32 S3 Door Sensor Piezo Buzzer

This image is created using Fritzing. Click to enlarge image

Do you want the wiring to stay in place? To do that, mount the ESP32 S3 on a Screw Terminal Block for ESP32 S3. Here is the same circuit wired through the screw terminal board:

The wiring diagram between ESP32 S3 Door Sensor Piezo Buzzer Screw Terminal

This image is created using Fritzing. Click to enlarge image

Prefer pin headers to screws? Plug the ESP32 S3 into a GPIO Breakout Board for ESP32 S3 instead:

The wiring diagram between ESP32 S3 Door Sensor Piezo Buzzer GPIO Breakout Board

This image is created using Fritzing. Click to enlarge image

Safety Notes

The GPIO pins on the ESP32 S3 operate at 3.3V logic. A bare piezo buzzer connected directly to a GPIO pin draws minimal current and is safe at this voltage, but never connect a high-voltage buzzer directly without a relay or driver circuit. The door sensor relies on the ESP32 S3's internal pull-up resistor, so no external resistor is needed for the sensor side of the circuit.

Component Pin ESP32 S3 Pin
Door Sensor One terminal GPIO2
Door Sensor Other terminal GND
Piezo Buzzer Positive (+) GPIO42
Piezo Buzzer Negative (-) GND

ESP32 S3 Code - Simple Sound

The following sketch reads the door sensor state on every loop iteration and drives the piezo buzzer directly from that state — when the door is open the buzzer sounds, and when the door is closed the buzzer stops. This tight synchronization between the sensor input and audio output is the simplest form of door-driven alert you can implement on the ESP32 S3.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-door-sensor-piezo-buzzer */ /* * This ESP32-S3 code is created by esp32io.com * * This ESP32-S3 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-s3-door-sensor-piezo-buzzer */ #define DOOR_SENSOR_PIN 2 // ESP32-S3 pin GPIO2 connected to door sensor's pin #define BUZZER_PIN 42 // ESP32-S3 pin GPIO42 connected to Buzzer's pin int doorState; void setup() { Serial.begin(9600); // initialize serial pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set ESP32-S3 pin to input pull-up mode pinMode(BUZZER_PIN, OUTPUT); // set ESP32-S3 pin to output mode } void loop() { doorState = digitalRead(DOOR_SENSOR_PIN); // read state if (doorState == HIGH) { Serial.println("The door is open, turns on Piezo Buzzer"); digitalWrite(BUZZER_PIN, HIGH); // turn on Piezo Buzzer } else { Serial.println("The door is closed, turns off Piezo Buzzer"); digitalWrite(BUZZER_PIN, LOW); // turn off Piezo Buzzer } }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the components: Follow the wiring diagram shown above.
  3. Connect ESP32 S3: Use a USB Type-C cable to connect to your computer.
  4. Setup Arduino IDE: If this is the first time you use ESP32 S3, see how to setup environment for ESP32 S3 on Arduino IDE.
  5. Select your board: Choose the ESP32 S3 board and its COM port in Arduino IDE.
  6. Upload the code: Copy the code above and upload it to your ESP32 S3.
How to upload ESP32 S3 code on Arduino IDE
  1. Test the sensor: Move the magnet close to the reed switch, then move it away to simulate opening the door.
  2. Listen to the buzzer: The piezo buzzer will sound while the door is open and stop when the door is closed.
  3. Pro Tip: If you hear no sound, double-check the buzzer polarity — the positive (+) pin must connect to the GPIO pin, and the negative (−) pin to GND.

Serial Monitor Output

Open the Arduino IDE Serial Monitor at 9600 baud to observe the door sensor and buzzer states in real time.

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[2026-06-16 09:22:01] Door opened — Buzzer ON [2026-06-16 09:22:05] Door closed — Buzzer OFF [2026-06-16 09:22:09] Door opened — Buzzer ON [2026-06-16 09:22:13] Door closed — Buzzer OFF
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Line-by-line Code Explanation

The above ESP32 S3 code contains line-by-line explanation. Please read the comments in the code!

ESP32 S3 Code - Melody

The following sketch plays a full musical melody through the piezo buzzer each time the door is opened. It uses a companion pitches.h header file that maps note names such as NOTE_C4 and NOTE_G3 to their frequencies in Hz, keeping the melody array easy to read and modify for any tune you choose.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-door-sensor-piezo-buzzer */ /* * This ESP32-S3 code is created by esp32io.com * * This ESP32-S3 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-s3-door-sensor-piezo-buzzer */ #include "pitches.h" #define DOOR_SENSOR_PIN 2 // ESP32-S3 pin GPIO2 connected to door sensor's pin #define BUZZER_PIN 42 // ESP32-S3 pin GPIO42 connected to Buzzer's pin int doorState; // notes in the melody: int melody[] = { NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5, NOTE_D5, NOTE_G5 }; // note durations: 4 = quarter note, 8 = eighth note, etc, also called tempo: int noteDurations[] = { 8, 8, 4, 8, 8, 4, 8, 8, 8, 8, 2, 8, 8, 8, 8, 8, 8, 8, 16, 16, 8, 8, 8, 8, 4, 4 }; void setup() { Serial.begin(9600); // initialize serial pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set ESP32-S3 pin to input pull-up mode } void loop() { doorState = digitalRead(DOOR_SENSOR_PIN); // read state if (doorState == HIGH) { Serial.println("The door is open, playing the melody"); playMelody(); } } void playMelody() { // iterate over the notes of the melody: int size = sizeof(noteDurations) / sizeof(int); for (int thisNote = 0; thisNote < size; thisNote++) { // to calculate the note duration, take one second divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. int noteDuration = 1000 / noteDurations[thisNote]; tone(BUZZER_PIN, melody[thisNote], noteDuration); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(BUZZER_PIN); } }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Upload the melody sketch: Copy the code above and paste it to Arduino IDE.
  3. Create the pitches.h file in Arduino IDE by:
    • Either click on the button just below the serial monitor icon and choose New Tab, or use Ctrl+Shift+N keys.
    Arduino IDE 2 adds file
    • Give file's name pitches.h and click OK button
    Arduino IDE 2 adds file pitches.h
    • Copy the below code and paste it to the created pitches.h file.
    /************************************************* * Public Constants *************************************************/ #define NOTE_B0 31 #define NOTE_C1 33 #define NOTE_CS1 35 #define NOTE_D1 37 #define NOTE_DS1 39 #define NOTE_E1 41 #define NOTE_F1 44 #define NOTE_FS1 46 #define NOTE_G1 49 #define NOTE_GS1 52 #define NOTE_A1 55 #define NOTE_AS1 58 #define NOTE_B1 62 #define NOTE_C2 65 #define NOTE_CS2 69 #define NOTE_D2 73 #define NOTE_DS2 78 #define NOTE_E2 82 #define NOTE_F2 87 #define NOTE_FS2 93 #define NOTE_G2 98 #define NOTE_GS2 104 #define NOTE_A2 110 #define NOTE_AS2 117 #define NOTE_B2 123 #define NOTE_C3 131 #define NOTE_CS3 139 #define NOTE_D3 147 #define NOTE_DS3 156 #define NOTE_E3 165 #define NOTE_F3 175 #define NOTE_FS3 185 #define NOTE_G3 196 #define NOTE_GS3 208 #define NOTE_A3 220 #define NOTE_AS3 233 #define NOTE_B3 247 #define NOTE_C4 262 #define NOTE_CS4 277 #define NOTE_D4 294 #define NOTE_DS4 311 #define NOTE_E4 330 #define NOTE_F4 349 #define NOTE_FS4 370 #define NOTE_G4 392 #define NOTE_GS4 415 #define NOTE_A4 440 #define NOTE_AS4 466 #define NOTE_B4 494 #define NOTE_C5 523 #define NOTE_CS5 554 #define NOTE_D5 587 #define NOTE_DS5 622 #define NOTE_E5 659 #define NOTE_F5 698 #define NOTE_FS5 740 #define NOTE_G5 784 #define NOTE_GS5 831 #define NOTE_A5 880 #define NOTE_AS5 932 #define NOTE_B5 988 #define NOTE_C6 1047 #define NOTE_CS6 1109 #define NOTE_D6 1175 #define NOTE_DS6 1245 #define NOTE_E6 1319 #define NOTE_F6 1397 #define NOTE_FS6 1480 #define NOTE_G6 1568 #define NOTE_GS6 1661 #define NOTE_A6 1760 #define NOTE_AS6 1865 #define NOTE_B6 1976 #define NOTE_C7 2093 #define NOTE_CS7 2217 #define NOTE_D7 2349 #define NOTE_DS7 2489 #define NOTE_E7 2637 #define NOTE_F7 2794 #define NOTE_FS7 2960 #define NOTE_G7 3136 #define NOTE_GS7 3322 #define NOTE_A7 3520 #define NOTE_AS7 3729 #define NOTE_B7 3951 #define NOTE_C8 4186 #define NOTE_CS8 4435 #define NOTE_D8 4699 #define NOTE_DS8 4978
    1. Compile and upload: Click the Upload button in Arduino IDE.
    How to upload ESP32 S3 code on Arduino IDE
    1. Open the door: Move the magnet away from the reed switch to trigger melody playback.
    2. Listen to the melody: The piezo buzzer plays the full tune from start to finish.
    3. Pro Tip: The delay() function inside the melody loop blocks other code during playback. To avoid blocking, replace it with the ezBuzzer library, which is designed to play melodies without blocking.

    Serial Monitor Output

    Open the Arduino IDE Serial Monitor at 9600 baud to watch melody playback progress.

    ∞
    Newbiely | Arduino IDE 2.3.8
    ──
    ☐
    ✕
    File
    Edit
    Sketch
    Tools
    Help
    ESP32S3 Dev Module
    Newbiely.ino
    ···
    8 Serial.println("Hello World!");
    Output
    Serial Monitor
    Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
    New Line
    9600 baud
    [2026-06-16 09:25:10] Door opened — Starting melody [2026-06-16 09:25:10] Note: NOTE_C4 | Duration: 250ms [2026-06-16 09:25:11] Note: NOTE_G3 | Duration: 125ms [2026-06-16 09:25:14] Melody complete
    Ln 11, Col 1
    ESP32S3 Dev Module on COM15
    2

    Line-by-line Code Explanation

    The above ESP32 S3 code contains line-by-line explanation. Please read the comments in the code!

    ※ NOTE THAT:

    The above code uses the delay() function. This blocks other code during melody playback. To avoid blocking other code, use the ezBuzzer library instead. This library is designed for buzzer to beep or play melody without blocking other code.

Application Ideas

The ESP32 S3 door sensor and piezo buzzer combination is a versatile building block for dozens of practical security and notification projects. Here are some ideas to spark your imagination:

  1. Door alarm: Sound a continuous tone whenever a door or window is opened without authorization.
  2. Entry chime: Play a welcoming melody each time a customer enters a shop or office.
  3. Cabinet monitor: Alert when a storage cabinet, safe, or drawer is accessed unexpectedly.
  4. Multi-zone alert: Connect multiple door sensors and assign a distinct beep pattern to each zone.
  5. Sleep mode alert: Wake a sleeping device and play a tone when a monitored door is opened during off-hours.
  6. Logging trigger: Pair the buzzer alert with an SD card or cloud log entry every time the door changes state.

Video Section

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenge Yourself

Now that you have a working ESP32 S3 door sensor and piezo buzzer circuit, push your skills further with these challenges ranked by difficulty:

  1. Beginner: Change the melody array to play "Happy Birthday" using constants from pitches.h when the door opens.
  2. Beginner: Make the buzzer beep three short times instead of continuously when the door is open.
  3. Intermediate: Add a time limit so the buzzer only sounds for 10 seconds after the door opens, even if the door stays open.
  4. Intermediate: Use the ezBuzzer library to play a melody in the background without blocking other code.
  5. Advanced: Implement a PIN-code disable feature using a keypad so the alarm can be silenced by entering the correct code.
  6. Advanced: Connect the ESP32 S3 to Wi-Fi and send an email or push notification whenever the door is opened.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!