NickRothbacher
Posts: 6
Joined: Fri Jul 17, 2015 11:56 pm

SPI Interface receiving no data from MCP3008

Sat Jul 18, 2015 7:23 am

I'm working on the project described here: http://www.raspberrypi-spy.co.uk/2014/0 ... n-mcp3008/ and currently when I run the test script, the raspberry pi only reads zeros from the MCP3008.

In trying to troubleshoot this issue I've added the lines contained in the sticky post in this forum and verified that there is signal going from the joystick to the MCP3008.

I've attached a picture of my wiring if that is helpful, I have a raspberry pi 2 and I am attempting to wire the MCP3008 according to this:
MCP3008 Pi Wire Colour
-------------- ---------------- -----------
Pin 1 (CH0) - Purple
Pin 2 (CH1) - Blue
Pin 3 (CH2) - Green
Pin 9 (DGND) Pin 6 (Ground) Blue
Pin 10 (CS) Pin 24 (GPIO8) Orange
Pin 11 (DIN) Pin 19 (GPIO10) Yellow
Pin 12 (DOUT) Pin 21 (GPIO9) Green
Pin 13 (CLK) Pin 23 (GPIO11) Blue
Pin 14 (AGND) Pin 6 (Ground) Green
Pin 15 (VREF) Pin 1 (3.3V) Yellow
Pin 16 (VDD) Pin 1 (3.3V) Orange.
Here is the code I'm using to test the device:

Code: Select all

#!/usr/bin/python
#--------------------------------------
# This script reads data from a
# MCP3008 ADC device using the SPI bus.
#
# Analogue joystick version!
#
# Author : Matt Hawkins
# Date   : 17/04/2014
#
# http://www.raspberrypi-spy.co.uk/
#
#--------------------------------------
 
import spidev
import time
import os
 
# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)
 
# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
  adc = spi.xfer2([1,(8+channel)<<4,0])
  data = ((adc[1]&3) << 8) + adc[2]
  return data
 
# Define sensor channels
# (channels 3 to 7 unused)
swt_channel = 0
vrx_channel = 1
vry_channel = 2
 
# Define delay between readings (s)
delay = 0.5
 
while True:
 
  # Read the joystick position data
  vrx_pos = ReadChannel(vrx_channel)
  vry_pos = ReadChannel(vry_channel)
 
  # Read switch state
  swt_val = ReadChannel(swt_channel)
 
  # Print out results
  print "--------------------------------------------"
  print("X : {}  Y : {}  Switch : {}".format(vrx_pos,vry_pos,swt_val))
 
  # Wait before repeating loop
  time.sleep(delay)
Thank you in advance for any help you might be able to provide.

ghp
Posts: 1399
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: SPI Interface receiving no data from MCP3008

Sun Jul 19, 2015 5:10 pm

Hello,
a short review looks good, regarding connections and code.
Try using spi.max_speed_hz = 1350000 before the spi.xfer2, as the device is sensitive to this parameter.

And try adding a print statement between
adc = spi.xfer2([1,(8+channel)<<4,0])
print( "raw data", adc)
data = ((adc[1]&3) << 8) + adc[2]
To see what is going on.

Good luck,
Gerhard

NickRothbacher
Posts: 6
Joined: Fri Jul 17, 2015 11:56 pm

Re: SPI Interface receiving no data from MCP3008

Mon Jul 20, 2015 5:14 pm

Hello,

Thanks for taking a look at my issue. I've added the lines that you suggested and there was no visible affect. The raw data that is being printed out is the matrix [0, 0, 0]. Does that clarify anything?

Thanks again,
Nick.

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

Re: SPI Interface receiving no data from MCP3008

Mon Jul 20, 2015 5:30 pm

A photo and description of the connections you have made will be useful.

NickRothbacher
Posts: 6
Joined: Fri Jul 17, 2015 11:56 pm

Re: SPI Interface receiving no data from MCP3008

Mon Jul 20, 2015 6:00 pm

Oh, I apologize, I had thought that I put up a picture.
IMG_0605 (1)-min (2).jpg
IMG_0605 (1)-min (2).jpg (62.02 KiB) Viewed 13492 times
What kind of description would be helpful? I know it came out kind of hard to read, but the original post has my attempt at a table of the connections.

Hope that helps,
Nick.

