thermistors support

This commit is contained in:
Nikolay Khabarov
2017-06-13 03:48:46 +03:00
parent 0f020562ee
commit e4acf83428
2 changed files with 72 additions and 3 deletions

View File

@@ -27,14 +27,13 @@ else:
def measure(channel):
"""
Measure voltage on chip input.
Raises OSError(Errno 121) "Remote I/O error" on reading error.
:param channel: chip channel to use.
:return: Voltage in Volts, None if no device connected.
:return: Voltage in Volts.
"""
global __i2c_dev
if channel < 0 or channel > 3:
raise ValueError("Wrong channel")
if __i2c_dev < 0:
return None
# configure
data = struct.pack(">BH",
0x01, # config register

70
cnc/sensors/thermistor.py Normal file
View File

@@ -0,0 +1,70 @@
"""
This module reads temperature from NTC thermistor connected to ads111x.
Circuit diagram for this module should be like this:
Vcc
---
|
|
.-.
| |
| | R1
'-'
|
o----------------o------------> ads111x input
| |
| | |
.-./ |
| / + |
|/| R0 NTC ----- 10 uF
/-' -----
/ | |
| |
_|_ _|_
GND GND
Since ads111x uses internal reference voltage, 3.3V should be well regulated.
"""
from __future__ import division
import math
import time
import ads111x as adc
CELSIUS_TO_KELVIN = 273.15
# Circuit parameters, resistance in Ohms, temperature in Celsius.
# Beta is thermistor parameter:
# https://en.wikipedia.org/wiki/Thermistor#B_or_.CE.B2_parameter_equation
Vcc = 3.3
R0 = 100000
T0 = 25
BETA = 4092
R1 = 4700
Rinf = R0 * math.exp(-BETA / (T0 + CELSIUS_TO_KELVIN))
def get_temperature(channel):
"""
Measure temperature on specified channel.
:param channel: ads111x channel.
:return: temperature in Celsius
"""
v = adc.measure(channel)
if v >= Vcc:
raise IOError("Thermistor not connected")
if v <= 0:
raise IOError("Short circuit")
r = v * R1 / (Vcc - v)
return (BETA / math.log(r / Rinf)) - CELSIUS_TO_KELVIN
# for test purpose
if __name__ == "__main__":
while True:
for i in range(0, 4):
print("T{}={}".format(i, get_temperature(i)))
print("-----------------------------")
time.sleep(0.5)