Advertisement
Guest User

MCP3008 TMP36 and LDR

a guest
Feb 22nd, 2013
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  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...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement