I'm pretty new in this forum and generally at wotking with Raspberry Pi. I'm trying to read the value of a potentiometer with my raspberry pi 4 B and am using MCP3008 through spi. My purpous is to control some elements in puredata with it.
I've already connected the MCP and potentiometer with a breadboard to the Rpi. At the moment I get only once the correct value with this code:
test.py (which I run with -> python test.py)
from MCP3008 import MCP3008 adc = MCP3008() value = adc.read( channel = 0 ) # the mcp channel connected to potentiometer print("Wert0: %.2f" % value)
The MCP3008 module:
from spidev import SpiDev class MCP3008: def __init__(self, bus = 0, device = 0): self.bus, self.device = bus, device self.spi = SpiDev() self.open() self.spi.max_speed_hz = 500000 # 500KHz def open(self): self.spi.open(self.bus, self.device) self.spi.max_speed_hz = 500000 # 500KHz def read(self, channel = 0): adc = self.spi.xfer2([1, (8 + channel) << 4, 0]) data = ((adc & 3) << 8) + adc return data def close(self): self.spi.close()
I got the Spidev from following repository: https://github.com/doceme/py-spidev
After the first time each time I run the same code I get zero. It turned out that after rebooting it works again, but again only for one time running the code! I'm pretty confused because I'm quiet sure that the wiring is correct, as I get the value correct at the first time. And what confuses me more is that the code I run stays the same but the value changes to zero! As if the interface breaks connection after one time or the voltage gets "absorbed" or something!
Photos of the setup:
Does anyone know what the possible reason could be and how to fix it?
Thanks in advance