can somebody explain to me about this mcp3002 library used in analog to digital conversion?? It has some connection to my first question here.

# botbook_mcp3002.py - read analog values from mcp3002
# (c) BotBook.com - Karvinen, Karvinen, Valtokari

# Installing spidev: 
#  sudo apt-get update
#  sudo apt-get -y install git python-dev
#  git clone https://github.com/doceme/py-spidev.git
#  cd py-spidev/
#  sudo python setup.py install

import spidev   # installation help in botbook_mcp3002.py comments
import time

def readAnalog(device = 0,channel = 0):
    assert device in (1, 0)
    assert channel in (1, 0)
    #open spi
    spi = spidev.SpiDev()
    spi.open(0, device)
    Protocol start bit (S), sql/diff (D), odd/sign (C), MSBF (M)
    Use leading zero for more stable clock cycle
    0000 000S DCM0 0000 0000 0000
    Sending 3 8bit packages so xpi.xfer2 will return the same amount.
    start bit = 1
    sql/diff = 1 SINGLE ENDED MODE  (2 channel mode) 
    odd/sign = channel 0/1
    MSBF = 0
    command = [1, (2 + channel) << 6, 0]
    #2 + channel shifted 6 to left
    #10 or 11 << 6 = 1000 0000 or 1100 0000
    reply = spi.xfer2(command)
    Parse right bits from 24 bit package (3*8bit)
    We need only data from last 2 bytes.
    And there we can discard last two bits to get 10 bit value 
    as MCP3002 resolution is 10bits
    Discard reply[0] byte and start from reply[1] where our data starts
    value = reply[1] & 31   
    #31 = 0001 1111 with & operation makes sure that we have all data from XXXX DDDD and nothing more. 0001 is for signed in next operation.
    value = value << 6  #Move to left to make room for next piece of data.
    #000D DDDD << 6 = 0DDD DD00 0000
    #Now we get the last of data from reply[2]
    value = value + (reply[2] >> 2)
    #Here we discard last to bits
    #DDDD DDXXX >> 2 = 00DD DDDD
    #0DDD DD00 0000 + 00DD DDDD = 0DDD DDDD DDDD
    return value

def main():
    #read channel 0 on device 0
    value = readAnalog(0, 0)
    print value

if __name__ == "__main__":

my question here is how do I change the value of "device" and "channel" so I can operate two sensors on one MCP3002 chip from my raspberry pi?? Should I declare a new function to my sensor program?? Thank you.

1 Answer 1


You can do what you want by using the existing readAnalog function.

def readAnalog(device = 0,channel = 0):

says that the function takes two arguments which default to 0 and 0 if not specified.

If your MCP3002 is connected to CE0 (gpio8, pin 24) use readAnalog(0, 0) or readAnalog(0, 1).

If your MCP3002 is connected to CE1 (gpio7, pin 26) use readAnalog(1, 0) or readAnalog(1, 1).

In each case the second (channel) argument is defining which channel (and hence which sensor) to read from the MCP3002.

Python spidev module documentation.

  • by the way, can I actually connect two mcp3002 to raspi at the same time??
    – Evorio
    Apr 22, 2015 at 20:23
  • Yes. The main SPI has two chip selects. The auxiliary SPI (A+/B+/PI2/Compute module) has three chip selects. So for recent Pis you could connect 5. If you control chip selects yourself you could have one device per gpio you dedicate as a chip select. With additional hardware you could further increase the number of SPI devices.
    – joan
    Apr 22, 2015 at 20:37
  • the first mcp3002 I installed into my raspi is based on this picture raspberry.io/media/images/project_gallery_images/ADC2_bb.png so then I'll just have to connect the second one to the same GPIO pins or use the other available pins?? I just want to make sure.
    – Evorio
    Apr 22, 2015 at 21:02
  • You connect the second device to the same MISO/MOSI/SCLK as the first. You connect the chip select of the second device to CE1 rather than CE0.
    – joan
    Apr 22, 2015 at 21:20
  • as i thought, thank you very much for the explanation :D
    – Evorio
    Apr 22, 2015 at 21:22

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.