|
1 | | -#include <Wire.h> // Used to establish serial communication on the I2C bus |
| 1 | +/* |
| 2 | + * --------------------------------------------------------------------------------- |
| 3 | + * Copyright (c) 2025, SparkFun Electronics Inc. |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: MIT |
| 6 | + * --------------------------------------------------------------------------------- |
| 7 | + */ |
| 8 | + |
| 9 | +/* |
| 10 | + * Example 1: Basic Readings |
| 11 | + * ------------------------- |
| 12 | + * This example shows the most basic way to read magnetic and temperature data from the TMAG5273 sensor. |
| 13 | + * The example initializes the sensor, then continuously reads and prints the X, Y, Z magnetic field |
| 14 | + * values along with the temperature to the Serial Monitor. |
| 15 | + * |
| 16 | + * Hardware Connections: |
| 17 | + * - Connect the TMAG5273 sensor to the SparkFun Qwiic connector on your SparkFun microcontroller board. |
| 18 | + * |
| 19 | + * Note: Make sure to install the SparkFun TMAG5273 Arduino Library before running this example. |
| 20 | + * You can install it via the Arduino Library Manager or download it from: |
| 21 | + * https://github.com/sparkfun/SparkFun_TMAG5273_Arduino_Library |
| 22 | + * |
| 23 | + */ |
| 24 | + |
2 | 25 | #include "SparkFun_TMAG5273_Arduino_Library.h" // Used to send and recieve specific information from our sensor |
| 26 | +#include <Wire.h> // Used to establish serial communication on the I2C bus |
3 | 27 |
|
4 | 28 | TMAG5273 sensor; // Initialize hall-effect sensor |
5 | 29 |
|
6 | 30 | // I2C default address |
7 | 31 | uint8_t i2cAddress = TMAG5273_I2C_ADDRESS_INITIAL; |
8 | 32 |
|
9 | | -void setup() |
| 33 | +void setup() |
10 | 34 | { |
11 | | - Wire.begin(); |
12 | | - // Start serial communication at 115200 baud |
13 | | - Serial.begin(115200); |
14 | | - |
15 | | - // Begin example of the magnetic sensor code (and add whitespace for easy reading) |
16 | | - Serial.println("TMAG5273 Example 1: Basic Readings"); |
17 | | - Serial.println(""); |
18 | | - |
19 | | - // If begin is successful (0), then start example |
20 | | - if(sensor.begin(i2cAddress, Wire) == 1) |
21 | | - { |
22 | | - Serial.println("Begin"); |
23 | | - } |
24 | | - else // Otherwise, infinite loop |
25 | | - { |
26 | | - Serial.println("Device failed to setup - Freezing code."); |
27 | | - while(1); // Runs forever |
28 | | - } |
| 35 | + delay(1000); |
| 36 | + Wire.begin(); |
| 37 | + // Start serial communication at 115200 baud |
| 38 | + Serial.begin(115200); |
29 | 39 |
|
30 | | -} |
| 40 | + // Begin example of the magnetic sensor code (and add whitespace for easy reading) |
| 41 | + Serial.println(""); |
| 42 | + Serial.println("------------------------------------------------------------------"); |
| 43 | + Serial.println("TMAG5273 Example 1: Basic Readings"); |
| 44 | + Serial.println("------------------------------------------------------------------"); |
| 45 | + Serial.println(""); |
31 | 46 |
|
| 47 | + // If begin is successful (0), then start example |
| 48 | + if (sensor.begin(i2cAddress, Wire) == 1) |
| 49 | + { |
| 50 | + Serial.println("Begin"); |
| 51 | + } |
| 52 | + else // Otherwise, infinite loop |
| 53 | + { |
| 54 | + Serial.println("Device failed to setup - Freezing code."); |
| 55 | + while (1) |
| 56 | + ; // Runs forever |
| 57 | + } |
| 58 | +} |
32 | 59 |
|
33 | | -void loop() |
| 60 | +void loop() |
34 | 61 | { |
35 | | - // Checks if mag channels are on - turns on in setup |
36 | | - if(sensor.getMagneticChannel() != 0) |
37 | | - { |
38 | | - sensor.setTemperatureEn(true); |
39 | | - |
40 | | - float magX = sensor.getXData(); |
41 | | - float magY = sensor.getYData(); |
42 | | - float magZ = sensor.getZData(); |
43 | | - float temp = sensor.getTemp(); |
44 | | - |
45 | | - Serial.print("("); |
46 | | - Serial.print(magX); |
47 | | - Serial.print(", "); |
48 | | - Serial.print(magY); |
49 | | - Serial.print(", "); |
50 | | - Serial.print(magZ); |
51 | | - Serial.println(") mT"); |
52 | | - Serial.print(temp); |
53 | | - Serial.println(" C"); |
54 | | - } |
55 | | - else |
56 | | - { |
57 | | - // If there is an issue, stop the magnetic readings and restart sensor/example |
58 | | - Serial.println("Mag Channels disabled, stopping.."); |
59 | | - while(1); |
60 | | - } |
61 | | - |
62 | | - delay(100); |
| 62 | + // Checks if mag channels are on - turns on in setup |
| 63 | + if (sensor.getMagneticChannel() != 0) |
| 64 | + { |
| 65 | + sensor.setTemperatureEn(true); |
| 66 | + |
| 67 | + float magX = sensor.getXData(); |
| 68 | + float magY = sensor.getYData(); |
| 69 | + float magZ = sensor.getZData(); |
| 70 | + float temp = sensor.getTemp(); |
| 71 | + |
| 72 | + Serial.print("Data - Magnetic: [ X: "); |
| 73 | + Serial.print(magX); |
| 74 | + Serial.print(", Y: "); |
| 75 | + Serial.print(magY); |
| 76 | + Serial.print(", Z: "); |
| 77 | + Serial.print(magZ); |
| 78 | + Serial.print(" ] mT, Temp: "); |
| 79 | + Serial.print(temp); |
| 80 | + Serial.println(" C"); |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + // If there is an issue, stop the magnetic readings and restart sensor/example |
| 85 | + Serial.println("Mag Channels disabled, stopping.."); |
| 86 | + while (1) |
| 87 | + ; |
| 88 | + } |
| 89 | + |
| 90 | + delay(300); |
63 | 91 | } |
0 commit comments