Arduino driver for the MLX90393 3-axis magnetometer sensor.
The MLX90393 is a high-performance magnetic field sensor with an I²C interface, capable of measuring magnetic field strength in X, Y, and Z axes. It is suitable for applications like compasses, position sensing, and industrial measurements.
This library provides a simple interface for configuring the sensor and reading magnetic field values in microtesla (µT).
- 3-axis magnetic field measurement (X, Y, Z)
- Raw data reading
- Configurable gain
- Resolution control (16–19 bit)
- Oversampling (OSR) configuration
- Digital filter configuration
- Offset calibration support
- Register-level access (advanced users)
- ESP32 custom I²C pin support
- Arduino UNO / Mega
- ESP32
- Any board supporting the Wire (I²C) library
Supported sensor:
7Semi 3-Axis Magnetometer I2C Breakout - MLX90393
The MLX90393 communicates using I²C.
| MLX90393 Pin | MCU Pin | Description |
|---|---|---|
| VCC | 3.3V | Sensor power |
| GND | GND | Ground |
| SCL | SCL | I²C clock |
| SDA | SDA | I²C data |
Default sensor address:
0x0C
Recommended I²C speed:
100 kHz – 400 kHz
- Open Arduino IDE
- Go to Library Manager
- Search for 7Semi MLX90393
- Click Install
- Download this repository as ZIP
- Arduino IDE → Sketch → Include Library → Add .ZIP Library
#include <7Semi_MLX90393.h>
MLX90393_7Semi mag;
void setup()
{
Serial.begin(115200);
if(!mag.begin())
{
Serial.println("MLX90393 not detected");
while(1);
}
}
void loop()
{
float x, y, z;
if(mag.readMag(x, y, z))
{
Serial.print("X: ");
Serial.print(x);
Serial.print(" Y: ");
Serial.print(y);
Serial.print(" Z: ");
Serial.println(z);
}
delay(500);
}
float x, y, z;
mag.readMag(x, y, z);
Serial.print("X: ");
Serial.print(x);
Serial.print(" Y: ");
Serial.print(y);
Serial.print(" Z: ");
Serial.println(z);
Returns magnetic field values in microtesla (µT).
int16_t x, y, z;
mag.readMagRaw(x, y, z);
Serial.print("Raw X: ");
Serial.println(x);
Returns raw ADC values.
mag.setGain(MLX90393_GAIN_0_751);
Available gain settings:
MLX90393_GAIN_0_751
MLX90393_GAIN_0_601
MLX90393_GAIN_0_451
MLX90393_GAIN_0_376
MLX90393_GAIN_0_300
MLX90393_GAIN_0_250
MLX90393_GAIN_0_200
MLX90393_GAIN_0_150
mag.setResolution(
MLX90393_RES_19BIT,
MLX90393_RES_19BIT,
MLX90393_RES_19BIT);
Resolution options:
MLX90393_RES_16BIT
MLX90393_RES_17BIT
MLX90393_RES_18BIT
MLX90393_RES_19BIT
Higher resolution increases precision but reduces speed.
mag.setOSR(MLX90393_OSR_3);
Available OSR settings:
MLX90393_OSR_0
MLX90393_OSR_1
MLX90393_OSR_2
MLX90393_OSR_3
Higher OSR improves accuracy but slows down measurements.
mag.setFilter(MLX90393_FILTER_7);
Available filter settings:
MLX90393_FILTER_0 to MLX90393_FILTER_7
Higher filter values provide smoother readings.
mag.setOffset(10, -5, 3);
Used for zero-field calibration.
Typical applications include:
- Digital compass
- Position sensing
- Magnetic field detection
- Industrial monitoring
- Robotics orientation
- Automotive sensing
MIT License
7Semi