← Beginner's guide to ESP32

Build an ESP32 motion alarm

Wave your hand in front of a small sensor and a buzzer goes off. It is the classic first project because it teaches the two things every ESP32 build uses: reading an input and driving an output.

What you will learn

Before you start: you need the Arduino IDE installed and a working Blink test. If you have not done that yet, follow steps 1 to 5 of the beginner's guide first. It takes about 20 minutes.

PIR sensorsees body heat moveoutput goes HIGH ESP32reads the pindecides what to do Buzzerpin goes HIGHbeep!
Input in, output out. Almost every project is this pattern with different parts.

Parts you need

Some links are affiliate links, so I may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases. Details. Buy buttons open a store search; check the product matches before you order.

Buzzer type matters. An active buzzer beeps by itself when you give it power. A passive buzzer needs the program to generate the tone. This guide uses an active one because it is simpler. If it came as a bare two-leg component with no board, it needs a small transistor to drive it, so buy the 3-pin module instead.

Wire it up

Do all wiring with the board unplugged from USB.

Check each pin's label. The three pins on the PIR and the buzzer module are printed on the board, but the order differs between makers. Match the names, not the positions shown in the diagram.

Red = power, black = ground, yellow = PIR signal, blue = buzzer signal. GPIO numbers are the numbers printed on your board (D27 or just 27).
Part pinConnects to (classic ESP32)
PIR VCC5V (the pin labelled VIN or 5V)
PIR GNDGND
PIR OUTGPIO 27
Buzzer VCC3V3 (or 5V if your buzzer module asks for 5V)
Buzzer GNDGND
Buzzer I/O (signal)GPIO 26
Using an ESP32-S3 or ESP32-C3 instead?
SignalClassic ESP32ESP32-S3ESP32-C3
PIR OUTGPIO 27GPIO 15GPIO 10
Buzzer signalGPIO 26GPIO 16GPIO 7

Change the two numbers at the top of the sketch to match.

Why 5V and 3.3V together? The PIR sensor needs 5V to power its circuit, but its output signal is only 3.3V, which is exactly what the ESP32 can safely read. Never connect a 5V signal to an ESP32 pin.

The code

  1. Open the Arduino IDE, choose File, New Sketch, and replace everything with this:
  2. Select your board and port (Tools, Board, ESP32 Dev Module, then Tools, Port), then click the Upload arrow. If it says "Connecting..." and stalls, hold the BOOT button on the board until the upload starts.
  3. Open Tools, Serial Monitor and set it to 115200 baud. You should see "Warming up...".

How the code works

Test it

  1. After upload, the on-board LED blinks for 30 seconds while the sensor warms up. Stay out of its view if you can.
  2. When the Serial Monitor says Armed, wave your hand in front of the white dome. The buzzer should beep for 5 seconds and the monitor prints MOTION DETECTED!
  3. Tune the sensor. The HC-SR501 has two small orange dials. One sets sensitivity (range, up to about 7 metres) and one sets how long the output stays high after motion. Turn them with a small screwdriver until it behaves how you like. Many people turn the delay dial all the way down for a short, snappy response.
Warm-up: LED blinks (30 s) Armed: watching Alarm (5 s, beeping) Serial: "Warming up..."Serial: "Armed!"Serial: "MOTION DETECTED!" 123
The three phases the sketch moves through.

Troubleshooting

ProblemWhat to try
Upload fails: "Failed to connect"Use a data cable, pick the right port, and hold the BOOT button while the upload starts. Install the CH340 or CP210x driver if no port appears.
Triggers all the timeLower the sensitivity dial. Keep the sensor away from heaters, sunlit windows and fans. Make sure it is powered from 5V and a solid ground; a loose jumper causes false alarms.
Never triggersWait the full 30 seconds. Check OUT goes to GPIO 27. Turn sensitivity up. The sensor detects movement across its view, not a person standing still.
Buzzer sounds constantly, quiet on motionYour module is "active low". Change BUZZER_ON to LOW.
Buzzer silentCheck its VCC and GND. A passive buzzer will not sound with this code. Try a 5V supply instead of 3.3V if the sound is weak.
Board reboots when the buzzer soundsThe USB port is not giving enough current. Try another port or a powered hub.

Make it better

Open this project in the Build Finder →

Where to next

Project 2: Wi-Fi relay switch. Control a device from a web page on your phone.

This guide was written from standard Arduino and ESP32 practice and the sensor makers' documentation. It has not been built on every ESP32 variant; check pins against your own board. Not affiliated with the part makers.

← Beginner's guide to ESP32