Skip to content

Commit 76ec81d

Browse files
committed
Add Example04 - not tested yet but works in my mind!
1 parent ba297b0 commit 76ec81d

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}

src/SparkFun_ADS1219.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,40 @@ bool SfeADS1219Driver::setInputMultiplexer(const ads1219_input_multiplexer_confi
8080
return (_theBus->writeRegisterByte(kSfeADS1219RegConfigWrite, config.byte) == kSTkErrOk); // Write the config register
8181
}
8282

83+
/// @brief Configure the gain.
84+
/// @return True if successful, false otherwise.
85+
bool setGain(const ads1219_gain_config_t gain)
86+
{
87+
sfe_ads1219_reg_cfg_t config;
88+
if (_theBus->readRegisterByte(kSfeADS1219RegConfigRead, config.byte) != kSTkErrOk) // Read the config register
89+
return false;
90+
config.gain = (uint8_t)gain; // Modify (only) the gain
91+
_adcGain = gain; // Update the local copy of the gain for voltage conversion
92+
return (_theBus->writeRegisterByte(kSfeADS1219RegConfigWrite, config.byte) == kSTkErrOk); // Write the config register
93+
}
94+
95+
/// @brief Configure the data rate (samples per second).
96+
/// @return True if successful, false otherwise.
97+
bool setDataRate(const ads1219_data_rate_config_t rate)
98+
{
99+
sfe_ads1219_reg_cfg_t config;
100+
if (_theBus->readRegisterByte(kSfeADS1219RegConfigRead, config.byte) != kSTkErrOk) // Read the config register
101+
return false;
102+
config.dr = (uint8_t)rate; // Modify (only) the data rate
103+
return (_theBus->writeRegisterByte(kSfeADS1219RegConfigWrite, config.byte) == kSTkErrOk); // Write the config register
104+
}
105+
106+
/// @brief Configure the voltage reference.
107+
/// @return True if successful, false otherwise.
108+
bool setVoltageReference(const ads1219_vref_config_t vRef)
109+
{
110+
sfe_ads1219_reg_cfg_t config;
111+
if (_theBus->readRegisterByte(kSfeADS1219RegConfigRead, config.byte) != kSTkErrOk) // Read the config register
112+
return false;
113+
config.vref = (uint8_t)vRef; // Modify (only) the voltage reference
114+
return (_theBus->writeRegisterByte(kSfeADS1219RegConfigWrite, config.byte) == kSTkErrOk); // Write the config register
115+
}
116+
83117
/// @brief Reads the ADC conversion data, converts it to a usable form, and
84118
/// saves it to the internal result variable.
85119
/// @return true if successful, false otherwise.

src/SparkFun_ADS1219.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ class SfeADS1219Driver
182182
/// @return True if successful, false otherwise.
183183
bool setInputMultiplexer(const ads1219_input_multiplexer_config_t config = ADS1219_CONFIG_MUX_DIFF_P0_N1);
184184

185+
/// @brief Configure the gain.
186+
/// @return True if successful, false otherwise.
187+
bool setGain(const ads1219_gain_config_t gain = ADS1219_GAIN_1);
188+
189+
/// @brief Configure the data rate (samples per second).
190+
/// @return True if successful, false otherwise.
191+
bool setDataRate(const ads1219_data_rate_config_t rate = ADS1219_DATA_RATE_20SPS);
192+
193+
/// @brief Configure the voltage reference.
194+
/// @return True if successful, false otherwise.
195+
bool setVoltageReference(const ads1219_vref_config_t vRef = ADS1219_VREF_INTERNAL);
196+
185197
/// @brief Return the conversion result which was read by readConversion.
186198
/// Convert it to mV using referenceVoltageMillivolts and the _adcGain.
187199
/// @param referenceVoltageMillivolts Usually the internal 2.048V reference voltage.

0 commit comments

Comments
 (0)