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 : https://gitlab.com/antoine.letouzey/boo ... ter/pot.py
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 http://ww1.microchip.com/downloads/en/D ... 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_SLAVE_COMMAND=0x0703

i2c_address = 0x4d

FileHandle =  io.open("/dev/i2c-1", "rb", buffering=0)

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



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



print("A/D:{}".format(ReadAD()))

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.