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

// 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() {
    (analogRead(ANALOG_IN) < THRESHOLD) ? HIGH : LOW