ghp
Posts: 1399
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: SPI Interface receiving no data from MCP3008

Mon Jul 20, 2015 6:05 pm

Hello,
looks as if MISO is constantly low. Could be your 3.3V-Supply is not arriving at the chip ?
You can do another check: disconnect the chip from MISO,MOSI and directly connect MISO and MOSI by a 1k-resistor and run the test again. The 1k-resistor protects the input-outputs against bad wiring. You should now see the bytes sent out directly read back. This is 'loopback' operation.
With this check, you validate basic SPI function. If this works, then reconnect the chip again DIN=MOSI, DOUT=MISO.

Good luck !
Gerhard

NickRothbacher
Posts: 6
Joined: Fri Jul 17, 2015 11:56 pm

Re: SPI Interface receiving no data from MCP3008

Mon Jul 20, 2015 7:36 pm

Hello Gerhard,

I tested the circuit with a multimeter and the 3.3v power is making it to the VDD and VREF pins on the MCP3008.

I only had a 10k resistor, so I tested the loopback with that but I'm still getting all zeroes. Would I need to use a 1k in order to get accurate results?

Thanks,
Nick.

ghp
Posts: 1399
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: SPI Interface receiving no data from MCP3008

Tue Jul 21, 2015 5:14 am

Hello,
10k works here for my pi. When this loopback fails, you did not use MISO / MOSI connections of your PI. It happened in my validation here last evening that I grabbed sclk and mosi instead and also got only zeroes. This is the reason why it is very useful to use a resistor for the connection. So please again count the pins on your pi2 pin header and validate.
When I break the loop (I disconnected MOSI from the resistor), I also got zeroes. So another possibility is that the patch cables you use have a broken connection or even the breadboard can have problems. For this test the connections are MISO (pin header) to breadboard, resistor, other side resistor to MOSI pin header. If you validated the pins on the pin header side, then mark the two cables used and replace the patch cables by other ones. If it then works, one or two of marked cables are broken.


Regards,
Gerhard

NickRothbacher
Posts: 6
Joined: Fri Jul 17, 2015 11:56 pm

Re: SPI Interface receiving no data from MCP3008

Tue Jul 21, 2015 5:41 pm

Hello Gerhard,

I just recounted the pins and tested again with new cables, new breadboard row and new resistor and its still showing all zeroes for me. Is it possible my SPI drivers are not working, or something along those lines?

Thanks,
Nick.

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

Re: SPI Interface receiving no data from MCP3008

Tue Jul 21, 2015 5:56 pm

You could use my piscope to check that SPI is working. I'd reduce the SPI speed to something like 50kbps to make sure the data is fully captured.

ghp
Posts: 1399
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: SPI Interface receiving no data from MCP3008

Tue Jul 21, 2015 6:07 pm

Hello, I agree: looks more like a software problem now. You have carefully checked hardware, so this can be taken as 'ok'.
If you own another sd-card, then setup a brand new raspbian.
I looked to the procedure in the web link above, I would propose the following change in setting up spidev:

'burn' raspbian
raspi-config, allow SPI and enable it at startup
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python-pip python-dev
sudo pip install spidev

(so do use the spidev from repository)
I assume you run the test with python (2.7) ? Is it a Pi2 or Pi you use ?
Then execute loopback test again.

Good luck
Gerhard

NickRothbacher
Posts: 6
Joined: Fri Jul 17, 2015 11:56 pm

Re: SPI Interface receiving no data from MCP3008

Wed Jul 22, 2015 11:49 pm

Hello,

I did a complete reinstall according to your instructions and now it works perfectly! Thank you so much for all your help, I could not have done it without you.

Thanks again,
Nick.

PS: If you're still curious, I'm using a Raspberry Pi 2 and python 2.7

paulera
Posts: 1
Joined: Sun Jun 05, 2016 11:21 pm

Re: SPI Interface receiving no data from MCP3008

Sun Jun 05, 2016 11:36 pm

For the record... Had the same problem, identical symptoms. In my case the MCP3008 chip just stopped working, replaced it and sorted. :lol: Maybe because of plugging cables in and out many times with everything turned on. ¯\_(ツ)_/¯

SuperPiBoy
Posts: 1
Joined: Mon Aug 14, 2017 12:05 pm

Re: SPI Interface receiving no data from MCP3008

