The ALERT pin
One output that does three jobs: a thermostat with hysteresis, a window watching both edges, or a pulse at the end of every conversion so your sketch stops guessing how long to wait. It can only pull down, which is why the board fits a resistor to pull it up.
An output that only pulls down
ALERT is open drain. The chip can connect it to ground and it cannot drive it high — releasing it just leaves it floating. That is why the board fits a 10 kΩ resistor from ALERT to VCC, and why the pin reads high whenever nothing is going on.
It also means several modules' ALERT pins can share one microcontroller input. Any one of them pulling down takes the shared line low, which is exactly the behaviour you want from an alarm.
Three jobs, one register bit apart
Press the button in each mode and watch when the lower trace drops.
Traditional is a thermostat. It trips when a reading exceeds the high threshold and holds until one falls below the low threshold, so a signal hovering at one value cannot chatter. The gap between the thresholds is your hysteresis — set them to the same number and you get the chatter this mode exists to prevent.
Window watches both sides. ALERT is low whenever the reading is outside the band and high the moment it is inside, with no memory. "Tell me if the battery leaves 11.5 to 14 V" is a window.
Conversion ready ignores thresholds entirely and pulses the pin for about 8 µs at the end of every conversion. Set the top bit of the high threshold register and clear the top bit of the low one, and the pin changes job.
Conversion ready is the one to reach for
Most projects do not need a comparator. What they need is to stop guessing.
Without it, a sketch asks for a conversion, waits some number of milliseconds it worked out from the data rate, and reads. Get the wait wrong on the short side and you read the previous conversion; get it wrong on the long side and you have thrown away time. With conversion-ready, an interrupt on the ALERT pin tells the microcontroller the exact moment the number exists.
At 8 SPS that is 125 ms per reading the processor does not have to spend waiting, which on a battery-powered logger is the difference between sleeping and not.
Latching, and why it exists
With latching on, an assertion holds until you read the conversion register. It is there so a brief excursion is not missed between two polls of the pin — a current spike that lasted one conversion still leaves the flag set when you get round to looking.
The cost is that you have to clear it, and a sketch that forgets sees a pin stuck low and concludes the chip is broken. If you are watching the pin continuously, leave latching off.
The code
Window mode: ALERT goes low whenever A0 leaves the band between 1.0 V and 2.5 V, and comes back on its own when the reading returns. The sketch watches the pin rather than polling the voltage, so the microcontroller has nothing to do until something happens.
// Wiring for this sketch.
//
// ESP32 3V3 -> VCC
// ESP32 GND -> GND
// ESP32 SDA -> SDA
// ESP32 SCL -> SCL
// ESP32 GPIO4 -> ALERT (the board already pulls this up)
// what you are watching -> A0, its ground -> GND
//
// Arduino IDE: any board. Library: "ADS1X15" by Rob Tillaart.
#include <Wire.h>
#include <ADS1X15.h>
#define ALERT_PIN 4
const float LOW_V = 1.0;
const float HIGH_V = 2.5;
ADS1115 ADS(0x48);
void setup() {
Serial.begin(115200);
delay(500);
Wire.begin();
if (!ADS.begin()) {
Serial.println("no ADS1115 at 0x48");
while (true) delay(1000);
}
ADS.setGain(1); // +/-4.096 V
ADS.setDataRate(4); // 128 SPS
ADS.setMode(0); // continuous - the comparator needs conversions happening
float perCount = ADS.toVoltage(1);
ADS.setComparatorThresholdLow(LOW_V / perCount);
ADS.setComparatorThresholdHigh(HIGH_V / perCount);
ADS.setComparatorMode(1); // 1 = window, 0 = traditional
ADS.setComparatorPolarity(0); // 0 = active low
ADS.setComparatorLatch(0); // 0 = clears itself when back in range
ADS.setComparatorQueConvert(0); // assert after one reading outside the band
ADS.requestADC(0); // start converting on A0
pinMode(ALERT_PIN, INPUT);
}
void loop() {
if (digitalRead(ALERT_PIN) == LOW) {
Serial.printf("out of range: %.3f V\n", ADS.toVoltage(ADS.getValue()));
delay(200);
}
delay(10);
}The thresholds are written as counts, not volts, so they have to be converted using the volts-per-count factor for the range you selected. toVoltage(1) returns exactly that factor, which is why the two threshold lines divide by it. Change the gain and the thresholds move unless you recompute them.
When it does not work
Three usual causes, in order. The chip must be converting — the comparator only looks at completed conversions, so single-shot mode with nothing requested means nothing to compare. The comparator queue must be enabled: set to 3 it is disabled and the pin stays high whatever happens. And the thresholds are counts, not volts, so a threshold written as 2.5 is two and a half counts, which everything exceeds.
Either the reading is genuinely still outside the band, or the comparator is latching. With latch on, the pin holds until you read the conversion register — that is the point of it, so nothing is missed between polls. Read a value to clear it, or turn latching off if you want the pin to follow the signal.
That is window mode working as specified — there is no hysteresis in it. Traditional mode is the fix: it trips above the high threshold and does not release until the reading drops below the low one, so the gap between your two thresholds becomes the hysteresis.
Not on this board — there is a 10 kΩ to VCC already fitted, which is why the pin reads high when nothing is happening. If you are chaining several modules' ALERT pins onto one microcontroller input, that works precisely because the pin is open drain, and you end up with several pull-ups in parallel.
A divider, the arithmetic that keeps it under the supply, and the two costs nobody mentions.
Measuring a 12 V battery →Edit this page — content/books/ads1115/the-alert-pin.mdx
Questions about this product
See what other owners have asked, and read their solutions.
This page covers several products. Choose yours to see the right discussions.
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.