Thank a lot guys... The issue is resolved. I am new to the microcontroller stuffs and this is my first project. I connected two resistors as shown in the diagram referred from internet.
I hope this is the pull up resistor. I am still confused of what and how a pull up resistors work, maybe i should do some homework related to it. Anyways thanks a lot for the support everyone.
This would ensure that over voltage on the D2 pin does not occur.
Good that it is functioning, just make sure you dress your wiring neatly, keep the sensor wiring away from the motor wiring as 6 metre is a nice long EMI antenna.
It doesn't look like the sensor cable is shielded.