Infineon Team
Published © MIT

How to master XENSIV™ 3D Magnetic Sensing with Arduino

Get ready to find all the necessary information to get started with the TLx493D sensor family!

BeginnerProtip2 hours638
How to master XENSIV™ 3D Magnetic Sensing with Arduino

Things used in this project

Hardware components

3D Magnetic Sensor 2Go
Infineon 3D Magnetic Sensor 2Go
×1
Infineon XENSIV™ TLV493D-A1B6 Shield2Go
Infineon XENSIV™ TLV493D-A1B6 Shield2Go
×1
Infineon XENSIV™ - TLE493D-W2B6 A0
Infineon XENSIV™ - TLE493D-W2B6 A0
×1
XENSIV™ - TLE493D-P2B6 A0
Infineon XENSIV™ - TLE493D-P2B6 A0
×1
Infineon XMC1400 2GO KIT
Infineon XMC1400 2GO KIT
×1
Magnet
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Code

read_3_different_iic_sensor.ino

Arduino
/** Project CPP includes. */ 
#include "TLx493D_inc.hpp"


using namespace ifx::tlx493d;


/** Declaration of three different sensor objects, 2 sensors of the second generation
 *  and 1 of the 3rd generation. 
 *  The 3rd generation sensor has a fixed address A0. Although A0 for the 3rd
 *  generation maps to a different IIC address than for the second generation,
 *  addresses for the 2nd generation sensors are changed to A1/A2 for demonstration purposes.
 * 
 *  ATTENTION
 *  All Kit2Go/S2Go boards have pull-up resistors attached to the IIC SCL and SDA lines.
 *  In case of sensors dut2 and dut3 these resistors actually pull-down the SCL/SDA lines while
 *  the sensors are NOT powered. Therefore the pull-up resistors of dut2 and dut3 have to be removed
 *  for this example to work. Otherwise not even dut1 can be initialized properly. Alternatively,
 *  pull-up resistors for all devices are removed and external pull-ups are added to SCL/SDA.
 *  This will have the additional benefit that the order of powering up the devices becomes irrelevant.
*/

TLx493D_P3B6 dut1(Wire, TLx493D_IIC_ADDR_A0_e);
TLx493D_W2BW dut2(Wire, TLx493D_IIC_ADDR_A0_e);
TLx493D_W2B6 dut3(Wire, TLx493D_IIC_ADDR_A0_e);


void setup() {
    Serial.begin(115200);
    delay(3000);

    /** The second generation is critical with respect to interrupts, so you have
     *  to make sure to initialize these sensors first by powering them up one by one
     *  and disabling interrupts in the sensor's begin() method.
     *  Otherwise an interrupt may be triggered on the IIC SCL line stalling the sensor's
     *  communication interface. 
     */
    dut2.setPowerPin(8, OUTPUT, INPUT, HIGH, LOW, 0, 250000);
    dut3.setPowerPin(9, OUTPUT, INPUT, HIGH, LOW, 0, 250000);

    /** Initialize sensor 1 (address cannot be changed). */
    dut1.begin();

    dut2.begin();
    dut2.setIICAddress(TLx493D_IIC_ADDR_A1_e);

    dut3.begin();
    dut3.setIICAddress(TLx493D_IIC_ADDR_A2_e);

    Serial.print("setup done.\n");
}

/** In the loop we're reading out the temperature values as well as the magnetic values in X, Y, Z-direction 
 *  of all three sensors. After that they're all printed to the serial output.
 */
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;

    dut1.getTemperature(&temp1); 
    dut2.getTemperature(&temp2); 
    dut3.getTemperature(&temp3); 

    dut1.getMagneticField(&valX1, &valY1, &valZ1);
    dut2.getMagneticField(&valX2, &valY2, &valZ2);
    dut3.getMagneticField(&valX3, &valY3, &valZ3);

    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");
   
    delay(2000);
}

read_iic_sensor.ino

Arduino
/** Project CPP includes. */
#include "TLx493D_inc.hpp"


using namespace ifx::tlx493d;


/* Definition of the power pin and sensor objects for Kit2Go XMC1100 boards. */
const uint8_t POWER_PIN = 15; // XMC1100 : LED2

// TLx493D_A1B6 dut(Wire, TLx493D_IIC_ADDR_A0_e);

