Simple test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: Copyright (c) 2024 Liz Clark for Adafruit Industries
2#
3# SPDX-License-Identifier: MIT
4
5import time
6import board
7import adafruit_mcp3421.mcp3421 as ADC
8from adafruit_mcp3421.analog_in import AnalogIn
9
10i2c = board.I2C()
11
12adc = ADC.MCP3421(i2c, gain=1, resolution=14, continuous_mode=True)
13adc_channel = AnalogIn(adc)
14# gain, resolution and mode can also be set after instantiation:
15
16# set gain to 1, 2, 4 or 8x
17# defaults to 1
18# adc.gain = 1
19
20# set resolution to 12, 14, 16 or 18
21# defaults to 14
22# adc.resolution = 14
23
24# set continuous read mode True or False for one-shot
25# defaults to True
26# adc.continuous_mode = True
27
28while True:
29 print(f"ADC value: {adc_channel.value}")
30 print(f"Current gain: {adc.gain}X")
31 print(f"Current resolution: {adc.resolution}-bit")
32 if adc.continuous_mode:
33 mode = "continuous"
34 else:
35 mode = "one-shot"
36 print(f"Mode: {mode}")
37 print()
38 time.sleep(0.01)