Mon Aug 14, 2017 12:13 pm

Hello all,
i am making the same project and i have exactly the same problem : no signal when i use the joystick. :)

I introduce my components : Raspberry PI 1 A+ 512 MB, a microship MCP3008 and a Adafruit analog Joystick
I follow too this web links for the job :

https://learn.adafruit.com/super-game-p ... g-joystick
https://learn.adafruit.com/raspberry-pi ... rs/mcp3008
http://www.raspberrypi-spy.co.uk/2014/0 ... n-mcp3008/
https://www.thingiverse.com/make:320067
script Driver : https://github.com/gianodog/MCP3008-GPI ... oystick.py
And other…

As there is not anymore Cupcade part PCB, i need to setup an ADC converter (MCP3008) with the analog Joystick. For three days, i tried all (until the limit of my knowledges in linux :) ) but nothing works, almost …
when i test my analog joystick with the script driver , i have signal sometime, i reboot and then , no signal anymore. I tried all python scripts from these websites and the same !
I tried too the test script but no data appears ! All at 0.
Now, I dont know what to do, this is not stable , do i miss something ?
I measured the voltage in all components , all is good on the MCP3008 (2x 3.3v) ans Joystick( 5v)
I have two MCP3008 and the same problem happen.
I checked and re checked and changed the cables : the same.
I reinstalled many time Retropie, made the upgrade , update packages needed ; tried also with Noobs OS, the same , nothing works. :cry:

Is there somebody who made this ? and all works great ?
Have a nice day.

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: SPI Interface receiving no data from MCP3008

Wed Aug 16, 2017 7:36 am

Ok most of the projects that I see people building they have been using a SPI MCP3008 for an ADC but I have been using an I2C ADS1115 ADC, they are 16 bit conversion but only 4 channel but nice and cheap. If your can't solve your problem with the MCP3008 I would suggest that you try replacing it with ADS1115. I have used it myself for running an analog joystick as well as other analog sensors :)

abhi123
Posts: 1
Joined: Wed Sep 20, 2017 10:19 am

Re: SPI Interface receiving no data from MCP3008

Wed Sep 20, 2017 10:33 am

