What you will learn
- How to read a digital input (
digitalRead) and switch an output (digitalWrite). - Why 3.3V and 5V both appear in the same project, and how to tell which is which.
- How to wait without freezing the program, using
millis()instead ofdelay().
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.
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.
| Part pin | Connects to (classic ESP32) |
|---|---|
| PIR VCC | 5V (the pin labelled VIN or 5V) |
| PIR GND | GND |
| PIR OUT | GPIO 27 |
| Buzzer VCC | 3V3 (or 5V if your buzzer module asks for 5V) |
| Buzzer GND | GND |
| Buzzer I/O (signal) | GPIO 26 |
Using an ESP32-S3 or ESP32-C3 instead?
| Signal | Classic ESP32 | ESP32-S3 | ESP32-C3 |
|---|---|---|---|
| PIR OUT | GPIO 27 | GPIO 15 | GPIO 10 |
| Buzzer signal | GPIO 26 | GPIO 16 | GPIO 7 |
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
- Open the Arduino IDE, choose File, New Sketch, and replace everything with this:
- 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.
- Open Tools, Serial Monitor and set it to 115200 baud. You should see "Warming up...".
How the code works
pinModetells the ESP32 whether each pin listens (INPUT) or speaks (OUTPUT).millis()returns how many milliseconds have passed since power-on. We compare it with stored times so the board keeps running while it "waits". If we useddelay(5000)the ESP32 would be frozen and could not notice anything else.(now / 250) % 2flips between 0 and 1 every quarter second, which is a simple way to blink without any extra variables.!BUZZER_ONmeans "the opposite of on", so one setting handles both kinds of buzzer module.
Test it
- After upload, the on-board LED blinks for 30 seconds while the sensor warms up. Stay out of its view if you can.
- 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!
- 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.
Troubleshooting
| Problem | What 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 time | Lower 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 triggers | Wait 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 motion | Your module is "active low". Change BUZZER_ON to LOW. |
| Buzzer silent | Check 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 sounds | The USB port is not giving enough current. Try another port or a powered hub. |
Make it better
- Add a push button so you can arm and disarm the alarm.
- Count how many times it triggered and print the total.
- Add Wi-Fi and send a notification to your phone (you will learn Wi-Fi in project 2).
- Use a relay to turn on a light when motion is detected. You will meet the relay in the next project.
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.