PabloD
Posts: 24
Joined: Sat Sep 19, 2020 10:37 pm
Location: Spain
Contact: Website

Irregular MCP3008 data

Sat Sep 19, 2020 11:16 pm

Good morning. I'm setting up a weather station and when I get to the weather vane I have trouble measuring voltages with the MCP3008.

Connections to MCP3008 are as follows:
  • Pin 16 (Vdd) - 3.3V
  • Pin 15 (Vref) - 3.3V
  • Pin 14 (AGND) - GND
  • Pin 13 (CLK) - SCLK
  • Pin 12 (Dout) - MISO
  • Pin 11 (Din) - MOSI
  • Pin 10 (CS/SHDN) - CE0
  • Pin 9 (DGND) - GND
  • Pin 1 (CH0) - 3.3V
To view the data I use this Python code:

Code: Select all

import spidev
import time

# Define Variables
delay = 0.5
ldr_channel = 0

# Create SPI
spi = spidev.SpiDev()
spi.open(0, 0)


def readadc(adcnum):
    # read SPI data from the MCP3008, 8 channels in total
    if adcnum > 7 or adcnum < 0:
        return -1
    r = spi.xfer2([1, 8 + adcnum << 4, 0])
    data = ((r[1] & 3) << 8) + r[2]
    return data


while True:
    ldr_value = readadc(ldr_channel)
    print("---------------------------------------")
    print("LDR Value: %d" % ldr_value)
    time.sleep(delay)

The program shows me the following results:

Code: Select all

---------------------------------------
LDR Value: 815
---------------------------------------
LDR Value: 815
---------------------------------------
LDR Value: 783
---------------------------------------
LDR Value: 815
---------------------------------------
LDR Value: 0
---------------------------------------
LDR Value: 0
---------------------------------------
LDR Value: 0
---------------------------------------
LDR Value: 0
---------------------------------------
LDR Value: 0
---------------------------------------
LDR Value: 0
---------------------------------------
LDR Value: 0
---------------------------------------
LDR Value: 783

The value returned by MCP3008 is not fixed, it does not stop varying even with a fixed input on channel 0 of 3.3V. In case there is no voltage on channel 0 it keeps showing me values ​​similar to what I have shown above.

I have been with this problem for days and of course I am not getting the data from the weather vane if I connect it to channel 0.

I have tried to read the data in several different ways than this and it is the same with all of them.

I have also checked the continuity of the cables to find a short circuit but everything connects well. I have also measured the input voltages to the MCP3008 chip and they are stable at 3.3V.

If anyone could help me I would appreciate it.

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

Re: Irregular MCP3008 data

Sun Sep 20, 2020 7:35 am

You need to post a set of clear photos so we can follow the connections between the Pi and the MCP3008. We need to be able to clearly see which Pi pin is connected to which MCP3008 pin.

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

Re: Irregular MCP3008 data

Sun Sep 20, 2020 7:35 am

You need to post a set of clear photos so we can follow the connections between the Pi and the MCP3008. We need to be able to clearly see which Pi pin is connected to which MCP3008 pin.

jayben
Posts: 89
Joined: Mon Aug 19, 2019 9:56 pm

Re: Irregular MCP3008 data

Sun Sep 20, 2020 8:24 am

This may be a hardware and/or software problem.

As far as I can tell, you are using the ADC to measure the supply voltage (3.3V on Ch0) which isn't a good idea, as the ADC is designed to measure voltage below its 3.3V reference. In theory you should just get a value of 3ff hex back, but it is possible the ADC doesn't do this correctly, since you are over-ranging it.

You mention incorrect measurements with 'no voltage' on the input; do you mean the input is floating, or grounded to zero volts? If the former, then you must expect random values; a floating input will drift around, and won't be zero voltage. Similarly, you don't say what analogue value you are actually trying to measure, but it does need to be of reasonably low impedance (maybe a few K ohms maximum) to get a reliable reading.

You haven't described the ADC wiring, but if the wires (especially the supply and ground) are too long, then the whole arrangement can oscillate. You can mitigate this by adding a capacitor (usually 0.1 uF) across the ADC supply and ground terminals, but it is much better to keep the leads short, say 4 inches (10 cm) or less.

