User avatar
coskukoz
Posts: 34
Joined: Sun May 31, 2015 4:01 pm
Location: Balıkesir, Turkey

Using Double MCP3008

Fri May 13, 2016 9:22 am

Hello,

I read 8 analog data via MCP3008 on Raspberry Pi. At the moment I have to read 14 analog data. I have no experiences about MUX. If you know how to do or different way to fix it please share. Basicaly I need to read 14 analog data.

Thank you very much.
Coskun

EDIT:
I changed the topic name according to answers. So now there is better explanation. Last topic name was: "MCP3008 and MUX"
Last edited by coskukoz on Tue May 17, 2016 5:12 am, edited 2 times in total.
cactiapps.blogspot.com

User avatar
Cancelor
Posts: 757
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: MCP3008 and MUX

Fri May 13, 2016 10:29 am

You can install another MCP3008 so you will have one on CE0 and the other on CE1 - GPIO 7 and 8
MOSI, MISO and SCLK will be used by both chips.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

User avatar
coskukoz
Posts: 34
Joined: Sun May 31, 2015 4:01 pm
Location: Balıkesir, Turkey

Re: MCP3008 and MUX

Tue May 17, 2016 4:45 am

That's great! Can you help me about code to read from CE1?
Basicaly my question is how can I read data from second MCP3008?

My code for one MCP3008:

Code: Select all

import Spidev
import time
import os
import urllib
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

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

def ReadChannel(channel):
 adc = spi.xfer2([1,(8+channel)<<4,0])
 data = ((adc[1]&3)<<8)+adc[2]
 return data

sensor1_channel = 0
sensor2_channel = 1
sensor3_channel = 2
sensor4_channel = 3
sensor5_channel = 4
sensor6_channel = 5
sensor7_channel = 6
sensor8_channel = 7
delay = 5
Last edited by coskukoz on Tue May 17, 2016 7:47 am, edited 1 time in total.
cactiapps.blogspot.com

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

Re: MCP3008 and MUX

Tue May 17, 2016 7:30 am

coskukoz wrote:That's great! Can you help me about code to read from CE1?
...
Perhaps

spi_ce0 = spidev.Spidev()
spi_ce0.open(0,0)

spi_ce1 = spidev.Spidev()
spi_ce1.open(0,1)

User avatar
coskukoz
Posts: 34
Joined: Sun May 31, 2015 4:01 pm
Location: Balıkesir, Turkey

Re: Using Double MCP3008

Tue May 17, 2016 7:53 am

joan wrote: Perhaps

spi_ce0 = spidev.Spidev()
spi_ce0.open(0,0)

spi_ce1 = spidev.Spidev()
spi_ce1.open(0,1)
So it's going to be like:
Can you check this part ' sensor8_channel = 8.... sensor15_channel = 15'

Code: Select all

    import Spidev
    import time
    import os
    import urllib
    import RPi.GPIO as GPIO
    GPIO.setmode(GPIO.BCM)

    spi_ce0 = spidev.Spidev()
    spi_ce0.open(0,0)

    spi_ce1 = spidev.Spidev()
    spi_ce1.open(0,1)

    def ReadChannel(channel):
     adc = spi.xfer2([1,(8+channel)<<4,0])
     data = ((adc[1]&3)<<8)+adc[2]
     return data

    sensor1_channel = 0
    sensor2_channel = 1
    sensor3_channel = 2
    sensor4_channel = 3
    sensor5_channel = 4
    sensor6_channel = 5
    sensor7_channel = 6
    sensor8_channel = 7

    sensor8_channel = 8
    sensor9_channel = 9
    sensor10_channel = 10
    sensor11_channel = 11
    sensor12_channel = 12
    sensor13_channel = 13
    sensor14_channel = 14
    sensor15_channel = 15

    delay = 5

cactiapps.blogspot.com

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

Re: Using Double MCP3008

Tue May 17, 2016 8:03 am

No.

Each ADC has 8 channels numbered 0-7. You read them as in your original code.

