Go Down

Topic: Analog comparator on Arduino Leonardo (Read 1 time) previous topic - next topic


Jul 09, 2014, 09:11 pm Last Edit: Jul 09, 2014, 10:18 pm by bogdanul2003 Reason: 1

I'm struggling to get the analog comparator working on arduino leonardo. This is what I do: I connect AIN- to internal bandgap (1.1 V) and set an interrupt on rising edge so when AIN0 voltage is higher than 1.1 V it should generate an interrupt.
Here is the code :

Code: [Select]
static volatile char flag;
static volatile int flag2=0;


   //with this test, ensure that indeed the interrupt result is 1 = V(AIN0) > V(AIN1)
   //(double check ?)
   if ( (ACSR & (1 << ACO))== 0 ) {

void setup(void) {
  while (!Serial) {
   ; // wait for serial port to connect. Needed for Leonardo only
 pinMode(A1, OUTPUT);
 pinMode(A3, OUTPUT);
 //pinMode(7,  INPUT);
 PORTE = 0;
 digitalWrite(A1, LOW);
 digitalWrite(A3, HIGH);
 sbi(DIDR1, AIN0D);

 (0<<ACD) |   // Analog Comparator: Enabled
 (0<<ACBG) |   // Analog Comparator Bandgap Select: AIN0 is applied to the positive input
 (0<<ACO) |   // Analog Comparator Output: Off
 (1<<ACI) |   // Analog Comparator Interrupt Flag: Clear Pending Interrupt
 (1<<ACIE) |   // Analog Comparator Interrupt: Enabled
 (0<<ACIC) |   // Analog Comparator Input Capture: Disabled
 (1<<ACIS1) | (1<<ACIS0);   // Analog Comparator Interrupt Mode: Comparator Interrupt on Rising Output Edge

void loop(void) {


When I connect the 5V pin from the arduino board to the AIN0 (digital 7)  no interrupt is generated. When I connect the GND pin to AIN0 the interrupt gets triggered.
Shouldn't it be the other way around? What am I doing wrong?

Moderator edit: [code] [/code] tags added.


The code I posted is taken from this thread:

Just some minor modifications to select de internal 1.1 V for AIN-

Any clues ?

Go Up