Temp Sensor (ds18b20)

Basic tutorial of how to setup a DS18b20 temp sensor with the raspberry pi.

PARTS:
RPI 3 – https://amzn.to/2VA9pQY
4 Amp Power Adapter – https://amzn.to/2CTptWu
16GB micro SD – https://amzn.to/2SFMwd3
120 pcs jumper cable: https://ebay.to/2VAb9cY
ds18b20 sensor – https://amzn.to/2M5yHSN
SCHEMATIC:

CODE:

#!/usr/bin/env python
import os

def sensor():
    for i in os.listdir('/sys/bus/w1/devices'):
        if i != 'w1_bus_master1':
            ds18b20 = i
    return ds18b20

def read(ds18b20):
    location = '/sys/bus/w1/devices/' + ds18b20 + '/w1_slave'
    tfile = open(location)
    text = tfile.read()
    tfile.close()
    secondline = text.split("\n")[1]
    temperaturedata = secondline.split(" ")[9]
    temperature = float(temperaturedata[2:])
    celsius = temperature / 1000
    farenheit = (celsius * 1.8) + 32
    return celsius, farenheit

def loop(ds18b20):
    while True:
        if read(ds18b20) != None:
            print "Current temperature : %0.3f C" % read(ds18b20)[0]
            print "Current temperature : %0.3f F" % read(ds18b20)[1]

def kill():
    quit()

if __name__ == '__main__':
    try:
        serialNum = sensor()
        loop(serialNum)
    except KeyboardInterrupt:
        kill()