Arduino IR sensor to trigger onboard LED

Use this sketch to detect an object very close to the IR sensor. This could be useful for model railroading where a train should be detected to trigger a warning light at a level-crossing.

// hardware: Arduino UNO, IR sensor from funduinoshop.de
// https://funduino.de/arduino-infrarot-abstandssensor

// port connected to analog read of IR sensor module
const int ANALOG_IN = A0;  
 
// onboard LED
const int LED = 13;   

// threshold, to be set to values which work in your situation
const int THRESHOLD = 300;

// value in ms, delay between measurements
const int DELAY = 100;      

void setup() {
  pinMode(LED, OUTPUT);     
}

void loop() {
  digitalWrite(
    LED,
    (analogRead(ANALOG_IN) < THRESHOLD) ? HIGH : LOW
  );     
  delay(DELAY);
}