raid23
Posts: 24
Joined: Tue Dec 19, 2017 7:26 am

MCP3008 troubleshooting

Wed Mar 07, 2018 11:40 am

I need some ideas how to debug where is the reason why I can't get anything but zeroes from MCP3008.
Setup is as follows:
* Raspberry Pi Zero W
* Raspbian Strech with Desktop (fresh install)
* SPI enabled
* python-dev installed
* adafruit mcp3008 installed
* wiring is done according to this example
* and in real life looks like this
* for testing purpose I've connected 3,3V on channel 0 (in order to receive something else than zero as reading)

All this said and done

Code: Select all

python Adafruit_Python_MCP3008/examples/simpletest.py
returns me nothing but
Reading MCP3008 values, press Ctrl-C to quit...
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---------------------------------------------------------
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
I've even tried loopback test connecting MISO and MOSI through 1K resistor as suggested here, but still nothing..

Any ideas how should I proceed, what should I test in order to find out where are the readings stuck?

User avatar
joan
Posts: 13557
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: MCP3008 troubleshooting

Wed Mar 07, 2018 12:08 pm

The wiring looks okay.

As a quick test try the following commands.

sudo pigpiod # start the daemon

pigs spio 0 40000 0

pigs spix 0 1 128 0
pigs spix 0 1 128 0
pigs spix 0 1 128 0

raid23
Posts: 24
Joined: Tue Dec 19, 2017 7:26 am

Re: MCP3008 troubleshooting

Wed Mar 07, 2018 12:49 pm

Code: Select all

[email protected]:~ $ pigs spio 0 40000 0
0
[email protected]:~ $ pigs spix 0 1 128 0
3 0 0 0
[email protected]:~ $ pigs spix 0 1 128 0
3 0 0 158
[email protected]:~ $ pigs spix 0 1 128 0
3 0 0 0

User avatar
joan
Posts: 13557
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: MCP3008 troubleshooting

Wed Mar 07, 2018 12:58 pm

You got one non-zero reading (the 158). That would suggest an intermittent connection. Perhaps make sure MISO is well seated or try another jumper wire.

raid23
Posts: 24
Joined: Tue Dec 19, 2017 7:26 am

Re: MCP3008 troubleshooting

Wed Mar 07, 2018 1:26 pm

Strange things happen..
When I tried some additional 15 times

Code: Select all

pigs spix 0 1 128 0
it returns only "3 0 0 0"

But in the meanwhile opened up simpletest.py from Adafruit MCP3008 module and changed this part:

Code: Select all

# Software SPI configuration:
CLK  = 11 #previous value 23
MISO = 9 #previous value 21
MOSI = 10 #previous value 19
CS   = 8 #previous value 24
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)
As I understand GPIO number (not physical pin numbers) are expected here.. Anyway, now I get also a non-zero value (1023 for channel 0, as expected)

What I still don't figure out is:
1. Why are the values of other channels "arbitrary"? It looks like values from 0-70 are thrown in randomly
2. If I comment out the code given above and uncomment the following section

Code: Select all

# Hardware SPI configuration:
SPI_PORT   = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
again, only zero values are returned.
3. When I try some other code snippets found from the web, like

Code: Select all

import mcp3008
with mcp3008.MCP3008() as adc:
    print adc.read([mcp3008.CH0]) # prints raw data [CH0]
or

Code: Select all


import spidev
from time import sleep

# Establish SPI device on Bus 0,Device 0

spi = spidev.SpiDev()
spi.open(0,0)

def getAdc (channel):
#check valid channel
    if ((channel>7)or(channel<0)):
        return -1

# Preform SPI transaction and store returned bits in 'r'

    r = spi.xfer([1, (8+channel) << 4, 0])

#Filter data bits from retruned bits

    adcOut = ((r[1]&3) << 8) + r[2]

    percent = int(round(adcOut/10.24))

#print out 0-1023 value and percentage

    print("ADC Output: {0:4d} Percentage: {1:3}%".format (adcOut,percent))

    sleep(0.5)

while True:
    getAdc(0)

I also get only zeroes.

Don't get me wrong - I'm happy that simpletest.py software configuration already returns something, but I want to understand why..

User avatar
joan
Posts: 13557
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: MCP3008 troubleshooting

Wed Mar 07, 2018 1:31 pm

