wiring diagrams updated for both 20-pin and 40-pin RPIs
This commit is contained in:
parent
95e8c4a17d
commit
5fa65a3a48
5 changed files with 10 additions and 27 deletions
BIN
Raspberry_Pi_DS18B20_Temperature_Sensing/ds18b20-basic-20pin.fzz
Normal file
BIN
Raspberry_Pi_DS18B20_Temperature_Sensing/ds18b20-basic-20pin.fzz
Normal file
Binary file not shown.
BIN
Raspberry_Pi_DS18B20_Temperature_Sensing/ds18b20-basic-40pin.fzz
Normal file
BIN
Raspberry_Pi_DS18B20_Temperature_Sensing/ds18b20-basic-40pin.fzz
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -1,33 +1,16 @@
|
|||
import os
|
||||
import glob
|
||||
import board
|
||||
import time
|
||||
|
||||
os.system('modprobe w1-gpio')
|
||||
os.system('modprobe w1-therm')
|
||||
from adafruit_onewire.bus import OneWireBus
|
||||
from adafruit_ds18x20 import DS18X20
|
||||
|
||||
base_dir = '/sys/bus/w1/devices/'
|
||||
device_folder = glob.glob(base_dir + '28*')[0]
|
||||
device_file = device_folder + '/w1_slave'
|
||||
# Initialize one-wire bus on board pin D5.
|
||||
ow_bus = OneWireBus(board.D4)
|
||||
|
||||
def read_temp_raw():
|
||||
f = open(device_file, 'r')
|
||||
lines = f.readlines()
|
||||
f.close()
|
||||
return lines
|
||||
# Scan for sensors and grab the first one found.
|
||||
ds18 = DS18X20(ow_bus, ow_bus.scan()[0])
|
||||
|
||||
def read_temp():
|
||||
lines = read_temp_raw()
|
||||
while lines[0].strip()[-3:] != 'YES':
|
||||
time.sleep(0.2)
|
||||
lines = read_temp_raw()
|
||||
equals_pos = lines[1].find('t=')
|
||||
if equals_pos != -1:
|
||||
temp_string = lines[1][equals_pos+2:]
|
||||
temp_c = float(temp_string) / 1000.0
|
||||
temp_f = temp_c * 9.0 / 5.0 + 32.0
|
||||
return temp_c, temp_f
|
||||
|
||||
# Main loop to print the temperature every second.
|
||||
while True:
|
||||
print(read_temp())
|
||||
time.sleep(1)
|
||||
|
||||
print('Temperature: {0:0.3f}C'.format(ds18.temperature))
|
||||
time.sleep(1.0)
|
||||
|
|
|
|||
Loading…
Reference in a new issue