# How to send a digital signal when analog sensor (QRE1113) reaches a threshold value without resorting to wasting analog input pin?

I'm building a miniature pinball machine and I am attempting to use an infrared reflector sensor (QRE1113) to detect when the ball rolls over it (I got the original idea from Ben Heck's videos).

In my testing the sensor does respond adequately to a ball rolling over it, and it's easy to make this determination through the Arduino analog input.

However a pinball machine has way more rollover sensors than the Arduino has analog inputs (final playfield will have ~10-20 rollovers).

My Arduino code doesn't need to know the analog value of the sensor, all it needs to know is whether the value has dropped below a certain threshold meaning a ball is on top of the sensor.

So ideally I would like to convert this analog value to a low or high depending on whether that threshold has been reached, and to then send that result to a digital pin instead of an analog one.

I sort of solved this problem by hooking the sensor up to a digital pin, but using a potentiometer to adjust the sensitivity so that the threshold voltage I want to detect happens to line up with the threshold of what Arduino considers to be high or low.

This is essentially identical to this schematic (but I'm NOT using the sparkfun breakout board, I'm using the bare sensor): http://cdn.sparkfun.com/datasheets/Sensors/Infrared/QRE1113%20Line%20Sensor%20Breakout%20-%20Analog.pdf

The only difference from that schematic is that instead of the 10K resistor I have a 1M resistor and a 500K potentiometer in series in place of where the 10K resistor would be (I think their schematic has a mistake, a 10K resistor gave me terrible results and through trial and error I found 1M works best).

This solution works but it feels cludgy because I'm not actually generating a proper 3.3V HIGH or 0V LOW signal and instead sending an analog signal that just so happens to match up with what Arduino considers the fringes of a high or a low.

Is there a better more "official" way to do this instead of my hack that would allow me to send 0V or 3.3V to the Arduino digital pin based on the analog sensor reaching a certain threshold (ideally adjustable since it takes some trial and error to get the right sensitivity).

I'm building this machine mostly to learn electronics so the "lower level" the solution the better. I know there are ADC boards will expand how many analog inputs I have via "magic", but it would be way more satisfying to build something out of basic components that I can understand.

PS: I'm mostly a software guy. Building electronics circuits is new to me so if I'm thinking about the problem all wrong let me know as well.

• This is what comparators do (you probably want the Schmitt trigger type..). Commented Jan 16, 2019 at 17:42
• Yup. A comparator. You probably want to make the threshold global, and adjustable, possibly from the micro. Expect to do some fussing to get it to work just right. Commented Jan 16, 2019 at 17:57

As mentioned in the comments, you want a comparator.

At a high level, a comparator has two inputs, a positive and a negative, and one output which is high whenever the positive input is at a greater voltage than the negative input. You might notice that this is just like what op amps do when you don't give them any feedback; indeed, the circuitry for a comparator is very similar to that for an op amp, and you can use an op amp as a comparator, but purpose-made comparators perform better.

As Eugene in the comments pointed out, you probably want a Schmitt trigger comparator, which, instead of a single threshold value, has two; after going high, it remains high until it reaches the lower of the two thresholds, and after going low it remains low until it reaches the higher threshold. This helps with noise immunity.

• Thank you so much! Schmitt trigger seems to have done the trick. I'll need to do some testing to see if this is sensitive enough, but even after whacking the ball over the sensor at really high speed, I was able to get it detected! Commented Jan 22, 2019 at 4:03
• with 20 rollover digital ports? Commented Jan 22, 2019 at 4:49

One solution is:

• binary detect with hysteresis, MUX and use a PISO register (Parallel In Serial Out), detect sensor in software and decode events or detect single events in hardware then clear when sent thru PISO register.

You can use as many HEX Schmitt trigger IC to convert the analog signal to digital providing the signal goes below 1/3 and above 2/3 Vcc.

If say ball that is blocking an IR signal = "1", this can be used as a leading edge, or wait for the trailing edge or just send the state to the MUX.

The MUX duration depends on speed of 8 bit byte interrupt sampling of the ball detect and software XOR decoding.

For hardware decoding you again can decide the leading edge, or trailing edge and just send "1" once and clear when the byte is read waiting for the next event.

These IC's are very common, so if you aren't familiar, Digikey has them all for many supply ranges in CMOS, SMD and THT.

• Thanks! Schmitt trigger did the trick after some fine-tuning! To be honest I'm not fully understanding the rest of this with the MUX and PISO, what would this do that the Schmitt alone wouldn't? Is this jsut to give more control over deciding what a 1 is vs what a 0 is? Commented Jan 22, 2019 at 4:03
• You had 2 issues. Noise and insufficient ports. My answer solves both. The other does not. PISO is parallel input serial output. Commented Jan 22, 2019 at 4:07
• I'm pretty sure my answer does solve both, because the insufficient ports problem was insufficient ADC ports. The main question is asking how to convert it to a signal that they wouldn't need to use an ADC for. Commented Jan 22, 2019 at 4:48
• @Hearth does it have sufficient digital ports for 20? Commented Jan 22, 2019 at 7:31
• I'm pretty sure every Arduino model has at least 20 digital inputs. I could be wrong, though. Commented Jan 22, 2019 at 12:48