Simple test

Ensure your device works with this simple test.

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