Skip to content

Commit

Permalink
Merge pull request #587 from adafruit/add-MCP3421-18bit-ADC
Browse files Browse the repository at this point in the history
Add-MCP3421-18bit-ADC
  • Loading branch information
tyeth authored May 21, 2024
2 parents 36d4397 + 3745e31 commit a4e63dc
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 10 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
category=Communication
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
architectures=*
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
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
30 changes: 21 additions & 9 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ lib_deps =
adafruit/Adafruit Si7021 Library
adafruit/Adafruit VCNL4020 Library
adafruit/Adafruit VCNL4040
adafruit/Adafruit MCP3421
adafruit/Adafruit MCP9808 Library
adafruit/Adafruit MCP9600 Library
adafruit/Adafruit MPL115A2
Expand Down Expand Up @@ -139,28 +140,33 @@ board_build.filesystem = littlefs
[env:featheresp32s2]
extends = common:esp32
board = featheresp32-s2
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S2
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S2 -DBOARD_HAS_PSRAM
board_build.partitions = tinyuf2-partitions-4MB.csv
extra_scripts = pre:rename_usb_config.py

; Adafruit Feather ESP32-S2 TFT
[env:adafruit_feather_esp32s2_tft]
extends = common:esp32
board = adafruit_feather_esp32s2_tft
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S2_TFT
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S2_TFT -DBOARD_HAS_PSRAM
board_build.partitions = tinyuf2-partitions-4MB.csv
extra_scripts = pre:rename_usb_config.py

; Adafruit Feather ESP32-S2 Reverse TFT
[env:adafruit_feather_esp32s2_reversetft]
extends = common:esp32
board = adafruit_feather_esp32s2_reversetft
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S2_REVTFT
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S2_REVTFT -DBOARD_HAS_PSRAM
board_build.partitions = tinyuf2-partitions-4MB.csv
extra_scripts = pre:rename_usb_config.py

; Adafruit Feather ESP32-S3 2MB PSRAM
[env:adafruit_feather_esp32s3]
extends = common:esp32
board = adafruit_feather_esp32s3
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S3
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S3 -DBOARD_HAS_PSRAM
;set partition to tinyuf2-partitions-4MB.csv as of idf 5.1
board_build.partitions = tinyuf2-partitions-4MB.csv
extra_scripts = pre:rename_usb_config.py

; Adafruit Feather ESP32-S3 NO PSRAM
Expand All @@ -176,7 +182,7 @@ extends = common:esp32
build_type = debug
debug_tool = esp-builtin
board = adafruit_feather_esp32s3_tft
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S3_TFT
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S3_TFT -DBOARD_HAS_PSRAM
;set partition to tinyuf2-partitions-4MB.csv as of idf 5.1
board_build.partitions = tinyuf2-partitions-4MB.csv
extra_scripts = pre:rename_usb_config.py
Expand All @@ -185,7 +191,7 @@ extra_scripts = pre:rename_usb_config.py
[env:adafruit_feather_esp32s3_reversetft]
extends = common:esp32
board = adafruit_feather_esp32s3_reversetft
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S3_REVTFT
build_flags = -DARDUINO_ADAFRUIT_FEATHER_ESP32S3_REVTFT -DBOARD_HAS_PSRAM
;set partition to tinyuf2-partitions-4MB.csv as of idf 5.1
board_build.partitions = tinyuf2-partitions-4MB.csv
extra_scripts = pre:rename_usb_config.py
Expand All @@ -194,14 +200,18 @@ extra_scripts = pre:rename_usb_config.py
[env:adafruit_magtag29_esp32s2]
extends = common:esp32
board = adafruit_magtag29_esp32s2
build_flags = -DARDUINO_MAGTAG29_ESP32S2
build_flags = -DARDUINO_MAGTAG29_ESP32S2 -DBOARD_HAS_PSRAM
;set partition to tinyuf2-partitions-4MB.csv as of idf 5.1
board_build.partitions = tinyuf2-partitions-4MB.csv
extra_scripts = pre:rename_usb_config.py

; Adafruit Metro ESP32-S2
[env:adafruit_metro_esp32s2]
extends = common:esp32
board = adafruit_metro_esp32s2
build_flags = -DARDUINO_METRO_ESP32S2
build_flags = -DARDUINO_METRO_ESP32S2 -DBOARD_HAS_PSRAM
;set partition to tinyuf2-partitions-4MB.csv as of idf 5.1
board_build.partitions = tinyuf2-partitions-4MB.csv
extra_scripts = pre:rename_usb_config.py

; Adafruit QT Py ESP32 Pico
Expand All @@ -225,7 +235,9 @@ board_build.partitions = min_spiffs.csv
[env:adafruit_qtpy_esp32s2]
extends = common:esp32
board = adafruit_qtpy_esp32s2
build_flags = -DARDUINO_ADAFRUIT_QTPY_ESP32S2
build_flags = -DARDUINO_ADAFRUIT_QTPY_ESP32S2 -DBOARD_HAS_PSRAM
;set partition to tinyuf2-partitions-4MB.csv as of idf 5.1
board_build.partitions = tinyuf2-partitions-4MB.csv
extra_scripts = pre:rename_usb_config.py

