Motion Sensor

Wiring Diagram

Program


#include <SR04.h>
#define TRIG_PIN 12
#define ECHO_PIN 11
SR04 sr04 = SR04(ECHO_PIN, TRIG_PIN);
int a;

void setup() {
    Serial.begin(9600);
    delay(1000);
    pinMode(8, OUTPUT);
}

void loop() {
    a = sr04.Distance();
    
    if (a < 12) 
    {
        for(int i = 0; i<3; i++) 
        {
            digitalWrite(8, HIGH);
            delay(100);
            digitalWrite(8, LOW);
            delay(100);
        }
    }
    else
    {
        digitalWrite(8, LOW);
    }
    delay(250);
}