ELEGOO MEGA · LESSON 17
HC-SR501 PIR Sensor
Light the Mega’s onboard L LED when the HC-SR501 reports motion.
Read a PIR module’s digital output and observe its retrigger and hold timing.
Parts List
- ELEGOO Mega 2560 R3 is a 5 V ATmega2560 AVR board with many I/O pins and four hardware UARTs for control systems with several peripherals. Long ELEGOO-branded Mega PCB with USB-B and barrel connector, ATmega2560 marking, extended side headers and a large double-row digital header at the end. Read printed Mega labels; its SPI hardware placement is not the same numeric pins as an Uno.
- A USB-A to USB-B cable carries USB power and, when its data conductors are present, a host/device data link. USB-A plug and square USB-B plug. Inspect the plug shape; micro-USB is not USB-C, and USB-B is the squarer device connector.
- A passive infrared motion module detects changing heat patterns across its field of view and returns a motion-trigger signal. White faceted Fresnel dome over a PCB. Some versions have sensitivity/delay trimmers or retrigger jumpers; their presence and function must be checked on the actual carrier.
- Male-to-female jumper leads make removable point-to-point connections for low-current prototype signals. Pin at one end and socket at the other. Check intact insulation and snug contacts before use.
Assembly
- Disconnect USB and connect the sensor by its pin labels; the lens-facing view reverses the header orientation.
- Choose H/retrigger mode if desired, then power the board and let the sensor settle for about one minute.

Source-reviewed wiring. Hardware not bench-tested; firmware uploads have not been performed.
Code
PlatformIO configuration
[env:mega]
platform = atmelavr@5.3.0
board = megaatmega2560
framework = arduino
monitor_speed = 9600
// Adapted from ELEGOO Lesson 17. Arduino.h supplies pinMode and digital I/O.
#include <Arduino.h>
// const keeps these pin assignments fixed; uint8_t holds a small unsigned number.
const uint8_t LED_PIN = 13;
const uint8_t PIR_PIN = 7;
// void means this function returns no value. setup runs once after reset.
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(PIR_PIN, INPUT); // The powered PIR module already drives its OUT pin.
digitalWrite(LED_PIN, LOW);
}
// Arduino calls loop repeatedly. HIGH from OUT means detection/hold time is active.
void loop() {
const int detected = digitalRead(PIR_PIN);
digitalWrite(LED_PIN, detected); // Mirror the module, including its retrigger delay.
}
Source: SOURCE_RECONCILED. Compile: COMPILE_VERIFIED. Hardware: NOT_TESTED. Upload: NOT_PERFORMED.
Expected behavior
L follows the PIR OUT signal: HIGH lights it, LOW clears it; the module may hold HIGH after movement stops. Startup pulses during settling are not a reliable detection test.
Experiments & Challenges
- Compare H/retrigger and L/single-trigger modes while keeping the delay setting unchanged.
Troubleshooting
- If L stays lit, stop moving and wait for the module’s hold time; check H mode and the delay trimmer.