Go Down

Topic: using arduino as an analog comparator (Read 2627 times) previous topic - next topic

Frogeraie

I would like to compare a device output (between 0 and 5v) to a reference voltage (let say 2.5 v) as we do with an opamp comparator.
From what I saw on the atmega168 datasheet it seems to be possible.
But I am not fluent enough with mcu to translate that in nice arduino language. Does someone know how to do that?

mike_pa

Hi,

the program should be something like this:

Code: [Select]

#define anain1 0 // use pin0 as analog input 1
#define anain2 1 // use pin1 as analog input 2
#define ledpin 13 // pin13 is digital output
#define hyst 5 // use hysteresis of 5

void setup()
{
 pinMode(digout,OUTPUT); // make pin3 an output
}

void loop()
{
 int ain1 = analogRead(anain1);
 int ain2 = analogRead(anain2);
 // if analog input 1 is higher than analog input 2, set output
 if (ain1 > ain2) digitalWrite(ledpin,HIGH);
 if (ain1 <= ain2-hyst) digtalWrite(ledpin,LOW);
}


This is a very easy example. It is better to use a mean value of the analog inputs for comparison.

Good luck
Mike

Frogeraie


Go Up