I have set the digital threshold to 3.3V. However, the digital measurement has a lot of noise.
There is a special parameter to set on, called glitch filter, to ignore signals shorter than a given period.
See this post at this thread What are glitches? - #8 by timreyes