Optical Reflective Sensor filtering in code possible?

I'm using Atmega328P. I have an encoder wheel with holes and I'm using a VCNT2020 sensor as an encoder to count the rotations of wheel and I am using an LM393DMR2G op amp to filter it so the output waves are square. Everything works great. Now I'm wondering if I can get rid of the LM393DMR2G to save some cost and instead do the filtering in the code? I've heard of others doing this with sensors, so I'm wondering if that may be possible? If so, how? Where can I find out more?

Thanks for the help!

The LM393 is a comparator. If you are using an AVR-based Arduino, you could use the built in comparator instead.

For more help on filtering input, describe the actual problem that you are trying to solve. It would be a good idea to post oscilloscope traces.

Sorry I should have mentioned I am using Atmega328P. Does that have a built in comparator? Apologies as I'm not too familiar.

I had no chance to use that optosensor, but most of similar sensors work OK without comparator (ie on any input pin) if connected like this

Does that have a built in comparator?

Yes, and it is described in the data sheet.

Tutorial here.