Example Code for Arduino-Object Detection

Use the infrared proximity sensor to detect the presence of an object within its sensing range and control the LED connected to pin 13 of the Arduino UNO board.

Hardware Preparation

Software Preparation

Wiring Diagram

UNO R3 PIN Infrared Proximity Sensor PIN
UNO R3 Digital pin 8 Infrared Proximity Sensor Signal pin(GREEN)
UNO R3 VCC Infrared Proximity Sensor VCC(RED)
UNO R3 GND Infrared Proximity Sensor GND(BLACK)

Sample Code

int led = 13;  // Pin 13 has an LED connected on most Arduino boards.
int sensor = 8;
void setup() {
  pinMode(led, OUTPUT);   // initialize the digital pin as an output.
  pinMode(sensor, INPUT);
  digitalWrite(led, LOW);
}

void loop() {
  int val = digitalRead(sensor);
  if (val)
  {
    digitalWrite(led, HIGH);
  }
  else
  {
    digitalWrite(led, LOW);
  }
}

Result

When the detected object is in the detection range of the sensor, the LED corresponding to pin 13 of the UNO board lights up; when it's outside the detection range, the LED goes out.

Was this article helpful?

TOP