### CircuitPython MCP3421 ADC Example Source: https://learn.adafruit.com/adafruit-mcp3421-18-bit-adc?view=all This example demonstrates reading analog values from the MCP3421 ADC in continuous mode using CircuitPython. It prints the ADC value, current gain, resolution, and mode. Ensure the Adafruit_CircuitPython_MCP3421 library is installed in the lib folder. ```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) ``` -------------------------------- ### Initialize and Read MCP3421 ADC in CircuitPython Source: https://learn.adafruit.com/adafruit-mcp3421-18-bit-adc/circuitpython-and-python This example demonstrates initializing the MCP3421 ADC over I2C and reading analog input in a loop. It prints the readings along with current gain, resolution, and mode settings to the serial console. Ensure the device is connected and CircuitPython is running. ```python import board import busio import adafruit_mcp3421 i2c = busio.I2C(board.SCL, board.SDA) # MCP3421 address is 0x68 (default) # You can also specify gain, resolution, and conversion mode # mcp = adafruit_mcp3421.MCP3421(i2c, address=0x68, gain=1, resolution=18, mode=adafruit_mcp3421.Mode.CONTINUOUS) mcp = adafruit_mcp3421.MCP3421(i2c) while True: # Read the ADC value value = mcp.value # Read the voltage voltage = mcp.voltage print("Raw: {} Value: {:.2f}V".format(value, voltage)) # Optional: Print gain, resolution, and mode print("Gain: {}".format(mcp.gain)) print("Resolution: {}".format(mcp.resolution)) print("Mode: {}".format(mcp.mode)) print("") time.sleep(0.5) ``` -------------------------------- ### Read ADC in Continuous Mode - Arduino Source: https://learn.adafruit.com/adafruit-mcp3421-18-bit-adc?view=all Continuously reads analog values from the MCP3421 ADC and prints the value, gain, resolution, and mode to the serial monitor. Ensure the Adafruit_MCP3421 library is installed. ```python # 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) ``` -------------------------------- ### MCP3421 Configuration and Data Reading Source: https://learn.adafruit.com/adafruit-mcp3421-18-bit-adc?view=all Initializes the MCP3421 sensor, configures gain, resolution, and mode, and continuously reads ADC values while tracking samples per second. ```cpp #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; } } ``` -------------------------------- ### CircuitPython MCP3421 ADC Configuration Options Source: https://learn.adafruit.com/adafruit-mcp3421-18-bit-adc?view=all This snippet shows how to configure the MCP3421 ADC's gain, resolution, and continuous mode after instantiation in CircuitPython. It comments out the settings for gain, resolution, and continuous mode, which can be adjusted as needed. ```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 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.