Using Analog Input as Analog Comparator Input of Arduino

 In the previous tutorial on how to use Arduino Analog Comparator we explained the various registers that are important that needs to be configured in order to use the comparator. We also provided simple C/C++ program or sketch example which compared two voltage inputs at the AIN0 and AIN1 and turned on and off a LED according the comparator result.

In this arduino analog comparator example we will show another way of using the analog comparator. Here we will use the analog input pin A1 as source of input signal for the comparator negative input. That is we will use A1 analog pin instead of the AIN1 pin(PD7) pin. We will apply voltage into the A1 pin using a 10KOhm potentiometer and vary it to compare with the positive input applied at the AIN0 pin. The positive input AIN0 which is PD6 will be fed also with varying voltage using 10KOhm POT.

Schematic Diagram for wiring Arduino Analog Comparator

 The following shows the schematic diagram for wiring Arduino Analog Comparator with inputs at analog input pin A1 as negative input to the internal comparator and with input at the AIN0(PD6) pin.


Program Code/Sketch for Arduino Analog Comparator

The following code can be used to configure the comparator to use the analog pin A1 as input the the negative input of the analog comparator and use the AIN0 pin as the positive input for the comparator. Based on which input voltage is higher the LED connected to PD4 is turn on or off.

void setup () {

	DDRD |= (1<<PD4);
        DIDR1 |= (1<<AIN0D); // Disable Digital Inputs at AIN0 and AIN1
	ADCSRA &= ~(1<<ADEN);
        ADCSRB |= (1<<ACME);	//Set ACME bit in ADCSRB to use external analog input at AIN1 -ve input
	ADMUX = 0x01; //select A1 as input 
	ACSR = 
	(0 << ACD) |    // Analog Comparator: Enabled
	(0 << ACBG) |   // Clear ACBG to use external input to AIN0 +ve input
	(0 << ACO) |    // Analog Comparator Output: OFF
	(1 << ACI) |    // Analog Comparator Interrupt Flag: Clear Pending Interrupt by setting the bit
	(0 << ACIE) |   // Analog Comparator Interrupt: Disabled 
	(0 << ACIC) |   // Analog Comparator Input Capture: Disabled
	(0 << ACIS1) | (0 << ACIS0);   // Analog Comparator Interrupt Mode: Comparator Interrupt on Output Toggle
}

void loop() {

	if (ACSR & (1<<ACO))
		PORTD |= (1<<PD4);
	else
		PORTD &= ~(1<<PD4);
	
}


Program notes:

- The AIN0D bit in the DIDR1 register should be disabled in order to save power consumption for the default digital input buffer.

DIDR1 |= (1<<AIN0D); 

- When any of the analog input is used for the negative input to the comparator(such as A1 here)

1) the ADC must be disabled by clearing the ADEN bit in the ADCSRA register. 

ADCSRA &= ~(1<<ADEN);

2) the ACME bit in the ADCSRB register must be set to use analog input pins instead of AIN1 pin

   ADCSRB |= (1<<ACME);

3) the MUX2:0 bits in ADMUX register must be configured to select which of the analog pins A0 to A7 to use.

ADMUX = 0x01;

- The ACSR register bits are configured according to the user application intention. 

1. The analog comparator must be enabled.

2. The user selects the input to the positive input of the comparator which can be either the external input via the AIN0 pin as used here or the bandgap reference voltage(1.1V). This is set using the ACBG bit.

3. Whether to use or not the output of the analog comparator. Here we don't need it so we turned it off.

4. We can clear the analog comparator interrupt flag.

5. We can disable the analog comparator interrupt.

6. We can use or not use the Analog Comparator Input Capture feature. Here we disabled it.

7. We can choose type of interrupt trigger mode. Here we set it to output toggle mode but this is not required since we did not use the interrupt feature.

 - In the main loop we can simply monitor the analog comparator output ACO bit in the ACSR register and turn on/off a LED.

 

Video Demonstration:

 




Post a Comment

Previous Post Next Post