You would need to double check what simpletest.py is using for GPIO numbering. You would get 1023 if you happen to be using a GPIO which is always high.

I would run http://abyz.me.uk/rpi/pigpio/examples.h ... monitor_py while you are running simpletest.py. That would show which GPIO are changing.

raid23
Posts: 24
Joined: Tue Dec 19, 2017 7:26 am

Re: MCP3008 troubleshooting

Wed Mar 07, 2018 1:50 pm

Yes, as I said above, I connected 3,3V on ch0, thus 1023 reading is just as expected.
What I didn't figure out was why other channels are showing "random numbers".
And why spidev module tools do not retrieve anything..

User avatar
joan
Posts: 13557
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: MCP3008 troubleshooting

Wed Mar 07, 2018 5:44 pm

I doubt the Adafruit module would produce correct results when the others do not. I suspect the Adafruit module is not talking to the SPI MISO line.

Could you run monitor.py and check that GPIO 8/9/10/11 only are changing.

raid23
Posts: 24
Joined: Tue Dec 19, 2017 7:26 am

Re: MCP3008 troubleshooting

Wed Mar 07, 2018 8:09 pm

It's quite difficult to follow monitor.py output, but when running above mentioned "SPI software configuration" piscope shows me the following.

When I try "SPI hardware configuration" mode, piscope is screen is quite empty

User avatar
joan
Posts: 13557
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: MCP3008 troubleshooting

Wed Mar 07, 2018 8:31 pm

I am at a loss to explain what you are seeing. The only explanation I can think of is your SPI hardware is broken. The Linux SPI driver and the pigpio SPI driver work in different ways. I'd expect at least one of them to work.

piscope doesn't seem to be showing the complete transaction in your Adafruit example. I suspect the SPI speed is higher than it can cope with (in the pigs example I used a speed of 40000 bits per second).

ddahms
Posts: 64
Joined: Tue Mar 18, 2014 3:38 pm

Re: MCP3008 troubleshooting

Thu Mar 08, 2018 2:43 am

I suggest one easy experiment--move your green wire to GND instead of 3,3V and see if your reading of 1023 changes to 0. This will tell you if the Pi is actually talking to the chip or not. If it does change, then try connecting 1 or 2 volts to pin 1 and see what reading it gives. You can create that voltage with two resistors or a potentiometer or even a AA battery.

The reason the other channels read random values is those pins are not connected to anything so their voltages just float around. You can tie them to gnd and they should all read 0. You could tie them all together and then they should all read the same value.

raid23
Posts: 24
Joined: Tue Dec 19, 2017 7:26 am

Re: MCP3008 troubleshooting

Thu Mar 08, 2018 7:30 am

Thanks for the useful hints!
I suggest one easy experiment--move your green wire to GND instead of 3,3V and see if your reading of 1023 changes to 0
Yep, it changed to 0, and I'm also able to get reading from the moisture sensors.

Also connected free channels to the ground and got the picture prettier.

But what I still can't figure out, why this part

Code: Select all

# Hardware SPI configuration:
SPI_PORT = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
From the Adafruit MCP3008 is not willing to work with me. As all the rest attempts to use gpizero or spidev modules..

User avatar
DougieLawson
Posts: 33796
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: MCP3008 troubleshooting

Thu Mar 08, 2018 9:48 am

Which SPI chip select are you using? GPIO08/PIN24 or GPIO07/PIN26?
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

raid23
Posts: 24
Joined: Tue Dec 19, 2017 7:26 am

Re: MCP3008 troubleshooting

Thu Mar 08, 2018 11:17 am

Code: Select all

GPIO08/PIN24

User avatar
DougieLawson
Posts: 33796
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: MCP3008 troubleshooting

Thu Mar 08, 2018 11:49 am

Get a photo of your wiring, post the photo on http://imgur.com, post the BBCode link to the photo on here.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

raid23
Posts: 24
Joined: Tue Dec 19, 2017 7:26 am

Re: MCP3008 troubleshooting

Thu Mar 08, 2018 12:07 pm

Photo of the wiring is here
Nb! Please don't mind the 3,3V connection on channel0 - I'm totally happy with reading 1023 at the moment.

User avatar
DougieLawson
Posts: 33796
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: MCP3008 troubleshooting

Thu Mar 08, 2018 1:42 pm

Image

Code: Select all

