Advertisement
schuetz

lightSensorTest

Apr 27th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.40 KB | None | 0 0
  1. import spidev
  2. import time
  3.  
  4. spi = spidev.SpiDev()
  5. spi.open(0,0)
  6.  
  7. def readadc(adcnum):
  8.     # read SPI data from MCP3004 chip, 4 possible adcs (0 thru 3)
  9.     if adcnum >3 or adcnum < 0:
  10.         return-1
  11.     r = spi.xfer2([1,8+adcnum <<4,0])
  12.     adcout = ((r[1] &3) <<8)+r[2]
  13.     return adcout
  14.  
  15. while True:
  16.     value=readadc(2)
  17.     volts=(value*3.3)/1024
  18.     print("%4d/1023 => %5.3f V" % (value, volts))
  19.     time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement