Wiring

Hooking up the MLX90393 to your Feather or Arduino is easy:

  • If you are running a Feather (3.3V), connect the 3V pin to 3.3V on the MLX90393 (red wire on STEMMA QT version)
  • If you are running a 5V Arduino (Uno, etc.), connect 5V to 5V on the MLX90393 (red wire on STEMMA QT version)
  • Connect GND on the MCU to GND on the MLX90393 (black wire on STEMMA QT version)
  •  Arduino SCL to MLX90393 SCL (yellow wire on STEMMA QT version)
  • Arduino SDA to MLX90393 SDA (blue wire on STEMMA QT version)

The final results should resemble the illustration above, showing an Adafruit Metro development board.

Only the SCL and SDA pins on the MLX90393 are level shifted and safe to use on 5V systems like the Arduino Uno. If you are using other pins on the breakout (INT, etc.) on a 5V system, you will need to level shift these yourself. We have some tutorials on how to do this in the learning system, simply search for 'level shifting'!

Installation

You can install the Adafruit MLX90393 Library for Arduino using the Library Manager in the Arduino IDE:

Click the Manage Libraries ... menu item, search for Adafruit MLX90393, and select the Adafruit MLX90393 library:

Load Example

Open up File -> Examples -> Adafruit MLX90393 -> basicdemo and upload to your Arduino wired up to the sensor

Upload the sketch to your board and open up the Serial Monitor (Tools->Serial Monitor). You should see the temperature in magnetic field values for X/Y/Z.

Example Code

The following example code is part of the standard library, but illustrates how you can retrieve sensor data from the MLX90393 for the X, Y and Z axis:

#include "Adafruit_MLX90393.h"

Adafruit_MLX90393 sensor = Adafruit_MLX90393();
#define MLX90393_CS 10

void setup(void)
{
  Serial.begin(115200);

  /* Wait for serial on USB platforms. */
  while (!Serial) {
      delay(10);
  }

  Serial.println("Starting Adafruit MLX90393 Demo");

  if (! sensor.begin_I2C()) {          // hardware I2C mode, can pass in address & alt Wire
  //if (! sensor.begin_SPI(MLX90393_CS)) {  // hardware SPI mode
    Serial.println("No sensor found ... check your wiring?");
    while (1) { delay(10); }
  }
  Serial.println("Found a MLX90393 sensor");

  sensor.setGain(MLX90393_GAIN_1X);
  // You can check the gain too
  Serial.print("Gain set to: ");
  switch (sensor.getGain()) {
    case MLX90393_GAIN_1X: Serial.println("1 x"); break;
    case MLX90393_GAIN_1_33X: Serial.println("1.33 x"); break;
    case MLX90393_GAIN_1_67X: Serial.println("1.67 x"); break;
    case MLX90393_GAIN_2X: Serial.println("2 x"); break;
    case MLX90393_GAIN_2_5X: Serial.println("2.5 x"); break;
    case MLX90393_GAIN_3X: Serial.println("3 x"); break;
    case MLX90393_GAIN_4X: Serial.println("4 x"); break;
    case MLX90393_GAIN_5X: Serial.println("5 x"); break;
  }

  // Set resolution, per axis. Aim for sensitivity of ~0.3 for all axes.
  sensor.setResolution(MLX90393_X, MLX90393_RES_17);
  sensor.setResolution(MLX90393_Y, MLX90393_RES_17);
  sensor.setResolution(MLX90393_Z, MLX90393_RES_16);

  // Set oversampling
  sensor.setOversampling(MLX90393_OSR_3);

  // Set digital filtering
  sensor.setFilter(MLX90393_FILTER_5);
}

void loop(void) {
  float x, y, z;

  // get X Y and Z data 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");
  } else {
      Serial.println("Unable to read XYZ data from the sensor.");
  }

  delay(500);

  /* Or....get a new sensor event, normalized to uTesla */
  sensors_event_t event;
  sensor.getEvent(&event);
  /* Display the results (magnetic field is measured in uTesla) */
  Serial.print("X: "); Serial.print(event.magnetic.x);
  Serial.print(" \tY: "); Serial.print(event.magnetic.y);
  Serial.print(" \tZ: "); Serial.print(event.magnetic.z);
  Serial.println(" uTesla ");

  delay(500);
}

You should get something resembling the following output when you open the Serial Monitor at 9600 baud:

Setting the Gain

The driver will default to 1x gain, but if you wish to adjust the gain you can do so using the `setGain(enum mlx90393_gain gain)` function, passing in one of the following values:

enum  mlx90393_gain {
    MLX90393_GAIN_5X          = (0x00),
    MLX90393_GAIN_4X,
    MLX90393_GAIN_3X,
    MLX90393_GAIN_2_5X,
    MLX90393_GAIN_2X,
    MLX90393_GAIN_1_67X,
    MLX90393_GAIN_1_33X,
    MLX90393_GAIN_1X
};

For example, to set the gain to 2x you would call the function as follows:

sensor.setGain(MLX90393_GAIN_2X);

Resolution is managed internally in the driver itself, and defaults to the maximum value of +/- 2^15 LSBs.

This guide was first published on Feb 13, 2019. It was last updated on Mar 29, 2024.

This page (Arduino) was last updated on Mar 29, 2024.

Text editor powered by tinymce.