#!/usr/bin/python3
from gpiozero import MCP3008
from time import sleep
tmp = MCP3008(channel=0, device=0)

while True:
    adcValue = (tmp.value * 3.3)*100
    print('{:.1f}'.format(adcValue), 10 * ' ')
    sleep(0.1)
When I turn the 10K trimpot I get a reading between 1.5 & 329.5.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

raid23
Posts: 24
Joined: Tue Dec 19, 2017 7:26 am

Re: MCP3008 troubleshooting

Thu Mar 08, 2018 2:04 pm

Using this code, for every channel the result is the same:

Code: Select all

[email protected]:~ $ python adctest.py 
('0.2', '          ')
('0.2', '          ')
('0.2', '          ')

User avatar
DougieLawson
Posts: 33796
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: MCP3008 troubleshooting

Thu Mar 08, 2018 2:34 pm

Time to spend a couple of dollars on a new MCP3008.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

ddahms
Posts: 64
Joined: Tue Mar 18, 2014 3:38 pm

Re: MCP3008 troubleshooting

Thu Mar 08, 2018 8:06 pm

As I understand the situation, presently it works using software-SPI and returns believable values. With hardware-SPI, it always returns zeros, and Piscope shows the SPI signals don't even move. To me, this feels like a Pi software or configuration issue. Does the /dev/spidev0.0 file exist, with crw-rw---- permissions?

For further troubleshooting of your SPI, you could eliminate the MCP3008 altogether. Connect MOSI directly to MISO and run a SPI loopback test.

raid23
Posts: 24
Joined: Tue Dec 19, 2017 7:26 am

Re: MCP3008 troubleshooting

Thu Mar 08, 2018 8:22 pm

Time to spend a couple of dollars on a new MCP3008.
That can't be it - I've already bought three chips from two different places, and as I mentioned - with software SPI configuration I get non-zero results.
As I understand the situation, presently it works using software-SPI and returns believable values. With hardware-SPI, it always returns zeros, and Piscope shows the SPI signals don't even move.
Yes, exactly like that.
Does the /dev/spidev0.0 file exist, with crw-rw---- permissions
Yes, it does.
Connect MOSI directly to MISO and run a SPI loopback test
I've done that also - in that case, I see something moving on GPIO8/CE0 only.. I'm on the opinion, too, it must be some kind of configuration issue, but I have no idea where to search..

User avatar
joan
Posts: 13557
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: MCP3008 troubleshooting

Thu Mar 08, 2018 9:11 pm

The only conclusion I can draw is that the main SPI peripheral on the Pi is bust. I don't know how that could be diagnosed.

Perhaps see if the auxiliary SPI is okay. That uses different hardware.

sudo pigpiod

$ pigs spio 0 40000 256
0
$ pigs spix 0 0xaa 0x55 0xaa
3 0 0 0
$ pigs spix 0 0xaa 0x55 0xaa
3 0 0 0
$ pigs spix 0 0xaa 0x55 0xaa
3 0 0 0

piscope should show as follows.
Last edited by joan on Thu Mar 08, 2018 9:16 pm, edited 1 time in total.

User avatar
joan
Posts: 13557
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: MCP3008 troubleshooting

Thu Mar 08, 2018 9:14 pm

Why can't I add screenshots any more? You use to be able to upload several to the page.

User avatar
DougieLawson
Posts: 33796
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: MCP3008 troubleshooting

Fri Mar 09, 2018 1:13 pm

joan wrote:
Thu Mar 08, 2018 9:14 pm
Why can't I add screenshots any more? You use to be able to upload several to the page.
It works for me with BBCode from Imgur

Code: Select all

[img]https://i.imgur.com/0ULPbsX.jpg[/img]
the URL must end with .jpg, .jpeg or .gif or the image won't display.

I've never used the "upload as an attachment" junk on the forum (there's too many restrictions on image size).
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
joan
Posts: 13557
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: MCP3008 troubleshooting

Fri Mar 09, 2018 2:00 pm

@DougieLawson

I don't want to upload photos to yet another site. I found it straightforward to take a screenshot and upload it using the forum software. I liked the challenge of reducing the jpeg quality to get the image just under 64kbytes.

Return to “Interfacing (DSI, CSI, I2C, etc.)”

Who is online

Users browsing this forum: markkuk and 13 guests