Skip to content

Commit

Permalink
ads1x1x: readded adc_voltage to make it easier to use existing thermi…
Browse files Browse the repository at this point in the history
…stors

Signed-off-by: Kontantin Koch <[email protected]>
  • Loading branch information
korsarNek committed Jan 10, 2025
1 parent 8f59447 commit 43a15f2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/Config_Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -4843,6 +4843,10 @@ chip: ADS1115
# Default value is 4.096V. The maximum voltage range used for the input. This
# scales all values read from the ADC. Options are: 6.144V, 4.096V, 2.048V,
# 1.024V, 0.512V, 0.256V
#adc_voltage: 4.096
# The suppy voltage for the device. The default value is the voltage specified
# by pga. This allows additional software scaling for all values read from the
# ADC.
i2c_mcu: host
i2c_bus: i2c.1
#address_pin: GND
Expand Down
18 changes: 18 additions & 0 deletions klippy/extras/ads1x1x.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ def isADS111X(chip):
'0.512V': 0x0800, # +/-0.512V range = Gain 8
'0.256V': 0x0A00 # +/-0.256V range = Gain 16
}
ADS1X1X_PGA_VALUE = {
0x0000: 6.144,
0x0200: 4.096,
0x0400: 2.048,
0x0600: 1.024,
0x0800: 0.512,
0x0A00: 0.256,
}
ADS111X_RESOLUTION = 32767.0
ADS111X_PGA_SCALAR = {
0x0000: 6.144 / ADS111X_RESOLUTION, # +/-6.144V range = Gain 2/3
Expand Down Expand Up @@ -176,6 +184,8 @@ def __init__(self, config):
self._ppins.register_chip(self.name, self)

self.pga = config.getchoice('pga', ADS1X1X_PGA, '4.096V')
self.adc_voltage = config.getfloat('adc_voltage', above=0.,
default=ADS1X1X_PGA_VALUE[self.pga])
# Comparators are not implemented, they would only be useful if the
# alert pin is used, which we haven't made configurable.
# But that wouldn't be useful for a normal temperature sensor anyway.
Expand Down Expand Up @@ -336,6 +346,14 @@ def _process_sample(self, eventtime):
else:
target_value = sample / ADS111X_RESOLUTION

# Thermistors expect a value between 0 and 1 to work. If we use a
# PGA with 4.096V but supply only 3.3V, the reference voltage for
# voltage divider is only 3.3V, not 4.096V. So we remap the range
# from what the PGA allows as range to end up between 0 and 1 for
# the thermistor logic to work as expected.
target_value = target_value * (ADS1X1X_PGA_VALUE[self.chip.pga] / \
self.chip.adc_voltage)

if target_value > self.maxval or target_value < self.minval:
self.invalid_count = self.invalid_count + 1
logging.warning("ADS1X1X: temperature outside range")
Expand Down

0 comments on commit 43a15f2

Please sign in to comment.