Reading Output of an Op Amp Comparator w/ Arduino - Page 1
Forum section: electronics / beginners

author: jGalt
author_rank: Contributor
timestamp: August 26, 2014, 06:45:50 am
content: I was working on a project earlier today where I had a near square wave output but it was a bit noisier than I wanted. I had one op amp left on my IC and I decided to cheat a little and throw my noisy signal through a comparator (this worked for my purposes because I only needed to get the frequency of my output). I set the negative input to 2V and everything worked perfectly. I probed the output and got a perfect DC square wave that went from ground to around 3.2V, which is the highest the op amp can go off of the 5V I'm supplying it.My question came about when I took my output and plugged it into an analog pin of the arduino and used the analogread() function to look at the voltages being input to the arduino (this may sound like a weird thing to have been doing but I did it anyways).When I watched the voltages using the serial monitor functions, it read the low as "2" and the high as "688." What?! Volts?! Madness! My thought is that the way the comparator is working is that when the positive input to the op amp (which is my signal) goes higher than 2V, the comparator takes that difference and amplifies it by something around 100,000 so you would expect a really large voltage but the op amp is limited to outputting 3.2V. Is the arduino reading 688 because the comparator is trying to output some ridiculously high voltage that's much greater than 3.2V? Also it can't be a coincidence that my negative input is 2V and when my signal is low the arduino reads 2V (this is confusing because the oscope tell me it's 0V). I know that when the signal is low the comparator should give a giant negative gain that causes the output to hit the negative rail of 0V... anyways it's clear that these are bad theories and I was hoping someone could shed some light.P.S. I did realize that I was using an analog pin to read a digital input but even knowing this, I don't understand what was going on.

author: Richard Crowley
author_rank: Super Contributor
timestamp: August 26, 2014, 07:43:11 am
content: The Arduino documentation explains that the analog input returns a number between 0 and 1^10 (1023) proportional to the analog input vs. the reference voltage (typically VCC unless you reference it to something else).

author: Kremmen
author_rank: Super Contributor
timestamp: August 26, 2014, 07:47:11 am
content: You are reading the numeric value of the A/D conversion of the input voltage. The A/D converter range appears to be 0-1023 and the reference voltage 5V. The exact reading for 3.2V would be close to 655 so that's what you are seeing. The math is simple:reading = Vin/Vref * max, wherereading = what you get,Vin = voltage to analog pin,Vref = A/D converter reference voltagemax = A/D converter max reading (when Vin = Vref)

author: Unknown
author_rank: Unknown
timestamp: August 26, 2014, 10:13:42 am
content: And to pre-emptively answer what may be your next question: the ATmega168/328 specs say logic high for the digital I/O pins >= 0.7*Vcc, or 3.5v.If you intend to use your Arduino to measure the frequency (i.e. using the FreqCount or FreqCounter libraries that use the timer interrupt on Pin 5), the 3.2v from your opamp may not be enough to drive it.

author: jGalt
author_rank: Contributor
timestamp: August 26, 2014, 05:37:13 pm
content: Thank you all, that cleared it up for me. Should've realized.