|
| 1 | +/* |
| 2 | + Example 5: Presence Subsweeps - Power Consumption Optimization |
| 3 | +
|
| 4 | + Using the Acconeer XM125 A121 60GHz Pulsed Coherent Radar Sensor. |
| 5 | +
|
| 6 | + This example shows how operate the XM125 when the device is in Presence Reading Mode. |
| 7 | + The sensor is initialized, then the presence values will print out to the terminal |
| 8 | + and trigger the GPIO1 pin high when there is a presence detected. |
| 9 | +
|
| 10 | + By: Madison Chodikov |
| 11 | + SparkFun Electronics |
| 12 | + Date: 2024/1/22 |
| 13 | + SparkFun code, firmware, and software is released under the MIT License. |
| 14 | + Please see LICENSE.md for further details. |
| 15 | +
|
| 16 | + Hardware Connections: |
| 17 | + QWIIC --> QWIIC |
| 18 | +
|
| 19 | + Serial.print it out at 115200 baud to serial monitor. |
| 20 | +
|
| 21 | + Feel like supporting our work? Buy a board from SparkFun! |
| 22 | + https://www.sparkfun.com/products/ - Qwiic XM125 Breakout |
| 23 | +*/ |
| 24 | +#include <Arduino.h> |
| 25 | +#include "SparkFun_Qwiic_XM125_Arduino_Library.h" |
| 26 | + |
| 27 | +SfeXM125 radarSensor; |
| 28 | + |
| 29 | +// I2C default address |
| 30 | +uint8_t i2cAddress = SFE_XM125_I2C_ADDRESS; |
| 31 | + |
| 32 | +// Presence distance values |
| 33 | +uint32_t distance = 0; |
| 34 | +uint32_t presenceDetected = 0; |
| 35 | +uint32_t presenceDetectedSticky = 0; |
| 36 | +uint32_t startVal = 0; |
| 37 | +uint32_t endVal = 0; |
| 38 | +uint32_t gpioUsage; |
| 39 | + |
| 40 | +// Error statuses |
| 41 | +uint32_t errorStatus = 0; |
| 42 | +uint32_t busyError = 0; |
| 43 | + |
| 44 | +void setup() |
| 45 | +{ |
| 46 | + // Start serial |
| 47 | + Serial.begin(115200); |
| 48 | + Serial.println("XM125 Example 5: Presence GPIO0 Pin Usage"); |
| 49 | + Serial.println(""); |
| 50 | + |
| 51 | + Wire.begin(); |
| 52 | + |
| 53 | + // If begin is successful (0), then start example |
| 54 | + int startErr = radarSensor.begin(i2cAddress, Wire); |
| 55 | + if(startErr == 1) |
| 56 | + { |
| 57 | + Serial.println("Begin"); |
| 58 | + } |
| 59 | + else // Otherwise, infinite loop |
| 60 | + { |
| 61 | + Serial.print("Start Error Code: "); |
| 62 | + Serial.println(startErr); |
| 63 | + Serial.println("Device failed to setup - Freezing code."); |
| 64 | + while(1); // Runs forever |
| 65 | + } |
| 66 | + |
| 67 | + delay(200); |
| 68 | + |
| 69 | + // Presence Sensor Setup |
| 70 | + // Reset sensor configuration to reapply configuration registers |
| 71 | + radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_RESET_MODULE); |
| 72 | + |
| 73 | + // Check error and busy bits |
| 74 | + radarSensor.getPresenceDetectorErrorStatus(errorStatus); |
| 75 | + if(errorStatus != 0) |
| 76 | + { |
| 77 | + Serial.print("Detector status error: "); |
| 78 | + Serial.println(errorStatus); |
| 79 | + } |
| 80 | + |
| 81 | + delay(100); |
| 82 | + |
| 83 | + // Set Start register - NOTE this is a factor 1000 larger than the RSS value |
| 84 | + if(radarSensor.setPresenceStart(300) != 0) |
| 85 | + { |
| 86 | + Serial.println("Presence Start Error"); |
| 87 | + } |
| 88 | + radarSensor.getPresenceStart(startVal); |
| 89 | + Serial.print("Start Val: "); |
| 90 | + Serial.println(startVal); |
| 91 | + delay(100); |
| 92 | + |
| 93 | + // Set End register - NOTE this is a factor 1000 larger than the RSS value |
| 94 | + if(radarSensor.setPresenceEnd(7000) != 0) |
| 95 | + { |
| 96 | + Serial.println("Presence End Error"); |
| 97 | + } |
| 98 | + radarSensor.getPresenceEnd(endVal); |
| 99 | + Serial.print("End Val: "); |
| 100 | + Serial.println(endVal); |
| 101 | + delay(100); |
| 102 | + |
| 103 | + // Turn presence detection on GPIO0 on |
| 104 | + if(radarSensor.setPresenceDetectionOnGPIO(1) != 0) |
| 105 | + { |
| 106 | + Serial.println("GPIO0 Pin Setup Error"); |
| 107 | + } |
| 108 | + radarSensor.getPresenceDetectionOnGPIO(gpioUsage); |
| 109 | + Serial.print("GPIO0 Detection Status: "); |
| 110 | + Serial.println(gpioUsage); |
| 111 | + |
| 112 | + // Apply configuration |
| 113 | + if(radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_APPLY_CONFIGURATION) != 0) |
| 114 | + { |
| 115 | + // Check for errors |
| 116 | + radarSensor.getPresenceDetectorErrorStatus(errorStatus); |
| 117 | + if(errorStatus != 0) |
| 118 | + { |
| 119 | + Serial.print("Detector status error: "); |
| 120 | + Serial.println(errorStatus); |
| 121 | + } |
| 122 | + |
| 123 | + Serial.println("Configuration application error"); |
| 124 | + } |
| 125 | + |
| 126 | + // Poll detector status until busy bit is cleared |
| 127 | + if(radarSensor.presenceBusyWait() != 0) |
| 128 | + { |
| 129 | + Serial.print("Busy wait error"); |
| 130 | + } |
| 131 | + |
| 132 | + // Check detector status |
| 133 | + radarSensor.getPresenceDetectorErrorStatus(errorStatus); |
| 134 | + if(errorStatus != 0) |
| 135 | + { |
| 136 | + Serial.print("Detector status error: "); |
| 137 | + Serial.println(errorStatus); |
| 138 | + } |
| 139 | + |
| 140 | + Serial.println(); |
| 141 | + |
| 142 | + // Original code below: |
| 143 | + // // Default start = 1000; Default stop = 5000 |
| 144 | + // int32_t sensorStartError = radarSensor.presenceDetectorStart(); |
| 145 | + // if(sensorStartError != 0) |
| 146 | + // { |
| 147 | + // Serial.println("Sensor Started Successfully"); |
| 148 | + // } |
| 149 | + // else |
| 150 | + // { |
| 151 | + // Serial.println("Sensor not initialized correctly - Freezing code."); |
| 152 | + // while(1); // Runs forever |
| 153 | + // } |
| 154 | + |
| 155 | + delay(1000); |
| 156 | +} |
| 157 | + |
| 158 | +void loop() |
| 159 | +{ |
| 160 | + |
| 161 | + // Poll detector status until busy bit is cleared - CHECK ON THIS! |
| 162 | + if(radarSensor.presenceBusyWait() != 0) |
| 163 | + { |
| 164 | + Serial.println("Busy wait error"); |
| 165 | + } |
| 166 | + |
| 167 | + // Check error bits |
| 168 | + radarSensor.getPresenceDetectorErrorStatus(errorStatus); |
| 169 | + if(errorStatus != 0) |
| 170 | + { |
| 171 | + Serial.print("Detector status error: "); |
| 172 | + Serial.println(errorStatus); |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | + // Start detector |
| 177 | + if(radarSensor.setPresenceCommand(SFE_XM125_PRESENCE_START_DETECTOR) != 0) |
| 178 | + { |
| 179 | + Serial.println("Start detector error"); |
| 180 | + } |
| 181 | + |
| 182 | + // Poll detector status until busy bit is cleared - CHECK ON THIS! |
| 183 | + if(radarSensor.presenceBusyWait() != 0) |
| 184 | + { |
| 185 | + Serial.println("Busy wait error"); |
| 186 | + } |
| 187 | + |
| 188 | + // Verify that no error bits are set in the detector status register |
| 189 | + radarSensor.getPresenceDetectorErrorStatus(errorStatus); |
| 190 | + if(errorStatus != 0) |
| 191 | + { |
| 192 | + Serial.print("Detector status error: "); |
| 193 | + Serial.println(errorStatus); |
| 194 | + } |
| 195 | + |
| 196 | + // Read detector result register and determine detection status |
| 197 | + radarSensor.getPresenceDetectorPresenceDetected(presenceDetected); |
| 198 | + radarSensor.getPresenceDetectorPresenceStickyDetected(presenceDetectedSticky); |
| 199 | + |
| 200 | + if((presenceDetected == 1) | (presenceDetectedSticky == 1)) |
| 201 | + { |
| 202 | + radarSensor.getPresenceDistance(distance); |
| 203 | + Serial.print("Presence Detected: "); |
| 204 | + Serial.print(distance); |
| 205 | + Serial.println("mm"); |
| 206 | + } |
| 207 | + |
| 208 | + // Delay 1 second between readings |
| 209 | + delay(1000); |
| 210 | +} |
0 commit comments