// TLx493D_A2B6 dut(Wire, TLx493D_IIC_ADDR_A0_e);
TLx493D_P2B6 dut(Wire, TLx493D_IIC_ADDR_A0_e);
// TLx493D_W2B6 dut(Wire, TLx493D_IIC_ADDR_A0_e);


/** Definition of the power pin and sensor objects for S2Go with XMC4700 Relax Lite board. */
// const uint8_t POWER_PIN = 8; // XMC : P1.10

// TLx493D_A1B6 dut(Wire, TLx493D_IIC_ADDR_A0_e);

// TLx493D_A2BW dut(Wire, TLx493D_IIC_ADDR_A0_e);
// TLx493D_W2B6 dut(Wire, TLx493D_IIC_ADDR_A0_e);
// TLx493D_W2BW dut(Wire, TLx493D_IIC_ADDR_A0_e);

// TLx493D_P3B6 dut(Wire, TLx493D_IIC_ADDR_A0_e);
// TLx493D_P3B6 dut(Wire, TLx493D_IIC_ADDR_A1_e);


/** Definition of the power pin and sensor objects for Arduino Uno boards */
/** Care must be taken to level shift down to 3.3V as the sensor boards expect only 3.3V ! */
/** Therefore disabled for here. */
// const uint8_t POWER_PIN = 7;

// TLx493D_W2B6 dut(Wire, TLx493D_IIC_ADDR_A0_e);
// TLx493D_W2BW dut(Wire, TLx493D_IIC_ADDR_A0_e);


/** Definition of a counter variable. */
uint8_t count = 0;


void setup() {
    Serial.begin(115200);
    delay(3000);

    /** Definition of the power pin to power up the sensor. */
    /** Set delay after power-on to 50 for A1B6 Kit2Go sensor. */
    dut.setPowerPin(POWER_PIN, OUTPUT, INPUT, HIGH, LOW, 0, 250000);
    dut.begin();

    Serial.print("setup done.\n");
}


/** In the loop we continuously reading the temperature value as well as the
 *  magnetic values in X, Y, Z-direction of the sensor and printing them to
 *  the serial monitor
 */
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");

    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");

    delay(1000);

    Serial.print("count : ");
    Serial.println(count);

    if( ++count == 4 ) {
        Serial.println("\nBefore reset -------------------------------------------------------------------------------------------------------");

        /** Reset does not work for W2BW : either drive strength too low or delay to stabilize critical. */
        dut.reset(true);

        Serial.println("\nAfter reset -------------------------------------------------------------------------------------------------------");
        count = 0;
    }
}

read_3_euall_iic_sensor.ino

Arduino
/** Project CPP includes. */ 
#include "TLx493D_inc.hpp"


using namespace ifx::tlx493d;


/** Declaration of three sensor objects with the same default address (A0).
 * 
 *  ATTENTION
 *  All Kit2Go/S2Go boards have pull-up resistors attached to the IIC SCL and SDA lines.
 *  In case of sensors dut2 and dut3 these resistors actually pull-down the SCL/SDA lines while
 *  the sensors are NOT powered. Therefore the pull-up resistors of dut2 and dut3 have to be removed
 *  for this example to work. Otherwise not even dut1 can be initialized properly. Alternatively,
 *  pull-up resistors for all devices are removed and external pull-ups are added to SCL/SDA.
 */
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);


void setup() {
    Serial.begin(115200);
    delay(3000);

    /** In this example we're using the XMC4700-Relax-Kit. 
     *  Here we're using three GPIOs to power up the sensors, one
     *  after the other. This is done by defining the pins with the
     *  Board Support Class and its functions. The power up pins are
     *  enabled during the begin() call to the sensor object as done below. 
    */
    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

    /** Here we're enabling one sensor after another. This procedure 
     *  is necessary in order to avoid an interrupt to occur on the SCL line before
     *  interrupts are disabled in the sensor's begin() method. Additionally, the IIC address
     *  is changed from the default A0 to a distinct one for each sensor.
     */
    dut1.begin(true, false, false, true);
    dut1.setIICAddress(TLx493D_IIC_ADDR_A2_e);
    dut1.printRegisters();

    dut2.begin();
    dut2.setIICAddress(TLx493D_IIC_ADDR_A1_e);
    dut2.printRegisters();

    dut3.begin();
    dut3.printRegisters();

    Serial.println("setup done.\n");
}

/** In the loop we're reading out the temperature value as well as the magnetic values in X, Y, Z-direction 
 *  of all three sensors. After that they're all printed to the serial output.
 */
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;

    dut1.getMagneticFieldAndTemperature(&valX1, &valY1, &valZ1, &temp1);
    dut1.printRegisters();
    dut2.getMagneticFieldAndTemperature(&valX2, &valY2, &valZ2, &temp2);
    dut2.printRegisters();
    dut3.getMagneticFieldAndTemperature(&valX3, &valY3, &valZ3, &temp3);
    dut3.printRegisters();

    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");

    delay(2000);
}

read_iic_a1b6_extended_addresses.ino

Arduino
/** Project CPP includes. */ 
#include "TLx493D_inc.hpp"


using namespace ifx::tlx493d;


/** Use XMC4700 Relax Kit for this test and connect to power and I2C SDA, respectively. */
// const uint8_t POWER_PIN       = 8; // XMC4700 : P1.10
// const uint8_t SDA_ADDRESS_PIN = 7; // XMC4700 : P1.9

/** Use XMC1100 Boot Kit for this test and connect to power and I2C SDA, respectively. */
const uint8_t POWER_PIN       = 5; // XMC1100
const uint8_t SDA_ADDRESS_PIN = 4; // XMC1100


/** Device at address 0x3E. */
TLx493D_A1B6 dut(Wire, TLx493D_IIC_ADDR_A4_e);


void setup() {
    Serial.begin(115200);
    delay(3000);

    /** Explicit power pin needed to power up board after the addressPin is pulled down.
     *  The value of 140 us has been determined for the XMC1100 board by measuring the
     *  delay between the rising edge of the power signal and the rising edge of the 
     *  pulled-down SDA pin. The data sheet specifies a delay of 200us between the 2 edges.
     *  These 200us are the combined delay of the delay after enable for the power pin (here 140us)
     *  plus the program execution delay. Once the address pin SDA has been enabled it is
     *  set as INPUT thereby detaching it from the SDA output signal.  
     */
    dut.setPowerPin(POWER_PIN, OUTPUT, INPUT, HIGH, LOW, 140, 250000);

    /** Set pin used to drive SDA/ADDR pin before power up.
      * This pin is then isolated from the I2C bus by switching it high-Z before I2C init. */
    dut.setAddressPin(SDA_ADDRESS_PIN, OUTPUT, INPUT, LOW, HIGH, 0, 0);
    
    /** Set the sensor constructor to activate extended address switching pin. */
    dut.begin(true, false, true, true);
    dut.printRegisters();

    /* Options to set the other remaining addresses when SDA low at power-up. */
    // dut.setIICAddress(TLx493D_IIC_ADDR_A5_e); // 0x36
    // dut.setIICAddress(TLx493D_IIC_ADDR_A6_e); // 0x1E
    dut.setIICAddress(TLx493D_IIC_ADDR_A7_e); // 0x16
  
    Serial.print("setup done.\n");
}


/** In the loop we're reading out the temperature value as well as the magnetic values in X, Y, Z-direction 
 *  of the sensors. After that they're all printed to the serial output.
 */
void loop() {
    double temp = 0.0;
    double valX = 0, valY = 0, valZ = 0;

    Serial.print(true == dut.getTemperature(&temp) ? "getTemperature ok\n" : "getTemperature error\n");

    Serial.print("Temperature is: ");
    Serial.print(temp);
    Serial.println("°C");

    Serial.print(true == dut.getMagneticField(&valX, &valY, &valZ) ? "getMagneticField ok\n" : "getMagneticField error\n");

    Serial.print("Value X is: ");
    Serial.print(valX);
    Serial.println(" mT");
    Serial.print("Value Y is: ");
    Serial.print(valY);
    Serial.println(" mT");
    Serial.print("Value Z is: ");
    Serial.print(valZ);
    Serial.println(" mT");

    dut.printRegisters();
    Serial.print("\n");

    delay(1000);
}

read_iic_sensor_c_style.ino

Arduino
/** Project CPP includes. */
#include "TLx493D_inc.hpp"


/** Definition of the power pin. */
const uint8_t POWER_PIN = 15; // XMC1100 : LED2


/** Declaration of the sensor. */
TLx493D_t dut;

uint8_t count = 0;
TLx493D_SupportedSensorType_t sensorType = TLx493D_W2B6_e;


void setup() {
    Serial.begin(115200);
    delay(3000);

    Serial.println(true ==  tlx493d_init(&dut, sensorType) ? "" : "tlx493d_init error");

    /** Setting the defined power up pin as output and set to VDD / high value. */
    pinMode(POWER_PIN, OUTPUT);

    digitalWrite(POWER_PIN, LOW);
    delayMicroseconds(250000UL);

    digitalWrite(POWER_PIN, HIGH);
    delayMicroseconds(0);

    /** Initialization of the sensor and the communication interface.
     *  After that the default configuration of the sensor is set. This means
     *  1-Byte read mode and interrupt disabled.
     */
    Serial.println(true ==  ifx::tlx493d::initCommunication(&dut, Wire, TLx493D_IIC_ADDR_A0_e, true) ? "" : "initCommunication error");
    Serial.println(true ==  tlx493d_setDefaultConfig(&dut) ? "" : "tlx493d_setDefaultConfig error");

    Serial.println("setup done.");
}

/** In the loop we continuously reading the temperature value as well as the
 *  magnetic values in X, Y, Z-direction of the sensor and printing them to
 *  the serial monitor.
 */
void loop() {
    double t, x, y, z;

    tlx493d_getMagneticFieldAndTemperature(&dut, &x, &y, &z, &t);
    tlx493d_printRegisters(&dut);

    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");

    delay(1000);

    Serial.print("count : ");
    Serial.println(count);

    if( ++count == 5 ) {
        Serial.println("\nBefore reset -------------------------------------------------------------------------------------------------------");

        // If reset fails for A1B6 and W2BW : check drive strength of power pin !
        ifx::tlx493d::deinitCommunication(&dut, sensorType != TLx493D_A1B6_e);
        dut.functions->deinit(&dut);

        digitalWrite(POWER_PIN, LOW);
        delayMicroseconds(250000UL);

        tlx493d_init(&dut, sensorType);

        digitalWrite(POWER_PIN, HIGH);
        delayMicroseconds(0);

        ifx::tlx493d::initCommunication(&dut, Wire, TLx493D_IIC_ADDR_A0_e, true);
        tlx493d_setDefaultConfig(&dut);

        Serial.println("\nAfter reset -------------------------------------------------------------------------------------------------------");
        count = 0;
    }
}

read_iic_sensor_with_wakeup.ino

Arduino
/** Project CPP includes. */
#include "TLx493D_inc.hpp"


using namespace ifx::tlx493d;


/** Definition of the upper and lower thresholds in X, Y, Z-direction.
 *  These thresholds define under which conditions the sensor wakes up and triggers his interrupt.
 */
const int16_t THRESHOLD = 200;

const int16_t LOWER_THRESHOLD_X = -THRESHOLD;
const int16_t LOWER_THRESHOLD_Y = -THRESHOLD;
const int16_t LOWER_THRESHOLD_Z = -THRESHOLD;

const int16_t UPPER_THRESHOLD_X = THRESHOLD;
const int16_t UPPER_THRESHOLD_Y = THRESHOLD;
const int16_t UPPER_THRESHOLD_Z = THRESHOLD;


/** Definition of the interrupt pin, which will detected the triggered interrupt.
 *  Please be aware that you have to change the pin according to your setup.
 *  Here a XCM1100-XMC2GO is used as microcontroller.
 */
// const uint8_t INTERRUPT_PIN = 9;


/** XMC4700-Relax-Kit */
const uint8_t INTERRUPT_PIN = 2;


/** Declaration of the sensor object. */
TLx493D_W2BW dut(Wire, TLx493D_IIC_ADDR_A0_e);

/** Definition of an interrupt flag. */
bool intTriggered = false;


