-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathADS1115.py
215 lines (189 loc) · 6.57 KB
/
ADS1115.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# Import of Librarys
from machine import I2C, Pin
import utime
# Register variables
_REGISTER_MASK = 0x03
_REGISTER_CONVERT = 0x00
_REGISTER_CONFIG = 0x01
_REGISTER_LOWTHRESH = 0x02
_REGISTER_HITHRESH = 0x03
_OS_MASK = 0x8000
_OS_SINGLE = 0x8000 # Write: Set to start a single - conversion
_OS_BUSY = 0x0000 # Read: Bit = 0 when conversion is in progress
_OS_NOTBUSY = 0x8000 # Read: Bit = 1 when no conversion is in progress
# Channel variables
_MUX_MASK = 0x7000
_MUX_DIFF_0_1 = 0x0000 # Differential P = AIN0, N = AIN1 (default)
_MUX_DIFF_0_3 = 0x1000 # Differential P = AIN0, N = AIN3
_MUX_DIFF_1_3 = 0x2000 # Differential P = AIN1, N = AIN3
_MUX_DIFF_2_3 = 0x3000 # Differential P = AIN2, N = AIN3
_MUX_SINGLE_0 = 0x4000 # Single - ended AIN0
_MUX_SINGLE_1 = 0x5000 # Single - ended AIN1
_MUX_SINGLE_2 = 0x6000 # Single - ended AIN2
_MUX_SINGLE_3 = 0x7000 # Single - ended AIN3
# Gain variables
_PGA_MASK = 0x0E00
_PGA_6_144V = 0x0000 # + /-6.144V range = Gain 2/3
_PGA_4_096V = 0x0200 # + /-4.096V range = Gain 1
_PGA_2_048V = 0x0400 # + /-2.048V range = Gain 2 (default)
_PGA_1_024V = 0x0600 # + /-1.024V range = Gain 4
_PGA_0_512V = 0x0800 # + /-0.512V range = Gain 8
_PGA_0_256V = 0x0A00 # + /-0.256V range = Gain 16
# Mode variables
_MODE_MASK = 0x0100
_MODE_CONTIN = 0x0000 # Continuous conversion mode
_MODE_SINGLE = 0x0100 # Power - down single - shot mode (default)
# Samplerate variables
_DR_MASK = 0x00E0 # ADS1115
_DR_128SPS = 0x0000 # 8 samples per second
_DR_250SPS = 0x0020 # 16 samples per second
_DR_490SPS = 0x0040 # 32 samples per second
_DR_920SPS = 0x0060 # 64 samples per second
_DR_1600SPS = 0x0080 # 128 samples per second (default)
_DR_2400SPS = 0x00A0 # 250 samples per second
_DR_3300SPS = 0x00C0 # 475 samples per second
_DR_860SPS = 0x00E0 # 860 samples per Second
_CMODE_MASK = 0x0010
_CMODE_TRAD = 0x0000 # Traditional comparator with hysteresis (default)
_CMODE_WINDOW = 0x0010 # Window comparator
_CPOL_MASK = 0x0008
_CPOL_ACTVLOW = 0x0000 # ALERT / RDY pin is low when active (default)
_CPOL_ACTVHI = 0x0008 # ALERT / RDY pin is high when active
_CLAT_MASK = 0x0004 # Determines if ALERT / RDY pin latches once asserted
_CLAT_NONLAT = 0x0000 # Non - latching comparator (default)
_CLAT_LATCH = 0x0004 # Latching comparator
_CQUE_MASK = 0x0003
_CQUE_1CONV = 0x0000 # Assert ALERT / RDY after one conversions
_CQUE_2CONV = 0x0001 # Assert ALERT / RDY after two conversions
_CQUE_4CONV = 0x0002 # Assert ALERT / RDY after four conversions
_CQUE_NONE = 0x0003 # Disable the comparator and put ALERT / RDY in high state (default)
# List of all usable gains
_GAINS = [
_PGA_6_144V, # 2 / 3x
_PGA_4_096V, # 1x
_PGA_2_048V, # 2x (default)
_PGA_1_024V, # 4x
_PGA_0_512V, # 8x
_PGA_0_256V # 16x
]
# List of voltage values corresponding to the list of usable gains
_GAINS_V = [
6.144, # 2 / 3x
4.096, # 1x
2.048, # 2x (default)
1.024, # 4x
0.512, # 8x
0.256 # 16x
]
# List of the different usable channels
_CHANNELS = [ # CH1|CH2
_MUX_SINGLE_0, # (0, None) (default)
_MUX_SINGLE_1, # (1, None)
_MUX_SINGLE_2, # (2, None)
_MUX_SINGLE_3, # (3, None)
_MUX_DIFF_0_1, # (0, 1)
_MUX_DIFF_0_3, # (0, 3)
_MUX_DIFF_1_3, # (1, 3)
_MUX_DIFF_2_3, # (2, 3)
]
# List of all usable sample rates
_RATES = [
_DR_128SPS, # 8 samples per second
_DR_250SPS, # 16 samples per second
_DR_490SPS, # 32 samples per second
_DR_920SPS, # 64 samples per second
_DR_1600SPS, # 128 samples per second (default)
_DR_2400SPS, # 250 samples per second
_DR_3300SPS, # 475 samples per second
_DR_860SPS # 860 samples per Second
]
i2c = I2C(0, sda = Pin(0), scl = Pin(1), freq = 100000)
utime.sleep_ms(10)
# All global variables used in the library, as well as some predefined variables that serve as default values.
adsaddress = 0x48
adsgain = _GAINS[2]
adsrate = _RATES[4]
adsmode = _MODE_CONTIN
adsgainv = 2
temp2 = bytearray(2)
# Initialization of the ADS based on user input.
def init(adr, gain, rate, mode):
global adsaddress
adsaddress = adr
setGain(gain)
setRate(rate)
setMode(mode)
# Change the gains based on the user input.
def setGain(gain):
global adsgain
global adsgainv
adsgain = _GAINS[gain]
adsgainv = gain
# Change the sampling rate based on user input.
def setRate(rate):
global adsrate
adsrate = _RATES[rate]
# Change the mode based on user input.
def setMode(mode = True):
global adsmode
if (mode):
adsmode = _MODE_SINGLE
else:
adsmode = _MODE_CONTIN
# Write the desired 16-bit value into the register.
def _write_register(reg, val):
temp2[0] = val >> 8
temp2[1] = val & 0xff
i2c.writeto_mem(adsaddress, reg, temp2)
# Read the 16-bit register value to be returned.
def _read_register(reg):
i2c.readfrom_mem_into(adsaddress, reg, temp2)
return (temp2[0] << 8) | temp2[1]
# Reads any raw value (raw) either from the previously specified channel or from another variable and converts it to voltages.
def raw_to_v(raw):
v_p_b = _GAINS_V[adsgainv] / 32767
return round(raw * v_p_b, 2)
# Reads the specified channel of the ADS1115.
def read(chan):
# Linking of all required data by the bitwise operator 'OR ('|')'.
config = 0x0000
config |= _CHANNELS[chan]
config |= adsgain
config |= adsrate
config |= adsmode
config |= _CQUE_NONE
# Send the required data to the specified register.
_write_register(_REGISTER_CONFIG, config)
while not _read_register(_REGISTER_CONFIG) & _OS_NOTBUSY:
break
# Reading the returned data.
res = _read_register(_REGISTER_CONVERT)
if (res < 32768):
return res
else:
res = res - 65469
return res
# Reading multiple ADS1115 channels at once
def readMulti(start, end):
res1 = 0
res2 = 0
res3 = 0
res4 = 0
if (start > 4): start = 4
if (start < 0): start = 0
if (end > 4): end = 4
if (end < 0): end = 0
for x in range (start, end):
if (x == 0):
res1 = read(x)
utime.sleep_ms(25)
if (x == 1):
res2 = read(x)
utime.sleep_ms(25)
if (x == 2):
res3 = read(x)
utime.sleep_ms(25)
if (x == 3):
res4 = read(x)
utime.sleep_ms(25)
return res1, res2, res3, res4