The TLx493D sensor operates based on the Hall effect. This principle states that when a current-carrying conductor is placed in a magnetic field, a voltage (Hall voltage) is generated perpendicular to both the current and the magnetic field. The TLx493D sensor integrates multiple Hall elements oriented to detect the magnetic field components in three orthogonal directions (X, Y, and Z).
Magnetic Field Measurement
- Hall Elements: The sensor contains three Hall elements arranged to measure the magnetic field along the X, Y, and Z axes.
- Signal Processing: The voltages generated by these Hall elements are processed by an internal signal processing unit to calculate the magnetic field strength in milliTesla (mT) for each axis.
Temperature Measurement
- Integrated Sensor: The TLx493D includes an integrated temperature sensor. This sensor measures the ambient temperature, which can be used for both environmental monitoring and temperature compensation of the magnetic field readings.
- Position Sensing: Used to detect the position of a magnet or magnetic object in 3D space, such as in contactless position sensing.
- Rotation Angle Measurement: Measures the rotational angle of a magnet or a rotating object, applicable in servo systems and motor controls.
- Proximity Sensing: Senses the presence or absence of magnetic objects, applicable in security systems and automation.
- Automotive Applications: Utilized in various automotive applications like gearshift position sensing, throttle position sensing, and steering angle measurement.
In this quick tutorial we will go through one of the XENSIV™ 3D Magnetic Sensors TLx493D library examples, which is available for all the sensors supported by this Arduino library. We will use the XMC 2Go as microcontroller, but you can choose any of the supported platforms in order to get this example working.
Hardware SetupFor this example we will use the I2C interface of the MCU. In order to connect the sensor properly to the XMC 2Go you have to solder pins to the XMC 2Go as well as to the XENSIV™ 3D Magnetic Sensor TLE493D-W2B6. For the XMC 2Go you need female pin headers and for the sensor you need male pin headers. If the soldering is done we can stack the XENSIV™ 3D Magnetic Sensor TLE493D-W2B6 on top of the XMC 2Go. Please look at the picture below in order to plug the sensor correctly into the XMC 2Go.
Here is another picture how the complete setup should look like.
If that is done you just have to connect the USB cable to the XMC 2Go as well as to the computer and get ready to use your magnet.
Software Installation1. Get started with Arduino IDE and XMC Boards
For this, check our documentation to help you install Arduino IDE and the XMC Boards!
2. Install the library
In the Arduino IDE, go to the menu Sketch > Include Library > Library Manager. Type XENSIV 3D Magnetic Sensor TLx493D and wait for the tool to find it. Click on install and give the IDE a moment to install the library.
Get Started!With everything prepared we’re ready to flash the first library example to the microcontroller. In order to do that you have to do the following steps in the Arduino IDE:
1. Select the right board
Once you have installed the XMC board family, you can select one of the supported boards from the menu: Tools > Board > Infineon’s XMC Microcontroller.
Or you can select the board in the drop down menu, if you’re using the new Arduino IDE. Here, you have to click on Select other board and port….
In our case we have to choose the XMC1400 XMC2Go Kit in order to select the correct board. Depending on which XMC-for-Arduino version you have installed the selection in the board menu can look a bit different.
2. Open the example
With the library installed in the Arduino IDE you can simply open one of the examples that are part of the library. To do that you have to go to the menu: File > Examples > XENSIV 3D Magnetic Sensor TLx493D and choose the example read_iic_sensor.
3. Build and run the example
Please make sure that you have chosen the right COM port. You can choose it from the menu: Tool > Port or again from the drop down menu (new Arduino IDE). If you’re not sure that you have selected the correct COM port you can do a simple trick. Just remove the USB cable from your computer and check all the available COM ports. With that done connect the board again and check the available COM ports again. There should be a new one now that has not been there before, this port is the right one to choose.
Now we use the verify button to check if the code is correct and compiles without errors. If the compilation is successful you can upload the sketch via the upload button to the XMC 2Go.
After the upload is complete open the serial monitor. If you just see a bunch of strange symbols, you probably forgot to choose the correct baudrate for the serial terminal. You can find the needed baudrate inside the Serial.begin()
function call of the Arduino-Sketch.
If everything is correctly set, you should see an output similar to this.
- Magnetic Field Values (Bx, By, Bz): Show the magnetic field strength along the three axes.
- Temperature (Temp, Temp2): Display the temperature readings.
- Diagnostic (Diag): Indicates the internal state or errors.
- Thresholds (XL, XH, YL, YH, ZL, ZH): Define the limits for detecting magnetic fields.
- Wake-Up Status (WU): Indicates power or wake-up conditions.
- Timing and Configuration (TMode, TPhase, Config): Show how the sensor is set up.
- Modes (MOD1, MOD2): Define specific operational modes.
- Version (Ver): Specifies the version of the sensor's firmware or hardware.
Now we will have a little breakdown of the library read_iic_sensor which is an example that demonstrates how you can read out data from one sensor with I2C.
#include "TLx493D_inc.hpp"
using namespace ifx::tlx493d;
Here, the code includes a header file TLx493D_inc.hpp
that contain declarations and functions for working with the TLx493D magnetic sensor family. The using namespace ifx::tlx493d;
directive allows you to use the functions and classes from the ifx::tlx493d
namespace without having to prefix them with the namespace name each time.
const uint8_t POWER_PIN = 15; // XMC1400 : LED2
TLx493D_W2B6 dut(Wire, TLx493D_IIC_ADDR_A0_e);
POWER_PIN
is defined to be pin 15, which is used to power the sensor.dut
is an instance of theTLx493D_W2B6
class, representing the specific sensor model. It is initialized with theWire
library and an I2C address,TLx493D_IIC_ADDR_A0_e
.
void setup() {
Serial.begin(115200);
delay(3000);
dut.setPowerPin(POWER_PIN, OUTPUT, INPUT, HIGH, LOW, 0, 250000);
dut.begin();
Serial.print("setup done.\n");
}
Serial.begin(115200);
initializes serial communication at a baud rate of 115200.delay(3000);
introduces a delay of 3 seconds to ensure everything is stable before proceeding.dut.setPowerPin(...)
sets up the power pin for the sensor with specific configurations. The parameters define pin modes and delays.dut.begin();
initializes the sensor.Serial.print("setup done.\n");
outputs a message indicating that the setup is complete.
void loop() {
double t, x, y, z;
dut.setSensitivity(TLx493D_FULL_RANGE_e);
dut.getMagneticFieldAndTemperature(&x, &y, &z, &t);
dut.printRegisters();
Serial.print("\nTemperature is: ");
Serial.print(t);
Serial.println("°C");
Serial.print("Value X is: ");
Serial.print(x);
Serial.println(" mT");
Serial.print("Value Y is: ");
Serial.print(y);
Serial.println(" mT");
Serial.print("Value Z is: ");
Serial.print(z);
Serial.println(" mT");
Variable Declarations: double t, x, y, z;
declares variables to store temperature and magnetic field values.
Read and Print Sensor Data:
dut.setSensitivity(TLx493D_FULL_RANGE_e);
sets the sensor's sensitivity to full range.dut.getMagneticFieldAndTemperature(&x, &y, &z, &t);
retrieves the magnetic field values in X, Y, Z directions, and temperature.dut.printRegisters();
prints the sensor registers to the serial monitor.- Several
Serial.print
statements print the temperature and magnetic field values to the serial monitor.
dut.setSensitivity(TLx493D_SHORT_RANGE_e);
dut.getMagneticFieldAndTemperature(&x, &y, &z, &t);
dut.printRegisters();
Serial.print("\nTemperature is: ");
Serial.print(t);
Serial.println("°C");
Serial.print("Value X is: ");
Serial.print(x);
Serial.println(" mT");
Serial.print("Value Y is: ");
Serial.print(y);
Serial.println(" mT");
Serial.print("Value Z is: ");
Serial.print(z);
Serial.println(" mT");
Serial.print("\n\n\n\n");
Read and Print Sensor Data with Short Range Sensitivity:
dut.setSensitivity(TLx493D_SHORT_RANGE_e);
sets the sensor's sensitivity to short range.dut.getMagneticFieldAndTemperature(&x, &y, &z, &t);
retrieves the sensor data again.- Similar to the previous block, the sensor data is printed to the serial monitor
Using multiple magnet sensors simultaneously offers you far more possibilities for realizing interesting and complex projects.
So to jump directly into the code, you can declare your three sensors you used (in our case the TLE493D_W2B6) like this:
TLx493D_W2B6 dut1(Wire, TLx493D_IIC_ADDR_A0_e);
TLx493D_W2B6 dut2(Wire, TLx493D_IIC_ADDR_A0_e);
TLx493D_W2B6 dut3(Wire, TLx493D_IIC_ADDR_A0_e);
The setup
function initializes the serial communication at a baud rate of 115200 and includes a delay of 3 seconds to allow for any necessary startup time.
void setup() {
Serial.begin(115200);
delay(3000);
Power pins are set for all three sensors, this ensures that the sensors are powered correctly before initialization.
dut1.setPowerPin(8, OUTPUT, INPUT, HIGH, LOW, 0, 250000); // XMC4700 Relax P1.10
dut2.setPowerPin(9, OUTPUT, INPUT, HIGH, LOW, 0, 250000); // XMC4700 Relax XMC P1.11
dut3.setPowerPin(10, OUTPUT, INPUT, HIGH, LOW, 0, 250000); // XMC4700 Relax XMC P3.10
Next the interresting part - we initialize each of the three magnet sensors individually. First, we start dut1
with address A0
and then change its address to A2
. This ensures that dut1
is the only sensor listening on address A2
.
Next, we start dut2
with address A0
and then change its address to A1
. This way, dut2
is the only sensor listening on address A1
.
Finally, we start dut3
with address A0
and leave it at that. At no point in time do any two sensors listen on the same address, which is crucial to prevent conflicts and ensure proper communication.
//Start dut1 with A0 and change address to A2; dut2 and 3 still off
dut1.begin(true, false, false, true);
dut1.setIICAddress(TLx493D_IIC_ADDR_A2_e);
dut1.printRegisters();
//Start dut2 with A0 and change address to A1
dut2.begin();
dut2.setIICAddress(TLx493D_IIC_ADDR_A1_e);
dut2.printRegisters();
//Start dut3 at A0 and leave it at that; at no time where any 2 sensors are listening at the same address !
dut3.begin();
dut3.printRegisters();
Serial.println("setup done.\n");
}
Now, it's time to get down to business! In the loop()
function, we're declaring a bunch of variables to store the temperature and 3D magnetic field values from each of our three sensors, getting ready to unleash a torrent of data and insights!
void loop() {
double temp1 = 0.0, temp2 = 0.0, temp3 = 0.0;
double valX1 = 0, valY1 = 0, valZ1 = 0, valX2 = 0, valY2 = 0, valZ2 = 0, valX3 = 0, valY3 = 0, valZ3 = 0;
The sensors are now awakened! We're calling the getMagneticFieldAndTemperature()
function for each sensor, which retrieves the 3D magnetic field values and temperature data, and stores them in our variables. And, just to be thorough, we're also printing out the sensor registers to keep an eye on their internal state.
dut1.getMagneticFieldAndTemperature(&valX1, &valY1, &valZ1, &temp1);
dut1.printRegisters();
dut2.getMagneticFieldAndTemperature(&valX2, &valY2, &valZ2, &temp2);
dut2.printRegisters();
dut3.getMagneticFieldAndTemperature(&valX3, &valY3, &valZ3, &temp3);
dut3.printRegisters();
The temperature and magnetic field values for each sensor are printed to the serial monitor in a formatted manner.
Serial.println("========================================");
Serial.print("Temperature of Sensor 1:\t");Serial.print(temp1);Serial.println(" °C");
Serial.print("Magnetic X-Value of Sensor 1:\t");Serial.print(valX1);Serial.println(" mT");
Serial.print("Magnetic Y-Value of Sensor 1:\t");Serial.print(valY1);Serial.println(" mT");
Serial.print("Magnetic Z-Value of Sensor 1:\t");Serial.print(valZ1);Serial.println(" mT");
Serial.println("----------------------------------------");
Serial.print("Temperature of Sensor 2:\t");Serial.print(temp2);Serial.println(" °C");
Serial.print("Magnetic X-Value of Sensor 2:\t");Serial.print(valX2);Serial.println(" mT");
Serial.print("Magnetic Y-Value of Sensor 2:\t");Serial.print(valY2);Serial.println(" mT");
Serial.print("Magnetic Z-Value of Sensor 2:\t");Serial.print(valZ2);Serial.println(" mT");
Serial.println("----------------------------------------");
Serial.print("Temperature of Sensor 3:\t");Serial.print(temp3);Serial.println(" °C");
Serial.print("Magnetic X-Value of Sensor 3:\t");Serial.print(valX3);Serial.println(" mT");
Serial.print("Magnetic Y-Value of Sensor 3:\t");Serial.print(valY3);Serial.println(" mT");
Serial.print("Magnetic Z-Value of Sensor 3:\t");Serial.print(valZ3);Serial.println(" mT");
Serial.println("========================================\n\n");
A delay of 2 seconds is introduced before the loop repeats, to control the frequency of data reading and printing.
delay(2000);
}
What’s next?Now that you know how to use your 3D Magnetic sensor correctly, embark in a journey of creativity and innovation to built an exciting project!
Comments
Please log in or sign up to comment.