|
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 |
5 | 28 |
|
6 | 29 | // I2C default address |
7 | 30 | uint8_t i2cAddress = TMAG5273_I2C_ADDRESS_INITIAL; |
8 | 31 |
|
9 | 32 | // 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 |
11 | 34 | uint8_t intPin = 4; |
12 | 35 |
|
13 | 36 | // Start the flag as false |
14 | | -bool interruptFlag = false; |
| 37 | +volatile bool interruptFlag = false; |
15 | 38 |
|
16 | 39 | // ISR to print what interrupt was triggered |
17 | | -void isr1() |
| 40 | +void isr1() |
18 | 41 | { |
19 | | - interruptFlag = true; |
| 42 | + interruptFlag = true; |
20 | 43 | } |
21 | 44 |
|
22 | | - |
23 | | -void setup() |
| 45 | +void setup() |
24 | 46 | { |
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()); |
68 | 96 | } |
69 | 97 |
|
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: |
71 | 99 | - sensor.getYThreshold(); |
72 | 100 | - sensor.getZThreshold(); |
73 | 101 | - sensor.getTemperatureThreshold(); |
74 | 102 | */ |
75 | 103 |
|
76 | 104 | void loop() |
77 | 105 | { |
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) |
88 | 107 | { |
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 |
105 | 147 | { |
106 | | - Serial.println("Mag Channels disabled, stopping.."); |
107 | | - while (1); |
| 148 | + Serial.println("Checking for Interrupts..."); |
108 | 149 | } |
109 | | - } |
110 | | - else |
111 | | - { |
112 | | - Serial.println("Checking for Interrupts..."); |
113 | | - } |
114 | | - |
115 | | - delay(500); |
116 | 150 |
|
| 151 | + delay(500); |
117 | 152 | } |
0 commit comments