|
| 1 | +/* |
| 2 | + Using the ADS1219 ADC Data Ready pin as an interrupt. |
| 3 | +
|
| 4 | + The ADS1219 can sample at 1000 samples per second if desired. |
| 5 | + When sampling that fast, polling the DRDY flag in the Status Register |
| 6 | + slows down the sampling. It is much more efficient to use the DRDY |
| 7 | + pin as an interrupt and use that to trigger the read. |
| 8 | +
|
| 9 | + Create an oscilloscope: use the Serial Plotter to plot the ADC readings! |
| 10 | +
|
| 11 | + By: Paul Clark |
| 12 | + SparkFun Electronics |
| 13 | + Date: 2023/12/11 |
| 14 | + SparkFun code, firmware, and software is released under the MIT License. |
| 15 | + Please see LICENSE.md for further details. |
| 16 | +
|
| 17 | + Hardware Connections: |
| 18 | + IoT RedBoard --> ADS1219 |
| 19 | + QWIIC --> QWIIC |
| 20 | + Connect an interrupt-capable IO pin to the DRDY breakout pad, using a jumper wire. |
| 21 | +
|
| 22 | + Open the serial monitor at 115200 baud to see the voltage. |
| 23 | +
|
| 24 | + Feel like supporting our work? Buy a board from SparkFun! |
| 25 | + https://www.sparkfun.com/products/23455 - Qwiic ADS1219 1x1 |
| 26 | +*/ |
| 27 | + |
| 28 | +// You will need the SparkFun Toolkit. Click here to get it: http://librarymanager/All#SparkFun_Toolkit |
| 29 | + |
| 30 | +#include <SparkFun_ADS1219.h> // Click here to get the library: http://librarymanager/All#SparkFun_ADS1219 |
| 31 | + |
| 32 | +SfeADS1219ArdI2C myADC; |
| 33 | + |
| 34 | +const int interruptPin = 4; // This is the FREE pin on the ESP32 Thing Plus C. Change this if required. |
| 35 | + |
| 36 | +bool interruptSeen = false; // A global flag to indicate if the DRDY interrupt has been seen. |
| 37 | + |
| 38 | +void dataReadyISR(void) |
| 39 | +{ |
| 40 | + // Always keep interrupt Interrupt Service Routines as short as possible. |
| 41 | + // Do not try to print or perform bus reads and writes inside them. |
| 42 | + // Here we only set the interruptSeen flag. |
| 43 | + // The ADC conversion result will be read in the loop(). |
| 44 | + interruptSeen = true; |
| 45 | +} |
| 46 | + |
| 47 | +void setup() |
| 48 | +{ |
| 49 | + |
| 50 | + delay(1000); // Allow time for the microcontroller to start up |
| 51 | + |
| 52 | + Serial.begin(115200); // Begin the Serial console |
| 53 | + while (!Serial) |
| 54 | + { |
| 55 | + delay(100); // Wait for the user to open the Serial Monitor |
| 56 | + }; |
| 57 | + Serial.println("SparkFun ADS1219 Example"); |
| 58 | + |
| 59 | + Wire.begin(); // Begin the I2C bus |
| 60 | + |
| 61 | + // Initialize ADC - this also performs a soft reset |
| 62 | + while (myADC.begin() == false) |
| 63 | + { |
| 64 | + Serial.println("ADC failed to begin. Please check your wiring! Retrying..."); |
| 65 | + delay(1000); |
| 66 | + } |
| 67 | + |
| 68 | + // Configure the input multiplexer |
| 69 | + myADC.setInputMultiplexer(ADS1219_CONFIG_MUX_SINGLE_0); // Read the voltage between AIN0 and GND |
| 70 | + |
| 71 | + // Configure the gain to x4, so we can measuure small voltages with higher resolution |
| 72 | + myADC.setGain(ADS1219_GAIN_4); |
| 73 | + |
| 74 | + // Configure the data rate. The ADC will sample at 1000 samples per second |
| 75 | + myADC.setDataRate(ADS1219_DATA_RATE_1000SPS); |
| 76 | + |
| 77 | + // Put the ADC into continuous mode |
| 78 | + myADC.setConversionMode(ADS1219_CONVERSION_CONTINUOUS); |
| 79 | + |
| 80 | + interruptSeen = false; // Make sure the interrupt flag is clear |
| 81 | + |
| 82 | + // Configure the interrupt. DRDY goes low when data is ready. |
| 83 | + attachInterrupt(digitalPinToInterrupt(interruptPin), dataReadyISR, FALLING); |
| 84 | + |
| 85 | + myADC.startSync(); // Start continuous conversions |
| 86 | +} |
| 87 | + |
| 88 | +void loop() |
| 89 | +{ |
| 90 | + if (interruptSeen) // Check if the conversion is complete. |
| 91 | + { |
| 92 | + interruptSeen = false; // Clear the flag ready for the next interrupt |
| 93 | + myADC.readConversion(); // Read the conversion result from the ADC. Store it internally. |
| 94 | + int32_t rawADC = myADC.getConversionRaw(); // Just to be different, read the raw ADC value. |
| 95 | + Serial.println(rawADC); // Print the raw ADC value |
| 96 | + } |
| 97 | +} |
0 commit comments