Skip to content

Commit e7e38fd

Browse files
authored
Merge pull request #44 from BiffoBear/Fix_ADC_16_bit_range
Fix ADC 16 bit range
2 parents faafbb0 + e6c7f27 commit e7e38fd

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

adafruit_mcp3xxx/analog_in.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
The ADC chips supported by this library do not use negative numbers. If the resulting
1515
differential read is less than 0, then the returned integer value (and voltage value) is ``0``.
1616
If for some reason the voltage on a channel is greater than the reference voltage or
17-
less than 0, then the returned integer value is ``65472`` or ``0`` respectively.
17+
less than 0, then the returned integer value is ``65535`` or ``0`` respectively.
1818
1919
"""
2020

@@ -54,15 +54,16 @@ def __init__(
5454

5555
@property
5656
def value(self) -> int:
57-
"""Returns the value of an ADC pin as an integer. Due to 10-bit accuracy of the chip, the
58-
returned values range [0, 65472]."""
59-
return (
60-
self._mcp.read(self._pin_setting, is_differential=self.is_differential) << 6
57+
"""Returns the value of an ADC pin as an integer in the range [0, 65535]."""
58+
# Initial result is only 10 bits.
59+
result = int(
60+
self._mcp.read(self._pin_setting, is_differential=self.is_differential)
6161
)
62+
# Stretch to 16 bits and cover full range.
63+
return (result << 6) | (result >> 4)
6264

6365
@property
6466
def voltage(self) -> float:
6567
"""Returns the voltage from the ADC pin as a floating point value. Due to the 10-bit
66-
accuracy of the chip, returned values range from 0 to (``reference_voltage`` *
67-
65472 / 65535)"""
68-
return (self.value * self._mcp.reference_voltage) / 65535
68+
accuracy of the chip, returned values range from 0 to ``reference_voltage``."""
69+
return self.value * self._mcp.reference_voltage / 65535

0 commit comments

Comments
 (0)