Skip to content

Commit d102bc7

Browse files
authored
Merge pull request #577 from tyeth/add-NAU7802
Add NAU7802
2 parents a4e63dc + 0089c50 commit d102bc7

File tree

7 files changed

+160
-3
lines changed

7 files changed

+160
-3
lines changed

.vscode/settings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"limits": "c",
44
"type_traits": "c"
55
},
6-
"C_Cpp.dimInactiveRegions": false,
7-
"dotnet.defaultSolution": "disable"
6+
"C_Cpp.dimInactiveRegions": true,
7+
"dotnet.defaultSolution": "disable",
8+
"cmake.configureOnOpen": false
89
}

ci

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 72918f8b8dd8655f5fc5e2a58f133a1656e43d7f

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
77
category=Communication
88
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
99
architectures=*
10-
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork
10+
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ lib_deps =
4747
adafruit/Adafruit MPL115A2
4848
adafruit/Adafruit MPRLS Library
4949
adafruit/Adafruit MS8607
50+
adafruit/Adafruit NAU7802 Library
5051
adafruit/Adafruit TMP117
5152
adafruit/Adafruit TSL2591 Library
5253
adafruit/Adafruit_VL53L0X

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
386386
_ltr329->configureDriver(msgDeviceInitReq);
387387
drivers.push_back(_ltr329);
388388
WS_DEBUG_PRINTLN("LTR329/303 Initialized Successfully!");
389+
} else if (strcmp("nau7802", msgDeviceInitReq->i2c_device_name) == 0) {
390+
_nau7802 = new WipperSnapper_I2C_Driver_NAU7802(this->_i2c, i2cAddress);
391+
if (!_nau7802->begin()) {
392+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize NAU7802");
393+
_busStatusResponse =
394+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
395+
return false;
396+
}
397+
_nau7802->configureDriver(msgDeviceInitReq);
398+
drivers.push_back(_nau7802);
399+
WS_DEBUG_PRINTLN("NAU7802 Initialized Successfully!");
389400
} else if (strcmp("sgp30", msgDeviceInitReq->i2c_device_name) == 0) {
390401
_sgp30 = new WipperSnapper_I2C_Driver_SGP30(this->_i2c, i2cAddress);
391402
if (!_sgp30->begin()) {

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "drivers/WipperSnapper_I2C_Driver_MPL115A2.h"
4646
#include "drivers/WipperSnapper_I2C_Driver_MPRLS.h"
4747
#include "drivers/WipperSnapper_I2C_Driver_MS8607.h"
48+
#include "drivers/WipperSnapper_I2C_Driver_NAU7802.h"
4849
#include "drivers/WipperSnapper_I2C_Driver_PCT2075.h"
4950
#include "drivers/WipperSnapper_I2C_Driver_PM25.h"
5051
#include "drivers/WipperSnapper_I2C_Driver_SCD30.h"
@@ -134,6 +135,7 @@ class WipperSnapper_Component_I2C {
134135
WipperSnapper_I2C_Driver_MPL115A2 *_mpl115a2 = nullptr;
135136
WipperSnapper_I2C_Driver_MPRLS *_mprls = nullptr;
136137
WipperSnapper_I2C_Driver_MS8607 *_ms8607 = nullptr;
138+
WipperSnapper_I2C_Driver_NAU7802 *_nau7802 = nullptr;
137139
WipperSnapper_I2C_Driver_TMP117 *_tmp117 = nullptr;
138140
WipperSnapper_I2C_Driver_TSL2591 *_tsl2591 = nullptr;
139141
WipperSnapper_I2C_Driver_VCNL4020 *_vcnl4020 = nullptr;
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_NAU7802.h
3+
*
4+
* Device driver for the NAU7802 24bit ADC / load cell breakout
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Tyeth Gundry for Adafruit Industries 2024
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
16+
#ifndef WipperSnapper_I2C_Driver_NAU7802_H
17+
#define WipperSnapper_I2C_Driver_NAU7802_H
18+
19+
#include "WipperSnapper_I2C_Driver.h"
20+
#include <Adafruit_NAU7802.h>
21+
22+
#define NAU7802_TIMEOUT_MS 250 ///< Timeout waiting for data from NAU7802
23+
24+
/**************************************************************************/
25+
/*!
26+
@brief Class that provides a driver interface for the NAU7802.
27+
*/
28+
/**************************************************************************/
29+
class WipperSnapper_I2C_Driver_NAU7802 : public WipperSnapper_I2C_Driver {
30+
public:
31+
/*******************************************************************************/
32+
/*!
33+
@brief Constructor for an NAU7802.
34+
@param i2c
35+
The I2C interface.
36+
@param sensorAddress
37+
7-bit device address.
38+
*/
39+
/*******************************************************************************/
40+
WipperSnapper_I2C_Driver_NAU7802(TwoWire *i2c, uint16_t sensorAddress)
41+
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
42+
_i2c = i2c;
43+
_sensorAddress = sensorAddress;
44+
_nau7802 = new Adafruit_NAU7802();
45+
}
46+
47+
/*******************************************************************************/
48+
/*!
49+
@brief Destructor for an NAU7802.
50+
*/
51+
/*******************************************************************************/
52+
~WipperSnapper_I2C_Driver_NAU7802() { _nau7802 = nullptr; }
53+
54+
/*******************************************************************************/
55+
/*!
56+
@brief Initializes the NAU7802 sensor and begins I2C.
57+
@returns True if initialized successfully, False otherwise.
58+
*/
59+
/*******************************************************************************/
60+
bool begin() { return _nau7802->begin(_i2c) && configure_nau7802(); }
61+
62+
/*******************************************************************************/
63+
/*!
64+
@brief Configures the NAU7802 sensor.
65+
@returns True if configured successfully, False otherwise.
66+
*/
67+
/*******************************************************************************/
68+
bool configure_nau7802() {
69+
if (!_nau7802->setLDO(NAU7802_3V0)) {
70+
WS_DEBUG_PRINTLN("Failed to set LDO to 3V0");
71+
return false;
72+
}
73+
74+
if (!_nau7802->setGain(NAU7802_GAIN_128)) {
75+
WS_DEBUG_PRINTLN("Failed to set gain to 128");
76+
return false;
77+
}
78+
79+
if (!_nau7802->setRate(NAU7802_RATE_10SPS) &&
80+
!_nau7802->setRate(NAU7802_RATE_10SPS)) {
81+
WS_DEBUG_PRINTLN("Failed to set sample rate to 10SPS");
82+
return false;
83+
}
84+
85+
// Take 10 readings to flush out old readings (10 samples per second)
86+
flushNAU7802(10);
87+
88+
for (int retries = 0; retries < 3; retries++) {
89+
if (_nau7802->calibrate(NAU7802_CALMOD_INTERNAL)) {
90+
WS_DEBUG_PRINTLN("Calibrated internal offset");
91+
return true;
92+
}
93+
WS_DEBUG_PRINTLN("Failed to calibrate internal offset, retrying!");
94+
delay(1000);
95+
}
96+
WS_DEBUG_PRINTLN("ERROR: Failed to calibrate internal offset of NAU7802.");
97+
return false;
98+
}
99+
100+
/*******************************************************************************/
101+
/*!
102+
@brief Gets datapoints from sensor and discards (flushes old data).
103+
@param count
104+
Number of readings to discard.
105+
*/
106+
/*******************************************************************************/
107+
void flushNAU7802(uint8_t count) {
108+
for (uint8_t skipCounter = 0; skipCounter < count; skipCounter++) {
109+
while (!_nau7802->available())
110+
delay(1);
111+
_nau7802->read();
112+
}
113+
}
114+
115+
/*******************************************************************************/
116+
/*!
117+
@brief Gets the sensor's raw "force" value.
118+
@param rawEvent
119+
Pointer to an Adafruit_Sensor event.
120+
@returns True if the reading was obtained successfully, False otherwise.
121+
*/
122+
/*******************************************************************************/
123+
bool getEventRaw(sensors_event_t *rawEvent) {
124+
unsigned long start = millis();
125+
126+
// Wait for the sensor to be ready
127+
while (!_nau7802->available()) {
128+
if (millis() - start > NAU7802_TIMEOUT_MS) {
129+
WS_DEBUG_PRINTLN("NAU7802 data not available");
130+
return false;
131+
}
132+
}
133+
rawEvent->data[0] = (float)_nau7802->read();
134+
return true;
135+
}
136+
137+
protected:
138+
Adafruit_NAU7802 *_nau7802 = nullptr; ///< NAU7802 object
139+
};
140+
141+
#endif // WipperSnapper_I2C_Driver_NAU7802_H

0 commit comments

Comments
 (0)