-
Notifications
You must be signed in to change notification settings - Fork 0
/
max6675.py
51 lines (40 loc) · 1.2 KB
/
max6675.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# adapted from https://github.com/archemius/MAX6675-Raspberry-pi-python
from machine import Pin
import time
class MAX6675:
# set pin number for communicate with MAX6675
def __init__(self, cs: int, sck: int, so: int) -> None:
self._cs_no = cs
self._cs = Pin(cs, Pin.OUT)
self._cs.on()
self._sck = Pin(sck, Pin.OUT)
self._sck.off()
self._so = Pin(so, Pin.IN)
self._so.on()
def read_temp(self) -> float:
self._cs.off()
time.sleep(0.002)
self._cs.on()
time.sleep(0.22)
self._cs.off()
self._sck.on()
time.sleep(0.001)
self._sck.off()
value = 0
for i in range(11, -1, -1):
self._sck.on()
value = value + (self._so.value() * (2 ** i))
self._sck.off()
self._sck.on()
error_tc = self._so.value()
self._sck.off()
for i in range(2):
self._sck.on()
time.sleep(0.001)
self._sck.off()
self._cs.on()
temp = value * 0.25 # Celsius
if error_tc != 0:
return float(str(self._cs_no))
else:
return float(str(temp))