Arduino
Using the TMAG5273 magnetometer with Arduino involves wiring up the magnetometer to your Arduino-compatible microcontroller, installing the Adafruit_TMAG5273 library and running the provided example code.
Wiring
Wire as shown for a 5V board like an Uno. If you are using a 3V board, like an Adafruit Feather, wire the board's 3V pin to the TMAG5273 VIN.
Here is an Adafruit Metro wired up to the TMAG5273 using the STEMMA QT connector:
-
Board 5V to breakout STEMMA VIN (red wire)
-
Board GND to breakout STEMMA GND (black wire)
-
Board SCL to breakout STEMMA SCL (yellow wire)
- Board SDA to breakout STEMMA SDA (blue wire)
Here is an Adafruit Metro wired up using a solderless breadboard:
-
Board 5V to breakout VIN (red wire)
-
Board GND to breakout GND (black wire)
-
Board SCL to breakout SCL (yellow wire)
- Board SDA to breakout SDA (blue wire)
Library Installation
You can install the Adafruit TMAG5273 library for Arduino using the Library Manager in the Arduino IDE.
Click the Manage Libraries ... menu item, search for Adafruit TMAG5273, and select the Adafruit TMAG5273 library:
If asked about dependencies, click "Install all".
If the "Dependencies" window does not come up, then you already have the dependencies installed.
/*!
* @file simpletest.ino
* @brief Simple test for the Adafruit TMAG5273 3-axis Hall-effect sensor
*
* Reads magnetic field (X, Y, Z in uT) and temperature, prints to serial.
*
* Written by Limor 'ladyada' Fried with assistance from Claude Code
* MIT license
*/
#include <Adafruit_TMAG5273.h>
Adafruit_TMAG5273 tmag;
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}
Serial.println(F("Adafruit TMAG5273 Simple Test"));
if (!tmag.begin()) {
Serial.println(F("Failed to find TMAG5273 sensor!"));
while (1) {
delay(10);
}
}
Serial.println(F("TMAG5273 found!"));
Serial.print(F("Manufacturer ID: 0x"));
Serial.println(tmag.getManufacturerID(), HEX);
Serial.print(F("Device ID: 0x"));
Serial.println(tmag.getDeviceID(), HEX);
bool is_x2 = (tmag.getDeviceID() & 0x03) == 0x02;
Serial.print(F("Variant: "));
Serial.println(is_x2 ? F("x2 (+/-133/266 mT)") : F("x1 (+/-40/80 mT)"));
}
void loop() {
float x = tmag.readMagneticX();
float y = tmag.readMagneticY();
float z = tmag.readMagneticZ();
float temp = tmag.getTemperature();
Serial.print(F("X: "));
Serial.print(x, 1);
Serial.print(F(" uT\tY: "));
Serial.print(y, 1);
Serial.print(F(" uT\tZ: "));
Serial.print(z, 1);
Serial.print(F(" uT\tT: "));
Serial.print(temp, 1);
Serial.println(F(" C"));
delay(100);
}
Upload the sketch to your board and open up the Serial Monitor (Tools -> Serial Monitor) at 115200 baud. You should see that the sketch has found your connected TMAG5273 sensor. It will also print out the variant type (A1 or A2). Then, you'll see the X, Y and Z readings along with the temperature reading printed to the Serial Monitor every second. This sensor is meant for sensing rotation or motion of an object nearby with a magnet embedded inside. It is not meant for measuring the magnetic field of the earth.
Page last edited March 24, 2026
Text editor powered by tinymce.