Hello all,
i m new on raspberry pi, i have used mq 2 sensor with mcp3008 on raspberry pi3 board and tried to run code from github (https://github.com/tutRPi/Raspberry-Pi-Gas-Sensor-MQ) but code aborts. i have also tried to run sample code named simpletest.py from (https://github.com/adafruit/Adafruit_Py ... r/examples) but there is no output while it works fine with differential.py code.

Thnak you in advance

Mellar
Posts: 1
Joined: Wed Sep 12, 2018 9:18 pm

Re: SPI Interface receiving no data from MCP3008

Wed Sep 12, 2018 11:05 pm

This worked out for me had the same problem ad just setting the frequency solved the error.
ghp wrote:
Sun Jul 19, 2015 5:10 pm
Hello,
a short review looks good, regarding connections and code.
Try using spi.max_speed_hz = 1350000 before the spi.xfer2, as the device is sensitive to this parameter.

And try adding a print statement between
adc = spi.xfer2([1,(8+channel)<<4,0])
print( "raw data", adc)
data = ((adc[1]&3) << 8) + adc[2]
To see what is going on.

Good luck,
Gerhard

RobinGreig
Posts: 15
Joined: Sat Oct 27, 2012 2:40 am
Location: Calgary, Alberta, Canada

Re: SPI Interface receiving no data from MCP3008

Thu Nov 15, 2018 2:40 am

Thanks Gerhard!
"Try using spi.max_speed_hz = 1350000 before the spi.xfer2, as the device is sensitive to this parameter."

I've been fighting with the MCP3003 and my Pi3, because sometimes it worked and other times it didn't.
Tried new breadboards, and jumpers.
Tonight I soldered a chip directly to a small perfboard and soldered that to a 40 pin connector to ensure connectivity from the chip to the Pi.
Still low numbers.

Once I added the spi.max_speed_hz line to my code, it worked perfectly!!!

I realise this is an old thread, but can anyone explain why the frequency is critical?
Does the Pi spi port run to fast for the chip?

Thanks again forum folks for pulling my project out of the recycle bin!!!
Robin
www.robingreig.ca
Arduino + Raspi + Amateur Radio enthusiast since 2012
VE6RBN Canadian Amateur Radio Callsign

lchang
Posts: 1
Joined: Tue Jan 22, 2019 9:29 pm
Location: Vancovuer, BC, Canada

Re: SPI Interface receiving no data from MCP3008

Tue Jan 22, 2019 9:36 pm

"Try using spi.max_speed_hz = 1350000 before the spi.xfer2, as the device is sensitive to this parameter."
I ran into the exact same problem last night by following the code from Raspberry Pi cookbook by Simon Monk and did not receive any useful value. After I visited this website and added the line the code works now.

I have the same questions too, why does adding the frequency line help the MCP3008 to read correctly? I think the default frequency is already at 1350000 from the datasheet?

pcmanbob
Posts: 6619
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: SPI Interface receiving no data from MCP3008

Wed Jan 23, 2019 6:44 pm

lchang wrote:
Tue Jan 22, 2019 9:36 pm
"Try using spi.max_speed_hz = 1350000 before the spi.xfer2, as the device is sensitive to this parameter."
I ran into the exact same problem last night by following the code from Raspberry Pi cookbook by Simon Monk and did not receive any useful value. After I visited this website and added the line the code works now.

I have the same questions too, why does adding the frequency line help the MCP3008 to read correctly? I think the default frequency is already at 1350000 from the datasheet?
I think you will find that in stretch that has been a change in the spi driver, the change means there is no default speed set by the driver which is why a speed setting is now required.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

umerbiomedical
Posts: 7
Joined: Wed Feb 13, 2019 9:05 pm

Re: SPI Interface receiving no data from MCP3008

Sat Feb 23, 2019 5:20 pm

Thank you so much. This worked in my case.
ghp wrote:
Sun Jul 19, 2015 5:10 pm
Hello,
a short review looks good, regarding connections and code.
Try using spi.max_speed_hz = 1350000 before the spi.xfer2, as the device is sensitive to this parameter.

And try adding a print statement between
adc = spi.xfer2([1,(8+channel)<<4,0])
print( "raw data", adc)
data = ((adc[1]&3) << 8) + adc[2]
To see what is going on.

Good luck,
Gerhard

syameeza natasha
Posts: 1
Joined: Fri Mar 29, 2019 8:05 am

Re: SPI Interface receiving no data from MCP3008

Fri Mar 29, 2019 8:10 am

umerbiomedical wrote:
Sat Feb 23, 2019 5:20 pm
Thank you so much. This worked in my case.
ghp wrote:
Sun Jul 19, 2015 5:10 pm
Hello,
a short review looks good, regarding connections and code.
Try using spi.max_speed_hz = 1350000 before the spi.xfer2, as the device is sensitive to this parameter.

And try adding a print statement between
adc = spi.xfer2([1,(8+channel)<<4,0])
print( "raw data", adc)
data = ((adc[1]&3) << 8) + adc[2]
To see what is going on.

Good luck,
Gerhard
im using this code but still have an error as below. Were I have to define 'spi'?. Please help me

Code: Select all

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/home/pi/Raspberry-Pi-Heartbeat-Pulse-Sensor-master/pulsesensor.py", line 30, in getBPMLoop
    Signal = self.adc.read(self.channel)
  File "/home/pi/Raspberry-Pi-Heartbeat-Pulse-Sensor-master/MCP3008.py", line 24, in read
    spi.max_speed_hz = 1350000
NameError: name 'spi' is not defined

pcmanbob
Posts: 6619
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: SPI Interface receiving no data from MCP3008

Fri Mar 29, 2019 9:53 am

syameeza natasha wrote:
Fri Mar 29, 2019 8:10 am

im using this code but still have an error as below. Were I have to define 'spi'?. Please help me

Code: Select all

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "/home/pi/Raspberry-Pi-Heartbeat-Pulse-Sensor-master/pulsesensor.py", line 30, in getBPMLoop
    Signal = self.adc.read(self.channel)
  File "/home/pi/Raspberry-Pi-Heartbeat-Pulse-Sensor-master/MCP3008.py", line 24, in read
    spi.max_speed_hz = 1350000
NameError: name 'spi' is not defined
Hi.

what you have posted or copied is only part of a program , if you look at the first post on this thread you will see an example of the full program that was being discussed in this thread, which includes the part to define SPI.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

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