Skip to content

Commit a4e63dc

Browse files
authored
Merge pull request #587 from adafruit/add-MCP3421-18bit-ADC
Add-MCP3421-18bit-ADC
2 parents 36d4397 + 3745e31 commit a4e63dc

File tree

5 files changed

+168
-10
lines changed

5 files changed

+168
-10
lines changed

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 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 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: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ lib_deps =
4141
adafruit/Adafruit Si7021 Library
4242
adafruit/Adafruit VCNL4020 Library
4343
adafruit/Adafruit VCNL4040
44+
adafruit/Adafruit MCP3421
4445
adafruit/Adafruit MCP9808 Library
4546
adafruit/Adafruit MCP9600 Library
4647
adafruit/Adafruit MPL115A2
@@ -139,28 +140,33 @@ board_build.filesystem = littlefs
139140
[env:featheresp32s2]
140141
extends = common:esp32
141142
board = featheresp32-s2
142-
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S2
143+
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S2 -DBOARD_HAS_PSRAM
144+
board_build.partitions = tinyuf2-partitions-4MB.csv
143145
extra_scripts = pre:rename_usb_config.py
144146

145147
; Adafruit Feather ESP32-S2 TFT
146148
[env:adafruit_feather_esp32s2_tft]
147149
extends = common:esp32
148150
board = adafruit_feather_esp32s2_tft
149-
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S2_TFT
151+
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S2_TFT -DBOARD_HAS_PSRAM
152+
board_build.partitions = tinyuf2-partitions-4MB.csv
150153
extra_scripts = pre:rename_usb_config.py
151154

152155
; Adafruit Feather ESP32-S2 Reverse TFT
153156
[env:adafruit_feather_esp32s2_reversetft]
154157
extends = common:esp32
155158
board = adafruit_feather_esp32s2_reversetft
156-
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S2_REVTFT
159+
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S2_REVTFT -DBOARD_HAS_PSRAM
160+
board_build.partitions = tinyuf2-partitions-4MB.csv
157161
extra_scripts = pre:rename_usb_config.py
158162

159163
; Adafruit Feather ESP32-S3 2MB PSRAM
160164
[env:adafruit_feather_esp32s3]
161165
extends = common:esp32
162166
board = adafruit_feather_esp32s3
163-
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S3
167+
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S3 -DBOARD_HAS_PSRAM
168+
;set partition to tinyuf2-partitions-4MB.csv as of idf 5.1
169+
board_build.partitions = tinyuf2-partitions-4MB.csv
164170
extra_scripts = pre:rename_usb_config.py
165171

166172
; Adafruit Feather ESP32-S3 NO PSRAM
@@ -176,7 +182,7 @@ extends = common:esp32
176182
build_type = debug
177183
debug_tool = esp-builtin
178184
board = adafruit_feather_esp32s3_tft
179-
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S3_TFT
185+
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S3_TFT -DBOARD_HAS_PSRAM
180186
;set partition to tinyuf2-partitions-4MB.csv as of idf 5.1
181187
board_build.partitions = tinyuf2-partitions-4MB.csv
182188
extra_scripts = pre:rename_usb_config.py
@@ -185,7 +191,7 @@ extra_scripts = pre:rename_usb_config.py
185191
[env:adafruit_feather_esp32s3_reversetft]
186192
extends = common:esp32
187193
board = adafruit_feather_esp32s3_reversetft
188-
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S3_REVTFT
194+
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S3_REVTFT -DBOARD_HAS_PSRAM
189195
;set partition to tinyuf2-partitions-4MB.csv as of idf 5.1
190196
board_build.partitions = tinyuf2-partitions-4MB.csv
191197
extra_scripts = pre:rename_usb_config.py
@@ -194,14 +200,18 @@ extra_scripts = pre:rename_usb_config.py
194200
[env:adafruit_magtag29_esp32s2]
195201
extends = common:esp32
196202
board = adafruit_magtag29_esp32s2
197-
build_flags = -DARDUINO_MAGTAG29_ESP32S2
203+
build_flags = -DARDUINO_MAGTAG29_ESP32S2 -DBOARD_HAS_PSRAM
204+
;set partition to tinyuf2-partitions-4MB.csv as of idf 5.1
205+
board_build.partitions = tinyuf2-partitions-4MB.csv
198206
extra_scripts = pre:rename_usb_config.py
199207

