Skip to content

Commit d7cd475

Browse files
committed
Initial implementation for MCP3421 18bit ADC
1 parent 82f2418 commit d7cd475

File tree

5 files changed

+128
-1
lines changed

5 files changed

+128
-1
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: 1 addition & 0 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

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: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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() {
50+
// Called when a MCP3421 component is deleted.
51+
delete _mcp3421;
52+
}
53+
54+
/*******************************************************************************/
55+
/*!
56+
@brief Initializes the MCP3421 sensor and begins I2C.
57+
@returns True if initialized successfully, False otherwise.
58+
*/
59+
/*******************************************************************************/
60+
bool begin() {
61+
_mcp3421 = new Adafruit_MCP3421();
62+
if (_mcp3421->begin((uint8_t)_sensorAddress, _i2c)) {
63+
WS_DEBUG_PRINTLN("Failed to find MCP3421 chip");
64+
return false;
65+
}
66+
67+
// NOTE: We should allow the gain to be set in future, like resolution
68+
// 12_BIT (240 SPS), 14_BIT (60 SPS), 16_BIT (15 SPS), 18_BIT (3.75 SPS)
69+
_mcp3421->setResolution(RESOLUTION_18_BIT);
70+
if (_mcp3421->getResolution() != RESOLUTION_18_BIT) {
71+
WS_DEBUG_PRINTLN("Failed to set resolution");
72+
return false;
73+
}
74+
75+
_mcp3421->setMode(MODE_ONE_SHOT);
76+
if (_mcp3421->getMode() != MODE_ONE_SHOT) {
77+
WS_DEBUG_PRINTLN("Failed to set mode");
78+
return false;
79+
}
80+
return true;
81+
}
82+
83+
/*******************************************************************************/
84+
/*!
85+
@brief Reads a voltage sensor and converts the
86+
reading into the expected SI unit.
87+
@param voltageEvent
88+
voltage sensor reading, in volts.
89+
@returns True if the sensor event was obtained successfully, False
90+
otherwise.
91+
*/
92+
/*******************************************************************************/
93+
bool getEventVoltage(sensors_event_t *voltageEvent) {
94+
ulong start = millis();
95+
if (!_mcp3421->startOneShotConversion()) {
96+
WS_DEBUG_PRINTLN("Failed to start one-shot conversion");
97+
return false;
98+
}
99+
while (!_mcp3421->isReady()) {
100+
if (millis() - start > 1000) {
101+
WS_DEBUG_PRINTLN("Timeout waiting for conversion to complete");
102+
return false;
103+
}
104+
}
105+
voltageEvent->voltage = (float)_mcp3421->readADC();
106+
return true;
107+
}
108+
109+
protected:
110+
Adafruit_MCP3421 *_mcp3421; ///< Pointer to MCP3421 temperature sensor object
111+
};
112+
113+
#endif // WipperSnapper_I2C_Driver_MCP3421

0 commit comments

Comments
 (0)