#!/usr/bin/env python # File read_arduino.py # use with sketch arduino_ds18b20X2.ino # http://www.bristolwatch.com/index.htm # By Lewis Loflin - lewis@bvu.net # access to GPIO must be through root import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) # disable warnings # These two functions used for DS18B20 sensor def degC(val): return val * 0.0625 def degF(val): return (val * 0.0625) * 9 / 5 + 32 CS = 18 CLK =23 dataBit = 24 Reset = 25 GPIO.setup(CS, GPIO.OUT) # CS to Arduino DP5 GPIO.setup(CLK, GPIO.OUT) # CLK to Arduino DP6 GPIO.setup(dataBit, GPIO.IN) # dataBit to Arduino DP4 GPIO.setup(Reset, GPIO.OUT) # Reset to Arduino Reset # Setup IO GPIO.output(CS, 0) GPIO.output(CLK, 0) GPIO.output(Reset, 0) # Reset Arduino to perform whatever reading def resetArduino(): GPIO.output(Reset, 1) time.sleep(.001) GPIO.output(Reset, 0) time.sleep(3) def readArduino(): temp = 0 GPIO.output(CS, 1) # time.sleep(.002) for i in range(0,15): # loop here 15 times GPIO.output(CLK, 1) time.sleep(.001) temp = temp + GPIO.input(dataBit) GPIO.output(CLK, 0) temp = temp << 1 # shift 1 bit left GPIO.output(CLK, 1) time.sleep(.001) temp = temp + GPIO.input(dataBit) # get bit 16 GPIO.output(CLK, 0) GPIO.output(CS, 0) # clk one more time GPIO.output(CLK, 1) # time.sleep(.002) GPIO.output(CLK, 0) return temp # begin demo resetArduino() print "Please wait for reading..." j = readArduino() print hex(j) print bin(j) print j print degC(j), "deg. C" print degF(j), "deg. F" GPIO.cleanup() print "Exit now." exit