With regard to the software, I have some sample Python code for the MCP3008 on my blog https://iosoft.blog/fast-data-capture-raspberry-pi/

PabloD
Posts: 24
Joined: Sat Sep 19, 2020 10:37 pm
Location: Spain
Contact: Website

Re: Irregular MCP3008 data

Sun Sep 20, 2020 10:20 am

Good morning. I leave here the photos of the wiring. The cables I'm using are 20cm because I don't have any others.

Since this morning the data I receive is 0 all the time. I have also tried putting a 4.7 ohm resistor from channel zero to GND, but it gives 0V.

Regarding that "you mention incorrect measurements with 'no voltage' on the input; do you mean the input is floating, or grounded to zero volts?" I wanted to say that the input is floating.

I have tried your jayben code but it keeps giving 0.

About the pictures:
- Red cable - 3.3v
- yellow cable - GND
- Purple cable - CLK
- White cable - MISO
- Orange cable - MOSI
- Gray cable - CE0
Attachments
IMG_20200920_113753_opt.jpg
IMG_20200920_113753_opt.jpg (152.9 KiB) Viewed 736 times
IMG_20200920_113842_opt.jpg
IMG_20200920_113842_opt.jpg (135.38 KiB) Viewed 736 times
IMG_20200920_114017_opt.jpg
IMG_20200920_114017_opt.jpg (160.8 KiB) Viewed 736 times

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

Re: Irregular MCP3008 data

Sun Sep 20, 2020 11:18 am

I can't follow those wires. There is too much going on.

I suggest you run piscope and monitor the SPI signals.

Firstly make sure that clock, mosi, miso, ce are as expected.

Then choose a free Pi GPIO and set it as an input. Connect from one of the SPI signals on the breadboard to that GPIO and check that the GPIO does reflect that SPI signal. Do that for each SPI signal. I expect there will be at least one discrepancy.

http://abyz.me.uk/rpi/pigpio/piscope.html

PabloD
Posts: 24
Joined: Sat Sep 19, 2020 10:37 pm
Location: Spain
Contact: Website

Re: Irregular MCP3008 data

Sun Sep 20, 2020 1:53 pm

I put new photos of the wiring to make it look better.

I also put the measurements of the "piscope". But the lines are flat.
Attachments
test.jpg
test.jpg (122.84 KiB) Viewed 694 times
IMG_20200920_151826_opt.jpg
IMG_20200920_151826_opt.jpg (123.42 KiB) Viewed 694 times
IMG_20200920_152017_opt.jpg
IMG_20200920_152017_opt.jpg (131.57 KiB) Viewed 694 times

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

Re: Irregular MCP3008 data

Sun Sep 20, 2020 5:51 pm

If there is no activity on the SPI lines (during a transfer) they are been held low/high by your circuit. That is a wiring problem.

PabloD
Posts: 24
Joined: Sat Sep 19, 2020 10:37 pm
Location: Spain
Contact: Website

Re: Irregular MCP3008 data

Mon Sep 21, 2020 11:40 am

joan wrote: If there is no activity on the SPI lines (during a transfer) they are been held low/high by your circuit. That is a wiring problem.

Do you mean that it is badly wired or that since the cable is very long, the signal may not reach it?

Anyway I bought another chip to see if it is a chip problem. But I'm not sure that's the problem so everything you recommend I'll keep it in mind.

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

Re: Irregular MCP3008 data

Mon Sep 21, 2020 12:06 pm

If you don't see any activity during a SPI transfer that is a wiring problem. Something is pulling the SPI signals low or high preventing them from changing. The length of your wires is not the problem. You should be able to use pigs to set the SPI signals high and low (e.g. pigs w 10 1 mils 250 w 10 0 will toggle MOSI). You will need to put the line back into SPI mode afterwards (e.g. pigs m 10 0).

PabloD
Posts: 24
Joined: Sat Sep 19, 2020 10:37 pm
Location: Spain
Contact: Website

Re: Irregular MCP3008 data

Sun Sep 27, 2020 5:27 pm

I have a total of 4 mcp3008 sensors and have not managed to get any of them to work.

I have reinstalled the operating system, python and all the libraries but it still gives me voltage 0.
joan wrote: You should be able to use pigs to set the SPI signals high and low (e.g. pigs w 10 1 mils 250 w 10 0 will toggle MOSI). You will need to put the line back into SPI mode afterwards (e.g. pigs m 10 0).
I'm sorry Joan but I haven't really understood what I have to do. If you can explain it to me again, I would appreciate it.

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

Re: Irregular MCP3008 data

Wed Sep 30, 2020 7:49 pm

Try adding this line to your existing code spi.max_speed_hz=1000000

Code: Select all

# Create SPI
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz=1000000
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

PabloD
Posts: 24
Joined: Sat Sep 19, 2020 10:37 pm
Location: Spain
Contact: Website

Re: Irregular MCP3008 data

Thu Oct 01, 2020 7:58 am

good morning:

I have tested it in the following code and still no voltage coming out. I have tried with two raspberry pi 3 B and 3 MCP3008 sensors and I can't see voltage.

Code: Select all

#  -------------------------------------- Name file mcp30083.py -------------------------------------- 
from spidev import SpiDev


class MCP3008:
    def __init__(self, bus=0, device=0):
        self.bus, self.device = bus, device
        self.spi = SpiDev()
        self.spi.open(0, 0)
        self.spi.max_speed_hz = 1000000

    def open(self):
        self.spi.open(self.bus, self.device)

    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()


adc = MCP3008()
# You can of course adapt the channel to be read out
value = adc.read(channel=0)
print("Applied voltage: %.2f" % (value / 1023.0 * 3.3))


I have also tested how Adafruit recommends in its code. But it doesn't work either. Both on the GPIO 5 pin as shown in your example and on the SPI0 CE0 (GPIO 8) and changing the data reception port in the code.

Code: Select all

# -------------------------------------- Name file mcp30081.py -------------------------------------- 
import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn

# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)

# create the cs (chip select)
cs = digitalio.DigitalInOut(board.D8)

# create the mcp object
mcp = MCP.MCP3008(spi, cs)

# create an analog input channel on pin 0
chan = AnalogIn(mcp, MCP.P0)

print('Raw ADC Value: ', chan.value)
print('ADC Voltage: ' + str(chan.voltage) + 'V')



The only program that gives me signals in piscope is the following but it still gives 0 voltage.

Code: Select all

#  -------------------------------------- Name file mcp30082.py -------------------------------------- 
# Simple example of reading the MCP3008 analog input channels and printing
# them all out.
# Author: Tony DiCola
# License: Public Domain
import time

# Import SPI library (for hardware SPI) and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008


# 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)

# Hardware SPI configuration:
# SPI_PORT   = 0
# SPI_DEVICE = 0
# mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))


print('Reading MCP3008 values, press Ctrl-C to quit...')
# Print nice channel column headers.
print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*range(8)))
print('-' * 57)
# Main program loop.
while True:
    # Read all the ADC channel values in a list.
    values = [0]*8
    for i in range(8):
        # The read_adc function will get the value of the specified channel (0-7).
        values[i] = mcp.read_adc(i)
    # Print the ADC values.
    print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*values))
    # Pause for half a second.
    time.sleep(0.5)


Extra information:
-I have checked all the cables to see if any are bad but it is not the case. Anyway I have changed them twice to make sure but I still have the same problem.
- The connections are as follows:
-Pin 16 (Vdd) - 3.3V
-Pin 15 (Vref) - 3.3V
-Pin 14 (AGND) - GND
-Pin 13 (CLK) - SCLK (GPIO 11)
-Pin 12 (Dout) - MISO (GPIO 9)
-Pin 11 (Din) - MOSI (GPIO 10)
-Pin 10 (CS/SHDN) - CE0 (GPIO 8)
-Pin 9 (DGND) - GND
-Pin 1 (CH0) - 3.3V
- SPI and I2C are enabled in raspberry
- imagen SO "Raspberry Pi OS (32-bit) with desktop" latest version
Attachments
mcp30083.JPG
mcp30083.JPG (92.76 KiB) Viewed 441 times
mcp30082.JPG
mcp30082.JPG (101.88 KiB) Viewed 441 times
mcp30081.JPG
mcp30081.JPG (91.72 KiB) Viewed 441 times

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

Re: Irregular MCP3008 data

