In our previous tutorial, we discussed how to use the Arduino Analog Comparator and the necessary registers to compare two voltage inputs. We also provided a simple C/C++ code that turns an LED on or off depending on the result of the comparator. In this tutorial, we'll demonstrate another way to use the Arduino analog comparator, using different pins for the positive and negative inputs.
What You Will Learn:
- How to wire Arduino analog comparator with input signals.
- A simple program code to compare two voltage signals and control an LED.
Arduino Analog Comparator Setup
In this example, we will use the A1 pin as the negative input (instead of AIN1), and the AIN0 pin as the positive input. We will use two 10KOhm potentiometers to apply varying voltages to both the A1 (negative) and AIN0 (positive) pins. Based on which input voltage is higher, the analog comparator will turn an LED on or off.
Wiring Diagram for Arduino Analog Comparator
Here's the schematic to wire the A1 pin as the negative input and AIN0 pin as the positive input for the analog comparator.
Arduino Program Code: Analog Comparator
The following code configures the Arduino analog comparator to compare voltages at A1 and AIN0 and control the LED based on which voltage is higher.
void setup() {
DDRD |= (1<<PD4); // Set PD4 as output for LED
DIDR1 |= (1<<AIN0D); // Disable digital inputs on AIN0 and AIN1
ADCSRA &= ~(1<<ADEN); // Disable ADC
ADCSRB |= (1<<ACME); // Set ACME to use external analog input at AIN1 (-ve input)
ADMUX = 0x01; // Select A1 as input
ACSR = (0 << ACD) | // Enable analog comparator
(0 << ACBG) | // Use external input for AIN0 (+ve input)
(0 << ACO) | // Analog comparator output: OFF
(1 << ACI) | // Clear any previous interrupts
(0 << ACIE) | // Disable analog comparator interrupt
(0 << ACIC) | // Disable input capture
(0 << ACIS1) | (0 << ACIS0); // Comparator interrupt on output toggle
}
void loop() {
if (ACSR & (1<<ACO)) // Check if ACO is set
PORTD |= (1<<PD4); // Turn on LED
else
PORTD &= ~(1<<PD4); // Turn off LED
}
Program Explanation
- Setting Up Pins: We configure PD4 as an output to control the LED.
- Disable Digital Inputs: To save power, we disable digital inputs on AIN0 and AIN1 using the
DIDR1
register. - Disabling ADC: The ADC is disabled by clearing the
ADEN
bit in the ADCSRA register because we are using analog inputs for the comparator, not for ADC conversions. - Using Analog Inputs: We enable the analog input pins by setting the ACME bit in the ADCSRB register and select A1 as the negative input (using the ADMUX register).
- Comparator Settings: The ACSR register controls the analog comparator's behavior. We configure it to:
- Enable the comparator.
- Use external analog inputs for AIN0 and A1.
- Disable the comparator output, interrupts, and input capture.
- Main Loop: The loop() checks the analog comparator's output (ACO bit in ACSR). If the positive input voltage is higher, the LED turns on; otherwise, it turns off.
Important Notes:
- Saving Power: To save power, the AIN0D bit in the DIDR1 register is disabled.
- Disabling ADC: When using A1 for the negative input, the ADC is disabled by clearing the ADEN bit in the ADCSRA register.
- Selecting Input: The ACME bit in ADCSRB is set to use analog inputs for the comparator instead of the default AIN1 pin.
- Controlling the LED: The LED is controlled based on the result of the comparison. If the positive voltage (on AIN0) is higher than the negative voltage (on A1), the LED turns on.
Conclusion
In this tutorial, we demonstrated how to use the Arduino Analog Comparator to compare voltages from two analog inputs (A1 and AIN0) and control an LED based on the comparison result. This is a simple yet powerful way to use the built-in analog comparator of Arduino to monitor voltage levels without needing an external ADC. This approach is useful for projects where you need to monitor and react to voltage changes, such as in sensor applications or automated systems.