Sensor E18-D80NK not working

I am trying to work with arduino mega 2560 and sensor e18-d80nk. I have connected red with 5V, black with GND and yellow with pin 2. written following program

int Pin=2;

void setup()
{
pinMode(Pin, INPUT);
Serial.begin(9600);
}

void loop()
{
int sensor = digitalRead(Pin);
if(sensor==HIGH)
{
Serial.println("Object Detected");
delay(500);
}
else
{
Serial.println("no Object detected");
delay(500);
}
}

Even no object is in front of sensor, it still shows as Object detected. I read somewhere that I need to connect 1k resistor between pin 2 and yellow wire. But no changes in result.

Can anyone help me with this?

Thanks in advance.

Try this instead

pinMode(Pin, INPUT_PULLUP);

Understand that the button states are reversed now, so that the logic is active low, i.e. button pressed is low, button released is high.

I tried your method, but still problem was there. I checked with sensor manufacturer, they have mentioned that sensor works on 6-36 VDC and arduino gives 5V, so I connected sensor to external supply of 12V. Now its working but signal also has 12V, so I can't connect it to arduino.

Can you suggest any better option for this?

Use a voltage divider

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.