Page 1 of 1

[python] MCP3021 resolution

Posted: Tue Apr 23, 2019 12:36 pm
by guyver2
Hi everyone,

I'm trying to read, through i2c and via python, values from a potentiometer using a very simple ADC, the MCP3021.
The datasheet claims 10 bits of precision but I can only get values from 0 to 15, which is you know, 4 bits of precision...

I was wondering if I'm doing something wrong here.
Here is the very simple class I use for that : ... ter/
The important line is l.23 where I read the data.

Any guess ?

I'm not a hardware expert so if you need more information just let me know.

Re: [python] MCP3021 resolution

Posted: Tue Apr 23, 2019 2:38 pm
by danjperron
First you need to read 2 bytes without command byte.

Check page 18 from ... 01805C.pdf

SMBus doesn't support it! You need to use IOCTL.

Code: Select all

#!/usr/bin/env python

import io
import fcntl
import time


i2c_address = 0x4d

FileHandle ="/dev/i2c-1", "rb", buffering=0)

# set device address
fcntl.ioctl(FileHandle, I2C_SLAVE_COMMAND, i2c_address)

def ReadAD():
     values = list(
     return ( values[0]* 256 + values[1]) / 4


Re: [python] MCP3021 resolution

Posted: Thu May 02, 2019 10:58 am
by guyver2
Thank you very much, I'll try that as soon as possible and come back to tell you if it worked out.

Re: [python] MCP3021 resolution

Posted: Sat May 18, 2019 12:30 pm
by guyver2
It wasn't as soon as I expected but I finally got the chance to try your solution and it worked very nicely. I now have a value in the expected range [0-1023] instead of [0-15].

Thanks a lot.