const int ledPin = 6; // The pin of your LED const int trigPin = 5; // The pin to which the TRIG is connected const int echoPin = 4; // The pin to which the ECHO is connected const int trigDistance = 20; // The distance (and lower than it) at which the sensor is triggered (in centimeters) int duration; int distance; void setup() { pinMode(ledPin, OUTPUT); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { digitalWrite(trigPin, LOW); digitalWrite(trigPin, HIGH); delay(1); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = duration * 0.034 / 2; if (distance <= trigDistance) { digitalWrite(ledPin, HIGH); delay(100); } else{ digitalWrite(ledPin, LOW); delay(100); } }