Skip to content

Commit f6ab47f

Browse files
committed
really improve stability by turning on PGA capacitor!
1 parent 11200f7 commit f6ab47f

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

Adafruit_NAU7802.cpp

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,34 @@ bool Adafruit_NAU7802::begin(TwoWire *theWire) {
6363

6464
if (!reset())
6565
return false;
66+
if (!enable(true))
67+
return false;
6668
if (!setLDO(NAU7802_3V0))
6769
return false;
6870
if (!setGain(NAU7802_GAIN_128))
6971
return false;
7072
if (!setRate(NAU7802_RATE_10SPS))
7173
return false;
72-
if (!enable(true))
74+
75+
// disable ADC chopper clock
76+
Adafruit_I2CRegister adc_reg = Adafruit_I2CRegister(i2c_dev, NAU7802_ADC);
77+
Adafruit_I2CRegisterBits chop =
78+
Adafruit_I2CRegisterBits(&adc_reg, 2, 4); // # bits, bit_shift
79+
if (!chop.write(0x3))
80+
return false;
81+
82+
// use low ESR caps
83+
Adafruit_I2CRegister pga_reg = Adafruit_I2CRegister(i2c_dev, NAU7802_PGA);
84+
Adafruit_I2CRegisterBits ldomode =
85+
Adafruit_I2CRegisterBits(&pga_reg, 1, 6); // # bits, bit_shift
86+
if (!ldomode.write(0))
87+
return false;
88+
89+
// PGA stabilizer cap on output
90+
Adafruit_I2CRegister pwr_reg = Adafruit_I2CRegister(i2c_dev, NAU7802_POWER);
91+
Adafruit_I2CRegisterBits capen =
92+
Adafruit_I2CRegisterBits(&pwr_reg, 1, 7); // # bits, bit_shift
93+
if (!capen.write(1))
7394
return false;
7495

7596
return true;
@@ -286,3 +307,31 @@ NAU7802_SampleRate Adafruit_NAU7802::getRate(void) {
286307

287308
return (NAU7802_SampleRate)rate_select.read();
288309
}
310+
311+
/**************************************************************************/
312+
/*!
313+
@brief Perform the internal calibration procedure
314+
@param mode The calibration mode to perform: NAU7802_CALMOD_INTERNAL,
315+
NAU7802_CALMOD_OFFSET or NAU7802_CALMOD_GAIN
316+
@returns True on calibrations success
317+
*/
318+
/**************************************************************************/
319+
bool Adafruit_NAU7802::calibrate(NAU7802_Calibration mode) {
320+
Adafruit_I2CRegister ctrl2_reg = Adafruit_I2CRegister(i2c_dev, NAU7802_CTRL2);
321+
Adafruit_I2CRegisterBits cal_start =
322+
Adafruit_I2CRegisterBits(&ctrl2_reg, 1, 2); // # bits, bit_shift
323+
Adafruit_I2CRegisterBits cal_err =
324+
Adafruit_I2CRegisterBits(&ctrl2_reg, 1, 3); // # bits, bit_shift
325+
Adafruit_I2CRegisterBits cal_mod =
326+
Adafruit_I2CRegisterBits(&ctrl2_reg, 2, 0); // # bits, bit_shift
327+
328+
if (!cal_mod.write(mode))
329+
return false;
330+
if (!cal_start.write(true))
331+
return false;
332+
while (!cal_start.read()) {
333+
delay(10);
334+
}
335+
336+
return !cal_err.read();
337+
}

Adafruit_NAU7802.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#define NAU7802_CTRL1 0x01 ///< Control/config register #1
2626
#define NAU7802_CTRL2 0x02 ///< Control/config register #2
2727
#define NAU7802_ADCO_B2 0x12 ///< ADC ouput LSB
28+
#define NAU7802_ADC 0x15 ///< ADC / chopper control
29+
#define NAU7802_PGA 0x1B ///< PGA control
30+
#define NAU7802_POWER 0x1C ///< power control
2831
#define NAU7802_REVISION_ID 0x1F ///< Chip revision ID
2932

3033
/*! The possible LDO voltages */
@@ -61,6 +64,13 @@ typedef enum _sample_rates {
6164
NAU7802_RATE_320SPS = 7,
6265
} NAU7802_SampleRate;
6366

67+
/*! The possible calibration modes */
68+
typedef enum _calib_mode {
69+
NAU7802_CALMOD_INTERNAL = 0,
70+
NAU7802_CALMOD_OFFSET = 2,
71+
NAU7802_CALMOD_GAIN = 3,
72+
} NAU7802_Calibration;
73+
6474
/**************************************************************************/
6575
/*!
6676
@brief NAU7802 driver.
@@ -81,6 +91,7 @@ class Adafruit_NAU7802 {
8191
NAU7802_Gain getGain(void);
8292
bool setRate(NAU7802_SampleRate gain);
8393
NAU7802_SampleRate getRate(void);
94+
bool calibrate(NAU7802_Calibration mode);
8495

8596
private:
8697
Adafruit_I2CRegister *_pu_ctrl_reg = NULL;

examples/nau7802_test/nau7802_test.ino

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,30 @@ void setup() {
4646
case NAU7802_RATE_80SPS: Serial.println("80 SPS"); break;
4747
case NAU7802_RATE_320SPS: Serial.println("320 SPS"); break;
4848
}
49+
50+
// Take 10 readings to flush out readings
51+
for (uint8_t i=0; i<10; i++) {
52+
while (! nau.available()) delay(1);
53+
nau.read();
54+
}
55+
56+
while (! nau.calibrate(NAU7802_CALMOD_INTERNAL)) {
57+
Serial.println("Failed to calibrate internal offset, retrying!");
58+
delay(1000);
59+
}
60+
Serial.println("Calibrated internal offset");
61+
62+
while (! nau.calibrate(NAU7802_CALMOD_OFFSET)) {
63+
Serial.println("Failed to calibrate system offset, retrying!");
64+
delay(1000);
65+
}
66+
Serial.println("Calibrated system offset");
4967
}
5068

5169
void loop() {
5270
while (! nau.available()) {
5371
delay(1);
5472
}
55-
Serial.println("Ready!");
5673
int32_t val = nau.read();
5774
Serial.print("Read "); Serial.println(val);
58-
delay(1000);
5975
}

0 commit comments

Comments
 (0)