0

I'm running a pi 3b+ and I'm having a weird issue with SPI. I have a breadboard with an mcp3008 chip, and a potentiometer connected to it. I can get voltage values some of the time. I either have to turn SPI on or turn it off to work. If I restart the pi and run the same code then it will give me a 0 reading. The only way I can get it to work is by toggling SPI. Currently it's giving me correct values with SPI off.

To make things even more weird, everything was working correctly. However when I reformatted the SD card and installed a new image, it started being funky. I replaced the wires, breadboard and installed the latest adafruit and GPIO packages.

When I did install the new image, I tried to make it so I can headlessly connect to the pie right off the bat but I don't know if that could have done something to change the SPI.

from gpiozero import MCP3008

pot = MCP3008(0)

while True:
    print(pot.value)

Using this wiring https://gpiozero.readthedocs.io/en/stable/_images/potentiometer_bb.svg

4
  • What do you mean by turn SPI off? Please post the code and a clear photo of the wiring.
    – joan
    Mar 22 at 17:30
  • I mean toggling the SPI setting on and off in the Pi settings Mar 22 at 18:06
  • You must enable SPI and leave it enabled. Need a photo of your wiring, I suggest you add a time.sleep(0.1) to the while loop.
    – joan
    Mar 22 at 18:12
  • SPI is on and I rebooted. If I run the code, it works for about 30 seconds and then goes back to giving me a 0 value Mar 22 at 18:50

1 Answer 1

0

I am currently also stuck on a problem with the SPI on a Compute Module 4 (Haven't used a Pi 3 in a long time). So I can only try and help you with the best of my knowledge, and share the steps that I have been taking.

Hardware

I always like to check/test the hardware first with these kind of problems, especially if they start to appear seemingly randomly.

Always double check the connections, and check if you might have been able to destroy some components, then check those components.

Do you have another MCP3008 chip you could test?

Maybe another Raspberry Pi as well? Where you could test your new image?

Software

Are you using the standard Raspberry Pi OS installation? (Or maybe Ubuntu, or the 64-bit version of Raspberry Pi OS?) This kind of information always helps with debugging. And since you mentioned Adafruit libraries, I am assuming you are using Python? Which packages? Which version of each package?

Are you certain that you have enabled SPI? (Either via raspi-config or via directly editing the /boot/config.txt file to contain the line: dtparam=spi=on)

When your application always gives a 0 reading, does your /dev/ folder have any entries for SPI? (You can check by running ls | grep spi in your /dev/ folder). It should return something like /dev/spidev0.0 or /dev/spidev0.1 etc...

Does your /sys/bus/spi/devices folder contain any entries?

Does the kernel logs say anything particular about the SPI bus? (you can check the kernel logs with the dmesg command. And filter on SPI with dmesg | grep spi)

I also read somewhere that enabling UART might get in the way of SPI (I can't seem to find the source again, I will edit if I find it). This does not seem logical to me (and should not need to be the case), but it might be worth a try. So you could set enable_uart=0 in the /boot/config.txt file, do a reboot and see if the problem persists.

Hopefully some of this helps!

Mats de Waard

4
  • SPI is turned on in confifg.txt. Ran the check on dev and it returned spidev0.0 spidev0.1. the spi folder has two folders for devices and drivers along with 3 files: drivers_autoprobe, drivers_probe, uevent. Nothing seems to come up on the kernal logs. There's nothing about uart in config.txt Mar 22 at 19:04
  • And you checked the hardware? Used different pins on the Pi? Did you try another MCP IC? You should leave SPI enabled, and if the image you are using is completely fresh, it probably would be a hardware problem. Also, add a delay in your while loop, like @joan stated. Could be that the loops repeats too often, which could make it seem like it will 'hang' or always give an old value or something alike. What is your memory and CPU usage during your testing? Mar 23 at 9:33
  • Yes the hardware works. I reformatted the boot sd card and installed the Adafruit mcp3xxx library. I can get it to work for hours. However I'll go on to install a different library and then it breaks the reading. Again, I can get it to work if I toggle the SPI on/off. BUt thats not a good solution Mar 23 at 23:19
  • And how is your CPU and memory usage at the moment of receiving unwanted values? Have you tried adding a delay to your loop, like @joan suggested? Mar 24 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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