### Adjusting Emissivity - Complete example Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/QUICK_START.md A complete example demonstrating how to read the current emissivity, set a new emissivity value, and inform the user about the required sensor restart. ```cpp #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); void setup() { Serial.begin(9600); mlx.begin(); // Read current value Serial.print("Current emissivity: "); Serial.println(mlx.readEmissivity()); // Set new value mlx.writeEmissivity(0.95); Serial.println("New emissivity set. Restart sensor."); } void loop() { } ``` -------------------------------- ### Reading Both Temperatures Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/QUICK_START.md An example demonstrating how to read both ambient and object temperatures simultaneously and handle potential read errors. ```cpp void loop() { double ambientC = mlx.readAmbientTempC(); double objectC = mlx.readObjectTempC(); if (!isnan(ambientC) && !isnan(objectC)) { Serial.print("Ambient: "); Serial.print(ambientC); Serial.print("C Object: "); Serial.print(objectC); Serial.println("C"); } else { Serial.println("Read error"); } delay(500); } ``` -------------------------------- ### Handling Read Failures Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/QUICK_START.md Example code showing how to check for `NAN` return values from temperature reading methods to handle I2C communication failures. ```cpp #include double temp = mlx.readObjectTempC(); if (isnan(temp)) { Serial.println("Failed to read temperature"); } else { Serial.print("Temperature: "); Serial.println(temp); } ``` -------------------------------- ### begin() Method Example Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/api-reference/Adafruit_MLX90614.md Initializes I2C communication with the MLX90614 sensor. Must be called before any temperature or emissivity readings. This example uses the default I2C address (0x5A) and Wire bus. ```cpp #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); void setup() { Serial.begin(9600); // Using default I2C address (0x5A) and Wire bus if (!mlx.begin()) { Serial.println("Failed to initialize sensor"); while (1); } Serial.println("Sensor ready"); } void loop() { double objTemp = mlx.readObjectTempC(); Serial.print("Object Temp: "); Serial.print(objTemp); Serial.println(" C"); delay(500); } ``` -------------------------------- ### Quick Example Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/README.md A basic example demonstrating how to initialize the MLX90614 sensor and read ambient and object temperatures in Celsius. ```cpp #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); void setup() { Serial.begin(9600); if (!mlx.begin()) { Serial.println("MLX sensor not found!"); while (1); } } void loop() { Serial.print("Ambient: "); Serial.print(mlx.readAmbientTempC()); Serial.print("C Object: "); Serial.print(mlx.readObjectTempC()); Serial.println("C"); delay(500); } ``` -------------------------------- ### Real-World Application Example: Thermal Monitoring System Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/USAGE_PATTERNS.md A comprehensive example demonstrating a thermal monitoring system that combines multiple usage patterns like exponential moving average filtering, alarm thresholds, and periodic reporting. ```cpp #include #include Adafruit_MLX90614 mlx = Adafruit_MLX90614 (); class ThermalMonitor { double smoothedTemp = 0; double alpha = 0.2; bool initialized = false; // Thresholds const double HIGH_TEMP_ALARM = 45.0; const double LOW_TEMP_ALARM = 10.0; const double CHANGE_THRESHOLD = 3.0; unsigned long lastAlarmTime = 0; const unsigned long ALARM_COOLDOWN = 5000; // 5 seconds double lastReportedTemp = 0; unsigned long lastReportTime = 0; const unsigned long REPORT_INTERVAL = 10000; // 10 seconds public: void update(double rawTemp) { if (isnan(rawTemp)) { return; } // Apply exponential moving average if (!initialized) { smoothedTemp = rawTemp; initialized = true; } else { smoothedTemp = (alpha * rawTemp) + ((1.0 - alpha) * smoothedTemp); } unsigned long now = millis(); // Check alarms if (smoothedTemp > HIGH_TEMP_ALARM || smoothedTemp < LOW_TEMP_ALARM) { if (now - lastAlarmTime > ALARM_COOLDOWN) { triggerAlarm(smoothedTemp); lastAlarmTime = now; } } // Periodic reporting if (now - lastReportTime > REPORT_INTERVAL) { double delta = fabs(smoothedTemp - lastReportedTemp); if (delta > CHANGE_THRESHOLD || lastReportTime == 0) { reportStatus(smoothedTemp); lastReportedTemp = smoothedTemp; lastReportTime = now; } } } double getSmoothedTemp() { return smoothedTemp; } private: void triggerAlarm(double temp) { Serial.print("ALARM: Temperature "); if (temp > HIGH_TEMP_ALARM) { Serial.print("HIGH - "); } else { Serial.print("LOW - "); } Serial.print(temp); Serial.println("C"); } void reportStatus(double temp) { Serial.print("Status: "); Serial.print(temp); Serial.print("C at "); Serial.println(millis()); } }; ThermalMonitor monitor; void setup() { Serial.begin(9600); if (!mlx.begin()) { Serial.println("ERROR: Sensor not found"); while (1); } Serial.println("Thermal monitoring started"); } void loop() { double rawTemp = mlx.readObjectTempC(); if (!isnan(rawTemp)) { monitor.update(rawTemp); } delay(100); } ``` -------------------------------- ### Logging Temperature Data Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/QUICK_START.md Logs ambient and object temperatures at a specified interval (e.g., every 10 seconds). ```cpp void logTemperatureData() { static unsigned long lastLog = 0; unsigned long now = millis(); // Log every 10 seconds if (now - lastLog >= 10000) { double ambientTemp = mlx.readAmbientTempC(); double objectTemp = mlx.readObjectTempC(); if (!isnan(ambientTemp) && !isnan(objectTemp)) { Serial.print(now); Serial.print(","); Serial.print(ambientTemp); Serial.print(","); Serial.println(objectTemp); } lastLog = now; } } void loop() { logTemperatureData(); delay(100); } ``` -------------------------------- ### Using Multiple I2C Buses Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/QUICK_START.md Demonstrates how to use multiple MLX90614 sensors on boards with multiple I2C buses, initializing each sensor on its respective bus. ```cpp #include Adafruit_MLX90614 mlx1 = Adafruit_MLX90614(); Adafruit_MLX90614 mlx2 = Adafruit_MLX90614(); void setup() { Serial.begin(9600); // Initialize first sensor on Wire (default I2C) if (!mlx1.begin(0x5A, &Wire)) { Serial.println("MLX1 not found"); } // Initialize second sensor on Wire1 (alternative I2C) if (!mlx2.begin(0x5A, &Wire1)) { Serial.println("MLX2 not found"); } } void loop() { Serial.print("Sensor 1: "); Serial.print(mlx1.readObjectTempC()); Serial.print("C Sensor 2: "); Serial.print(mlx2.readObjectTempC()); Serial.println("C"); delay(500); } ``` -------------------------------- ### Handling Initialization Failures Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/QUICK_START.md Checks if the MLX sensor is found during initialization. If not, it prints an error and halts execution. ```cpp #include #include "Adafruit_MLX90614.h" Adafruit_MLX90614 mlx = Adafruit_MLX90614(); void setup() { Serial.begin(9600); while (!Serial); if (!mlx.begin()) { Serial.println("Error: MLX sensor not found"); Serial.println("Check wiring and I2C address"); while (1); } Serial.println("MLX90614 Found!"); } void loop() { // Your main code here } ``` -------------------------------- ### Example: Setting emissivity for aluminum Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/configuration.md Demonstrates how to read the current emissivity and set a new value for measuring aluminum surfaces. ```cpp #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); void setup() { Serial.begin(9600); mlx.begin(); // Read current emissivity double currentEmissivity = mlx.readEmissivity(); Serial.print("Current emissivity: "); Serial.println(currentEmissivity); // Set emissivity for aluminum surface double aluminumEmissivity = 0.08; mlx.writeEmissivity(aluminumEmissivity); Serial.print("Emissivity set to: "); Serial.println(aluminumEmissivity); Serial.println("Please restart the sensor for changes to take effect."); } void loop() { } ``` -------------------------------- ### begin() Initialization Failure Example Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/errors.md Demonstrates how to check for initialization errors when using the `begin()` function of the Adafruit MLX90614 library. ```cpp #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); void setup() { Serial.begin(9600); if (!mlx.begin()) { Serial.println("Error connecting to MLX sensor. Check wiring."); Serial.println("Possible causes:"); Serial.println(" - Sensor not connected"); Serial.println(" - Wrong I2C address"); Serial.println(" - I2C pull-up resistors missing"); Serial.println(" - Wrong Wire bus specified"); while (1); } Serial.println("Sensor connected successfully"); } ``` -------------------------------- ### Reading Ambient Temperature Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/QUICK_START.md Code snippets to read the ambient temperature in Celsius and Fahrenheit, including error checking. ```cpp // In Celsius double tempC = mlx.readAmbientTempC(); // In Fahrenheit double tempF = mlx.readAmbientTempF(); // Check for errors if (!isnan(tempC)) { Serial.print("Ambient: "); Serial.println(tempC); } ``` -------------------------------- ### Example: Using a custom I2C address Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/configuration.md Demonstrates how to initialize the MLX90614 sensor with a custom I2C address. ```cpp #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); void setup() { Serial.begin(9600); // Initialize with custom I2C address uint8_t customAddr = 0x5B; if (!mlx.begin(customAddr)) { Serial.println("Sensor not found at address 0x5B"); // Try default address if (!mlx.begin(0x5A)) { Serial.println("Sensor not found"); while (1); } } Serial.println("Sensor initialized"); } void loop() { double temp = mlx.readAmbientTempC(); Serial.println(temp); delay(500); } ``` -------------------------------- ### Pattern 1: Simple Temperature Polling Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/USAGE_PATTERNS.md The most basic usage pattern: initialize the sensor and read temperatures repeatedly. ```cpp #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); void setup() { Serial.begin(9600); if (!mlx.begin()) { Serial.println("Sensor initialization failed"); while (1); } } void loop() { double ambientTemp = mlx.readAmbientTempC(); double objectTemp = mlx.readObjectTempC(); Serial.print("Ambient: "); Serial.print(ambientTemp); Serial.print(" C, Object: "); Serial.print(objectTemp); Serial.println(" C"); delay(1000); // Read once per second } ``` -------------------------------- ### Example: Using Wire1 on a board with multiple I2C buses Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/configuration.md Shows how to specify an alternative Wire instance (e.g., Wire1) for I2C communication. ```cpp #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); void setup() { Serial.begin(9600); // Use alternative I2C bus (Wire1) if (!mlx.begin(MLX90614_I2CADDR, &Wire1)) { Serial.println("Sensor not found on Wire1"); while (1); } Serial.println("Sensor initialized on Wire1"); } void loop() { double temp = mlx.readAmbientTempC(); Serial.println(temp); delay(500); } ``` -------------------------------- ### readEmissivity() Example Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/api-reference/Adafruit_MLX90614.md Example code demonstrating how to read the current emissivity value from the MLX90614 sensor. ```cpp #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); void setup() { Serial.begin(9600); mlx.begin(); double emissivity = mlx.readEmissivity(); if (!isnan(emissivity)) { Serial.print("Current emissivity: "); Serial.println(emissivity); } else { Serial.println("Failed to read emissivity"); } } ``` -------------------------------- ### Read Operation Example Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/I2C_PROTOCOL.md Example of reading object temperature from register 0x07 using a write-then-read sequence. ```C++ uint8_t buffer[3]; buffer[0] = 0x07; // Register address // Library calls: i2c_dev->write_then_read(buffer, 1, buffer, 3); // Result: buffer[0] = LSB, buffer[1] = MSB, buffer[2] = PEC ``` -------------------------------- ### writeEmissivity() Example Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/api-reference/Adafruit_MLX90614.md Example code demonstrating how to write a scaled emissivity value (0.1 to 1.0) to the MLX90614 sensor's EEPROM register. ```cpp #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); void setup() { Serial.begin(9600); mlx.begin(); // Read current emissivity Serial.print("Current emissivity = "); Serial.println(mlx.readEmissivity()); // Set new emissivity (0.95 is typical for many surfaces) double newEmissivity = 0.95; Serial.print("Setting emissivity to "); Serial.println(newEmissivity); mlx.writeEmissivity(newEmissivity); // Verify the write delay(100); Serial.print("New emissivity = "); Serial.println(mlx.readEmissivity()); Serial.println("Please restart the sensor for changes to take full effect."); } void loop() { } ``` -------------------------------- ### readAmbientTempC() Example Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/api-reference/Adafruit_MLX90614.md Reads the ambient temperature in Celsius and prints it to the serial monitor. ```cpp #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); void setup() { Serial.begin(9600); mlx.begin(); } void loop() { double ambientTemp = mlx.readAmbientTempC(); if (!isnan(ambientTemp)) { Serial.print("Ambient Temp: "); Serial.print(ambientTemp); Serial.println(" C"); } delay(500); } ``` -------------------------------- ### readAmbientTempF() Example Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/api-reference/Adafruit_MLX90614.md Reads both ambient and object temperatures in Fahrenheit and prints them to the serial monitor. ```cpp #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); void setup() { Serial.begin(9600); mlx.begin(); } void loop() { double ambientF = mlx.readAmbientTempF(); double objectF = mlx.readObjectTempF(); Serial.print("Ambient = "); Serial.print(ambientF); Serial.print("*F\tObject = "); Serial.print(objectF); Serial.println("*F"); delay(500); } ``` -------------------------------- ### writeEmissivityReg() Example Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/api-reference/Adafruit_MLX90614.md Example code demonstrating how to write a raw emissivity value to the MLX90614 sensor's EEPROM register. ```cpp #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); void setup() { Serial.begin(9600); mlx.begin(); // Write raw emissivity value (example: 0xFFFF for maximum) mlx.writeEmissivityReg(0xFFFF); Serial.println("Emissivity written. Restart the sensor for changes to take effect."); } void loop() { } ``` -------------------------------- ### readObjectTempF() Example Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/api-reference/Adafruit_MLX90614.md Reads the object temperature in Fahrenheit and prints it to the serial monitor. ```cpp #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); void setup() { Serial.begin(9600); mlx.begin(); } void loop() { double objTempF = mlx.readObjectTempF(); if (!isnan(objTempF)) { Serial.print("Object Temp: "); Serial.print(objTempF); Serial.println(" F"); } delay(1000); } ``` -------------------------------- ### Write Operation Example Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/I2C_PROTOCOL.md Example of writing to the Emissivity Register (0x24) with CRC protection. ```C++ uint8_t buffer[4]; buffer[0] = _addr << 1; // I2C address in write mode buffer[1] = 0x24; // Register address (emissivity) buffer[2] = data & 0xff; // Data low byte buffer[3] = data >> 8; // Data high byte uint8_t pec = crc8(buffer, 4); // Calculate PEC buffer[0] = buffer[1]; // Reorganize for transmission buffer[1] = buffer[2]; buffer[2] = buffer[3]; buffer[3] = pec; i2c_dev->write(buffer, 4); ``` -------------------------------- ### Data Logging to EEPROM Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/USAGE_PATTERNS.md Logs temperature readings to Arduino EEPROM, which has limited storage. The readings are quantized to fit into bytes. ```cpp #include #include #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); const int LOG_CAPACITY = 100; // Number of readings to store const int LOG_START = 0; // EEPROM starting address int logIndex = 0; struct TemperatureLog { byte objectTempC; // Stored as byte (±100 range) byte ambientTempC; }; void logTemperature(double objectTemp, double ambientTemp) { if (logIndex >= LOG_CAPACITY) { Serial.println("Log full"); return; } if (isnan(objectTemp) || isnan(ambientTemp)) { return; } // Quantize to byte (assume -50 to 50°C range) byte objectByte = (byte)((int)objectTemp + 50); byte ambientByte = (byte)((int)ambientTemp + 50); int address = LOG_START + (logIndex * sizeof(TemperatureLog)); EEPROM.write(address, objectByte); EEPROM.write(address + 1, ambientByte); logIndex++; } void dumpLog() { Serial.println("Temperature Log:"); for (int i = 0; i < logIndex; i++) { int address = LOG_START + (i * sizeof(TemperatureLog)); byte objectByte = EEPROM.read(address); byte ambientByte = EEPROM.read(address + 1); double objectTemp = objectByte - 50; double ambientTemp = ambientByte - 50; Serial.print(i); Serial.print(": Object="); Serial.print(objectTemp); Serial.print("C, Ambient="); Serial.print(ambientTemp); Serial.println("C"); } } void setup() { Serial.begin(9600); mlx.begin(); } void loop() { double objectTemp = mlx.readObjectTempC(); double ambientTemp = mlx.readAmbientTempC(); if (!isnan(objectTemp) && !isnan(ambientTemp)) { logTemperature(objectTemp, ambientTemp); Serial.print("Logged: Object="); Serial.print(objectTemp); Serial.print("C, Ambient="); Serial.print(ambientTemp); Serial.println("C"); } // Dump log when full if (logIndex >= LOG_CAPACITY) { dumpLog(); logIndex = 0; // Reset for circular buffer } delay(5000); // Log every 5 seconds } ``` -------------------------------- ### Multiple Sensors Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/USAGE_PATTERNS.md Demonstrates how to use multiple MLX90614 sensors on the same I2C bus by assigning them different I2C addresses. ```cpp #include Adafruit_MLX90614 sensor1, sensor2; void setup() { Serial.begin(9600); // Initialize sensors at different addresses if (!sensor1.begin(0x5A)) { Serial.println("Sensor 1 initialization failed"); } if (!sensor2.begin(0x5B)) { Serial.println("Sensor 2 initialization failed"); } } void loop() { double temp1 = sensor1.readObjectTempC(); double temp2 = sensor2.readObjectTempC(); Serial.print("Sensor 1: "); Serial.print(temp1); Serial.print("C, Sensor 2: "); Serial.print(temp2); Serial.println("C"); delay(500); } ``` -------------------------------- ### Using Default and Custom I2C Addresses Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/types.md Examples demonstrating how to initialize the sensor using its default I2C address and a custom address. ```cpp // Use default address mlx.begin(); // Use custom address mlx.begin(0x5B); ``` -------------------------------- ### Example 1: Reading Object Temperature (0x07) Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/I2C_PROTOCOL.md Illustrates the I2C transaction sequence for reading the object temperature from the MLX90614 sensor. ```I2C Client → 0x5A, Write → Register: 0x07 Client ← (RepeatedStart) ← 0x5A, Read ← Data LSB (variable) ← Data MSB (variable) ← PEC (calculated) → NACK → STOP Result: Temperature value assembled from LSB and MSB ``` -------------------------------- ### Setting I2C Speed (Arduino) Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/I2C_PROTOCOL.md Example of how to set the I2C clock speed in Arduino. ```cpp #include void setup() { Wire.begin(); Wire.setClock(100000); // 100 kHz (default) // or Wire.setClock(400000); // 400 kHz (fast mode) } ``` -------------------------------- ### Pattern 2: Robust Reading with Error Handling Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/USAGE_PATTERNS.md Include proper error checking for I2C read failures. ```cpp #include #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); void setup() { Serial.begin(9600); if (!mlx.begin()) { Serial.println("ERROR: Sensor not found"); while (1); } Serial.println("Sensor ready"); } void loop() { double ambientTemp = mlx.readAmbientTempC(); double objectTemp = mlx.readObjectTempC(); // Check for read failures if (isnan(ambientTemp)) { Serial.println("ERROR: Failed to read ambient temperature"); } else if (isnan(objectTemp)) { Serial.println("ERROR: Failed to read object temperature"); } else { Serial.print("Ambient: "); Serial.print(ambientTemp); Serial.print(" C, Object: "); Serial.print(objectTemp); Serial.println(" C"); } delay(1000); } ``` -------------------------------- ### Implementing Direct I2C Read (Advanced) Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/I2C_PROTOCOL.md A low-level C++ code example demonstrating a direct I2C read operation using the Arduino Wire library. ```cpp // Low-level I2C read (direct Wire library usage) uint8_t buffer[3]; buffer[0] = MLX90614_TOBJ1; Wire.beginTransmission(0x5A); Wire.write(buffer[0]); Wire.endTransmission(false); // RepeatedStart Wire.requestFrom(0x5A, 3); buffer[0] = Wire.read(); // Data LSB buffer[1] = Wire.read(); // Data MSB buffer[2] = Wire.read(); // PEC uint16_t raw = buffer[0] | (buffer[1] << 8); // Verify PEC if needed: crc8(buffer, 3) should equal buffer[2] ``` -------------------------------- ### Reading and Writing Scaled Emissivity Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/types.md Example of reading the current scaled emissivity and then writing a new value. ```cpp // Example: Read and write emissivity double currentEmissivity = mlx.readEmissivity(); if (!isnan(currentEmissivity)) { Serial.print("Current: "); Serial.println(currentEmissivity); // Write new value double newEmissivity = 0.95; mlx.writeEmissivity(newEmissivity); } ``` -------------------------------- ### Multiple Sensors Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/README.md Example demonstrating how to use multiple MLX90614 sensors on the same I2C bus or separate buses. ```cpp Adafruit_MLX90614 mlx1, mlx2; void setup() { mlx1.begin(0x5A, &Wire); mlx2.begin(0x5A, &Wire1); } void loop() { Serial.print(mlx1.readObjectTempC()); Serial.print(", "); Serial.println(mlx2.readObjectTempC()); } ``` -------------------------------- ### begin() Method Signature Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/configuration.md The begin() method controls initialization and accepts I2C address and Wire instance parameters. ```cpp bool begin(uint8_t addr = MLX90614_I2CADDR, TwoWire* wire = &Wire); ``` -------------------------------- ### Temperature Averaging Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/QUICK_START.md Calculates an average temperature over multiple readings to improve stability and reduce noise. ```cpp const int SAMPLES = 10; double readAveragedTemperature() { double sum = 0; int validSamples = 0; for (int i = 0; i < SAMPLES; i++) { double temp = mlx.readObjectTempC(); if (!isnan(temp)) { sum += temp; validSamples++; } delay(50); } if (validSamples == 0) { return NAN; } return sum / validSamples; } void loop() { double averageTemp = readAveragedTemperature(); if (!isnan(averageTemp)) { Serial.print("Average: "); Serial.println(averageTemp); } delay(1000); } ``` -------------------------------- ### Temperature Change Detection Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/QUICK_START.md Monitors temperature changes and triggers an action if the change exceeds a defined threshold. ```cpp double lastTemp = 0; const double THRESHOLD = 1.0; // 1 degree change threshold void loop() { double currentTemp = mlx.readObjectTempC(); if (!isnan(currentTemp)) { double delta = fabs(currentTemp - lastTemp); if (delta > THRESHOLD) { Serial.print("Temperature changed by "); Serial.print(delta); Serial.print(" degrees to "); Serial.println(currentTemp); lastTemp = currentTemp; } } delay(500); } ``` -------------------------------- ### Adjusting Emissivity - Read current emissivity Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/QUICK_START.md Code to read the current emissivity setting of the MLX90614 sensor. ```cpp double emissivity = mlx.readEmissivity(); Serial.print("Emissivity: "); Serial.println(emissivity); ``` -------------------------------- ### Reading Object Temperature Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/QUICK_START.md Code snippets to read the object temperature in Celsius and Fahrenheit, including error checking. ```cpp // In Celsius double tempC = mlx.readObjectTempC(); // In Fahrenheit double tempF = mlx.readObjectTempF(); // Always check for read errors if (!isnan(tempC)) { Serial.print("Object: "); Serial.println(tempC); } ``` -------------------------------- ### Robust Reading with Retries Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/QUICK_START.md A utility function to read temperature with a specified number of retries, handling potential `NaN` (Not a Number) values. ```cpp #include double readWithRetry(double (*readFunc)(void), int maxRetries = 3) { for (int i = 0; i < maxRetries; i++) { double value = readFunc(); if (!isnan(value)) { return value; } delay(50); // Wait before retry } return NAN; // Failed after all retries } void loop() { double temp = readWithRetry(&mlx.readObjectTempC); if (!isnan(temp)) { Serial.println(temp); } else { Serial.println("Failed to read after retries"); } delay(500); } ``` -------------------------------- ### File Structure Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/MANIFEST.md The directory structure of the Adafruit MLX90614 library documentation. ```bash /workspace/home/output/ ├── README.md (11 KB) Project overview ├── INDEX.md (16 KB) Navigation guide ├── QUICK_START.md (9 KB) Getting started ├── configuration.md (4 KB) Configuration options ├── errors.md (9 KB) Error handling ├── types.md (8 KB) Type system ├── I2C_PROTOCOL.md (13 KB) Protocol details ├── USAGE_PATTERNS.md (20 KB) Code examples ├── MANIFEST.md (This file) └── api-reference/ └── Adafruit_MLX90614.md (14 KB) API reference ``` -------------------------------- ### I2C Scanner Sketch Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/QUICK_START.md A utility sketch to scan the I2C bus and identify connected devices, useful for troubleshooting sensor detection issues. ```cpp #include void setup() { Wire.begin(); Serial.begin(9600); Serial.println("I2C Scanner"); } void loop() { byte error, address; int nDevices; Serial.println("Scanning..."); nDevices = 0; for (address = 1; address < 127; address++) { Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print("Device at 0x"); Serial.println(address, HEX); nDevices++; } } if (nDevices == 0) { Serial.println("No devices found"); } delay(5000); } ``` -------------------------------- ### Verify EEPROM writes Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/errors.md This example shows how to verify EEPROM write operations for emissivity settings by reading the value before and after writing, and checking for successful updates. ```cpp #include #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); bool setAndVerifyEmissivity(double targetEmissivity) { // Read current value double beforeValue = mlx.readEmissivity(); Serial.print("Before: "); Serial.println(beforeValue); // Write new value mlx.writeEmissivity(targetEmissivity); Serial.print("Writing: "); Serial.println(targetEmissivity); delay(100); // Read back to verify double afterValue = mlx.readEmissivity(); Serial.print("After: "); Serial.println(afterValue); // Check if write was successful (allow small tolerance due to rounding) if (fabs(afterValue - targetEmissivity) < 0.01) { Serial.println("Emissivity update successful"); return true; } else { Serial.println("Emissivity update failed or not yet applied"); return false; } } void setup() { Serial.begin(9600); mlx.begin(); setAndVerifyEmissivity(0.95); Serial.println("Restart the sensor for EEPROM changes to take full effect."); } void loop() { } ``` -------------------------------- ### Adjusting Emissivity - Write new emissivity Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/QUICK_START.md Code to set a new emissivity value for the MLX90614 sensor. Note that a sensor restart is required for changes to take effect. ```cpp // Set emissivity to 0.95 (typical for organic surfaces) mlx.writeEmissivity(0.95); Serial.println("Restart sensor for changes to take effect"); ``` -------------------------------- ### Error Handling - Initialization Failure Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/README.md Checks if the sensor initialization fails using the begin() method, which returns false on failure. ```cpp if (!mlx.begin()) { Serial.println("Sensor not found"); while (1); } ``` -------------------------------- ### Basic Reading Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/INDEX.md Demonstrates how to initialize the sensor and read the object temperature in Celsius. ```cpp #include Adafruit_MLX90614 mlx = Adafruit_MLX90614(); void setup() { mlx.begin(); } void loop() { double temp = mlx.readObjectTempC(); Serial.println(temp); } ``` -------------------------------- ### Example CRC Calculation Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/I2C_PROTOCOL.md Example of calculating the CRC for a write operation, including address, register, and data bytes. ```C++ // Calculate CRC for write operation uint8_t buffer[4]; buffer[0] = (0x5A << 1); // I2C address in write mode (0xB4) buffer[1] = 0x24; // Register address (emissivity) buffer[2] = 0xFF; // Data low byte buffer[3] = 0xAB; // Data high byte uint8_t pec = crc8(buffer, 4); // Calculate PEC ``` -------------------------------- ### Constructor Source: https://github.com/adafruit/adafruit-mlx90614-library/blob/master/_autodocs/configuration.md The Adafruit_MLX90614 constructor takes no parameters. ```cpp Adafruit_MLX90614 mlx = Adafruit_MLX90614(); ```