forked from adafruit/Adafruit_CircuitPython_ADS1x15
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ads1x15_fast_read.py
executable file
·76 lines (63 loc) · 2.04 KB
/
ads1x15_fast_read.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
import time
import board
import busio
from digitalio import DigitalInOut, Direction
from adafruit_ads1x15.analog_in import AnalogIn
import adafruit_ads1x15.ads1115 as ADS
SAMPLES = 1000
RATE = 860
i2c = busio.I2C(board.SCL, board.SDA)
ads = ADS.ADS1115(i2c)
ads.gain = 1
ads.data_rate = RATE
chan0 = AnalogIn(ads, ADS.P0)
chan1 = AnalogIn(ads, ADS.P1)
#--------------------------------------------------
# Normal Mode (single shot)
#--------------------------------------------------
ads.mode = 0x0100 # single shot
print("Acquiring normal...")
start = time.time()
for _ in range(SAMPLES):
foo = chan0.value
end = time.time()
total_time = end - start
print("Time of capture: {}s".format(total_time))
print("Sample rate requested={} actual={}".format(RATE, SAMPLES / total_time))
#--------------------------------------------------
# Fast Mode (continuous)
#--------------------------------------------------
ads.mode = 0x0000 # continuous
ads.comparator_config = 0
ready_pin = DigitalInOut(board.D23)
ready_pin.direction = Direction.INPUT
print("Acquiring fast...")
start = time.time()
with chan0 as chan:
for _ in range(SAMPLES):
# wait for ready pulse to go low to indicate conversion complete
while ready_pin.value:
pass
# get the value
foo = chan.value
# wait for ready pulse to go back to high
while not ready_pin.value:
pass
end = time.time()
total_time = end - start
print("Time of capture: {}s".format(total_time))
print("Sample rate requested={} actual={}".format(RATE, SAMPLES / total_time))
#--------------------------------------------------
# Fast Mode (continuous) w/o polling
#--------------------------------------------------
ads.mode = 0x0000
ads.comparator_config = 0
print("Acquiring fast w/o polling...")
start = time.time()
with chan0 as chan:
for _ in range(SAMPLES):
foo = chan.value
end = time.time()
total_time = end - start
print("Time of capture: {}s".format(total_time))
print("Sample rate requested={} actual={}".format(RATE, SAMPLES / total_time))