Forum Rule: Always post complete source code & details to reproduce any issue!

# Thread: Digital Logic Voltage Threshold (w/ FreqMeasure library)

1. ## Digital Logic Voltage Threshold (w/ FreqMeasure library)

I am trying to create an optical tachometer using a QRB1114 IR reflectance sensor, a Teensy 3.1, and the FreqMeasure library to count the rotations of a spindle that has reflective tape on it between 1Hz and 300Hz.

Before amplification, the sensor (phototransistor) outputs 0.2V when hitting the tape and 0.02V when not hitting the tape. These are my questions:

1. When trying to register an analog signal as a digital input--not only for this application but also, say, if I want to trigger an interrupt with an Analog signal--what is the "threshold voltage" I should be aiming to amplify my signal to to cross the line between digital LOW and HIGH? I assume that this is important for FreqMeasure to work.
2. Is the FreqMeasure library doing something functionally different from a CHANGE pin interrupt in terms of the voltage threshold it looks for? Before I found the FreqMeasure library I was planning on using an interrupt like this.
3. Do I need to use a Schmitt trigger, and if so, how do I find the best one for my purposes?

Thanks so much!

2. You will have to look at the spec sheet for the CPU chip. On other Arduinos, the unknown range is 0.25 * Vcc to 0.75 * Vcc. You must be above or below these limits to guarantee the correct logic input. In practice though, many have a trip point close to Vcc/2.

3. Hi there,

On vacation at the moment and trying to pull up the manual on my mobile phone won't be particularly fun. Nevertheless, I'll try and help without opening up data sheets.

1) you'll be able to find this voltage in the mk20dx256 data sheet. It's normally specified as a low voltage range and high voltage range, with the area between being undefined operation.
An alternate methodology would be to use an analogy input pin, read the value, then compare against a threshold. May need to do some work to make that work with the freqmeasure library.

2) Not sure, but that sounds likely. Just open up the library in a text editor and have a look.

3) Schmidt triggers tend to be useful for particularly noisy stuff. But without looking at the sheet, I imagine your sensor already filters the output somewhat.

4. Originally Posted by trent
Before amplification, the sensor (phototransistor) outputs 0.2V when hitting the tape and 0.02V when not hitting the tape.
....
1. ... what is the "threshold voltage" I should be aiming to amplify my signal to to cross the line between digital LOW and HIGH?
You want to get it below 1.15 to be sure it reads as low, and above 2.31 to be sure it reads as high. The actual threshold is somewhere between those two, with approx 60 mA hysteresis.

2. Is the FreqMeasure library doing something functionally different from a CHANGE pin interrupt in terms of the voltage threshold it looks for?
Yes, it's using timer capture. The change causes the hardware to capture a very rapidly incrementing timer, as well as trigger an interrupt. Then when software responds sometime shortly thereafter, the timer capture contains an extremely precise measure of the exact moment the change occurred.

With only a normal change interrupt, you'd do something like micros() to get the time. But that's after the change and involves software latency, which can vary quite a lot if USB or serial or other interrupts delay the response. Timer capture uses hardware to precisely capture the a timestamp, so (reasonable) variability in the response time does not cause any decrease in the accuracy.

5. Thanks guys, especially for the quick reply! I went with 9x amplification to give me around 2V "high", 0.2V "low" and FreqMeasure picks it up!

And thanks for the library, Paul. I think writing my own code to do something like this would be a little out of my comfort zone.

Now I just have to figure out how to filter the signal enough to get frequencies that I actually want, as it's outputting values roughly 15x greater than I'm expecting it to give.....but that's a problem for another day.

6. Regarding signal conditioning, it's good practice to not rely on threshold voltages but to use the full voltage difference available. The slower a signals rose and fall times, the longer it transitions through the range of undefined behavior, add a little noise and FreqMeasure or any pin-change ISR may fire multiple times per transition, messing up your measurement.
I suggest an op amp to amplify the signal and feed that into a schmitt trigger buffer. This way you have a maximum of reliability and you could tap into the op amp output signal if you need analog values.

Those will run from a 3.3V supply:
http://www.mouser.com/ds/2/268/21733e-41017.pdf
http://www.mouser.com/ds/2/302/74LVC...100-839680.pdf

7. Ben--this is really helpful! I'm already using an LM358 to amplify my signal. Thanks for speccing a Schmitt trigger for me--I'm hoping that it might help my current random-/over-sampling problem a bit.

Here's my fancy setup, by the way. When it's spinning, the whole thing is covered (and light is blocked) by a shroud. You can see the edge of the reflective tape on the spindle.

8. Maybe those indentations on the lock collar trigger multiple transitions per revolution? You could check with analogRead to serial monitor at very low rpm. The Arduino Software has a built in serial plotter.

9. Try a much shorter piece of tape. Maybe the irregular texture is creating multiple hits.

10. quick question Ben (or anyone else), what's the advantage of using an IC Schmitt trigger versus an opamp (EDIT: or dedicated comparator) circuit like this one?

11. Originally Posted by Ben
Maybe those indentations on the lock collar trigger multiple transitions per revolution? You could check with analogRead to serial monitor at very low rpm. The Arduino Software has a built in serial plotter.
Good idea, here's what it outputted (in my circuit with amplification but no filtration):

(about 16Hz for the first, 32 Hz for the second). This is the signal that's feeding directly into the FreqMeasure library. The sharp peaks are definitely the tape, and I can't see any lesser peaks. I think I'll try using just a Schmitt trigger first, and then maybe a LPF if that doesn't work.

12. Yeah, looks like you're going to need a little low-pass filtering and probably also the voltage comparator with some hysteresis feedback.

At least the software side seems to be working.

13. Originally Posted by trent
quick question Ben (or anyone else), what's the advantage of using an IC Schmitt trigger versus an opamp (EDIT: or dedicated comparator) circuit like this one?
Using Op amps with no negative feedback *can* work, but I wouldn't recommend unless the OP is explicitly specified for this. Op amps can, for example, show a behavior called "phase reversal", where the output suddenly jumps, caused by starving internal gain stages of the OP from bias current. dedicated comparators are more robust in this regard, especially with high input common mode voltages. If you use a comparator without internal schmitt trigger in the circuit you posted thats fine, all you do is adding schmitt behavior, aka hysteresis.

Originally Posted by trent
Good idea, here's what it outputted (in my circuit with amplification but no filtration):

(about 16Hz for the first, 32 Hz for the second). This is the signal that's feeding directly into the FreqMeasure library. The sharp peaks are definitely the tape, and I can't see any lesser peaks. I think I'll try using just a Schmitt trigger first, and then maybe a LPF if that doesn't work.
Ahh, exactly like I suspected. See those noisy spikes in the transition slopes of your signal? That's what will set up a comparator if it has no hysteresis. And there may be many spikes per slope, it may just be you don't see them all because the ADC is too slow. But the comparator is much quicker and will trigger on many of them. The idea of low pass filtering is also ok, if you design a PCB you should reserve some board space to add a low pass filter. Maybe a Dial OP, 1st stage with adjustable gain as a buffer, 2nd stage as LPF with gain=1 (with the buffer before the LPF you can easily design up to 3rd order filter), then into the schmitt.
I'd try the schmitt trigger first and if that doesn't help go for the low pass.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•