I have a LM35 sensor connected to my Pi using the MCP3204(12bit ADC). To read the values I found a python wrapper for the Spi protocol called py-spidev.

I am following tutorials which uses use the MCP3008 Here is the function that reads the value from the sensor.

def readadc(adcnum):

if adcnum > 7 or adcnum < 0: return -1 r = spi.xfer2([1, 8 + adcnum << 4, 0]) adcout = ((r[1] & 3) << 8) + r[2] return adcout

My question is Should I be changing something if I am using a 12 bit ADC?

  • Probably not. But just try it out, and you'll see if it works.
    – Gerben
    Aug 5, 2013 at 11:40

1 Answer 1


The above code is only capturing a 10 bit output.

adcout = ((r[1] & 3) << 8) + r[2]

It only captures two bits from r[1] ((r[1] & 3) << 8) plus the 8 from r[2]. In reviewing the datasheet, you will need to capture the four lowest bits of r[1], so you would need to change that line to read:

adcout = ((r[1] & 15) << 8) + r[2]

15 = 0x0F or 00001111, four lowest bits. Three of the four upper bits are unknown, which is why we are zeroing any bits which are not of interest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.