|
| 1 | +#include <Wire.h> // Used to establish serial communication on the I2C bus |
| 2 | +#include "SparkFun_TMAG5273.h" // Used to send and recieve specific information from our sensor |
| 3 | + |
| 4 | +TMAG5273 sensor; // Initialize hall-effect sensor |
| 5 | + |
| 6 | +void setup() |
| 7 | +{ |
| 8 | + Wire.begin(); |
| 9 | + // Start serial communication at 115200 baud |
| 10 | + Serial.begin(115200); |
| 11 | + // Set clock speed to be the fastest for better communication |
| 12 | + Wire.setClock(400000); |
| 13 | + |
| 14 | + // Begin example of the magnetic sensor code (and add whitespace for easy reading) |
| 15 | + Serial.println("TMAG5273 Example 1: Basic Readings"); |
| 16 | + Serial.println(""); |
| 17 | + // If begin is successful (0), then start example |
| 18 | + if(sensor.begin(0X0D, Wire) == false) |
| 19 | + { |
| 20 | + Serial.println("Begin"); |
| 21 | + } |
| 22 | + else // Otherwise, infinite loop |
| 23 | + { |
| 24 | + Serial.println("Device failed to setup - Freezing code."); |
| 25 | + //while(1); // Runs forever |
| 26 | + } |
| 27 | + |
| 28 | + // Reset oscillator error flag |
| 29 | + sensor.setOscillatorError(1); |
| 30 | + |
| 31 | + // Check device status register to make sure no errors are present |
| 32 | + if(sensor.getDeviceStatus() == 1) |
| 33 | + { |
| 34 | + Serial.println("Oscillator Error"); |
| 35 | + } |
| 36 | + else if(sensor.getDeviceStatus() == 2) |
| 37 | + { |
| 38 | + Serial.println("Interrupt Pin Error"); |
| 39 | + } |
| 40 | + else if(sensor.getDeviceStatus() == 3) |
| 41 | + { |
| 42 | + Serial.println("OTP CRC Error"); |
| 43 | + } |
| 44 | + else if(sensor.getDeviceStatus() == 4) |
| 45 | + { |
| 46 | + Serial.println("Undervoltage Error"); |
| 47 | + } |
| 48 | + |
| 49 | + // Define I2C Read mode |
| 50 | + sensor.setReadMode(0); |
| 51 | + if(sensor.setReadMode(0) == 0) |
| 52 | + { |
| 53 | + Serial.println("Read Mode Set Successfully!"); |
| 54 | + delay(100); |
| 55 | + } |
| 56 | + |
| 57 | + // Set low active current mode |
| 58 | + sensor.setLowPower(0); |
| 59 | + if(sensor.setLowPower(0) == 0) |
| 60 | + { |
| 61 | + Serial.println("Low Active Current Set Successfully!"); |
| 62 | + delay(100); |
| 63 | + } |
| 64 | + |
| 65 | + // Set operating mode to continuous measure |
| 66 | + sensor.setOperatingMode(2); |
| 67 | + if(sensor.setOperatingMode(2) == 0) |
| 68 | + { |
| 69 | + Serial.println("Operating Mode Set Successfully!"); |
| 70 | + delay(100); |
| 71 | + } |
| 72 | + |
| 73 | + // Enable all magnetic channels (X, Y, Z) |
| 74 | + int magCheck = 0; |
| 75 | + magCheck = sensor.setMagChannel(7); |
| 76 | + Serial.print("Mag Check: "); |
| 77 | + Serial.println(magCheck); |
| 78 | + int magChannel = sensor.getMagChannel(); |
| 79 | + Serial.println(magChannel); |
| 80 | + |
| 81 | + // Set the I2C Read Mode to be standard 3-byte command |
| 82 | + sensor.setReadMode(0); |
| 83 | + |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +void loop() |
| 90 | +{ |
| 91 | + // Do we need dataReady? Do we have that option? --> Look into more |
| 92 | + |
| 93 | + |
| 94 | + if((sensor.getMagChannel() != 0) || (sensor.getMagChannel() == -1)) // Checks if mag channels are on - turns on in setup |
| 95 | + { |
| 96 | + float magX = sensor.getXData(); |
| 97 | + float magY = sensor.getYData(); |
| 98 | + float magZ = sensor.getZData(); |
| 99 | + float temp = sensor.getTemp(); |
| 100 | + |
| 101 | + Serial.println(); // Create a whitespace for easy viewing |
| 102 | + Serial.print("Magnetic Field, X in mT: "); |
| 103 | + Serial.println(magX); |
| 104 | + //Serial.print("Magnetic Field, Y in mT: "); |
| 105 | + //Serial.println(magY); |
| 106 | + //Serial.print("Magnetic Field, Z in mT: "); |
| 107 | + //Serial.println(magZ); |
| 108 | + //Serial.print("Temperature in Celsius: "); |
| 109 | + //Serial.println(temp); |
| 110 | + delay(500); // Delay added for easier readings |
| 111 | + |
| 112 | + } |
| 113 | + else |
| 114 | + { |
| 115 | + Serial.println("Mag Channels disabled, stopping.."); |
| 116 | + while(1); |
| 117 | + } |
| 118 | +} |
0 commit comments