Skip to content

Commit 6c5f80a

Browse files
authored
Merge pull request #685 from adafruit/Raspberry_Pi_DS18B20_Temperature_Sensing
Raspberry Pi DS18B20 temperature sensing
2 parents ccb6470 + c8c2ca0 commit 6c5f80a

6 files changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Raspberry_Pi_DS18B20_Temperature_Sensing
2+
3+
Code to accompany this tutorial:
4+
https://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing
36.3 KB
Binary file not shown.
45.4 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import glob
2+
import time
3+
4+
base_dir = '/sys/bus/w1/devices/'
5+
device_folder = glob.glob(base_dir + '28*')[0]
6+
device_file = device_folder + '/w1_slave'
7+
8+
def read_temp_raw():
9+
f = open(device_file, 'r')
10+
lines = f.readlines()
11+
f.close()
12+
return lines
13+
14+
def read_temp():
15+
lines = read_temp_raw()
16+
while lines[0].strip()[-3:] != 'YES':
17+
time.sleep(0.2)
18+
lines = read_temp_raw()
19+
equals_pos = lines[1].find('t=')
20+
if equals_pos != -1:
21+
temp_string = lines[1][equals_pos+2:]
22+
temp_c = float(temp_string) / 1000.0
23+
temp_f = temp_c * 9.0 / 5.0 + 32.0
24+
return temp_c, temp_f
25+
26+
while True:
27+
print(read_temp())
28+
time.sleep(1)

0 commit comments

Comments
 (0)