pimoroni / automation-hat

Python library and examples for the Pimoroni Automation HAT, pHAT and HAT Mini
https://shop.pimoroni.com/products/automation-hat
MIT License
121 stars 42 forks source link

Why round to 2 decimal places? #25

Closed shortbloke closed 4 years ago

shortbloke commented 4 years ago

More of a question before I look to make changes and submit a PR.

The Automation HAT features a 12-bit ADC, thus there are 4096 values:

In order to show the resolution represented by each bit shouldn't the output be to:

Surely part of the purpose of ADC4 is to provide a higher resolution for lower voltage measurements?

Anyone wanting to handler a lower resolution can round the result themselves. Right?

Gadgetoid commented 4 years ago

The ADC is 12-bit, but the 12th bit is the sign-bit so positive voltages between 0 and 24v (actually 25.85v because of how the resistor divider is set up) so it's closer to 0.012v granularity.

Except the full-scale range of the ADC when reading a 0-25.85v value is actually set to 4.096v (not 3.3v) giving only ~1649 possible usable values, which brings us to a granularity of 0.015v (25.85 / 1649) for 24v and 0.002v for the 3.3v inputs.

In this instance we could probably still deal with 3 decimal places on the output voltage and let the end-user oversample the ADC and round as they see fit. So you're still right. A PR would be appreciated.

This is documented pretty exhaustively here - https://forums.pimoroni.com/t/automation-hat-accuracy/7252/3 (I should put a link to this post somewhere in the Automation HAT docs!)