|
| 1 | +/* |
| 2 | + Configuring the ADS1219 ADC voltage reference. |
| 3 | +
|
| 4 | + By default, the ADS1219 uses an internal 2.048V voltage reference for each conversion. |
| 5 | + This means, by default, the voltage measurements are limited to +/-2.048V. |
| 6 | + But it can also be configured to use the REFP and REFN pins, allowing the reference |
| 7 | + volatge to be defined by the user. |
| 8 | + In this example, use jumper wires to set REFP to VDDA (3.3V) and REFN to 0V (GND). |
| 9 | + This allows voltage measurements in the range +/-3.3V. |
| 10 | +
|
| 11 | + By: Paul Clark |
| 12 | + SparkFun Electronics |
| 13 | + Date: 2024/02/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 | +
|
| 21 | + REFP and REFN: |
| 22 | + Use jumper wires or jumper links (https://www.sparkfun.com/products/9044) to connect: |
| 23 | + REFP --> VDDA (3.3V) |
| 24 | + REFN --> GND |
| 25 | +
|
| 26 | + Open the serial monitor at 115200 baud to see the voltage. |
| 27 | +
|
| 28 | + Feel like supporting our work? Buy a board from SparkFun! |
| 29 | + https://www.sparkfun.com/products/23455 - Qwiic ADS1219 1x1 |
| 30 | +*/ |
| 31 | + |
| 32 | +// You will need the SparkFun Toolkit. Click here to get it: http://librarymanager/All#SparkFun_Toolkit |
| 33 | + |
| 34 | +#include <SparkFun_ADS1219.h> // Click here to get the library: http://librarymanager/All#SparkFun_ADS1219 |
| 35 | + |
| 36 | +SfeADS1219ArdI2C myADC; |
| 37 | + |
| 38 | +void setup() |
| 39 | +{ |
| 40 | + delay(1000); // Allow time for the microcontroller to start up |
| 41 | + |
| 42 | + Serial.begin(115200); // Begin the Serial console |
| 43 | + while (!Serial) |
| 44 | + { |
| 45 | + delay(100); // Wait for the user to open the Serial Monitor |
| 46 | + } |
| 47 | + Serial.println("SparkFun ADS1219 Example"); |
| 48 | + |
| 49 | + Wire.begin(); // Begin the I2C bus |
| 50 | + |
| 51 | + // Initialize ADC - this also performs a soft reset |
| 52 | + while (myADC.begin() == false) |
| 53 | + { |
| 54 | + Serial.println("ADC failed to begin. Please check your wiring! Retrying..."); |
| 55 | + delay(1000); |
| 56 | + } |
| 57 | + |
| 58 | + // Configure the input multiplexer. Options are: |
| 59 | + // ADS1219_CONFIG_MUX_DIFF_P0_N1 (Default) |
| 60 | + // ADS1219_CONFIG_MUX_DIFF_P2_N3 |
| 61 | + // ADS1219_CONFIG_MUX_DIFF_P1_N2 |
| 62 | + // ADS1219_CONFIG_MUX_SINGLE_0 |
| 63 | + // ADS1219_CONFIG_MUX_SINGLE_1 |
| 64 | + // ADS1219_CONFIG_MUX_SINGLE_2 |
| 65 | + // ADS1219_CONFIG_MUX_SINGLE_3 |
| 66 | + // ADS1219_CONFIG_MUX_SHORTED |
| 67 | + myADC.setInputMultiplexer(ADS1219_CONFIG_MUX_DIFF_P0_N1); // Read the differential voltage between AIN0 (+) and AIN1 (-) |
| 68 | + |
| 69 | + // Configure the voltage reference. Options are: |
| 70 | + // ADS1219_VREF_INTERNAL (The internal 2.048V reference. Default) |
| 71 | + // ADS1219_VREF_EXTERNAL (The REFP and REFN pins) |
| 72 | + // Note: for the external reference, we need to tell getConversionMillivolts what the reference voltage is. See below. |
| 73 | + myADC.setVoltageReference(ADS1219_VREF_EXTERNAL); // Use the REFP and REFN pins |
| 74 | + |
| 75 | + Serial.println("ADC initialized"); |
| 76 | +} |
| 77 | + |
| 78 | +void loop() |
| 79 | +{ |
| 80 | + myADC.startSync(); // Start a single-shot conversion. |
| 81 | + |
| 82 | + while (myADC.dataReady() == false) // Check if the conversion is complete. This will return true if data is ready. |
| 83 | + { |
| 84 | + delay(10); // The conversion is not complete. Wait a little to avoid pounding the I2C bus. |
| 85 | + } |
| 86 | + |
| 87 | + myADC.readConversion(); // Read the conversion result from the ADC. Store it internally. |
| 88 | + float milliVolts = myADC.getConversionMillivolts(3300.0); // Convert to millivolts - using a reference of 3300mV (3.3V) |
| 89 | + Serial.print("ADC voltage (mV): "); |
| 90 | + Serial.println(milliVolts, 3); // Print milliVolts with 3 decimal places |
| 91 | +} |
0 commit comments