200208
; Adafruit Metro ESP32-S2
201209
[env:adafruit_metro_esp32s2]
202210
extends = common:esp32
203211
board = adafruit_metro_esp32s2
204-
build_flags = -DARDUINO_METRO_ESP32S2
212+
build_flags = -DARDUINO_METRO_ESP32S2 -DBOARD_HAS_PSRAM
213+
;set partition to tinyuf2-partitions-4MB.csv as of idf 5.1
214+
board_build.partitions = tinyuf2-partitions-4MB.csv
205215
extra_scripts = pre:rename_usb_config.py
206216

207217
; Adafruit QT Py ESP32 Pico
@@ -225,7 +235,9 @@ board_build.partitions = min_spiffs.csv
225235
[env:adafruit_qtpy_esp32s2]
226236
extends = common:esp32
227237
board = adafruit_qtpy_esp32s2
228-
build_flags = -DARDUINO_ADAFRUIT_QTPY_ESP32S2
238+
build_flags = -DARDUINO_ADAFRUIT_QTPY_ESP32S2 -DBOARD_HAS_PSRAM
239+
;set partition to tinyuf2-partitions-4MB.csv as of idf 5.1
240+
board_build.partitions = tinyuf2-partitions-4MB.csv
229241
extra_scripts = pre:rename_usb_config.py
230242

