# Using digital pins for reading analog value

When I connect the pressure sensor to an analog pin of WEMOS D1 MINI board I'm getting values between 300 when the sensor is not pressed, and 700 when it is pressed. But I wanted to connect 10 pressure sensors so I connected it to digital pins because I need only 2 states 0 and 1. I thought that, if the value is less than 512 digitalRead method returns 0, and 1 if the value is more than 512. But now I getting spam on serial port because values are changing all the time. When pressure sensor is not active I'm getting only 000000000 and that's ok, but when I press the sensor I'm getting 01011101.

Am I right that when I connect the sensor to digital I can get values 0 and 1, or should I use an analog multiplexer to connect everything through analog pins?

• I don't know what logic family those pins will be but the high/low thresholds will be defined, and are often not half the supply voltage. For reference: learn.sparkfun.com/tutorials/logic-levels/…. One approach could be to have a comparator circuit comparing the reading to an appropriate reference voltage which will generate a nice clean 0 or 1 that is read by the digital pin. – jramsay42 May 20 '20 at 7:29

If you connect analog output to digital pin then you would get 0 or 1 only if analog voltage levels differs from 0V to VCC (power supply voltage). If the vlotage level of analog sensor changes, let say, from 0V to 1V, then you will have or logic 0 on your input, or nonsense (e.g. noise, some chaotic digital level changes etc).

I would divide your question to two parts:

1. If you need to read only two states from the analog line (e.g. 0 and 1), you can use analog comparator IC (with or without hysteresis), like LM393 etc, or digital Schmitt trigger IC on the digital inputs, like 74HC14 etc. With comparator IC you can distinguish any the smallest voltage level difference. With digital Schmitt trigger IC you can distinguish voltages close to logic levels.

2. If there is a noise (e.g. sometimes signal changes rapidly to opposite state and then back), then you should use some digital filtering algorithms, for example Kalman filter or something different, depending on your particular application.

The digitalRead() function can only return HIGH(1) or LOW(0). How could you get 01011101 then? You must have an error somewhere else in your sketch.