Overview
The Adafruit MCP3421 18-Bit ADC is a simple, inexpensive, and easy to use 18-bit, 240 SPS, single-channel ADC with an I2C interface that can run up to 3.4MHz clock rate. A perfect component whenever you need an ADC that has differential inputs, adjustable gain, and a built-in precision/low-drift reference voltage.
One of the trade-offs with getting 18-bit precision is that the ADC is not going to be very fast: you can configure the chip to do a faster 12-bit conversion at 240 SPS, but at 18-bits it slows down to 3.5 SPS. That's because the way a sigma-delta ADC works, it 'guesses' the analog voltage and uses a comparator to determine whether the input is higher or lower. Each 'guess' takes an extra step, and thus halves the throughput, so 12-bit is 240 SPS, and 14-bit is 1/4 (2-bit) slower, 60 SPS. Ditto 16-bit is 1/4 slower, 15 SPS, and finally 18-bit is 3.75 SPS.
The MCP3421 is already set up for differential inputs, which means that you can read positive or negative differences between the two inputs, as long as both signals are between 0 and 2.048V. This means its 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. It is great, however, for reading sensors like strain gauges, pressure sensors, or thermocouples.
We have a ready to go Arduino library that makes usage simple: select the sample rate / precision you want and one-shot or continuous mode. Then read values over I2C! This sensor has a fixed address, so you may want to use a multiplexor if you want to connect multiple MCP3421's on a single I2C bus.
To get you going fast, we spun up a custom-made PCB in the STEMMA QT form factor, making it easy to interface with. The STEMMA QT connectors on either side are compatible with the SparkFun Qwiic I2C connectors. This allows you to make solderless connections between your development board and the MCP or to chain it with a wide range of other sensors and accessories using a compatible cable.
QT Cable is not included, but we have a variety in the shop.
The board comes with a bit of 0.1" standard header in case you want to use it with a breadboard or perfboard. There are four mounting holes for easy attachment.
Page last edited March 08, 2024
Text editor powered by tinymce.
Pinouts
The default I2C address is 0x68.
Power Pins
- VIN - this is the power pin. Since the ADC chip uses 3-5 VDC to power the board, give it the same power as the logic level of your microcontroller - e.g. for a 5V micro like Arduino, use 5V.
- GND - common ground for power and logic.
I2C Logic Pins
- SCL - I2C clock pin, connect to your microcontroller I2C clock line. This pin can use 3-5V logic, and there's a 10K pullup on this pin.
- SDA - I2C data pin, connect to your microcontroller I2C data line. This pin can use 3-5V logic, and there's a 10K pullup on this pin.
- STEMMA QT - These connectors allow you to connectors to dev boards with STEMMA QT connectors or to other things with various associated accessories
ADC Inputs
The MCP3421 has two inputs: V+ and V-. V+ is the positive input and V- is the negative input. The inputs are available via the terminal block at the top of the board or the two pins labeled V+ and V- at the bottom of the board.
The MCP3421 is set up for differential inputs, which means that you can read positive or negative differences between the two inputs, as long as both signals are between 0 and 2.048V.
Page last edited March 08, 2024
Text editor powered by tinymce.
CircuitPython and Python
It's easy to use the MCP3421 with Python or CircuitPython, and the Adafruit_CircuitPython_MCP3421 module. This module allows you to easily write Python code to read input on the ADC.
You can use this driver with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.
CircuitPython Microcontroller Wiring
First wire up the breakout to your board exactly as follows. The following is the breakout wired to a Feather RP2040 (shown using a STEMMA QT cable):
-
Board STEMMA 3V to breakout VIN (red wire)
-
Board STEMMA GND to breakout GND (black wire)
-
Board STEMMA SCL to breakout SCL (yellow wire)
- Board STEMMA SDA to breakout SDA (blue wire)
The following is the breakout wired to a Feather RP2040 using a solderless breadboard:
-
Board 3V 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)
Python Computer Wiring
Since there are dozens of Linux computers/boards you can use, we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported.
Here's the Raspberry Pi wired with I2C using a STEMMA QT connector:
-
Pi 3V to breakout VIN (red wire)
-
Pi GND to breakout GND (black wire)
-
Pi SCL to breakout SCL (yellow wire)
- Pi SDA to breakout SDA (blue wire)
Here's the Raspberry Pi wired with I2C using a solderless breadboard:
-
Pi 3V to breakout VIN (red wire)
-
Pi GND to breakout GND (black wire)
-
Pi SCL to breakout SCL (yellow wire)
- Pi SDA to breakout SDA (blue wire)
Python Installation of MCP3421 Library
You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!
Once that's done, from your command line run the following command:
pip3 install adafruit-circuitpython-mcp3421
If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!
CircuitPython Usage
To use with CircuitPython, you need to first install the Adafruit_CircuitPython_MCP3421 library, and its dependencies, into the lib folder on your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following folders:
- adafruit_bus_device/
- adafruit_mcp3421/
Python Usage
Once you have the library pip3 installed on your computer, copy or download the following example to your computer, and run the following, replacing code.py with whatever you named the file:
python3 code.py
Example Code
If running CircuitPython: Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
If running Python: The console output will appear wherever you are running Python.
# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import time
import board
import adafruit_mcp3421.mcp3421 as ADC
from adafruit_mcp3421.analog_in import AnalogIn
i2c = board.I2C()
adc = ADC.MCP3421(i2c, gain=1, resolution=14, continuous_mode=True)
adc_channel = AnalogIn(adc)
# gain, resolution and mode can also be set after instantiation:
# set gain to 1, 2, 4 or 8x
# defaults to 1
# adc.gain = 1
# set resolution to 12, 14, 16 or 18
# defaults to 14
# adc.resolution = 14
# set continuous read mode True or False for one-shot
# defaults to True
# adc.continuous_mode = True
while True:
print(f"ADC value: {adc_channel.value}")
print(f"Current gain: {adc.gain}X")
print(f"Current resolution: {adc.resolution}-bit")
if adc.continuous_mode:
mode = "continuous"
else:
mode = "one-shot"
print(f"Mode: {mode}")
print()
time.sleep(0.01)
The MCP3421 is initialized over I2C. Then in the loop, the analog input is read and printed to the serial monitor, along with the current gain, resolution and mode settings.
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.
Page last edited March 08, 2024
Text editor powered by tinymce.
Arduino
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.
Page last edited March 08, 2024
Text editor powered by tinymce.
WipperSnapper
What is WipperSnapper
WipperSnapper is a firmware designed to turn any WiFi-capable board into an Internet-of-Things device without programming a single line of code. WipperSnapper connects to Adafruit IO, a web platform designed (by Adafruit!) to display, respond, and interact with your project's data.
Simply load the WipperSnapper firmware onto your board, add credentials, and plug it into power. Your board will automatically register itself with your Adafruit IO account.
From there, you can add components to your board such as buttons, switches, potentiometers, sensors, and more! Components are dynamically added to hardware, so you can immediately start interacting, logging, and streaming the data your projects produce without writing code.
If you've never used WipperSnapper, click below to read through the quick start guide before continuing.
First, wire up an MCP3421 to your board exactly as follows. Here is an example of the MCP3421 wired to an Adafruit ESP32 Feather V2 using I2C with a STEMMA QT cable (no soldering required)
-
Board 3V to sensor VIN (red wire on STEMMA QT)
-
Board GND to sensor GND (black wire on STEMMA QT)
-
Board SCL to sensor SCL (yellow wire on STEMMA QT)
- Board SDA to sensor SDA (blue wire on STEMMA QT)
Usage
Connect your board to Adafruit IO Wippersnapper and navigate to the WipperSnapper board list.
On this page, select the WipperSnapper board you're using to be brought to the board's interface page.
If you do not see your board listed here - you need to connect your board to Adafruit IO first.
On the device page, quickly check that you're running the latest version of the WipperSnapper firmware.
The device tile on the left indicates the version number of the firmware running on the connected board.
- If the firmware version is green with a checkmark - continue with this guide.
- If the firmware version is red with an exclamation mark "!" - update to the latest WipperSnapper firmware on your board before continuing.
Next, make sure the sensor is plugged into your board and click the I2C Scan button.
You should see the MCP3421's default I2C address of 0x68 pop-up in the I2C scan list.
First, double-check the connection and/or wiring between the sensor and the board.
Then, reset the board and let it re-connect to Adafruit IO WipperSnapper.
With the sensor detected in an I2C scan, you're ready to add the sensor to your board.
Click the New Component button or the + button to bring up the component picker.
Adafruit IO supports a large amount of components. To quickly find your sensor, type MCP3421 into the search bar, then select the MCP3421 component.
On the component configuration page, the MCP3421's sensor address should be listed along with the sensor's settings.
The Send Every option is specific to each sensor's measurements. This option will tell the Feather how often it should read from the MCP3421 sensor and send the data to Adafruit IO. Measurements can range from every 30 seconds to every 24 hours.
For this example, set the Send Every interval to every 30 seconds.
Your device interface should now show the sensor components you created. After the interval you configured elapses, WipperSnapper will automatically read values from the sensor(s) and send them to Adafruit IO.
To view the data that has been logged from the sensor, click on the graph next to the sensor name.
Here you can see the feed history and edit things about the feed such as the name, privacy, webhooks associated with the feed and more. If you want to learn more about how feeds work, check out this page.
Page last edited March 08, 2024
Text editor powered by tinymce.
Downloads
Page last edited March 08, 2024
Text editor powered by tinymce.