From 43a15f2208cb2e804559a7a6d471f72f466e9b90 Mon Sep 17 00:00:00 2001 From: Konstantin Date: Thu, 2 Jan 2025 18:53:38 +0100 Subject: [PATCH] ads1x1x: readded adc_voltage to make it easier to use existing thermistors Signed-off-by: Kontantin Koch --- docs/Config_Reference.md | 4 ++++ klippy/extras/ads1x1x.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/docs/Config_Reference.md b/docs/Config_Reference.md index 4ae98481bf32..bd44cf754691 100644 --- a/docs/Config_Reference.md +++ b/docs/Config_Reference.md @@ -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 diff --git a/klippy/extras/ads1x1x.py b/klippy/extras/ads1x1x.py index e092365e44d6..f0ac7f13bfa4 100644 --- a/klippy/extras/ads1x1x.py +++ b/klippy/extras/ads1x1x.py @@ -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 @@ -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. @@ -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")