Skip to content

Commit 5fa65a3

Browse files
author
Mikey Sklar
committed
wiring diagrams updated for both 20-pin and 40-pin RPIs
1 parent 95e8c4a commit 5fa65a3

5 files changed

Lines changed: 10 additions & 27 deletions

File tree

36.3 KB
Binary file not shown.
45.4 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
1-
import os
2-
import glob
1+
import board
32
import time
43

5-
os.system('modprobe w1-gpio')
6-
os.system('modprobe w1-therm')
4+
from adafruit_onewire.bus import OneWireBus
5+
from adafruit_ds18x20 import DS18X20
76

8-
base_dir = '/sys/bus/w1/devices/'
9-
device_folder = glob.glob(base_dir + '28*')[0]
10-
device_file = device_folder + '/w1_slave'
7+
# Initialize one-wire bus on board pin D5.
8+
ow_bus = OneWireBus(board.D4)
119

12-
def read_temp_raw():
13-
f = open(device_file, 'r')
14-
lines = f.readlines()
15-
f.close()
16-
return lines
10+
# Scan for sensors and grab the first one found.
11+
ds18 = DS18X20(ow_bus, ow_bus.scan()[0])
1712

18-
def read_temp():
19-
lines = read_temp_raw()
20-
while lines[0].strip()[-3:] != 'YES':
21-
time.sleep(0.2)
22-
lines = read_temp_raw()
23-
equals_pos = lines[1].find('t=')
24-
if equals_pos != -1:
25-
temp_string = lines[1][equals_pos+2:]
26-
temp_c = float(temp_string) / 1000.0
27-
temp_f = temp_c * 9.0 / 5.0 + 32.0
28-
return temp_c, temp_f
29-
13+
# Main loop to print the temperature every second.
3014
while True:
31-
print(read_temp())
32-
time.sleep(1)
33-
15+
print('Temperature: {0:0.3f}C'.format(ds18.temperature))
16+
time.sleep(1.0)

0 commit comments

Comments
 (0)