-
Notifications
You must be signed in to change notification settings - Fork 2
/
dallas.py
63 lines (51 loc) · 1.61 KB
/
dallas.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
52
53
54
55
56
57
58
59
60
61
62
#simple ds1820 ds18b20 module
#reads only one sensor per pin
from machine import Pin
import _onewire
class ds1820:
#configure pin to onewire transmission
def init(pin):
Pin(pin, Pin.OPEN_DRAIN, Pin.PULL_UP)
#send convert command to sensor
def convert(pin):
_onewire.reset(Pin(pin))
_onewire.writebyte(Pin(pin), 0xcc) #skip rom
_onewire.writebyte(Pin(pin), 0x44) #convert
#send read temperature command to sensor
def read(pin):
_onewire.reset(Pin(pin))
_onewire.writebyte(Pin(pin), 0xcc) #skip rom
_onewire.writebyte(Pin(pin), 0xbe) #read scratchpad
tlo = _onewire.readbyte(Pin(pin))
thi = _onewire.readbyte(Pin(pin))
_onewire.reset(Pin(pin))
#convert raw values to human eye readable
temp = tlo + thi * 256
if temp > 32767:
temp = temp - 65536
temp = temp * 0.5
return(temp)
class ds18b20:
#configure pin to onewire transmission
def init(pin):
Pin(pin, Pin.OPEN_DRAIN, Pin.PULL_UP)
#send convert command to sensor
def convert(pin):
_onewire.reset(Pin(pin))
_onewire.writebyte(Pin(pin), 0xcc) #skip rom
_onewire.writebyte(Pin(pin), 0x44) #convert
#send read temperature command to sensor
def read(pin):
_onewire.reset(Pin(pin))
_onewire.writebyte(Pin(pin), 0xcc) #skip rom
_onewire.writebyte(Pin(pin), 0xbe) #read scratchpad
#read raw temperature
tlo = _onewire.readbyte(Pin(pin))
thi = _onewire.readbyte(Pin(pin))
_onewire.reset(Pin(pin))
#convert raw values to human eye readable
temp = tlo + thi * 256
if temp > 32767:
temp = temp - 65536
temp = temp * 0.0625
return(temp)