You are viewing an older revision! See the latest version
MCP3221
Introduction
This library was written because I needed to an analogIn accurately in the range 2.5v to 5.0v, at a resolution of 20mV.
I was using a comparator circuit designed around an opAmp to level shift the signal, but realised there were better ways of doing it with an ADC being supplied from 5.0v.
If you use an external ADC then these give a reading referenced to the supply voltage. So, if you use an ADC such as an MCP3221, and supply it with 5.0v, then the ain can swing between 0 and 5.0v.
The chosen IC is an I2C device, and at 12BIT, it has a resolution of 4096 "steps" between Vss (usually GND) and Vdd (Supply voltage).
This means that if it is supplied with 5.0v, it has a resolution of 1mV.
Notes on Addressing
This library is for the default part : MCP3221A5T-I/OT
The "A5" in the part number denotes the specific address of this part. This ADC is available with different addresses (different part codes) so that you can have more than 1 on the I2C bus.
Studying the datasheet showed that it is not a completely standard I2C part. You cannot write to an MCP3221, it has no writable registers. You open a conversation with it by sending it a byte terminating in a "1" bit, these bytes are usually "reads" in the I2C architecture.
MCP3221 also requires an ACKnowledge between each byte it sends, before it will send the next byte. So we need to be a bit manual with how we talk to it.
It also needs an (NOT) ACKnowledge after the second byte or it will keep sending bytes (continuous sampling)(You can continuous sample from it if you want, but this library does not support that) This is a simple, "set it up and read from it, getting a value back as a voltage" library.
to communicate with it you must..
I2C.START Send 8 bit device/ part address to open conversation. (See .h file for part explanation) read a byte (with ACK) read a byte (with NAK) I2C.STOP Convert the 2 byte value to something meaningful by referencing the supply voltage
Library
Example Program