Surprisingly, it works when i removed the capacitor. Resistor remains 3.9K.
The code also works fine with or without the capacitor.

Thanks again..