SHARE
TWEET

MCP3008 TMP36 and LDR

a guest Feb 22nd, 2013 687 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from __future__ import print_function      
  2. import spidev
  3. import time
  4. import os
  5.  
  6. spi = spidev.SpiDev()
  7. spi.open(0,0)
  8.  
  9. def get_adc(adcnum):
  10.         r = spi.xfer2([1,(8+adcnum)<<4,0])
  11.         adcout = ((r[1]&3) << 8) + r[2]
  12.         return adcout
  13.  
  14. while True:
  15.         get_adc(0) # throw away first reading as a test. didn't fix anything
  16.         retrieve0 = get_adc(0) # get TMP36 #1
  17.         time.sleep(1)
  18.         get_adc(1) # throw away first reading as a test. didn't fix anything
  19.         retrieve1 = get_adc(1) # get TMP36 #2
  20.         time.sleep(1)
  21.         get_adc(2) # throw away first reading as a test. didn't fix anything
  22.         retrieve2 = get_adc(2) # get LDR #1 light level reading
  23.         time.sleep(1)
  24.         get_adc(3) # throw away first reading as a test. didn't fix anything
  25.         retrieve3 = get_adc(3) # get LDR #2 light level reading
  26.         time.sleep(1)
  27.  
  28.         #convert TMP36 #1 digital reading to Celsius temperature
  29.         c_temp0 = (((retrieve0 * ( 3300.0 / 1024.0)) - 100.0) / 10.0) - 40.0
  30.         c_temp0 = "%.1f" % c_temp0
  31.    
  32.         #convert TMP36 #2 digital reading to Celsius temperature
  33.         c_temp1 = (((retrieve1 * ( 3300.0 / 1024.0)) - 100.0) / 10.0) - 40.0    
  34.         c_temp1 = "%.1f" % c_temp1
  35.  
  36.         print("TMP1:")
  37.         print(retrieve0)
  38.         print(c_temp0, "C")
  39.         print("TMP2:")
  40.         print(retrieve1)
  41.         print(c_temp1, "C")
  42.         print("LDR1:")
  43.         print(retrieve2)
  44.         print("LDR2:")
  45.         print(retrieve3)
  46.         time.sleep(0.5) # sleepy time...
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top