Thu Oct 01, 2020 9:46 am

I would remove that HAT or whatever it is and attach wires direct from the Pi header to the breadboard.

It will be much easier to pin-point the problem and once it is working you could try to put the HAT back.

PabloD
Posts: 24
Joined: Sat Sep 19, 2020 10:37 pm
Location: Spain
Contact: Website

Re: Irregular MCP3008 data

Thu Oct 01, 2020 10:42 am

I thought the same as you so now I have it without the hat but I still have the same problem.
I was going to pass you an image of the cablado with fritzing but there is no raspberry pi 3 b in the version that I downloaded.

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

Re: Irregular MCP3008 data

Thu Oct 01, 2020 10:58 am

We need to be able follow from the Pi pins to breadboard to chip. So perhaps three overhead photos would be best.

PabloD
Posts: 24
Joined: Sat Sep 19, 2020 10:37 pm
Location: Spain
Contact: Website

Re: Irregular MCP3008 data

Thu Oct 01, 2020 12:45 pm

Okay. tonight I'll upload the photos.

PabloD
Posts: 24
Joined: Sat Sep 19, 2020 10:37 pm
Location: Spain
Contact: Website

Re: Irregular MCP3008 data

Thu Oct 01, 2020 8:17 pm

If you don't see something, tell me and I'll take another photo for you.
Attachments
3.jpeg
3.jpeg (216.32 KiB) Viewed 341 times
2_opt.jpg
2_opt.jpg (102.57 KiB) Viewed 341 times
1_opt.jpg
1_opt.jpg (79.04 KiB) Viewed 341 times

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

Re: Irregular MCP3008 data

Thu Oct 01, 2020 9:04 pm

Unfortunately I can see nothing wrong with those connections.

What does the following report?

sudo pigpiod

pigs spio 0 50000 0

pigs spix 0 1 128 0

PabloD
Posts: 24
Joined: Sat Sep 19, 2020 10:37 pm
Location: Spain
Contact: Website

Re: Irregular MCP3008 data

Thu Oct 01, 2020 9:12 pm

Show this.
Attachments
Captura.JPG
Captura.JPG (28.53 KiB) Viewed 321 times

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

Re: Irregular MCP3008 data

Thu Oct 01, 2020 9:14 pm

Please connect 3V3 to the breadboard row for MISO (black wire). What does pigs report then?

PabloD
Posts: 24
Joined: Sat Sep 19, 2020 10:37 pm
Location: Spain
Contact: Website

Re: Irregular MCP3008 data

Thu Oct 01, 2020 9:21 pm

It seems to detect it well
Attachments
Captura.JPG
Captura.JPG (16.26 KiB) Viewed 319 times

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

Re: Irregular MCP3008 data

Thu Oct 01, 2020 9:25 pm

Okay. That suggests the Pi end is okay. Remove that 3V3 line.

Connect an additional line from the breadboard to the Pi for clk, miso, mosi, and ce to spare GPIO . Set those GPIO as INPUTS (e.g. with pigs you could use pigs m x r, where x is the Broadcom GPIO number).

Run the pigs spix command and check that the SPI signals also appear on the GPIO set as inputs (with piscope).

PabloD
Posts: 24
Joined: Sat Sep 19, 2020 10:37 pm
Location: Spain
Contact: Website

Re: Irregular MCP3008 data

Thu Oct 01, 2020 9:48 pm

If I have understood it correctly, you mean to connect clk, miso, mosi and ce to the same GPIO. In my case the GPIO 5.
and run the following commands:
sudo pigpiod
pigs m 5 x
pigs spio 0 50000 0
pigs spix 0 1 128 0
Attachments
Captura.JPG
Captura.JPG (76.14 KiB) Viewed 302 times

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

Re: Irregular MCP3008 data

Thu Oct 01, 2020 9:58 pm

No, sorry I have mislead you.

Use a different GPIO for each signal.

Say GPIO 6 for CLK, 13 for MISO, 19 for MOSI, 26 for chip select.

Then set them to be inputs.

pigs m 6 r m 13 r m 19 r m 26 r

The purpose is to double check that each signal is as expected. On piscope GPIO 6 should be the same as the clock line, 13 the same as MISO etc.

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