guyver2
Posts: 33
Joined: Thu Feb 26, 2015 4:05 pm

[python] MCP3021 resolution

Tue Apr 23, 2019 12:36 pm

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.

danjperron
Posts: 3331
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: [python] MCP3021 resolution

Tue Apr 23, 2019 2:38 pm

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()))

guyver2
Posts: 33
Joined: Thu Feb 26, 2015 4:05 pm

Re: [python] MCP3021 resolution

Thu May 02, 2019 10:58 am

Thank you very much, I'll try that as soon as possible and come back to tell you if it worked out.

guyver2
Posts: 33
Joined: Thu Feb 26, 2015 4:05 pm

Re: [python] MCP3021 resolution

Sat May 18, 2019 12:30 pm

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.

Return to “Interfacing (DSI, CSI, I2C, etc.)”