231243
; Adafruit QT Py ESP32-S3 NO PSRAM

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
420420
_si7021->configureDriver(msgDeviceInitReq);
421421
drivers.push_back(_si7021);
422422
WS_DEBUG_PRINTLN("SI7021/SHT20 Initialized Successfully!");
423+
} else if (strcmp("mcp3421", msgDeviceInitReq->i2c_device_name) == 0) {
424+
_mcp3421 = new WipperSnapper_I2C_Driver_MCP3421(this->_i2c, i2cAddress);
425+
if (!_mcp3421->begin()) {
426+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize MCP3421!");
427+
_busStatusResponse =
428+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
429+
return false;
430+
}
431+
_mcp3421->configureDriver(msgDeviceInitReq);
432+
drivers.push_back(_mcp3421);
433+
WS_DEBUG_PRINTLN("MCP3421 Initialized Successfully!");
423434
} else if (strcmp("mcp9808", msgDeviceInitReq->i2c_device_name) == 0) {
424435
_mcp9808 = new WipperSnapper_I2C_Driver_MCP9808(this->_i2c, i2cAddress);
425436
if (!_mcp9808->begin()) {

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "drivers/WipperSnapper_I2C_Driver_LTR329_LTR303.h"
4141
#include "drivers/WipperSnapper_I2C_Driver_LTR390.h"
4242
#include "drivers/WipperSnapper_I2C_Driver_MAX17048.h"
43+
#include "drivers/WipperSnapper_I2C_Driver_MCP3421.h"
4344
#include "drivers/WipperSnapper_I2C_Driver_MCP9808.h"
4445
#include "drivers/WipperSnapper_I2C_Driver_MPL115A2.h"
4546
#include "drivers/WipperSnapper_I2C_Driver_MPRLS.h"
@@ -128,6 +129,7 @@ class WipperSnapper_Component_I2C {
128129
WipperSnapper_I2C_Driver_INA219 *_ina219 = nullptr;
129130
WipperSnapper_I2C_Driver_LTR329_LTR303 *_ltr329 = nullptr;
130131
WipperSnapper_I2C_Driver_LTR390 *_ltr390 = nullptr;
132+
WipperSnapper_I2C_Driver_MCP3421 *_mcp3421 = nullptr;
131133
WipperSnapper_I2C_Driver_MCP9808 *_mcp9808 = nullptr;
132134
WipperSnapper_I2C_Driver_MPL115A2 *_mpl115a2 = nullptr;
133135
WipperSnapper_I2C_Driver_MPRLS *_mprls = nullptr;
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_MCP3421.h
3+
*
4+
* Device driver for the MCP3421 18-bit ADC sensor.
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 2024 for Adafruit Industries.
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#ifndef WipperSnapper_I2C_Driver_MCP3421_H
16+
#define WipperSnapper_I2C_Driver_MCP3421_H
17+
18+
#include "WipperSnapper_I2C_Driver.h"
19+
#include "Wippersnapper.h"
20+
#include <Adafruit_MCP3421.h>
21+
22+
/**************************************************************************/
23+
/*!
24+
@brief Class that provides a driver interface for a MCP3421 sensor.
25+
*/
26+
/**************************************************************************/
27+
class WipperSnapper_I2C_Driver_MCP3421 : public WipperSnapper_I2C_Driver {
28+
public:
29+
/*******************************************************************************/
30+
/*!
31+
@brief Constructor for the MCP3421 sensor.
32+
@param i2c
33+
The I2C interface.
34+
@param sensorAddress
35+
7-bit device address.
36+
*/
37+
/*******************************************************************************/
38+
WipperSnapper_I2C_Driver_MCP3421(TwoWire *i2c, uint16_t sensorAddress)
39+
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
40+
_i2c = i2c;
41+
_sensorAddress = sensorAddress;
42+
}
43+
44+
/*******************************************************************************/
45+
/*!
46+
@brief Destructor for an MCP3421 sensor.
47+
*/
48+
/*******************************************************************************/
49+
~WipperSnapper_I2C_Driver_MCP3421() { delete _mcp3421; }
50+
51+
/*******************************************************************************/
52+
/*!
53+
@brief Initializes the MCP3421 sensor and begins I2C.
54+
@returns True if initialized successfully, False otherwise.
55+
*/
56+
/*******************************************************************************/
57+
bool begin() {
58+
_mcp3421 = new Adafruit_MCP3421();
59+
if (!_mcp3421->begin((uint8_t)_sensorAddress, _i2c)) {
60+
WS_DEBUG_PRINTLN("Failed to find MCP3421 chip");
61+
return false;
62+
}
63+
64+
if (!configureSensor()) {
65+
WS_DEBUG_PRINTLN("Failed to configure MCP3421 sensor");
66+
return false;
67+
}
68+
return true;
69+
}
70+
71+
/*******************************************************************************/
72+
/*!
73+
@brief Configures the MCP3421 sensor.
74+
@returns True if the sensor was configured successfully, False otherwise.
75+
*/
76+
/*******************************************************************************/
77+
bool configureSensor() {
78+
// NOTE: We should allow the gain to be set in future, like resolution
79+
// 12_BIT (240 SPS), 14_BIT (60 SPS), 16_BIT (15 SPS), 18_BIT (3.75 SPS)
80+
_mcp3421->setResolution(RESOLUTION_18_BIT);
81+
if (_mcp3421->getResolution() != RESOLUTION_18_BIT) {
82+
WS_DEBUG_PRINTLN("Failed to set resolution to 18-bit");
83+
return false;
84+
}
85+
86+
_mcp3421->setGain(GAIN_8X);
87+
if (_mcp3421->getGain() != GAIN_8X) {
88+
WS_DEBUG_PRINTLN("Failed to set gain to 8x");
89+
return false;
90+
}
91+
92+
_mcp3421->setMode(MODE_ONE_SHOT);
93+
if (_mcp3421->getMode() != MODE_ONE_SHOT) {
94+
WS_DEBUG_PRINTLN("Failed to set mode to One-Shot");
95+
return false;
96+
}
97+
return true;
98+
}
99+
100+
/*******************************************************************************/
101+
/*!
102+
@brief Reads the ADC sensor with short wait for data.
103+
@param rawEvent
104+
ADC sensor reading
105+
@returns True if the sensor event was obtained successfully, False
106+
otherwise.
107+
*/
108+
/*******************************************************************************/
109+
bool getEventRaw(sensors_event_t *rawEvent) {
110+
ulong start = millis();
111+
if (!_mcp3421->startOneShotConversion()) {
112+
WS_DEBUG_PRINTLN("Failed to start one-shot conversion");
113+
return false;
114+
}
115+
while (!_mcp3421->isReady()) {
116+
ulong newMillis = millis();
117+
if (newMillis - start > 500) {
118+
WS_DEBUG_PRINTLN("Timeout waiting for conversion to complete");
119+
return false;
120+
} else if (start > newMillis) {
121+
start = millis(); // rollover
122+
}
123+
delay(50);
124+
}
125+
rawEvent->data[0] = (float)_mcp3421->readADC();
126+
return true;
127+
}
128+
129+
protected:
130+
Adafruit_MCP3421 *_mcp3421; ///< Pointer to MCP3421 sensor object
131+
};
132+
133+
#endif // WipperSnapper_I2C_Driver_MCP3421

0 commit comments

Comments
 (0)