Skip to content

Added temperature alert configuration functions#31

Open
samspencer5991 wants to merge 2 commits intoadafruit:masterfrom
samspencer5991:temp-alert
Open

Added temperature alert configuration functions#31
samspencer5991 wants to merge 2 commits intoadafruit:masterfrom
samspencer5991:temp-alert

Conversation

@samspencer5991
Copy link
Copy Markdown

Description

This PR adds temperature alert configuration functionality to the MCP9808 library, enabling users to configure and use the built-in alert output feature of the MCP9808 sensor.

New Features

Alert Configuration Functions

  • setTempAlert(temp, activeHigh, alertMode, hysteresis) - Convenient one-function alert setup
  • setUpperTemp(temp) / getUpperTemp() - Upper temperature boundary
  • setLowerTemp(temp) / getLowerTemp() - Lower temperature boundary
  • setCriticalTemp(temp) / getCriticalTemp() - Critical temperature threshold
  • setHysteresis(hyst) / getHysteresis() - Temperature hysteresis (0°C, 1.5°C, 3°C, 6°C)
  • setAlertPolarity(activeHigh) - Configure active-high or active-low output
  • setAlertMode(interruptMode) - Choose comparator or interrupt mode
  • setAlertSelectCriticalOnly(critOnly) - Monitor critical temp only or all boundaries
  • enableAlert(enable) - Enable/disable alert output
  • getAlertStatus() - Read current alert status

Supported Use Cases

  • Comparator mode: Auto-clearing alerts for thermostat applications (fans, cooling systems)
  • Interrupt mode: Latching alerts for microcontroller-based event logging
  • Temperature windows: Monitor both upper and lower boundaries
  • Critical-only: Simple over-temperature protection

Implementation Details

  • Added temperature conversion helpers (tempToReg(), regToTemp()) for proper register formatting
  • All functions validate input ranges (-40°C to +125°C)
  • Hysteresis applies when temperature decreases below threshold
  • Alert Select bit configures critical-only vs. window monitoring
  • Compatible with existing library functions

Testing

Tested on a custom RP2040 board with:

  • Comparator mode with various hysteresis settings
  • Interrupt mode with ISR handling
  • Temperature crossing thresholds in both directions
  • Multiple heating/cooling cycles

Example Usage

#include <Adafruit_MCP9808.h>

Adafruit_MCP9808 tempsensor = Adafruit_MCP9808();

void setup() {
  tempsensor.begin(0x18);
  
  // Simple critical temperature alert at 30°C
  // Active-low, comparator mode, 1.5°C hysteresis using the critical temperature only
  tempsensor.setTempAlert(30.0, false, false, MCP9808_HYST_1_5C, true);
  
  // Or configure individual settings:
  tempsensor.setCriticalTemp(30.0);
  tempsensor.setAlertPolarity(false);  // Active-low
  tempsensor.setAlertMode(false);      // Comparator mode
  tempsensor.setHysteresis(MCP9808_HYST_1_5C);
  tempsensor.setAlertSelectCriticalOnly(true);
  tempsensor.enableAlert(true);
}

void loop() {
  float temp = tempsensor.readTempC();
  bool alert = tempsensor.getAlertStatus();
  
  Serial.print("Temp: ");
  Serial.print(temp);
  Serial.print("°C | Alert: ");
  Serial.println(alert ? "ACTIVE" : "Inactive");
  
  delay(500);
}

Backwards Compatibility

All existing library functions remain unchanged. This PR only adds new functionality.

Checklist

  • Code compiles without warnings
  • Functions are documented with doxygen-style comments
  • Tested on hardware
  • Follows existing library code style

Related Documentation

MCP9808 Datasheet sections referenced:

  • Section 5.1.1: Configuration Register (Register 5-2)
  • Section 5.1.2: Temperature Limit Registers (Register 5-3)
  • Section 5.2.2: Temperature Hysteresis
  • Section 5.2.3: Alert Output Configuration

- Add setTempAlert() convenience function for critical temperature alerts
- Add individual configuration functions (setUpperTemp, setLowerTemp, etc.)
- Add hysteresis control with setHysteresis()/getHysteresis()
- Add alert polarity, mode, and enable control functions
- Add alert status reading functions
- Add setAlertSelectCriticalOnly() to choose monitoring mode
- Add helper functions for temperature register conversion
- Support both comparator and interrupt alert modes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant