33
44TMAG5273 sensor; // Initialize hall-effect sensor
55
6+ // I2C default address
7+ uint8_t i2cAddress = I2C_ADDRESS_INITIAL;
8+
69void setup ()
710{
811 Wire.begin ();
912 // Start serial communication at 115200 baud
1013 Serial.begin (115200 );
1114 // Set clock speed to be the fastest for better communication
12- Wire.setClock (1000000 );
15+ Wire.setClock (1000000 );
1316
14-
1517 // Begin example of the magnetic sensor code (and add whitespace for easy reading)
1618 Serial.println (" TMAG5273 Example 1: Basic Readings" );
1719 Serial.println (" " );
1820 // If begin is successful (0), then start example
19- if (sensor.begin (0X22 , Wire) == false )
21+ if (sensor.begin (i2cAddress , Wire) == true )
2022 {
2123 Serial.println (" Begin" );
2224 }
@@ -26,59 +28,35 @@ void setup()
2628 while (1 ); // Runs forever
2729 }
2830
29- // TESTING CODE:
30- // Print out the I2C address
31- uint16_t address = sensor.getI2CAddress ();
32- Serial.print (" I2C Address: " );
33- Serial.println (address, HEX);
34-
35- sensor.setMagChannel (7 );
36- int magChannel = sensor.getMagChannel ();
37- Serial.print (" Magnetic Channel set: " );
38- Serial.println (magChannel);
39-
40- sensor.setXYAxisRange (1 );
41-
4231}
4332
4433
45-
4634void loop ()
4735{
48-
49- if (sensor.getMagChannel () != 0 ) // Checks if mag channels are on - turns on in setup
36+ // Checks if mag channels are on - turns on in setup
37+ if (sensor.getMagneticChannel () != 0 )
5038 {
5139 float magX = sensor.getXData ();
5240 float magY = sensor.getYData ();
5341 float magZ = sensor.getZData ();
5442 float temp = sensor.getTemp ();
55-
56- int xyField = sensor.getXYAxisRange ();
57- int xLSB = sensor.getXLSB ();
58- int xMSB = sensor.getXMSB ();
59- Serial.println (); // Create a whitespace for easy viewing
60- Serial.print (" Magnetic Field, X : " );
43+
44+ Serial.print (" (" );
6145 Serial.print (magX);
62- Serial.println (" mT" );
63- Serial.print (" XY Axis Range: " );
64- Serial.println (xyField);
65- Serial.print (" XLSB: " );
66- Serial.println (xLSB, BIN);
67- Serial.print (" XMSB: " );
68- Serial.println (xMSB, BIN);
69- // Serial.print("Magnetic Field, Y in mT: ");
70- // Serial.println(magY);
71- // Serial.print("Magnetic Field, Z in mT: ");
72- // Serial.println(magZ);
73- // Serial.print("Temperature in Celsius: ");
74- // Serial.println(temp);
75- delay (500 ); // Delay added for easier readings
46+ Serial.print (" , " );
47+ Serial.print (magY);
48+ Serial.print (" , " );
49+ Serial.print (magZ);
50+ Serial.println (" ) mT" );
51+ Serial.print (temp);
52+ Serial.println (" C" );
7653 }
7754 else
7855 {
56+ // If there is an issue, stop the magnetic readings and restart sensor/example
7957 Serial.println (" Mag Channels disabled, stopping.." );
8058 while (1 );
8159 }
8260
83- delay (1000 );
61+ delay (100 );
8462}
0 commit comments