DEV Community

Cover image for Arduino Nano - Motion Sensor
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Arduino Nano - Motion Sensor

This build turns an Arduino Nano into a motion detector using the HC-SR501 PIR sensor, the kind behind automatic lights and doors. It is a good first hardware project if you are used to writing code but new to reading sensor datasheets, since the whole interface boils down to one digital pin.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino Nano - Motion Sensor

What you need

Wiring

Arduino Nano - Motion Sensor wiring diagram

The HC-SR501 has three pins. GND goes to the Arduino's GND (0V), VCC goes to 5V, and OUTPUT connects to one of the Arduino's digital input pins. That OUTPUT pin is LOW when no motion is detected and HIGH when motion is detected, so your code only needs to watch that single pin to know what the sensor sees.

How it works

The sensor detects motion by noticing changes in infrared radiation coming from a moving object. For it to trigger, the object has to both be moving and emitting infrared, which is why a toy car won't trigger it (no infrared) and a person standing still won't either (no motion). Humans and animals satisfy both conditions, so the sensor works well as a human-motion detector, with one caveat: it only sees motion, not presence. A person sitting still in the room will read as 'no motion,' which can make lights or automation switch off while someone is still there.

Code

The sketch sets the Arduino pin connected to OUTPUT as an INPUT with pinMode(), then repeatedly checks its state with digitalRead(). By comparing the current reading to the previous one, the code can detect the moment motion starts (state goes from LOW to HIGH) and the moment it stops (HIGH back to LOW), which is enough to drive things like turning a light on or off.

arduino-nano-motion-sensor.ino

prev_motion_state = motion_state; // store old state
motion_state = digitalRead(MOTION_SENSOR_PIN);   // read new state

if (prev_motion_state == LOW && motion_state == HIGH) {   // pin state change: LOW -> HIGH
  Serial.println("Motion detected!");
}
Enter fullscreen mode Exit fullscreen mode

Things that go wrong

The HC-SR501 has two potentiometers and a jumper that change its behavior without touching any code. One potentiometer sets detection range, roughly 3 to 7 meters. The other sets a time delay before OUTPUT drops back to LOW, from about 3 seconds to 5 minutes. The jumper picks between single trigger mode, where OUTPUT can toggle repeatedly during continuous motion, and repeatable trigger mode, where OUTPUT stays HIGH for the whole motion plus the delay. If your readings look odd, check these physical settings before suspecting your code.

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-nano/arduino-nano-motion-sensor

Top comments (0)