|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +perform a single-shot measurement |
| 4 | +
|
| 5 | +In single-shot mode, the ADC performs one conversion of the input signal |
| 6 | +upon request, stores the conversion value to an internal conversion |
| 7 | +register, and then enters a power-down state. This mode is intended to |
| 8 | +provide significant power savings in systems that only require periodic |
| 9 | +conversions or when there are long idle periods between conversions. |
| 10 | +
|
| 11 | +This example works with all variations: |
| 12 | + - ADS1013, ADS1014, ADS1015 |
| 13 | + - ADS1113, ADS1114, ADS1115 |
| 14 | +
|
| 15 | +usage: |
| 16 | + pdm run examples/01_singleshot.py |
| 17 | +""" |
| 18 | + |
| 19 | +import time |
| 20 | + |
| 21 | +import board # type: ignore |
| 22 | + |
| 23 | +# module busio and board provide no type hints |
| 24 | +import busio # type: ignore |
| 25 | + |
| 26 | +from feeph.ads1xxx import DRS, UNIT, Ads1113, Ads1113Config |
| 27 | + |
| 28 | +if __name__ == '__main__': |
| 29 | + i2c_bus = busio.I2C(scl=board.SCL, sda=board.SDA) |
| 30 | + ads1x13 = Ads1113(i2c_bus=i2c_bus) |
| 31 | + |
| 32 | + # create a configuration |
| 33 | + # Without any parameters provided this will match the default |
| 34 | + # configuration for this device. |
| 35 | + my_config = Ads1113Config() |
| 36 | + |
| 37 | + # take our first measurement |
| 38 | + # It is recommended to provide a configuration at least once in order |
| 39 | + # to ensure the device uses the expected configuration. |
| 40 | + value1 = ads1x13.get_ssc_measurement(config=my_config) |
| 41 | + print("value1: {value1}µV") |
| 42 | + |
| 43 | + # take a second measurement |
| 44 | + # if no configuration is provided we continue to use the previous one |
| 45 | + value2 = ads1x13.get_ssc_measurement() |
| 46 | + print("value2: {value1}µV") |
| 47 | + |
| 48 | + # convert from MicroVolt to Volt |
| 49 | + # This might skew the value due to conversion to float! |
| 50 | + value3 = float(value1) / (1000 * 1000) |
| 51 | + print("value3: {value2:0.6f}V") |
| 52 | + |
| 53 | + # show the raw value |
| 54 | + # range: |
| 55 | + # -32768 ≤ x ≤ 32767 |
| 56 | + # granularity: |
| 57 | + # ADS101x: 15 steps (..., 15, 30, 45, ...) |
| 58 | + # ADS111x: 1 step (..., 1, 2, 3, 4, ...) |
| 59 | + value4 = ads1x13.get_ssc_measurement(unit=UNIT.STEPS) |
| 60 | + print("value4: {value1}") |
| 61 | + |
| 62 | + print('-' * 80) |
| 63 | + |
| 64 | + # take multiple measurements and alternate between 2 configurations |
| 65 | + config1 = Ads1113Config(drs=DRS.MODE4) |
| 66 | + config2 = Ads1113Config(drs=DRS.MODE5) |
| 67 | + while True: |
| 68 | + value4 = ads1x13.get_ssc_measurement(config=config1) |
| 69 | + print("value4: {value1}µV") |
| 70 | + value5 = ads1x13.get_ssc_measurement(config=config2) |
| 71 | + print("value5: {value1}µV") |
| 72 | + time.sleep(1) |
0 commit comments