To read the ADC connected to CE0 use spi_ce0.xfer2 calls (although I don't know why people are so fascinated with using xfer2 rather than the correct xfer). Read the other ADC using spi_ce1.xfer2 calls.

User avatar
Cancelor
Posts: 757
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Using Double MCP3008

Tue May 17, 2016 8:12 am

I think you need import spidev not import Spidev it is case sensitive.

The spi.open(0,0) line follows the format spi.open(bus, device) and opens the specified SPI device /dev/spidev-bus.device

You can use the command find /sys/bus | grep spi in a terminal if you want to view the connected SPI devices. It will show you something a bit like this:-

Code: Select all

[email protected] ~ $ find /sys/bus | grep spi
/sys/bus/spi
/sys/bus/spi/devices
/sys/bus/spi/devices/spi0.0
/sys/bus/spi/devices/spi0.1
/sys/bus/spi/drivers
/sys/bus/spi/drivers/mcp251x
/sys/bus/spi/drivers/mcp251x/bind
/sys/bus/spi/drivers/mcp251x/module
/sys/bus/spi/drivers/mcp251x/uevent
/sys/bus/spi/drivers/mcp251x/unbind
/sys/bus/spi/drivers/spidev
/sys/bus/spi/drivers/spidev/bind
The pi only presents one SPI bus (bus 0), you are connecting two devices to that bus.

Code: Select all

spi_ce0.open(0,0)    # Open SPI bus 0, device 0 (CE0 - aka GPIO8 - header pin 24)
will open one device

Code: Select all

spi_ce1.open(0,1)    # Open SPI bus 0, device 1 (CE1 - aka GPIO7 - header pin 26)
will open the other device. CE0 and CE1 are also known as Chip Select or Chip Enable.

Use spi_ce0.close() and spi_ce1.close() to close.

Each of your devices has 8 channels.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

User avatar
coskukoz
Posts: 34
Joined: Sun May 31, 2015 4:01 pm
Location: Balıkesir, Turkey

Re: Using Double MCP3008

Tue May 17, 2016 8:21 am

joan wrote: To read the ADC connected to CE0 use spi_ce0.xfer2 calls (although I don't know why people are so fascinated with using xfer2 rather than the correct xfer). Read the other ADC using spi_ce1.xfer2 calls.
I think I got what you mean but as you see I'm not good at it. I defined another ReadChannel and don't know how to define sensor8, sensor9 and more. Can you please bear with me.

Code: Select all

        import Spidev
        import time
        import os
        import urllib
        import RPi.GPIO as GPIO
        GPIO.setmode(GPIO.BCM)

        spi_ce0 = spidev.Spidev()
        spi_ce0.open(0,0)

        spi_ce1 = spidev.Spidev()
        spi_ce1.open(0,1)

        def ReadChannel(channel):
         adc = spi_ce0.xfer2([1,(8+channel)<<4,0])
         data = ((adc[1]&3)<<8)+adc[2]
         return data

        def ReadChannel(channel):
         adc = spi_ce1.xfer2([1,(8+channel)<<4,0])
         data = ((adc[1]&3)<<8)+adc[2]
         return data

        sensor1_channel = 0
        sensor2_channel = 1
        sensor3_channel = 2
        sensor4_channel = 3
        sensor5_channel = 4
        sensor6_channel = 5
        sensor7_channel = 6
        sensor8_channel = 7

        delay = 5
cactiapps.blogspot.com

User avatar
Cancelor
Posts: 757
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Using Double MCP3008

Tue May 17, 2016 8:32 am

joan wrote:.... I don't know why people are so fascinated with using xfer2 rather than the correct xfer...
I don't understand the difference, just found this but I'm none the wiser!
xfer(list of values[, speed_hz, delay_usec, bits_per_word])
Performs an SPI transaction. Chip-select should be released and reactivated between blocks. Delay specifies the delay in usec between blocks.

xfer2(list of values[, speed_hz, delay_usec, bits_per_word])
Performs an SPI transaction. Chip-select should be held active between blocks.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

User avatar
Cancelor
Posts: 757
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Using Double MCP3008

Tue May 17, 2016 8:40 am

To write the code out in long hand it would be something like this :-

Code: Select all

#!/usr/bin/env python

# MCP3008 connections are:-
# CLK  =>  SCLK  
# DOUT =>  MISO
# DIN  =>  MOSI
# CS   =>  CE0

import spidev
import time

spi_ce0 = spidev.Spidev()
spi_ce1 = spidev.Spidev()

spi_ce0.open(0,0)
spi_ce1.open(0,1)


adc1 = spi_ce0.xfer([1,(8+0)<<4,0])
adc2 = spi_ce0.xfer([1,(8+1)<<4,0])
adc3 = spi_ce0.xfer([1,(8+2)<<4,0])
#... add more lines
adc14 = spi_ce1.xfer([1,(8+6)<<4,0])
adc15 = spi_ce1.xfer([1,(8+7)<<4,0])
adc16 = spi_ce1.xfer([1,(8+8)<<4,0])


sensor1 = ((adc1[1]&3)<<8)+adc1[2]
sensor2 = ((adc2[1]&3)<<8)+adc2[2]
sensor3 = ((adc3[1]&3)<<8)+adc3[2]
#... add more lines
sensor14 = ((adc14[1]&3)<<8)+adc14[2]
sensor15 = ((adc15[1]&3)<<8)+adc15[2]
sensor16 = ((adc16[1]&3)<<8)+adc16[2]

delay = 5
Last edited by Cancelor on Tue May 17, 2016 9:53 am, edited 2 times in total.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

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

Re: Using Double MCP3008

Tue May 17, 2016 8:42 am

Cancelor wrote:
joan wrote:.... I don't know why people are so fascinated with using xfer2 rather than the correct xfer...
I don't understand the difference, just found this but I'm none the wiser!
xfer(list of values[, speed_hz, delay_usec, bits_per_word])
Performs an SPI transaction. Chip-select should be released and reactivated between blocks. Delay specifies the delay in usec between blocks.

xfer2(list of values[, speed_hz, delay_usec, bits_per_word])
Performs an SPI transaction. Chip-select should be held active between blocks.
For the sort of SPI devices I use you select the device by setting CE low, transfer x bytes, and then deselect the device by setting CE high.

There are a few devices out there which require you to put a delay between the data bytes. For those you'd set CE low, do the first transfer, delay, do the second transfer, set CE high. For that rare scenario you would use xfer2 (set CE low, transfer) for the first transfer, delay, and xfer (set CE low, transfer, set CE high) for the second transfer. The second set CE low by xfer would have no effect as CE would already be low.

Hope that makes some sort of sense.

User avatar
Cancelor
Posts: 757
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Using Double MCP3008

Tue May 17, 2016 9:53 am

I see, thnx.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

User avatar
coskukoz
Posts: 34
Joined: Sun May 31, 2015 4:01 pm
Location: Balıkesir, Turkey

Re: Using Double MCP3008

Tue May 17, 2016 10:02 am

Thank you very much guys!
cactiapps.blogspot.com

Return to “Beginners”