ADS1115 - Getting Data from Sensor

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
garybronson
 
Posts: 17
Joined: Mon Jul 21, 2014 10:37 pm

ADS1115 - Getting Data from Sensor

Post by garybronson »

I'm using this board and properly wired in a photocell to A0. With a voltmeter i can see the voltage change at this input between 0 - 3vdc as i cover and uncover it. I converted the sample program from this site to C# and I am using a 3rd party I2C library to write and read registers. I have already successfully connected and can read the TSL2561 light sensor so i know the I2C library is working. For some reason i can't get the ADS1115 interface correct as it seems a little more involved. I'm looking for someone to take a quick look that maybe could spot something i have missed. From what I understand i setup the connection using the proper slave ID. It works since it returns a handle to it. Then i write some defaults, and then set loop to read the return values. Below is the code. My goal is to constantly read the changing value and I'm a bit confused on the single shot vs continuous mode. I don't need to monitor differential. Some hints to make it work right would be helpful.

Code: Select all

        private int readADC_SingleEnded(int channel)
        {

            string CQUE_NONE =          "0x0000";
            string CLAT_NONLAT =        "0x0000";
            string CPOL_ACTVLOW =       "0x0003";
            string CMODE_TRAD =         "0x0000";
            string DR_1600SPS =         "0x0080";
            string MODE_SINGLE =        "0x0100";
            string PGA_6_144V =         "0x0000"; 
            string MUX_SINGLE_0 =       "0x4000";
            string OS_SINGLE =          "0x8000";
            string POINTER_CONFIG =     "0x01";
            string POINTER_CONVERT =    "0x00";
            string MODE_CONTIN =        "0x0000";

            if (channel > 3)
            {
                return 0;
            }

            //Start with default values
            int ADS1015_REG_CONFIG_CQUE_NONE = this.GetHexFromString(ref CQUE_NONE);   // Disable the comparator (default val)
            int ADS1015_REG_CONFIG_CLAT_NONLAT = this.GetHexFromString(ref CLAT_NONLAT);    // Non-latching (default val)
            int ADS1015_REG_CONFIG_CPOL_ACTVLOW = this.GetHexFromString(ref CPOL_ACTVLOW);   // Alert/Rdy active low   (default val)
            int ADS1015_REG_CONFIG_CMODE_TRAD = this.GetHexFromString(ref CMODE_TRAD);    // Traditional comparator (default val)
            int ADS1015_REG_CONFIG_DR_1600SPS = this.GetHexFromString(ref DR_1600SPS);  // 1600 samples per second (default)
            int ADS1015_REG_CONFIG_MODE_SINGLE = this.GetHexFromString(ref MODE_SINGLE);

            //other settings
            int ADS1015_REG_CONFIG_PGA_6_144V = this.GetHexFromString(ref PGA_6_144V);
            int ADS1015_REG_CONFIG_MUX_SINGLE_0 = this.GetHexFromString(ref MUX_SINGLE_0);
            int ADS1015_REG_CONFIG_OS_SINGLE = this.GetHexFromString(ref OS_SINGLE);

            //continous read mode ?
            int ADS1015_REG_CONFIG_MODE_CONTIN = this.GetHexFromString(ref MODE_CONTIN);

            //reg pointer
            int ADS1015_REG_POINTER_CONFIG = this.GetHexFromString(ref POINTER_CONFIG);

            //reg pointer convert
            int ADS1015_REG_POINTER_CONVERT = this.GetHexFromString(ref POINTER_CONVERT);

   
            //Start with default values
            int config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
                              ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
                              ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low   (default val)
                              ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
                              ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
                              ADS1015_REG_CONFIG_MODE_SINGLE;   // Single-shot mode (default)


           
            // Set PGA/voltage range
            config |= ADS1015_REG_CONFIG_PGA_6_144V;

            //// Set single-ended input channel
            switch (channel)
            {
                case (0):
                    config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
                    break;
                case (1):
                    //config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
                    break;
                case (2):
                    //config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
                    break;
                case (3):
                    //config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
                    break;
            }

            // Set 'start single-conversion' bit
            config |= ADS1015_REG_CONFIG_OS_SINGLE;

            //assign all
            this.write_data = config;


            // Write config register to the ADC
            int iReturnVal = I2CWriteData(this.g_i2chandle, ADS1015_REG_POINTER_CONFIG, ref this.write_data, 2);

            Trace.Write("\n Write Return Value: " + Convert.ToBoolean(iReturnVal));

            //delay
            Thread.Sleep(100);

            int iReadReturn = I2CReadData(this.g_i2chandle, ADS1015_REG_POINTER_CONVERT, ref this.read_data, 2);

            Trace.Write("\n Read Return Value: " + Convert.ToBoolean(iReadReturn));

            //display in a textbox on the form
            this.ShowData2(this.read_data.ToString());

            //return value
            return this.read_data;

        }
With all the parameters it looks like it OR's them all together and then i write that out to ADS1015_REG_POINTER_CONFIG to set the config then i should be able to read from ADS1015_REG_POINTER_CONVERT.


Here is the structure for the library to read and write

WRITE:
UINT I2CWriteData (
HANDLE hContext,
UINT32 subAddr,
VOID *pBuffer,
UINT32 count
)
Description
This function writes the particular number of bytes to the I2C device.

Parameters
hContext – Handle returned by I2CDrvrOpen function.
subAddr – Slave Address of the I2C Device to which data has to be written
pBuffer – Pointer to the buffer holding the data.
count – Number of Bytes to be written




READ:

UINT I2CReadData(
HANDLE hContext,
UINT32 subAddr,
VOID *pBuffer,
UINT32 count
)
Description
This function reads the particular number of bytes from the I2C device.

Parameters
hContext – Handle returned by I2CDrvrOpen function.
subAddr – Slave Address of the I2C Device from which the data has to be read
*pBuffer – Pointer to the buffer to hold the data.
count – Number of Bytes to be read

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: ADS1115 - Getting Data from Sensor

Post by adafruit_support_rick »

[moved to General Project Help]

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”