mcp3008.py 615 B

12345678910111213141516171819202122232425
  1. from spidev import SpiDev
  2. import time
  3. class MCP3008:
  4. def __init__(self, bus = 0, device = 0):
  5. self.bus, self.device = bus, device
  6. self.spi = SpiDev()
  7. self.open()
  8. def open(self):
  9. self.spi.open(self.bus, self.device)
  10. def read(self, channel = 0):
  11. adc = self.spi.xfer2([1, (8 + channel) << 4, 0])
  12. data = ((adc[1] & 3) << 8) + adc[2]
  13. return data
  14. def close(self):
  15. self.spi.close()
  16. if __name__ == "__main__":
  17. adc = MCP3008(0,0)
  18. while True:
  19. print("Rohwerte:", [adc.read(i) for i in range(8)])
  20. time.sleep(.3)