I'm using the TLV493D-A1B6 sensor connected to an Arduino Uno: I've attached all necessary documentation at the bottom.
This is a 3D magnetic field position sensor that detects the magnetic field strength of a nearby magnetic field in the X, Y, and Z directions. I am NOT using the 2GO evaluation board. The sensor uses I2C protocol for communication.
I have this sensor all wired up, soldered correctly to a PCB with pinheadings, and ready to go on my breadboard. Also, I am using a bidirectional logic level converter that is recommended for this sensor. I can confirm this as the I2C scanner sketch detects the sensor and prints out its expected defulat address.
Specifically, I am having trouble understanding the data sheets and how to setup Arduino using the 'Wire.h' library to communicate with the sensor over I2C to obtain the X, Y, Z RAW/decoded readings continuously.
I have tried code from this thread: https://forum.arduino.cc/index.php?topic=419380.0 I have tried code from this dedicated tutorial for this sensor: https://www.allaboutcircuits.com/technical-articles/tutorial-and-overview-of-infineons-3d-magnetic-2go-kit/
I have even tried code from Github for this sensor: https://github.com/IRNAS/TLV493D-3D-Magnetic-Sensor-Arduino-Library/blob/master/TLV493D/TLV493D.h
Using any of the above, I will get readings of "X=-1, Y=-1, Z=-1" or the sensor will output random readings of "255" and "0" when exposed to any magnet.
I've written my own code and have failed vigorously to get anything but random, clearly wrong readouts, this was my latest attempt, I chopped this code down for simplicity and clarity. I've also tried variations using the I2C.h master library, but no luck:
#include "Wire.h"
const int tlv_addr = 0x1F; // obtained by i2c scanner; all default address of tlv sensor - 0x1F, 0x5E, or 0x3E
const int config_reg = 0x00; // I have scene this in other examples of code for this sensor, I cannot find the "configure" or "command register" address on any of its datasheets
const int lp_mode = 0x05; //Have found this on the data sheet as the low power mode command
// Note: I switched from const byte to const int for HEX assignments because I get a warning when compiling the sketch about this and I found online that this is the solution?
void setup() {
Wire.begin(); //join I2C bus
Serial.begin(9600); //start Serial
while(!Serial); //wait for Serial to be available
Wire.beginTransmission(tlv_addr); //initiate communication with sensor
Wire.write(config_reg); //set pointer/access configuration register
Wire.write(lp_mode); //from what I read, the sensor defaults to power down mode on start up, so I send the low power mode command to the configure register to activate the sensor in low power mode which should cause it to start "sensing"
Wire.endTransmission(); //end configurations
delay(100); //delay to allow time for sensor the update
Serial.print("Setup Complete\n\n");
}
void loop(){
Wire.requestFrom(tlv_addr, 3); //request the 3 raw, UN-decoded bytes from the sensor's readings of X, Y, Z axes
//---- Read in each byte of sensor data one by one for each axis ----//
byte Bx = Wire.read(); //Have scene the "*" added to the beginning of the variable on a lot of code, not sure why this is necessary?
byte By = Wire.read();
byte Bz = Wire.read();
//---- Print Out Sensor Axis Readings ----//
Serial.println("X= ");
Serial.print(Bx); //Have also scene "&" added to the beginning of the variable on a lot of code like Serial.print(&Bx);?
Serial.print("\tY= ");
Serial.print(By);
Serial.print("\tZ= ");
Serial.print(Bz);
//Expecting something like this: 01011011
//Getting -1 or 255 or 0 randomly?
delay(1000); //arbitrary delay
}
And furthermore, I understand that these values needed to be decoded to make sense of the actual data, I have no idea what is going on the register map sheet and why they are shifting different bits of segments of each byte, seemingly like totally picking the bytes apart then reordering them, and then casting them as integers to get real data?
I am very new to Arduino and especially to I2C. If you could please help me understand especially what I'm missing from the data sheet, or what I'm doing wrong in the simplest of terms, I want to intuitively understand whats going on at every step so I can do this again with another I2C sensor on my own. If anyone could please help me with this.
Register Map: https://www.infineon.com/dgdl/Infineon-TLV493D-A1B6_3DMagnetic-UM-v01_03-EN.pdf?fileId=5546d46261d5e6820161e75721903ddd