Skip to content

Commit f28460d

Browse files
committed
Wait for conversions in continuous example
Previously polling rate (apparent sample rate) was limited purely by I2C speed, script will now wait a full conversion cycle until it next reads the conversion result. NOTE: Not implementing a time.sleep() in _read(self, pin) as this would be blocking and would inhibit easy implementation of interrupt driven sampling by the ALERT/RDY pin.
1 parent 205146d commit f28460d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

examples/ads1x15_fast_read.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
SAMPLES = 1000
1111

1212
# Create the I2C bus with a fast frequency
13+
# NOTE: Your device may not respect the frequency setting
14+
# Raspberry Pis must change this in /boot/config.txt
15+
1316
i2c = busio.I2C(board.SCL, board.SDA, frequency=1000000)
1417

1518
# Create the ADC object using the I2C bus
@@ -26,16 +29,23 @@
2629

2730
data = [None] * SAMPLES
2831

32+
time_last_sample = time.monotonic()
2933
start = time.monotonic()
3034

3135
# Read the same channel over and over
3236
for i in range(SAMPLES):
37+
# Wait for expected conversion finish time
38+
while time.monotonic() < (time_last_sample + (1.0 / ads.data_rate)):
39+
pass
40+
41+
# Read conversion value for ADC channel
42+
time_last_sample = time.monotonic()
3343
data[i] = chan0.value
44+
3445
# Detect repeated values due to over polling
3546
if data[i] == data[i - 1]:
3647
repeats += 1
3748

38-
3949
end = time.monotonic()
4050
total_time = end - start
4151

0 commit comments

Comments
 (0)