; Adafruit QT Py ESP32-S3 NO PSRAM
Expand Down
11 changes: 11 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_si7021->configureDriver(msgDeviceInitReq);
drivers.push_back(_si7021);
WS_DEBUG_PRINTLN("SI7021/SHT20 Initialized Successfully!");
} else if (strcmp("mcp3421", msgDeviceInitReq->i2c_device_name) == 0) {
_mcp3421 = new WipperSnapper_I2C_Driver_MCP3421(this->_i2c, i2cAddress);
if (!_mcp3421->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize MCP3421!");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_mcp3421->configureDriver(msgDeviceInitReq);
drivers.push_back(_mcp3421);
WS_DEBUG_PRINTLN("MCP3421 Initialized Successfully!");
} else if (strcmp("mcp9808", msgDeviceInitReq->i2c_device_name) == 0) {
_mcp9808 = new WipperSnapper_I2C_Driver_MCP9808(this->_i2c, i2cAddress);
if (!_mcp9808->begin()) {
Expand Down
2 changes: 2 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "drivers/WipperSnapper_I2C_Driver_LTR329_LTR303.h"
#include "drivers/WipperSnapper_I2C_Driver_LTR390.h"
#include "drivers/WipperSnapper_I2C_Driver_MAX17048.h"
#include "drivers/WipperSnapper_I2C_Driver_MCP3421.h"
#include "drivers/WipperSnapper_I2C_Driver_MCP9808.h"
#include "drivers/WipperSnapper_I2C_Driver_MPL115A2.h"
#include "drivers/WipperSnapper_I2C_Driver_MPRLS.h"
Expand Down Expand Up @@ -128,6 +129,7 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_INA219 *_ina219 = nullptr;
WipperSnapper_I2C_Driver_LTR329_LTR303 *_ltr329 = nullptr;
WipperSnapper_I2C_Driver_LTR390 *_ltr390 = nullptr;
WipperSnapper_I2C_Driver_MCP3421 *_mcp3421 = nullptr;
WipperSnapper_I2C_Driver_MCP9808 *_mcp9808 = nullptr;
WipperSnapper_I2C_Driver_MPL115A2 *_mpl115a2 = nullptr;
WipperSnapper_I2C_Driver_MPRLS *_mprls = nullptr;
Expand Down
133 changes: 133 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_MCP3421.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*!
* @file WipperSnapper_I2C_Driver_MCP3421.h
*
* Device driver for the MCP3421 18-bit ADC sensor.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Tyeth Gundry 2024 for Adafruit Industries.
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef WipperSnapper_I2C_Driver_MCP3421_H
#define WipperSnapper_I2C_Driver_MCP3421_H

#include "WipperSnapper_I2C_Driver.h"
#include "Wippersnapper.h"
#include <Adafruit_MCP3421.h>

/**************************************************************************/
/*!
@brief Class that provides a driver interface for a MCP3421 sensor.
*/
/**************************************************************************/
class WipperSnapper_I2C_Driver_MCP3421 : public WipperSnapper_I2C_Driver {
public:
/*******************************************************************************/
/*!
@brief Constructor for the MCP3421 sensor.
@param i2c
The I2C interface.
@param sensorAddress
7-bit device address.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_MCP3421(TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
_i2c = i2c;
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
/*!
@brief Destructor for an MCP3421 sensor.
*/
/*******************************************************************************/
~WipperSnapper_I2C_Driver_MCP3421() { delete _mcp3421; }

/*******************************************************************************/
/*!
@brief Initializes the MCP3421 sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() {
_mcp3421 = new Adafruit_MCP3421();
if (!_mcp3421->begin((uint8_t)_sensorAddress, _i2c)) {
WS_DEBUG_PRINTLN("Failed to find MCP3421 chip");
return false;
}

if (!configureSensor()) {
WS_DEBUG_PRINTLN("Failed to configure MCP3421 sensor");
return false;
}
return true;
}

/*******************************************************************************/
/*!
@brief Configures the MCP3421 sensor.
@returns True if the sensor was configured successfully, False otherwise.
*/
/*******************************************************************************/
bool configureSensor() {
// NOTE: We should allow the gain to be set in future, like resolution
// 12_BIT (240 SPS), 14_BIT (60 SPS), 16_BIT (15 SPS), 18_BIT (3.75 SPS)
_mcp3421->setResolution(RESOLUTION_18_BIT);
if (_mcp3421->getResolution() != RESOLUTION_18_BIT) {
WS_DEBUG_PRINTLN("Failed to set resolution to 18-bit");
return false;
}

_mcp3421->setGain(GAIN_8X);
if (_mcp3421->getGain() != GAIN_8X) {
WS_DEBUG_PRINTLN("Failed to set gain to 8x");
return false;
}

_mcp3421->setMode(MODE_ONE_SHOT);
if (_mcp3421->getMode() != MODE_ONE_SHOT) {
WS_DEBUG_PRINTLN("Failed to set mode to One-Shot");
return false;
}
return true;
}

/*******************************************************************************/
/*!
@brief Reads the ADC sensor with short wait for data.
@param rawEvent
ADC sensor reading
@returns True if the sensor event was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool getEventRaw(sensors_event_t *rawEvent) {
ulong start = millis();
if (!_mcp3421->startOneShotConversion()) {
WS_DEBUG_PRINTLN("Failed to start one-shot conversion");
return false;
}
while (!_mcp3421->isReady()) {
ulong newMillis = millis();
if (newMillis - start > 500) {
WS_DEBUG_PRINTLN("Timeout waiting for conversion to complete");
return false;
} else if (start > newMillis) {
start = millis(); // rollover
}
delay(50);
}
rawEvent->data[0] = (float)_mcp3421->readADC();
return true;
}

protected:
Adafruit_MCP3421 *_mcp3421; ///< Pointer to MCP3421 sensor object
};

#endif // WipperSnapper_I2C_Driver_MCP3421

0 comments on commit a4e63dc

Please sign in to comment.