Skip to content

Angle Calculation Example

Description

The Example3_AngleCalculations.ino example file can be accessed from the File > Examples > SparkFun TMAG5273 Arduino Library > Example3_AngleCalculations drop-down menu. This example demonstrates one of the advanced features of the TMAG5273; it enables a CORDIC engine to calculate the angle of a magnet rotating about the z-axis.

Example3_AngleCalculations.ino
Code 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)
#include <Wire.h>            // Used to establish serial communication on the I2C bus
#include "SparkFun_TMAG5273_Arduino_Library.h" // Used to send and recieve specific information from our sensor

// Create a new sensor object
TMAG5273 sensor; 

// I2C default address
uint8_t i2cAddress = TMAG5273_I2C_ADDRESS_INITIAL;

// Set constants for setting up device
uint8_t conversionAverage = TMAG5273_X32_CONVERSION;
uint8_t magneticChannel = TMAG5273_XYX_ENABLE;
uint8_t angleCalculation = TMAG5273_XY_ANGLE_CALCULATION;

void setup() 
{
  Wire.begin();
  // Start serial communication at 115200 baud
  Serial.begin(115200);   

  // Begin example of the magnetic sensor code (and add whitespace for easy reading)
  Serial.println("TMAG5273 Example 3: Angle Calculations");
  Serial.println("");
  // If begin is successful (0), then start example
  if(sensor.begin(i2cAddress, Wire) == true)
  {
    Serial.println("Begin");
  }
  else // Otherwise, infinite loop
  {
    Serial.println("Device failed to setup - Freezing code.");
    while(1); // Runs forever
  }

  // Set the device at 32x average mode 
  sensor.setConvAvg(conversionAverage);

  // Choose new angle to calculate from
  // Can calculate angles between XYX, YXY, YZY, and XZX
  sensor.setMagneticChannel(magneticChannel);

  // Enable the angle calculation register
  // Can choose between XY, YZ, or XZ priority
  sensor.setAngleEn(angleCalculation);

}


void loop() 
{
  if((sensor.getMagneticChannel() != 0) && (sensor.getAngleEn() != 0)) // Checks if mag channels are on - turns on in setup
  {
    float angleCalculation = sensor.getAngleResult();

    Serial.print("XYX: ");
    Serial.print(angleCalculation, 4);
    Serial.println("°");

  }
  else
  {
    Serial.println("Mag Channels disabled, stopping..");
    while(1);
  }

  delay(1000);
}

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 sensor connected to a RedBoard Plus

The mini sensor connected to a RedBoard Plus

The Qwiic Hall-Effect Sensor boards are connected to a RedBoard Plus, with a Qwiic cable.

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 enables the CORDIC engine to calculate the angle of a magnet rotating about the z-axis, which is then displayed in the Serial Monitor.

Data stream in the serial monitor

The angle of the magnet is streamed from the TMAG5273 sensor into the Serial Monitor.

Tip

For this example to work, users should rotate a magnet's polarity about the z-axis of the TMAG5273 hall-effect sensor. Similar to how the magnetic knob is used for this stove top:

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.