Support Forum

Share your projects and post your questions

Register   or   Sign In
The Forum

read and configure ADC using i2c-tools

3294 Views - Created 13/01/2016


Posted by:

eskowitz Avatar

I am able to use i2cdetect -y -r 1 to see that the ADC Differential Pi is located at i2c addresses 68 and 69 as expected.
I now want to use i2cget and i2cset to configure the device and read results from the ADC. Both functions take the i2c address of 68 and 69 and then expect a data address (and write value in the case of i2cset).
The data sheet refers to a configuration register but does not discuss anything in the context of data addresses such as are used with i2cget/i2cset. The data sheet also indicates that the power on default value for the configuration register will be 0x90. When I do i2cdump -y 1 0x68 I do not see any values displayed with the setting 0x90. I see all 0xFFs for byte mode or 0xDCFF if I do word mode.
Is it possible to use i2cget/i2cset to access the ADC results and set the configuration data? If so, how?


Posted by:

andrew Avatar

United Kingdom

andrew Twitter  andrew Website  

I am afraid I do not know if you can use i2cget/i2cset to access the ADC as I don't have any experience using those tools on the Raspberry Pi.

To read a value from the ADC Differential Pi you first need to write to the configuration register and then read three or four bytes depending on the selected bit rate.

In the 18-bit mode, you read 4 bytes in all of the other modes you read three bytes. Once the configuration register has been written it will take a few milliseconds for the ADC to read the voltage and write it to the registers at which point it will set a ready bit to 0. This readybit is bit 7 of the last returned byte so you need to keep performing a read from the ADC chip until the ready bit shows that the sample has been taken. You then need to combine the bytes from the last read to create the raw data value that can then be converted into a voltage. The bytes will need to be combined in different ways depending on the selected bit rate.

One of the bits in the returned value is a sign bit which shows if the value is positive or negative. In our libraries, we save this value to a variable called signbit to be used later in the voltage calculations and then set the bit to 0. The location of this bit is dependent on the bit rate so you will need to check the datasheet to find out which bit to read and then clear.

One thing to be aware of is when you perform an I2C read to get the values the ADC may not send an ACK bit with the last byte so if your software is looking for ACK bits to check if the data was received it may fail when the last byte is read.

If you understand the C programming language then it may be worth reading through the read_raw() function in our ADC Differential Pi C library so you can see how the library builds the configuration byte, sends it to the ADC and then reads back the data and combines it together.

Sign in to post your reply

Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view.
Download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.