So before adding an op-amp buffer between sensor and the non-inverting input of a schmitt trigger, if the output of the sensor was for example 0.4V, the moment I connected it to the non-inverting input somehow the output of the sensor would suddenly become about 2V. Ater I added the buffer the problem was solved and the output of the sensor didn’t change anymore. So I have done some reading on buffers applications but can’t really understand what’s going on here and why buffer was the solution.
Your sensor has a high output impedance.
Imagine the sensor as a voltage source in series with a resistor Rout. If the output of the comparator is high, you get the following equivalent circuit:
Here, V_OpAmp is the output voltage of the op amp. I've assumed it's railed high at 5V. The open-circuit voltage V_sensor of the sensor is 0.4V, but because of it's got a high output impedance, the op amp output voltage pulls it high - what you actually measure at the output of the sensor is V_sensor_out = 2V.
I have picked some resistor values that replicate the measured results you mention. Knowing Rin and Rf, you could easily calculate the actual value of Rout.
Adding the buffer presents the sensor with a high-impedance load, and the Schmidt trigger with a low impedance driver, effectively eliminating the effect.
For clarity, what you have shown on your drawing is a comparator with a positive feedback, which makes it a Schmitt trigger.
Without a positive feedback, the comparator output goes to its maximum voltage (up to VCC for some comparators), when the voltage on its non-inverting input exceeds the voltage on its inverting input and it goes to its minimum voltage (Vss or ground), when the voltage on its inverting input exceeds the voltage on its non-inverting input. If the voltage between the compartor inputs is close to zero, its output could be switching back and forth chaotically and confuse everyone.
The positive feedback is used to avoid this chaotic switching by reinforcing the initial difference between the inputs. If it was positive to start with (i.e., the non-inverting input was higher than the inverting input), the output will go high and a fraction of it, through the positive feedback, will make the non-inverting input a little higher and the difference more positive. If it was negative, the output will go low and will make the non-inverting input a little lower and the difference more negative.
A simple version of a comparator/Schmitt trigger circuit that you could use in your application is shown below:
It is very similar to your circuit, except that the sensor is connected to the non-inverting input. With this circuit, you can calculate precisely how much the switching output will affect the non-inverting input, since it is just a resistive feedback network.
In your circuit, the effect of the feedback is more difficult to predict because the sensor is a part of the feedback network and its impedance may be changing over time (e.g., when it senses something).
In addition, the behavior of such circuit could confuse you: when you see the voltage on the output of the sensor increasing, you might think that this is because the state of the sensor has changed, when in fact it may be caused by the comparator output switching from low to high with a fraction of that voltage showing on the output of the sensor.
Of course if the sensor has very low output impedance, the positive feedback will be more predictable and will be determined by the ratio of Rf and Rin. The buffer, as you already know, would also isolate the sensor from the positive feedback, but it is an extra component.
In summary, if you want to make your circuit work more predictably without adding a buffer, you can use the inverting input for the sensor and let the non-inverting input do its thing.