0

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[1] & 3) << 8) + adc[2]
        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:

enter image description here enter image description here enter image description here

Does anyone know what the possible reason could be and how to fix it?

Thanks in advance

4
  • What is a "poti"? There is no need to "get" Spidev - it should be included in Raspberry Pi OS. Have you enabled the kernel driver with dtparam=spi=on? I suggest you use python3 - python2 will no longer be the default from next year (and has already gone from most distos)
    – Milliways
    Nov 11, 2020 at 23:08
  • I don't know where the OP is from, but in German, "poti" is used as a short for potentiometer, i.e., a variable resistor. Which I guess they are using as (part of) a voltage divider, and trying to read through the MCP3008 SPI ADC. A circuit diagram would, of course, be helpful to see that everything is wired correctly. Nov 11, 2020 at 23:22
  • yes I meant potentiometer, I corrected it in my post. I enabled spi via raspi-config window, does it do the same as dtparam=spi=on?
    – solargun
    Nov 12, 2020 at 7:41
  • Odd. I can't think of any reason for the symptoms you describe.
    – joan
    Nov 12, 2020 at 8:42

3 Answers 3

2

Try to pin-point the problem. Software or hardware.

I suggest you try the following (with my pigs utility).

sudo pigpiod # start the pigpio daemon

pigs spio 0 50000 0 # open spi 0.0 at 50 kbs

pigs spix 0 1 128 0 # read MCP3008 channel 0
pigs spix 0 1 128 0 # read MCP3008 channel 0
pigs spix 0 1 128 0 # read MCP3008 channel 0
pigs spix 0 1 128 0 # read MCP3008 channel 0
pigs spix 0 1 128 0 # read MCP3008 channel 0

Does the read give readings which seem reasonable.

http://abyz.me.uk/rpi/pigpio/pigs.html#SPIX

3
  • Thank you. I tried this and got 3 0 0 0 as result. What does it mean? Btw why is there 5 times the same line in your code?
    – solargun
    Nov 12, 2020 at 18:50
  • It means there is not an MCP3008 connected to the SPI bus 0.0. The identical lines take one reading each. Could you add a clear photo of the connections between the Pi and MCP3008? You will probably need several close-ups.
    – joan
    Nov 12, 2020 at 19:03
  • sure I'll add today
    – solargun
    Nov 14, 2020 at 9:49
1

I see the class you're using has a close function. Perhaps you should call it at the end of your code.

1
  • I've already tried it, doesn't help...
    – solargun
    Nov 12, 2020 at 18:45
0

Ok apparently it works now. I had used a t-cobber before but now I have removed it and connected the pins directly to the breadboard. I still do not know what the problem was. But anyways it works! Thanks again for your support

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.