|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import time |
| 4 | +import smbus |
| 5 | +from I2C import Adafruit_I2C |
| 6 | + |
| 7 | +# =========================================================================== |
| 8 | +# ADS1115 Class |
| 9 | +# =========================================================================== |
| 10 | +class ADS1115: |
| 11 | + i2c = None |
| 12 | + |
| 13 | + # IC Identifiers |
| 14 | + __IC_ADS1115 = 0x01 |
| 15 | + |
| 16 | + # Config Register |
| 17 | + __ADS1115_REG_CONFIG_DR_8SPS = 0x0000 # 8 samples per second |
| 18 | + __ADS1115_REG_CONFIG_DR_16SPS = 0x0020 # 16 samples per second |
| 19 | + __ADS1115_REG_CONFIG_DR_32SPS = 0x0040 # 32 samples per second |
| 20 | + __ADS1115_REG_CONFIG_DR_64SPS = 0x0060 # 64 samples per second |
| 21 | + __ADS1115_REG_CONFIG_DR_128SPS = 0x0080 # 128 samples per second |
| 22 | + __ADS1115_REG_CONFIG_DR_250SPS = 0x00A0 # 250 samples per second (default) |
| 23 | + __ADS1115_REG_CONFIG_DR_475SPS = 0x00C0 # 475 samples per second |
| 24 | + __ADS1115_REG_CONFIG_DR_860SPS = 0x00E0 # 860 samples per second |
| 25 | + |
| 26 | + __ADS1015_REG_CONFIG_CQUE_MASK = 0x0003 |
| 27 | + __ADS1015_REG_CONFIG_CQUE_1CONV = 0x0000 # Assert ALERT/RDY after one conversions |
| 28 | + __ADS1015_REG_CONFIG_CQUE_2CONV = 0x0001 # Assert ALERT/RDY after two conversions |
| 29 | + __ADS1015_REG_CONFIG_CQUE_4CONV = 0x0002 # Assert ALERT/RDY after four conversions |
| 30 | + __ADS1015_REG_CONFIG_CQUE_NONE = 0x0003 # Disable the comparator and put ALERT/RDY in high state (default) |
| 31 | + |
| 32 | + __ADS1015_REG_CONFIG_CMODE_MASK = 0x0010 |
| 33 | + __ADS1015_REG_CONFIG_CMODE_TRAD = 0x0000 # Traditional comparator with hysteresis (default) |
| 34 | + __ADS1015_REG_CONFIG_CMODE_WINDOW = 0x0010 # Window comparator |
| 35 | + |
| 36 | + __ADS1015_REG_CONFIG_CPOL_MASK = 0x0008 |
| 37 | + __ADS1015_REG_CONFIG_CPOL_ACTVLOW = 0x0000 # ALERT/RDY pin is low when active (default) |
| 38 | + __ADS1015_REG_CONFIG_CPOL_ACTVHI = 0x0008 # ALERT/RDY pin is high when active |
| 39 | + |
| 40 | + __ADS1015_REG_CONFIG_CLAT_MASK = 0x0004 # Determines if ALERT/RDY pin latches once asserted |
| 41 | + __ADS1015_REG_CONFIG_CLAT_NONLAT = 0x0000 # Non-latching comparator (default) |
| 42 | + __ADS1015_REG_CONFIG_CLAT_LATCH = 0x0004 # Latching comparator |
| 43 | + |
| 44 | + __ADS1015_REG_CONFIG_MODE_MASK = 0x0100 |
| 45 | + __ADS1015_REG_CONFIG_MODE_CONTIN = 0x0000 # Continuous conversion mode |
| 46 | + __ADS1015_REG_CONFIG_MODE_SINGLE = 0x0100 # Power-down single-shot mode (default) |
| 47 | + |
| 48 | + __ADS1015_REG_CONFIG_PGA_MASK = 0x0E00 |
| 49 | + __ADS1015_REG_CONFIG_PGA_6_144V = 0x0000 # +/-6.144V range |
| 50 | + __ADS1015_REG_CONFIG_PGA_4_096V = 0x0200 # +/-4.096V range |
| 51 | + __ADS1015_REG_CONFIG_PGA_2_048V = 0x0400 # +/-2.048V range (default) |
| 52 | + __ADS1015_REG_CONFIG_PGA_1_024V = 0x0600 # +/-1.024V range |
| 53 | + __ADS1015_REG_CONFIG_PGA_0_512V = 0x0800 # +/-0.512V range |
| 54 | + __ADS1015_REG_CONFIG_PGA_0_256V = 0x0A00 # +/-0.256V range |
| 55 | + |
| 56 | + __ADS1015_REG_CONFIG_MUX_MASK = 0x7000 |
| 57 | + __ADS1015_REG_CONFIG_MUX_DIFF_0_1 = 0x0000 # Differential P = AIN0, N = AIN1 (default) |
| 58 | + __ADS1015_REG_CONFIG_MUX_DIFF_0_3 = 0x1000 # Differential P = AIN0, N = AIN3 |
| 59 | + __ADS1015_REG_CONFIG_MUX_DIFF_1_3 = 0x2000 # Differential P = AIN1, N = AIN3 |
| 60 | + __ADS1015_REG_CONFIG_MUX_DIFF_2_3 = 0x3000 # Differential P = AIN2, N = AIN3 |
| 61 | + __ADS1015_REG_CONFIG_MUX_SINGLE_0 = 0x4000 # Single-ended AIN0 |
| 62 | + __ADS1015_REG_CONFIG_MUX_SINGLE_1 = 0x5000 # Single-ended AIN1 |
| 63 | + __ADS1015_REG_CONFIG_MUX_SINGLE_2 = 0x6000 # Single-ended AIN2 |
| 64 | + __ADS1015_REG_CONFIG_MUX_SINGLE_3 = 0x7000 # Single-ended AIN3 |
| 65 | + |
| 66 | + # Config Register |
| 67 | + __ADS1015_REG_CONFIG_OS_MASK = 0x8000 |
| 68 | + __ADS1015_REG_CONFIG_OS_SINGLE = 0x8000 # Write: Set to start a single-conversion |
| 69 | + __ADS1015_REG_CONFIG_OS_BUSY = 0x0000 # Read: Bit = 0 when conversion is in progress |
| 70 | + __ADS1015_REG_CONFIG_OS_NOTBUSY = 0x8000 # Read: Bit = 1 when device is not performing a conversion |
| 71 | + |
| 72 | + # Pointer Register |
| 73 | + __ADS1015_REG_POINTER_MASK = 0x03 |
| 74 | + __ADS1015_REG_POINTER_CONVERT = 0x00 |
| 75 | + __ADS1015_REG_POINTER_CONFIG = 0x01 |
| 76 | + __ADS1015_REG_POINTER_LOWTHRESH = 0x02 |
| 77 | + __ADS1015_REG_POINTER_HITHRESH = 0x03 |
| 78 | + |
| 79 | + # Dictionaries with the sampling speed values |
| 80 | + # These simplify and clean the code (avoid the abuse of if/elif/else clauses) |
| 81 | + spsADS1115 = { |
| 82 | + 8:__ADS1115_REG_CONFIG_DR_8SPS, |
| 83 | + 16:__ADS1115_REG_CONFIG_DR_16SPS, |
| 84 | + 32:__ADS1115_REG_CONFIG_DR_32SPS, |
| 85 | + 64:__ADS1115_REG_CONFIG_DR_64SPS, |
| 86 | + 128:__ADS1115_REG_CONFIG_DR_128SPS, |
| 87 | + 250:__ADS1115_REG_CONFIG_DR_250SPS, |
| 88 | + 475:__ADS1115_REG_CONFIG_DR_475SPS, |
| 89 | + 860:__ADS1115_REG_CONFIG_DR_860SPS |
| 90 | + } |
| 91 | + |
| 92 | + # Dictionariy with the programable gains |
| 93 | + pgaADS1x15 = { |
| 94 | + 6144:__ADS1015_REG_CONFIG_PGA_6_144V, |
| 95 | + 4096:__ADS1015_REG_CONFIG_PGA_4_096V, |
| 96 | + 2048:__ADS1015_REG_CONFIG_PGA_2_048V, |
| 97 | + 1024:__ADS1015_REG_CONFIG_PGA_1_024V, |
| 98 | + 512:__ADS1015_REG_CONFIG_PGA_0_512V, |
| 99 | + 256:__ADS1015_REG_CONFIG_PGA_0_256V |
| 100 | + } |
| 101 | + |
| 102 | + # Constructor |
| 103 | + def __init__(self, address=0x48, ic=__IC_ADS1115, debug=False): |
| 104 | + self.i2c = Adafruit_I2C(address) |
| 105 | + self.address = address |
| 106 | + self.debug = debug |
| 107 | + |
| 108 | + # Make sure the IC specified is valid |
| 109 | + if (ic > self.__IC_ADS1115): |
| 110 | + return -1 |
| 111 | + else: |
| 112 | + self.ic = ic |
| 113 | + |
| 114 | + # Set pga value, so that getLastConversionResult() can use it, |
| 115 | + # any function that accepts a pga value must update this. |
| 116 | + self.pga = 6144 |
| 117 | + |
| 118 | + def readADCSingleEnded(self, channel=0, pga=6144, sps=250): |
| 119 | + ''' |
| 120 | + Gets a single-ended ADC reading from the specified channel in mV. |
| 121 | + The sample rate for this mode (single-shot) can be used to lower the noise |
| 122 | + (low sps) or to lower the power consumption (high sps) by duty cycling, |
| 123 | + see datasheet page 14 for more info. |
| 124 | + The pga must be given in mV, see page 13 for the supported values. |
| 125 | + ''' |
| 126 | + |
| 127 | + # With invalid channel return -1 |
| 128 | + if (channel > 3): |
| 129 | + return -1 |
| 130 | + |
| 131 | + # Disable comparator, Non-latching, Alert/Rdy active low |
| 132 | + # traditional comparator, single-shot mode |
| 133 | + config = self.__ADS1015_REG_CONFIG_CQUE_NONE | \ |
| 134 | + self.__ADS1015_REG_CONFIG_CLAT_NONLAT | \ |
| 135 | + self.__ADS1015_REG_CONFIG_CPOL_ACTVLOW | \ |
| 136 | + self.__ADS1015_REG_CONFIG_CMODE_TRAD | \ |
| 137 | + self.__ADS1015_REG_CONFIG_MODE_SINGLE |
| 138 | + |
| 139 | + # Set sample per seconds, defaults to 250sps |
| 140 | + config |= self.spsADS1115.setdefault(sps, self.__ADS1115_REG_CONFIG_DR_250SPS) |
| 141 | + |
| 142 | + # Set PGA/voltage range, defaults to +-6.144V |
| 143 | + config |= self.pgaADS1x15.setdefault(pga, self.__ADS1015_REG_CONFIG_PGA_6_144V) |
| 144 | + self.pga = pga |
| 145 | + |
| 146 | + # Set the channel to be converted |
| 147 | + if channel == 3: |
| 148 | + config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_3 |
| 149 | + elif channel == 2: |
| 150 | + config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_2 |
| 151 | + elif channel == 1: |
| 152 | + config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_1 |
| 153 | + else: |
| 154 | + config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_0 |
| 155 | + |
| 156 | + # Set 'start single-conversion' bit |
| 157 | + config |= self.__ADS1015_REG_CONFIG_OS_SINGLE |
| 158 | + |
| 159 | + # Write config register to the ADC |
| 160 | + bytes = [(config >> 8) & 0xFF, config & 0xFF] |
| 161 | + self.i2c.writeList(self.__ADS1015_REG_POINTER_CONFIG, bytes) |
| 162 | + |
| 163 | + # Wait for the ADC conversion to complete |
| 164 | + # The minimum delay depends on the sps: delay >= 1/sps |
| 165 | + # We add 0.1ms to be sure |
| 166 | + delay = 1.0/sps+0.0001 |
| 167 | + time.sleep(delay) |
| 168 | + |
| 169 | + # Read the conversion results |
| 170 | + result = self.i2c.readList(self.__ADS1015_REG_POINTER_CONVERT, 2) |
| 171 | + # Return a mV value for the ADS1115 |
| 172 | + # (Take signed values into account as well) |
| 173 | + val = (result[0] << 8) | (result[1]) |
| 174 | + if val > 0x7FFF: |
| 175 | + return (val - 0xFFFF)*pga/32768.0 |
| 176 | + else: |
| 177 | + return ( (result[0] << 8) | (result[1]) )*pga/32768.0 |
0 commit comments