Example Code for Arduino-Read Sensor Status

Achieve function: read real-time status of IO4, when the sensor detects an object, serial prints 1; when no object is detected, serial prints 0.

Hardware Preparation

Software Preparation

Wiring Diagram

Other Preparation Work

How to adjust detection distance

Keep the sensor powered on and in the normal operating status, and place a roughly 80x80cm of kraft paperboard (or other objects) in front of the sensor at 70cm. The distance adjustment steps are as shown in the diagram, enter the distance adjustment mode: hold on the adjustment button, when the red LED indicator flashes, enter the automatic distance adjustment mode; don't move the sensor and paperboard when the LED flashes, when the LED indicator stops flashing, the distance adjustment is done.

Sample Code

int  OUT = 4;
int  i = 0;
void setup()
{
  Serial.begin(9600);
  pinMode(OUT, INPUT);
}
void loop()
{
   i = digitalRead (OUT);
   Serial.println(i);
   delay(1000);
}

Result

Achieve function: read real-time status of IO4, when the sensor detects an object, serial prints 1; when no object is detected, serial prints 0.

Was this article helpful?

TOP