Once the Arduino library has been installed, the Example1_BasicReadings.ino example file can be accessed from the File > Examples > SparkFun TMAG5273 Arduino Library > Example1_BasicReadings drop-down menu. This example reads the raw magnetic flux (mT) and temperature (°C) data from the TMAG5273 sensor through the I2C interface and displays them in the Serial Monitor.
Example1_BasicReadings.inoCode Verification
This code was last verified to be functional under the following parameters:
Arduino IDE Version: 2.2.1
Arduino Library Version: 1.0.3
Hardware Platform:
- SparkFun RedBoard Plus
- SparkFun Linear 3D Hall-Effect Sensor - TMAG5273 (Qwiic)
- SparkFun Mini Linear 3D Hall-Effect Sensor - TMAG5273 (Qwiic)
/* * --------------------------------------------------------------------------------- * Copyright (c) 2025, SparkFun Electronics Inc. * * SPDX-License-Identifier: MIT * --------------------------------------------------------------------------------- *//* * Example 1: Basic Readings * ------------------------- * This example shows the most basic way to read magnetic and temperature data from the TMAG5273 sensor. * The example initializes the sensor, then continuously reads and prints the X, Y, Z magnetic field * values along with the temperature to the Serial Monitor. * * Hardware Connections: * - Connect the TMAG5273 sensor to the SparkFun Qwiic connector on your SparkFun microcontroller board. * * Note: Make sure to install the SparkFun TMAG5273 Arduino Library before running this example. * You can install it via the Arduino Library Manager or download it from: * https://github.com/sparkfun/SparkFun_TMAG5273_Arduino_Library * */#include"SparkFun_TMAG5273_Arduino_Library.h" // Used to send and recieve specific information from our sensor#include<Wire.h> // Used to establish serial communication on the I2C busTMAG5273sensor;// Initialize hall-effect sensor// I2C default addressuint8_ti2cAddress=TMAG5273_I2C_ADDRESS_INITIAL;voidsetup(){delay(1000);Wire.begin();// Start serial communication at 115200 baudSerial.begin(115200);// Begin example of the magnetic sensor code (and add whitespace for easy reading)Serial.println("");Serial.println("------------------------------------------------------------------");Serial.println("TMAG5273 Example 1: Basic Readings");Serial.println("------------------------------------------------------------------");Serial.println("");// If begin is successful (0), then start exampleif(sensor.begin(i2cAddress,Wire)==1){Serial.println("Begin");}else// Otherwise, infinite loop{Serial.println("Device failed to setup - Freezing code.");while(1);// Runs forever}}voidloop(){// Checks if mag channels are on - turns on in setupif(sensor.getMagneticChannel()!=0){sensor.setTemperatureEn(true);floatmagX=sensor.getXData();floatmagY=sensor.getYData();floatmagZ=sensor.getZData();floattemp=sensor.getTemp();Serial.print("Data - Magnetic: [ X: ");Serial.print(magX);Serial.print(", Y: ");Serial.print(magY);Serial.print(", Z: ");Serial.print(magZ);Serial.print(" ] mT, Temp: ");Serial.print(temp);Serial.println(" C");}else{// If there is an issue, stop the magnetic readings and restart sensor/exampleSerial.println("Mag Channels disabled, stopping..");while(1);}delay(300);}
Hardware Connections
For this example, users simply need to connect their Qwiic Hall-Effect Sensor board to their microcontroller, utilizing the I2C interface. With the recommended hardware, users can easily connect their boards with the Qwiic connection system.
*The Qwiic Hall-Effect Sensor boards are connected to a [RedBoard Plus](https://www.sparkfun.com/products/18158), with a [Qwiic cable](https://www.sparkfun.com/products/15081).*
Pin Connections
For users with a development board without a Qwiic connector, the table below illustrates the required pin connections. Make sure that the logic-level of the sensor is compatible with the development board that is being connected.
Sensor Pin
Microcontroller Pin
RedBoard/Uno
SCL
I2C - Serial Clock
SCL/A5
SDA
I2C - Serial Data
SDA/A4
3V3
Power: 1.7 to 3.6V
3.3V
GND
Ground
GND
Serial Monitor
This example reads the magnetic flux (mT) and temperature values (°C) from the TMAG5273 sensor and displays them in the Serial Monitor.
The magnetic flux (mT) and temperature (°C) values streamed from the TMAG5273 sensor into the Serial Monitor.
Tip
For this example to work, users will need to move a magnet near the sensor.
Warning
The casing of rare Earth magnets is often conductive. Users should take precautions to avoid shorting out the components or electrical contacts with these types of magnets.