1

Alright, I have to start off with the fact I'm a n00b. So there's a good chance I'm screwing something up.

I've got an MCP3008 10-bit ADC hooked up to my Raspberry Pi B+ via a Cobbler.

Here are the particulars of my kernel:

pi@raspberrypi ~ $ uname -a
Linux raspberrypi 3.18.11+ #781 PREEMPT Tue Apr 21 18:02:18 BST 2015 armv6l GNU/Linux

I've wired up the default SPI0_SCLK, MISO, MOSI, and CE0_N to the MCP3008. I roughly followed raspberrypi-spy's guidlines, but making appropriate adaptations for the B+ rather than their A.

I enabled SPI via raspi-config and confirm /dev/spidev0.0 and /dev/spidev/0.1 exist. I also ensured I have the spi_bcm2708 driver enabled.

pi@raspberrypi ~ $ ll /dev/spidev0.*
crw-rw---T 1 root spi 153, 1 Jan  1  1970 /dev/spidev0.1
crw-rw---T 1 root spi 153, 0 Jan  1  1970 /dev/spidev0.0
pi@raspberrypi ~ $ lsmod
Module                  Size  Used by
ctr                     3993  1 
ccm                     8238  1 
snd_bcm2835            21149  0 
snd_pcm                90778  1 snd_bcm2835
snd_seq                61097  0 
snd_seq_device          7209  1 snd_seq
snd_timer              23007  2 snd_pcm,snd_seq
snd                    66325  5 snd_bcm2835,snd_timer,snd_pcm,snd_seq,snd_seq_device
arc4                    1964  2 
rt2800usb              18958  0 
rt2800lib              81833  1 rt2800usb
rt2x00usb              11926  1 rt2800usb
rt2x00lib              47785  3 rt2x00usb,rt2800lib,rt2800usb
mac80211              552848  3 rt2x00lib,rt2x00usb,rt2800lib
spi_bcm2708             6018  0 
i2c_bcm2708             6200  0 
cfg80211              462846  2 mac80211,rt2x00lib
crc_ccitt               1612  1 rt2800lib
rfkill                 22347  2 cfg80211
uio_pdrv_genirq         3666  0 
uio                     9897  1 uio_pdrv_genirq

Just to add more information, but /boot/config.txt

pi@raspberrypi ~ $ cat /boot/config.txt 
# For more options and information see 
# http://www.raspberrypi.org/documentation/configuration/config-txt.md
# Some settings may impact device functionality. See link above for details

# uncomment if you get no picture on HDMI for a default "safe" mode
#hdmi_safe=1

# uncomment this if your display has a black border of unused pixels visible
# and your display can output without overscan
#disable_overscan=1

# uncomment the following to adjust overscan. Use positive numbers if console
# goes off screen, and negative if there is too much border
#overscan_left=16
#overscan_right=16
#overscan_top=16
#overscan_bottom=16

# uncomment to force a console size. By default it will be display's size minus
# overscan.
#framebuffer_width=1280
#framebuffer_height=720

# uncomment if hdmi display is not detected and composite is being output
#hdmi_force_hotplug=1

# uncomment to force a specific HDMI mode (this will force VGA)
#hdmi_group=1
#hdmi_mode=1

# uncomment to force a HDMI mode rather than DVI. This can make audio work in
# DMT (computer monitor) modes
#hdmi_drive=2

# uncomment to increase signal to HDMI, if you have interference, blanking, or
# no display
#config_hdmi_boost=4

# uncomment for composite PAL
#sdtv_mode=2

#uncomment to overclock the arm. 700 MHz is the default.
#arm_freq=800

dtparam=spi=on
dtparam=i2c_arm=on
#dtoverlay=spi-bcm2835-overlay
dtoverlay=spi-bcm2708-overlay

I'm using the python py-spidev. I cannot recall where I installed it from, but I did find this in the .egg:

Metadata-Version: 1.1
Name: spidev
Version: 3.0
Summary: Python bindings for Linux SPI access through spidev
Home-page: http://github.com/doceme/py-spidev
Author: Stephen Caudle
Author-email: scaudle@doceme.com
License: GPLv2
Description: Python Spidev

I have nothing connected to the 8 MCP3008's ADC inputs, so I should get something like 0, or close there abouts. My code is pretty easy. I borrowed the ReadChannel function from the aforementioned raspberrypi-spy link. I assume that is fairly robust.

pi@raspberrypi ~ $ cat adctest.py 
#!/usr/bin/python

import time
import spidev

# 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

####################################################################################

fmt= '{0:5d}'
while True:
   chans = [0]*8
   for t in range(8):
      chans[t] = ReadChannel(t)
      #time.sleep(0.01)
   line = []
   for chan in chans:
      line.append(fmt.format(chan))
   print ' '.join(line)
   time.sleep(0.25)

The resultant output is a little perplexing:

pi@raspberrypi ~ $ sudo ./adctest.py 
   21    30     0    13     7    17     2     0
    0     7     0     6     5    17     6     7
   41    40    39    49    57    71    87   126
   72    54    46    48    53    60    76   110
   58    30    13     8     0     0     0     0
    0     0     0     3     1     6     0     0
   11     7     4    13    16    30    39    68
   65    48    48    50    54    64    79   125
   73    52    47    47    54    59    73   105
   57    42    29    27    24    20    22    21
    0     0     0     0     0     0     0     0
    0     4     0     4     4     6     3     4
   12    11     8    21    24    32    43    80
   69    55    48    50    56    67    81   125
   73    50    42    46    51    56    72   106
   53    34    20    21    18    14    16     7
    0     0     0     0     0     0     0     0
    1     4     2     5     4     7     3     7
   16    16    18    25    34    44    57    94
   73    54    48    53    61    65    84   123
   69    51    41    44    48    55    68    96

I connected a device to one channel and I get the exact same thing. I reconnected that ADC channel to GND and I get the same sinusoidal behavior. Same issue if I connect a channel to +3.3VDC. I've swapped out the MCP3008 and I get the same response.

The SPI driver on my pi appears to function correctly. I disconnected my circuit, and tied MOSI to MISO and ran the loopback test, mentioned on raspberrypi.org. This appears to work correctly.

I'm completely perplexed. I had this all functioning a few weeks ago. Maybe something in a recent apt-get upgrade broke things. Any adivce would be most helpful.

Thanks a million, ~Rich

3

OMG. After staring at this for hours, I finally figured it out.

Looks like in my haste of breaking apart my breadboard and recabling a more basic circuit I inadvertently removed the wires tying GND and 3.3VDC from one side of the board to the other. Thus, when I tested the ADC against GND and +3VDC, I was not connecting them to anything.

So, in summary, the MCP3008 returns a sinusoidal signal if the pin is floating. I solved it myself!

Any way, I guess posting a plea for help got me thinking more. Thanks anyway. Hope this post helps people the future.

~Rich

  • I have the exact same problem as you. I have checked every connection multiple times and there is no way they are wrong. I'm getting sine wave as output. If I connect +3VDC with any resistance to any of sensor pins then it goes straight to 1023. – somerandomusername Mar 25 '17 at 12:26

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.