Example Code for Arduino-Motion Detection
Last revision 2025/12/15
This article offers a comprehensive guide on setting up motion detection with Arduino, including hardware and software preparation, wiring diagrams, sample code, and expected results, making it ideal for beginners looking to explore sensor-based projects.
Wiring Diagram

Sample Code
const int buttonPin = 2;
const int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
if (digitalRead(buttonPin) == HIGH)
{
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}
Result
When sensors detect people closed, the light will be lighted.
Was this article helpful?
