Skip to content

Commit 9a0a7e2

Browse files
committed
add headers to files; cleanup and update examples
1 parent 085a64d commit 9a0a7e2

File tree

5 files changed

+457
-262
lines changed

5 files changed

+457
-262
lines changed

examples/Example1_BasicReadings/Example1_BasicReadings.ino

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
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+
125
#include "SparkFun_TMAG5273_Arduino_Library.h" // Used to send and recieve specific information from our sensor
226
#include <Wire.h> // Used to establish serial communication on the I2C bus
327

@@ -8,12 +32,16 @@ uint8_t i2cAddress = TMAG5273_I2C_ADDRESS_INITIAL;
832

933
void setup()
1034
{
35+
delay(1000);
1136
Wire.begin();
1237
// Start serial communication at 115200 baud
1338
Serial.begin(115200);
1439

1540
// Begin example of the magnetic sensor code (and add whitespace for easy reading)
41+
Serial.println("");
42+
Serial.println("------------------------------------------------------------------");
1643
Serial.println("TMAG5273 Example 1: Basic Readings");
44+
Serial.println("------------------------------------------------------------------");
1745
Serial.println("");
1846

1947
// If begin is successful (0), then start example
Lines changed: 124 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,152 @@
1-
#include <Wire.h> // Used to establish serial communication on the I2C bus
2-
#include "SparkFun_TMAG5273_Arduino_Library.h" // Used to send and recieve specific information from our sensor
3-
4-
TMAG5273 sensor; // Initialize hall-effect sensor
1+
/*
2+
* ---------------------------------------------------------------------------------
3+
* Copyright (c) 2025, SparkFun Electronics Inc.
4+
*
5+
* SPDX-License-Identifier: MIT
6+
* ---------------------------------------------------------------------------------
7+
*/
8+
9+
/*
10+
* Example 2: Interrupts
11+
* -------------------------
12+
* This example demonstrates how to configure the TMAG5273 sensor to use interrupts. The example is
13+
* setup to trigger an interrupt when the X-axis magnetic field exceeds a specified threshold.
14+
*
15+
* * Hardware Connections:
16+
*. - None reqquired - this example is intended to be used with the SparkFun IoT Brushless Motor Driver
17+
*
18+
* Note: Make sure to install the SparkFun TMAG5273 Arduino Library before running this example.
19+
* You can install it via the Arduino Library Manager or download it from:
20+
* https://github.com/sparkfun/SparkFun_TMAG5273_Arduino_Library
21+
*
22+
*/
23+
24+
#include "SparkFun_TMAG5273_Arduino_Library.h" // Used to send and recieve specific information from our sensor
25+
#include <Wire.h> // Used to establish serial communication on the I2C bus
26+
27+
TMAG5273 sensor; // Initialize hall-effect sensor
528

629
// I2C default address
730
uint8_t i2cAddress = TMAG5273_I2C_ADDRESS_INITIAL;
831

932
// Interrupt pin used
10-
// NOTE: This pin is specific to the SparkFun IoT Motor Driver
33+
// NOTE: This pin is specific to the SparkFun IoT Brushless Motor Driver
1134
uint8_t intPin = 4;
1235

1336
// Start the flag as false
14-
bool interruptFlag = false;
37+
volatile bool interruptFlag = false;
1538

1639
// ISR to print what interrupt was triggered
17-
void isr1()
40+
void isr1()
1841
{
19-
interruptFlag = true;
42+
interruptFlag = true;
2043
}
2144

22-
23-
void setup()
45+
void setup()
2446
{
25-
Wire.begin();
26-
// Start serial communication at 115200 baud
27-
Serial.begin(115200);
28-
29-
// Configure Interrupt Pin
30-
pinMode(intPin, INPUT);
31-
// Attach interrupt to pin 4 as a digital, falling pin
32-
attachInterrupt(digitalPinToInterrupt(intPin), isr1, CHANGE);
33-
34-
// Begin example of the magnetic sensor code (and add whitespace for easy reading)
35-
Serial.println("TMAG5273 Example 2: Interrupts");
36-
Serial.println("");
37-
// If begin is successful (0), then start example
38-
if (sensor.begin(i2cAddress, Wire) == true)
39-
{
40-
Serial.println("Begin");
41-
}
42-
else // Otherwise, infinite loop
43-
{
44-
Serial.println("Device failed to setup - Freezing code.");
45-
while (1); // Runs forever
46-
}
47-
48-
// Set interrupt through !INT
49-
sensor.setInterruptMode(TMAG5273_INTERRUPT_THROUGH_INT);
50-
51-
// Set the !INT pin state - pulse for 10us
52-
sensor.setIntPinState(true);
53-
54-
// Enable the interrupt response for the thresholds
55-
sensor.setThresholdEn(true);
56-
57-
//int pinStatus = sensor.getInterruptPinStatus();
58-
pinMode(intPin, INPUT);
59-
60-
// Set X, Y, Z, and T Thresholds for interrupt to be triggered
61-
sensor.setXThreshold(5); // mT
62-
//sensor.setYThreshold(5); // mT
63-
//sensor.setZThreshold(5); // mT
64-
//sensor.setTemperatureThreshold(50); // C
65-
66-
Serial.print("X Threshold Set: ");
67-
Serial.println(sensor.getXThreshold());
47+
delay(1000);
48+
Wire.begin();
49+
// Start serial communication at 115200 baud
50+
Serial.begin(115200);
51+
52+
// Configure Interrupt Pin
53+
pinMode(intPin, INPUT);
54+
// Attach interrupt to pin 4 as a digital, falling pin
55+
attachInterrupt(digitalPinToInterrupt(intPin), isr1, CHANGE);
56+
57+
// Begin example of the magnetic sensor code (and add whitespace for easy reading)
58+
Serial.println("");
59+
Serial.println("------------------------------------------------------------------");
60+
Serial.println("TMAG5273 Example 2: Interrupts");
61+
Serial.println("------------------------------------------------------------------");
62+
63+
Serial.println("");
64+
// If begin is successful (0), then start example
65+
if (sensor.begin(i2cAddress, Wire) == true)
66+
{
67+
Serial.println("Begin");
68+
}
69+
else // Otherwise, infinite loop
70+
{
71+
Serial.println("Device failed to setup - Freezing code.");
72+
while (1)
73+
; // Runs forever
74+
}
75+
76+
// Set interrupt through !INT
77+
sensor.setInterruptMode(TMAG5273_INTERRUPT_THROUGH_INT);
78+
79+
// Set the !INT pin state - pulse for 10us
80+
sensor.setIntPinState(true);
81+
82+
// Enable the interrupt response for the thresholds
83+
sensor.setThresholdEn(true);
84+
85+
// int pinStatus = sensor.getInterruptPinStatus();
86+
pinMode(intPin, INPUT);
87+
88+
// Set X, Y, Z, and T Thresholds for interrupt to be triggered
89+
sensor.setXThreshold(5); // mT
90+
// sensor.setYThreshold(5); // mT
91+
// sensor.setZThreshold(5); // mT
92+
// sensor.setTemperatureThreshold(50); // C
93+
94+
Serial.print("X Threshold Set: ");
95+
Serial.println(sensor.getXThreshold());
6896
}
6997

70-
/* To use the other thresholds, simply change the names and use the other functions:
98+
/* To use the other thresholds, simply change the names and use the other functions:
7199
- sensor.getYThreshold();
72100
- sensor.getZThreshold();
73101
- sensor.getTemperatureThreshold();
74102
*/
75103

76104
void loop()
77105
{
78-
if (interruptFlag == true)
79-
{
80-
interruptFlag = false;
81-
Serial.println("X Threshold Reached!");
82-
83-
int xThresh = sensor.getXThreshold();
84-
Serial.print("X Threshold: ");
85-
Serial.println(xThresh);
86-
87-
if (sensor.getMagneticChannel() != 0) // Checks if mag channels are on - turns on in setup
106+
if (interruptFlag == true)
88107
{
89-
float magX = sensor.getXData();
90-
float magY = sensor.getYData();
91-
float magZ = sensor.getZData();
92-
float temp = sensor.getTemp();
93-
94-
Serial.print("(");
95-
Serial.print(magX);
96-
Serial.print(", ");
97-
Serial.print(magY);
98-
Serial.print(", ");
99-
Serial.print(magZ);
100-
Serial.println(") mT");
101-
Serial.print(temp);
102-
Serial.println(" °C");
103-
}
104-
else
108+
interruptFlag = false;
109+
Serial.print("X Threshold Reached! ");
110+
111+
int xThresh = sensor.getXThreshold();
112+
Serial.print("X Threshold: ");
113+
Serial.println(xThresh);
114+
115+
if (sensor.getMagneticChannel() != 0) // Checks if mag channels are on - turns on in setup
116+
{
117+
float magX = sensor.getXData();
118+
float magY = sensor.getYData();
119+
float magZ = sensor.getZData();
120+
float temp = sensor.getTemp();
121+
122+
// Serial.print("(");
123+
// Serial.print(magX);
124+
// Serial.print(", ");
125+
// Serial.print(magY);
126+
// Serial.print(", ");
127+
// Serial.print(magZ);
128+
// Serial.println(") mT");
129+
// Serial.print(temp);
130+
// Serial.println(" °C");
131+
132+
// This examples is primarily intended for use with the SparkFun IoT Brushless Motor Driver
133+
// board, which is ESP32 based. Because of this, we are using Serial.printf for easier
134+
// formatted output. If you are using an AVR based board, you can replace this with the
135+
// commented out Serial.print statements above.
136+
Serial.printf(" Magnetic: [ X: %7.2f Y: %7.2f Z: %7.2f ] mT, Temp:\t%6.2f C\r\n", magX, magY, magZ,
137+
temp);
138+
}
139+
else
140+
{
141+
Serial.println("Mag Channels disabled, stopping..");
142+
while (1)
143+
;
144+
}
145+
}
146+
else
105147
{
106-
Serial.println("Mag Channels disabled, stopping..");
107-
while (1);
148+
Serial.println("Checking for Interrupts...");
108149
}
109-
}
110-
else
111-
{
112-
Serial.println("Checking for Interrupts...");
113-
}
114-
115-
delay(500);
116150

151+
delay(500);
117152
}

0 commit comments

Comments
 (0)