void setup() {
    Serial.begin(115200);
    delay(3000);

    /** Definition of the interrupt pin and attaching of a callback-function to it. 
     *  In the attachInterrupt function we can also define when the service routine should
     *  be triggered. In our case on a falling edge.  
     */
    pinMode(INTERRUPT_PIN, INPUT);
    attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), wakeUp_ISR, FALLING);

    dut.begin();

    /** Here we're setting the thresholds for all three directions of the sensor.
     *  After this we enable the wake up mode as well as the interrupt mode.
     *  Both is necessary in order to enable the wake up feature of the sensor.
     * 
     *  The datasheet also recommends to disable the interrupt after it is triggered.
     *  The reason for that is that the sensor continues to trigger the interrupt
     *  as long as one of the thresholds is exceeded.
     */
    dut.setWakeUpThresholdsAsInteger(LOWER_THRESHOLD_X, UPPER_THRESHOLD_X, LOWER_THRESHOLD_Y, UPPER_THRESHOLD_Y, LOWER_THRESHOLD_Z, UPPER_THRESHOLD_Z);
    dut.enableWakeUpMode();
    dut.enableInterrupt();
    
    /** Here we are checking if the wake up is set correctly.
     *  This can be done due to an internal flag in the register, which is
     *  representing the status of the wake up.
     */
    Serial.print("isWakeUpEnabled : ");
    Serial.println(dut.isWakeUpEnabled() ? "enabled" : "not enabled");
    Serial.print("setup done.\n");
}

/** The code in the loop is constantly checking if the interrupt flag is set.
 *  If this condition is fulfilled, we're reading the temperature as well as the
 *  magnetic values in X, Y, Z-direction and printing them to the console.
 *  Afterwards we're reseting the flag and wait for two seconds.
 */
void loop() {
    if( intTriggered ) {
        double temp1 = 0.0;
        double valX1 = 0, valY1 = 0, valZ1 = 0;

        dut.printRegisters();

        dut.getTemperature(&temp1); 
        dut.getMagneticField(&valX1, &valY1, &valZ1);
    
        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");

        delay(2000);

        intTriggered = false;
    }
}

/** The interrupt service routine is simply setting a boolean flag as soon the sensor interrupt is triggered */
void wakeUp_ISR() {
    intTriggered = true;
}

read_spi_sensor.ino

Arduino
/** Project CPP includes. */
#include "TLx493D_inc.hpp"


using namespace ifx::tlx493d;


/** Definition of the power and chip select pin. */
const uint8_t CHIP_SELECT_PIN = 3;

/** Declaration of the sensor object. */
TLx493D_P3I8 dut(SPI);

/** Definition of a counter. */
uint8_t count = 0;


void setup() {
    Serial.begin(115200);
    delay(3000);

    /** Setting the chip select pin for the SPI board using the functions of the Board Support Class.
     * Power supply is constantly on and cannot be changed for the current experimental board.
     */
    dut.setSelectPin(CHIP_SELECT_PIN, OUTPUT, INPUT, LOW, HIGH, 50, 50);
    dut.begin();

    Serial.print("setup done.\n");
}

/** In the loop we're reading out the temperature value (in °C) as well as the magnetic fields values in X, Y, Z-direction (in mT).
 *  We're also reading out the raw temperature value (in LSB).
 */
void loop() {
    double temp = 0.0;
    double valX = 0, valY = 0, valZ = 0;
    int16_t tempRaw = 0;

    /** Note that you don't have to toggle any chip select signal, this is done in the background within the transfer function. */
    Serial.print(true == dut.getTemperature(&temp) ? "getTemperature ok\n" : "getTemperature error\n");

    Serial.print("Temperature is: ");
    Serial.print(temp);
    Serial.println(" °C");

    dut.getRawTemperature(&tempRaw);

    Serial.print("Raw temperature is: ");
    Serial.print(tempRaw);
    Serial.println(" LSB");

    Serial.print(true == dut.getMagneticField(&valX, &valY, &valZ) ? "getMagneticField ok\n" : "getMagneticField error\n");

    Serial.print("Value X is: ");
    Serial.print(valX);
    Serial.println(" mT");
    Serial.print("Value Y is: ");
    Serial.print(valY);
    Serial.println(" mT");
    Serial.print("Value Z is: ");
    Serial.print(valZ);
    Serial.println(" mT");

    dut.printRegisters();
    Serial.print("\n");

    delay(1000);

    Serial.print("count : ");
    Serial.println(count);

    if( ++count == 10 ) {
        Serial.println("Before reset -------------------------------------------------------------------------------------------------------");

        /* The current experimental board does not support power down as the chip VDD is hard wired ! Therefore either use the software reset
         * directly or use dut.reset() to execute a software reset. **/
        dut.reset();
        // dut.softwareReset();

        Serial.println("After reset -------------------------------------------------------------------------------------------------------");
        count = 0;
    }
}

Credits

Infineon Team
102 projects • 165 followers
Contact

Comments

Please log in or sign up to comment.