### MLX90393 with SSD1306 OLED Display Example Source: https://context7.com/adafruit/adafruit_mlx90393_library/llms.txt This example demonstrates how to read magnetic field data from the MLX90393 sensor and display it in real-time on an SSD1306 OLED screen. It includes sensor and display initialization, error handling, and continuous data reading and display. ```cpp #include #include "Adafruit_MLX90393.h" Adafruit_MLX90393 sensor = Adafruit_MLX90393(); Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire); void setup() { Serial.begin(115200); // Initialize magnetometer if (!sensor.begin_I2C()) { Serial.println("No MLX90393 sensor found!"); while (1); } // Initialize OLED display if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println("SSD1306 allocation failed"); while (1); } display.clearDisplay(); display.setTextSize(1); display.setTextColor(SSD1306_WHITE); display.setCursor(0, 0); display.println("MLX90393 Ready!"); display.display(); delay(1000); } void loop() { float x, y, z; display.clearDisplay(); if (sensor.readData(&x, &y, &z)) { // Serial output Serial.print("X: "); Serial.print(x, 2); Serial.print(" Y: "); Serial.print(y, 2); Serial.print(" Z: "); Serial.println(z, 2); // OLED display display.setCursor(0, 0); display.println("- Adafruit MLX90393 -"); display.print("X: "); display.print(x, 1); display.println(" uT"); display.print("Y: "); display.print(y, 1); display.println(" uT"); display.print("Z: "); display.print(z, 1); display.println(" uT"); } else { display.setCursor(0, 0); display.println("Sensor read error!"); } display.display(); delay(50); } ``` -------------------------------- ### Get Sensor Metadata Source: https://context7.com/adafruit/adafruit_mlx90393_library/llms.txt Retrieves and prints sensor metadata using the Adafruit Unified Sensor interface. Ensure the sensor is connected via I2C. ```cpp #include "Adafruit_MLX90393.h" Adafruit_MLX90393 sensor = Adafruit_MLX90393(); void setup() { Serial.begin(115200); while (!Serial) delay(10); if (!sensor.begin_I2C()) { Serial.println("Sensor not found!"); while (1); } // Get sensor metadata sensor_t sensorInfo; sensor.getSensor(&sensorInfo); Serial.println("=== Sensor Information ==="); Serial.print("Name: "); Serial.println(sensorInfo.name); Serial.print("Version: "); Serial.println(sensorInfo.version); Serial.print("Sensor ID: "); Serial.println(sensorInfo.sensor_id); Serial.print("Type: "); Serial.println(sensorInfo.type); Serial.print("Min Value: "); Serial.print(sensorInfo.min_value); Serial.println(" uT"); Serial.print("Max Value: "); Serial.print(sensorInfo.max_value); Serial.println(" uT"); Serial.print("Resolution: "); Serial.print(sensorInfo.resolution); Serial.println(" uT"); } void loop() { // Main application code } ``` -------------------------------- ### Initialization Methods Source: https://context7.com/adafruit/adafruit_mlx90393_library/llms.txt Methods to initialize the MLX90393 sensor using either I2C or SPI communication protocols. ```APIDOC ## begin_I2C() ### Description Initializes the MLX90393 sensor using the I2C protocol. Performs a soft reset and sets default configuration (1x gain, 16-bit resolution, OSR_3, FILTER_7). ### Parameters #### Query Parameters - **i2c_address** (uint8_t) - Optional - Custom I2C address (default 0x0C) - **wire** (TwoWire*) - Optional - Custom Wire instance ### Response - **bool** - Returns true if sensor is found, false otherwise. ``` ```APIDOC ## begin_SPI() ### Description Initializes the MLX90393 sensor using the SPI protocol. Operates at 1MHz, MSB-first, SPI Mode 3. ### Parameters #### Query Parameters - **cs_pin** (int8_t) - Required - Chip select pin number - **spi** (SPIClass*) - Optional - Custom SPI instance ### Response - **bool** - Returns true if sensor is found, false otherwise. ``` -------------------------------- ### Initialize MLX90393 via SPI Source: https://context7.com/adafruit/adafruit_mlx90393_library/llms.txt Initializes the sensor using the SPI protocol. Requires a chip select pin and supports custom SPI instances. ```cpp #include "Adafruit_MLX90393.h" Adafruit_MLX90393 sensor = Adafruit_MLX90393(); #define MLX90393_CS 10 // Chip select pin void setup() { Serial.begin(115200); while (!Serial) { delay(10); } // Initialize with SPI using default SPI instance if (!sensor.begin_SPI(MLX90393_CS)) { Serial.println("No sensor found ... check your wiring?"); while (1) { delay(10); } } Serial.println("MLX90393 sensor initialized via SPI!"); // Alternative: specify custom SPI instance // if (!sensor.begin_SPI(MLX90393_CS, &SPI1)) { ... } } void loop() { // Sensor ready for use } ``` -------------------------------- ### Initialize MLX90393 via I2C Source: https://context7.com/adafruit/adafruit_mlx90393_library/llms.txt Initializes the sensor using the I2C protocol with default settings. Supports optional custom I2C addresses and Wire instances. ```cpp #include "Adafruit_MLX90393.h" Adafruit_MLX90393 sensor = Adafruit_MLX90393(); void setup() { Serial.begin(115200); while (!Serial) { delay(10); } // Initialize with default I2C address (0x0C) if (!sensor.begin_I2C()) { Serial.println("No sensor found ... check your wiring?"); while (1) { delay(10); } } Serial.println("MLX90393 sensor initialized successfully!"); // Alternative: specify custom I2C address and Wire instance // if (!sensor.begin_I2C(0x18, &Wire1)) { ... } } void loop() { // Sensor ready for use } ``` -------------------------------- ### Configure Sensor Gain with setGain() and getGain() Source: https://context7.com/adafruit/adafruit_mlx90393_library/llms.txt Adjusts the analog gain to balance sensitivity and saturation. Higher gain increases sensitivity for weak fields. ```cpp #include "Adafruit_MLX90393.h" Adafruit_MLX90393 sensor = Adafruit_MLX90393(); void setup() { Serial.begin(115200); while (!Serial) delay(10); if (!sensor.begin_I2C()) { Serial.println("Sensor not found!"); while (1); } // Set gain to 1x (default, best for most applications) sensor.setGain(MLX90393_GAIN_1X); // Available gain options: // MLX90393_GAIN_5X - Highest sensitivity // MLX90393_GAIN_4X // MLX90393_GAIN_3X // MLX90393_GAIN_2_5X // MLX90393_GAIN_2X // MLX90393_GAIN_1_67X // MLX90393_GAIN_1_33X // MLX90393_GAIN_1X - Lowest sensitivity (default) // Verify the gain setting Serial.print("Gain set to: "); switch (sensor.getGain()) { case MLX90393_GAIN_1X: Serial.println("1x"); break; case MLX90393_GAIN_1_33X: Serial.println("1.33x"); break; case MLX90393_GAIN_1_67X: Serial.println("1.67x"); break; case MLX90393_GAIN_2X: Serial.println("2x"); break; case MLX90393_GAIN_2_5X: Serial.println("2.5x"); break; case MLX90393_GAIN_3X: Serial.println("3x"); break; case MLX90393_GAIN_4X: Serial.println("4x"); break; case MLX90393_GAIN_5X: Serial.println("5x"); break; } } void loop() { float x, y, z; if (sensor.readData(&x, &y, &z)) { Serial.print("X: "); Serial.print(x); Serial.println(" uT"); } delay(500); } ``` -------------------------------- ### Calibrated Compass Application Source: https://context7.com/adafruit/adafruit_mlx90393_library/llms.txt Builds a calibrated compass by applying hard-iron and soft-iron corrections to magnetometer data and calculating heading with optional magnetic declination. Requires calibration offsets and declination values. ```cpp #include "Adafruit_MLX90393.h" // Hard-iron calibration offsets (from calibration software like MotionCal) // Set to {0, 0, 0} for uncalibrated operation const float hard_iron[3] = { -5.2, 12.8, -3.1 }; // Soft-iron calibration matrix (from calibration software) // Set to identity matrix for uncalibrated operation const float soft_iron[3][3] = { { 1.021, 0.012, -0.008 }, { 0.012, 0.985, 0.015 }, { -0.008, 0.015, 1.002 } }; // Magnetic declination for your location (degrees) // Find yours at: https://www.magnetic-declination.com/ // East is positive (+), West is negative (-) const float mag_decl = -12.5; // Example: San Francisco Adafruit_MLX90393 mlx = Adafruit_MLX90393(); void setup() { Serial.begin(115200); while (!Serial) delay(10); if (!mlx.begin_I2C()) { Serial.println("ERROR: Could not connect to magnetometer"); while(1); } // Configure for compass application mlx.setGain(MLX90393_GAIN_1X); mlx.setResolution(MLX90393_X, MLX90393_RES_17); mlx.setResolution(MLX90393_Y, MLX90393_RES_17); mlx.setResolution(MLX90393_Z, MLX90393_RES_16); mlx.setOversampling(MLX90393_OSR_3); mlx.setFilter(MLX90393_FILTER_5); Serial.println("Calibrated Compass Ready"); } void loop() { float mag_data[3]; float hi_cal[3]; float heading; if (mlx.readData(&mag_data[0], &mag_data[1], &mag_data[2])) { // Apply hard-iron offsets for (uint8_t i = 0; i < 3; i++) { hi_cal[i] = mag_data[i] - hard_iron[i]; } // Apply soft-iron scaling for (uint8_t i = 0; i < 3; i++) { mag_data[i] = (soft_iron[i][0] * hi_cal[0]) + (soft_iron[i][1] * hi_cal[1]) + (soft_iron[i][2] * hi_cal[2]); } // Calculate heading (assuming board is level, +X points forward) heading = (atan2(mag_data[1], mag_data[0]) * 180.0) / M_PI; // Apply magnetic declination heading += mag_decl; // Normalize to 0-360 degrees if (heading < 0) heading += 360; if (heading >= 360) heading -= 360; // Print compass heading Serial.print("Heading: "); Serial.print(heading, 1); Serial.print("° "); // Print cardinal direction if (heading >= 337.5 || heading < 22.5) Serial.println("N"); else if (heading < 67.5) Serial.println("NE"); else if (heading < 112.5) Serial.println("E"); else if (heading < 157.5) Serial.println("SE"); else if (heading < 202.5) Serial.println("S"); else if (heading < 247.5) Serial.println("SW"); else if (heading < 292.5) Serial.println("W"); else Serial.println("NW"); } else { Serial.println("Read error"); } delay(100); } ``` -------------------------------- ### Perform Non-blocking Measurements Source: https://context7.com/adafruit/adafruit_mlx90393_library/llms.txt Uses startSingleMeasurement() and readMeasurement() to allow the microcontroller to perform other tasks while waiting for sensor conversion. ```cpp #include "Adafruit_MLX90393.h" Adafruit_MLX90393 sensor = Adafruit_MLX90393(); void setup() { Serial.begin(115200); while (!Serial) delay(10); if (!sensor.begin_I2C()) { Serial.println("Sensor not found!"); while (1); } // Configure for faster conversion sensor.setFilter(MLX90393_FILTER_5); sensor.setOversampling(MLX90393_OSR_2); } void loop() { float x, y, z; // Start measurement (non-blocking) if (sensor.startSingleMeasurement()) { // Do other work here while measurement is in progress delay(50); // Wait for conversion (time depends on filter/OSR settings) // Now read the measurement data if (sensor.readMeasurement(&x, &y, &z)) { Serial.print("X: "); Serial.print(x, 2); Serial.print(" Y: "); Serial.print(y, 2); Serial.print(" Z: "); Serial.println(z, 2); } } delay(100); } ``` -------------------------------- ### Configure Oversampling Rate Source: https://context7.com/adafruit/adafruit_mlx90393_library/llms.txt Use setOversampling() to configure the oversampling rate for improved measurement accuracy. Higher rates reduce noise but increase conversion time. Options range from OSR_0 (fastest) to OSR_3 (most accurate). ```cpp #include "Adafruit_MLX90393.h" Adafruit_MLX90393 sensor = Adafruit_MLX90393(); void setup() { Serial.begin(115200); while (!Serial) delay(10); if (!sensor.begin_I2C()) { Serial.println("Sensor not found!"); while (1); } // Set oversampling (higher = more accurate but slower) sensor.setOversampling(MLX90393_OSR_3); // Available oversampling options: // MLX90393_OSR_0 - Fastest (minimal oversampling) // MLX90393_OSR_1 // MLX90393_OSR_2 // MLX90393_OSR_3 - Most accurate (maximum oversampling) Serial.print("Oversampling set to: OSR_"); Serial.println(sensor.getOversampling()); } void loop() { float x, y, z; if (sensor.readData(&x, &y, &z)) { Serial.print("X: "); Serial.print(x, 4); Serial.println(" uT"); } delay(200); } ``` -------------------------------- ### Retrieve Sensor Data with getEvent() Source: https://context7.com/adafruit/adafruit_mlx90393_library/llms.txt Uses the Adafruit Unified Sensor interface to retrieve standardized sensor event structures. ```cpp #include "Adafruit_MLX90393.h" Adafruit_MLX90393 sensor = Adafruit_MLX90393(); void setup() { Serial.begin(115200); while (!Serial) delay(10); if (!sensor.begin_I2C()) { Serial.println("Sensor not found!"); while (1); } } void loop() { sensors_event_t event; // Get sensor event (Adafruit Unified Sensor format) if (sensor.getEvent(&event)) { Serial.print("Sensor ID: "); Serial.println(event.sensor_id); Serial.print("Type: "); Serial.println(event.type); Serial.print("Timestamp: "); Serial.print(event.timestamp); Serial.println(" ms"); Serial.print("X: "); Serial.print(event.magnetic.x); Serial.println(" uT"); Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.println(" uT"); Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.println(" uT"); Serial.println(); } delay(500); } // Expected output: // Sensor ID: 90393 // Type: 2 // Timestamp: 1234 ms // X: -12.34 uT // Y: 45.67 uT // Z: -23.45 uT ``` -------------------------------- ### Configure Digital Low-Pass Filter Source: https://context7.com/adafruit/adafruit_mlx90393_library/llms.txt Use setFilter() to configure the digital low-pass filter for noise reduction. Higher values yield smoother readings but increase conversion time. Filter settings range from 0 (fastest) to 7 (smoothest). ```cpp #include "Adafruit_MLX90393.h" Adafruit_MLX90393 sensor = Adafruit_MLX90393(); void setup() { Serial.begin(115200); while (!Serial) delay(10); if (!sensor.begin_I2C()) { Serial.println("Sensor not found!"); while (1); } // Set digital filter (higher = smoother but slower) sensor.setFilter(MLX90393_FILTER_5); // Available filter options: // MLX90393_FILTER_0 - Fastest (1.27-5.30ms conversion time) // MLX90393_FILTER_1 // MLX90393_FILTER_2 // MLX90393_FILTER_3 // MLX90393_FILTER_4 // MLX90393_FILTER_5 - Good balance // MLX90393_FILTER_6 // MLX90393_FILTER_7 - Smoothest (25.65-200.37ms conversion time) Serial.print("Filter set to: "); Serial.println(sensor.getFilter()); } void loop() { float x, y, z; if (sensor.readData(&x, &y, &z)) { Serial.print("Magnetic field: "); Serial.print(x); Serial.print(", "); Serial.print(y); Serial.print(", "); Serial.println(z); } delay(100); } ``` -------------------------------- ### Configure ADC Resolution with setResolution() and getResolution() Source: https://context7.com/adafruit/adafruit_mlx90393_library/llms.txt Sets the ADC resolution per axis to balance precision and noise. Supports 16 to 19 bits. ```cpp #include "Adafruit_MLX90393.h" Adafruit_MLX90393 sensor = Adafruit_MLX90393(); void setup() { Serial.begin(115200); while (!Serial) delay(10); if (!sensor.begin_I2C()) { Serial.println("Sensor not found!"); while (1); } // Set resolution per axis (aim for ~0.3 uT/LSB sensitivity) sensor.setResolution(MLX90393_X, MLX90393_RES_17); sensor.setResolution(MLX90393_Y, MLX90393_RES_17); sensor.setResolution(MLX90393_Z, MLX90393_RES_16); // Available resolution options: // MLX90393_RES_16 - 16-bit resolution // MLX90393_RES_17 - 17-bit resolution // MLX90393_RES_18 - 18-bit resolution // MLX90393_RES_19 - 19-bit resolution (highest) // Verify resolution settings Serial.print("X Resolution: RES_"); Serial.println(16 + sensor.getResolution(MLX90393_X)); Serial.print("Y Resolution: RES_"); Serial.println(16 + sensor.getResolution(MLX90393_Y)); Serial.print("Z Resolution: RES_"); Serial.println(16 + sensor.getResolution(MLX90393_Z)); } void loop() { float x, y, z; if (sensor.readData(&x, &y, &z)) { Serial.print("X: "); Serial.print(x, 4); Serial.print(" Y: "); Serial.print(y, 4); Serial.print(" Z: "); Serial.println(z, 4); } delay(500); } ``` -------------------------------- ### Configure TRIG_INT Pin Mode Source: https://context7.com/adafruit/adafruit_mlx90393_library/llms.txt Use setTrigInt() to configure the TRIG_INT pin. Set to 'true' for interrupt output (INT) to signal new data, or 'false' for trigger input (TRIG). ```cpp #include "Adafruit_MLX90393.h" Adafruit_MLX90393 sensor = Adafruit_MLX90393(); void setup() { Serial.begin(115200); while (!Serial) delay(10); if (!sensor.begin_I2C()) { Serial.println("Sensor not found!"); while (1); } // Set pin to interrupt output mode (true = INT, false = TRIG) sensor.setTrigInt(true); // Enable interrupt output Serial.println("TRIG_INT pin configured as interrupt output"); // Or set to trigger input mode // sensor.setTrigInt(false); // Configure as trigger input } void loop() { float x, y, z; if (sensor.readData(&x, &y, &z)) { Serial.print("Data: "); Serial.print(x); Serial.print(", "); Serial.print(y); Serial.print(", "); Serial.println(z); } delay(100); } ``` -------------------------------- ### Data Acquisition Methods Source: https://context7.com/adafruit/adafruit_mlx90393_library/llms.txt Methods for retrieving magnetic field data from the sensor, including both blocking and non-blocking approaches. ```APIDOC ## readData() ### Description Performs a complete blocking measurement cycle on all three axes and returns calibrated values in microtesla (uT). ### Parameters #### Request Body - **x** (float*) - Required - Pointer to store X-axis value - **y** (float*) - Required - Pointer to store Y-axis value - **z** (float*) - Required - Pointer to store Z-axis value ### Response - **bool** - Returns true if measurement was successful. ``` ```APIDOC ## startSingleMeasurement() and readMeasurement() ### Description Non-blocking measurement workflow. `startSingleMeasurement` triggers the conversion, and `readMeasurement` retrieves the results after a delay. ### Parameters #### Request Body - **x** (float*) - Required - Pointer to store X-axis value - **y** (float*) - Required - Pointer to store Y-axis value - **z** (float*) - Required - Pointer to store Z-axis value ### Response - **bool** - Returns true if operation was successful. ``` -------------------------------- ### Read Magnetic Field Data with readData() Source: https://context7.com/adafruit/adafruit_mlx90393_library/llms.txt Performs a blocking measurement cycle to retrieve calibrated magnetic field values for all three axes. ```cpp #include "Adafruit_MLX90393.h" Adafruit_MLX90393 sensor = Adafruit_MLX90393(); void setup() { Serial.begin(115200); while (!Serial) delay(10); if (!sensor.begin_I2C()) { Serial.println("Sensor not found!"); while (1); } } void loop() { float x, y, z; // Read all three axes at once if (sensor.readData(&x, &y, &z)) { Serial.print("X: "); Serial.print(x, 4); Serial.println(" uT"); Serial.print("Y: "); Serial.print(y, 4); Serial.println(" uT"); Serial.print("Z: "); Serial.print(z, 4); Serial.println(" uT"); Serial.println(); } else { Serial.println("Unable to read XYZ data from the sensor."); } delay(500); } // Expected output: // X: -12.3456 uT // Y: 45.6789 uT // Z: -23.4567 uT ``` -------------------------------- ### Reset MLX90393 Sensor Source: https://context7.com/adafruit/adafruit_mlx90393_library/llms.txt Use reset() to perform a soft reset of the sensor, returning all registers to their default values. This is useful for recovering from error states or reinitializing the sensor. Note that a reset clears all previous settings. ```cpp #include "Adafruit_MLX90393.h" Adafruit_MLX90393 sensor = Adafruit_MLX90393(); void setup() { Serial.begin(115200); while (!Serial) delay(10); if (!sensor.begin_I2C()) { Serial.println("Sensor not found!"); while (1); } // Perform soft reset if (sensor.reset()) { Serial.println("Sensor reset successful"); } else { Serial.println("Reset failed!"); } // Reconfigure after reset (reset clears all settings) sensor.setGain(MLX90393_GAIN_1X); sensor.setResolution(MLX90393_X, MLX90393_RES_17); sensor.setResolution(MLX90393_Y, MLX90393_RES_17); sensor.setResolution(MLX90393_Z, MLX90393_RES_16); } void loop() { float x, y, z; if (sensor.readData(&x, &y, &z)) { Serial.print("X: "); Serial.println(x); } delay(500); } ``` -------------------------------- ### Exit Sensor Measurement Mode Source: https://context7.com/adafruit/adafruit_mlx90393_library/llms.txt Use exitMode() to cancel an ongoing measurement and return the sensor to an idle state. This function is called internally during initialization but can be manually invoked to stop a measurement. ```cpp #include "Adafruit_MLX90393.h" Adafruit_MLX90393 sensor = Adafruit_MLX90393(); void setup() { Serial.begin(115200); while (!Serial) delay(10); if (!sensor.begin_I2C()) { Serial.println("Sensor not found!"); while (1); } } void loop() { // Start a measurement sensor.startSingleMeasurement(); // Cancel the measurement by exiting the mode if (sensor.exitMode()) { Serial.println("Exited measurement mode"); } delay(1000); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.