Using the MCP3421 breakout with Arduino involves wiring up the breakout to your Arduino-compatible microcontroller, installing the Adafruit_MCP3421 library, and running the provided example code.
Wiring
Wire as shown for a 5V board like an Uno. If you are using a 3V board, like an Adafruit Feather, wire the board's 3V pin to the breakout VIN.
Here is an Adafruit Metro wired up to the breakout using the STEMMA QT connector:
-
Board 5V to breakout VIN (red wire)
-
Board GND to breakout GND (black wire)
-
Board SCL to breakout SCL (yellow wire)
- Board SDA to breakout SDA (blue wire)
Here is an Adafruit Metro wired up using a solderless breadboard:
-
Board 5V to breakout VIN (red wire)
-
Board GND to breakout GND (black wire)
-
Board SCL to breakout SCL (yellow wire)
- Board SDA to breakout SDA (blue wire)
Library Installation
You can install the Adafruit_MCP3421 library for Arduino using the Library Manager in the Arduino IDE.
Click the Manage Libraries ... menu item, search for Adafruit_MCP3421, and select the Adafruit MCP3421 library:
If asked about dependencies, click "Install all".
If the "Dependencies" window does not come up, then you already have the dependencies installed.
#include "Adafruit_MCP3421.h" Adafruit_MCP3421 mcp; void setup() { Serial.begin(115200); while (!Serial) { delay(10); // Wait for serial port to connect. Needed for native USB port only } // Begin can take an optional address and Wire interface if (!mcp.begin(0x68, &Wire)) { Serial.println("Failed to find MCP3421 chip"); while (1) { delay(10); // Avoid a busy-wait loop } } Serial.println("MCP3421 Found!"); // Options: GAIN_1X, GAIN_2X, GAIN_4X, GAIN_8X mcp.setGain(GAIN_1X); Serial.print("Gain set to: "); switch (mcp.getGain()) { case GAIN_1X: Serial.println("1X"); break; case GAIN_2X: Serial.println("2X"); break; case GAIN_4X: Serial.println("4X"); break; case GAIN_8X: Serial.println("8X"); break; } // The resolution affects the sample rate (samples per second, SPS) // Other options: RESOLUTION_14_BIT (60 SPS), RESOLUTION_16_BIT (15 SPS), RESOLUTION_18_BIT (3.75 SPS) mcp.setResolution(RESOLUTION_14_BIT); // 240 SPS (12-bit) Serial.print("Resolution set to: "); switch (mcp.getResolution()) { case RESOLUTION_12_BIT: Serial.println("12 bits"); break; case RESOLUTION_14_BIT: Serial.println("14 bits"); break; case RESOLUTION_16_BIT: Serial.println("16 bits"); break; case RESOLUTION_18_BIT: Serial.println("18 bits"); break; } // Test setting and getting Mode mcp.setMode(MODE_CONTINUOUS); // Options: MODE_CONTINUOUS, MODE_ONE_SHOT Serial.print("Mode set to: "); switch (mcp.getMode()) { case MODE_CONTINUOUS: Serial.println("Continuous"); break; case MODE_ONE_SHOT: Serial.println("One-shot"); break; } } uint32_t lastSecond = millis(); // Store the last time we printed SPS uint32_t sampleCount = 0; // Count how many samples were taken void loop() { // Check if MCP3421 has completed a conversion in continuous mode if (mcp.isReady()) { int32_t adcValue = mcp.readADC(); // Read ADC value Serial.print("ADC reading: "); Serial.println(adcValue); sampleCount++; // Increment the sample count } uint32_t currentMillis = millis(); if (currentMillis - lastSecond >= 1000) { // Check if a second has passed Serial.print("SPS (Samples Per Second): "); Serial.println(sampleCount); // Reset the counter and update the time sampleCount = 0; lastSecond = currentMillis; } }
Upload the sketch to your board and open up the Serial Monitor (Tools -> Serial Monitor) at 115200 baud. You'll see the MCP3421 recognized over I2C. The example sets the gain to 1X, resolution to 14-bit and the mode to continuous. The reading from the ADC will print to the Serial Monitor.
This ADC is great for reading sensors like strain gauges, pressure sensors, or thermocouples. It's not going to be great for reading stuff like potentiometers, where you have a single-end reading referenced to ground, and you want to read the full range from 0 to Vcc.
Text editor powered by tinymce.