Skip to content

Commit c8af02c

Browse files
committed
Use AnalogIn to read the tank level
1 parent a0468c5 commit c8af02c

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

config.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ lcd = /dev/lcd
2121

2222
[adc]
2323
channel = 1
24-
gain = 0.6666666666666666
25-
low = 83
26-
high = 1665
24+
gain = 4
25+
low = 0.6
26+
high = 1.0
2727

2828
[1-wire]
2929
pool = 28-00000c86dc29

controller/device.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,8 @@ def value(self):
221221

222222

223223
class TankSensorDevice(SensorDevice):
224-
def __init__(self, name, adc, channel, gain, low, high):
224+
def __init__(self, name, channel, low, high):
225225
super().__init__(name)
226-
self.__adc = adc
227-
self.__adc.gain = gain
228226
self.__channel = channel
229227
self.__low = low
230228
self.__high = high
@@ -234,15 +232,15 @@ def value(self):
234232
values = []
235233
for _ in range(10):
236234
try:
237-
values.append(self.__adc.read(self.__channel))
235+
values.append(self.__channel.voltage)
238236
time.sleep(0.05)
239237
except OSError:
240238
logger.exception(f"Unable to read ADC {self.name}")
241239
time.sleep(0.5)
242240
# In case we got really no readings, we return 0 in order for the system to go into
243241
# emergency stop.
244242
value = sum(values) / len(values) if values else 0
245-
logger.debug(f"Tank sensor average ADC={value:.2f}")
243+
logger.debug(f"Tank sensor average ADC voltage={value:.2f}")
246244
return constrain(mapping(value, self.__low, self.__high, 0, 100), 0, 100)
247245

248246

poupool.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,13 @@ def setup_rpi(registry):
9090

9191
# ADC
9292
import adafruit_ads1x15.ads1015 as ADS
93+
from adafruit_ads1x15.analog_in import AnalogIn
9394

9495
# Create the ADC object using the I2C bus
9596
adc = ADS.ADS1015(i2c)
96-
# With a gain of 2/3 and a sensor output of 0.25V-5V, the values should be around 83 and 1665
97-
params = ((int, "channel"), (float, "gain"), (int, "low"), (int, "high"))
98-
registry.add_sensor(TankSensorDevice("tank", adc, *[t(config["adc", n]) for t, n in params]))
97+
adc.gain = float(config["adc", "gain"])
98+
channel = AnalogIn(adc, int(config["adc", "channel"]))
99+
registry.add_sensor(TankSensorDevice("tank", channel, float(config["adc", "low"]), float(config["adc", "high"])))
99100

100101
# DAC
101102
import adafruit_mcp4725

0 commit comments

Comments
 (0)