HCADS1xxx - Library for ADS1113/4/5 16bit ADCs (HCMODU0098)

Useful guides, libraries, and example sketches to support our Arduino based products.
Post Reply
admin
Site Admin
Posts: 865
Joined: Sun Aug 05, 2012 4:02 pm

HCADS1xxx - Library for ADS1113/4/5 16bit ADCs (HCMODU0098)

Post by admin » Wed Jun 08, 2016 10:48 am

Image



Arduino library header for the ADS1015, ADS1113, ADS1114, & ADS1115 serial analogue to digital devices. Currently supported products:

ADS1015 I2C 12Bit ADC Module (HCMODU0099) available from hobbycomponents.com
ADS1115 I2C 16Bit ADC Module (HCMODU0098) available from hobbycomponents.com

This Arduino library can be used to configure and obtain measurements from the Texas Instruments ADS1015, ADS1113, ADS1114, & ADS1115 16bit serial ADC devices.

You will need to download (please log in to download the library) and unzip this library to the Arduino development environments library area.

On Windows:
My Documents\Arduino\libraries\

On Mac:
Documents/Arduino/libraries/

Linux:
Usually found within the users home area under /Arduino/libraries/


Using the HCADS1xxx library

To use the library just include the HCADS1xxx.h header file and then create an instance of the library. E.g:

Code: Select all

#include "HCADS1xxx.h"
HCADS1xxx HCADS1xxx(ADS1xxx_I2C_ADD_LOW);

The ADS111x devices and our module can be set to one of 4 I2C address which are dependent on the state of the sensors address select pin (ADDR ), or the address selection pin on the HCMODU098. This can be in one of four states, low, high, connected to SDA, or connected to SCL:

ADS1xxx_I2C_ADD_LOW (I2C address: 0x48)
ADS1xxx_I2C_ADD_HIGH (I2C address: 0x49)
ADS1xxx_I2C_ADD_SDA (I2C address: 0x4A)
ADS1xxx_I2C_ADD_SCL (I2C address: 0x49)

By default the HCMODU0098 & HCMODU0099 module is set to ADS1xxx_I2C_ADD_LOW.


To initialise the library add the following line to the setup() section of your sketch:

Code: Select all

HCADS1xxx.Init();


The following functions are available with this library:

Code: Select all

HCADS1xxx.StartConversion();

Triggers a new conversion when in single shot mode. The device will power up, make one measurement and then automatically go back in to low power mode.

Code: Select all

int Result = Read();
Reads the result from the last conversion.

Returns a 16 bit signed 2's compliment integer value representing the last conversion result.

Note for the ADS1015 the 12 bit result is contained in upper bits (bits 4 to 15) with lower 4 bits set to 0's.

Code: Select all

boolean State = HCADS1xxx.ConversionState();
Gets the current conversion state when in single shot mode.

Returns a boolean value representing the current conversion state where:
false = device is currently performing a conversion
true = device is not currently performing a conversion.


Code: Select all

HCADS1xxx.Input(Mode);
Sets the state of the input multiplexer where:

Mode is the required multiplexer state. Valid values are
A0_A1 (measures the differential voltage between pins A0 & A1)
A0_A3 (measures the differential voltage between pins A0 & A3)
A1_A3 (measures the differential voltage between pins A1 & A3)
A2_A3 (measures the differential voltage between pins A2 & A3)
A0 (measures the voltage at pin A0 with reference to GND)
A1 (measures the voltage at pin A1 with reference to GND)
A2 (measures the voltage at pin A2 with reference to GND)
A3 (measures the voltage at pin A3 with reference to GND)


Code: Select all

HCADS1xxx.FullScale(Voltage);
Sets the full scale voltage (programmable gain amplifier) where:

Voltage is the required setting for the full scale voltage. Valid values are
FS_6_144V (full scale voltage is set 6.144 Volts)
FS_4_096V (full scale voltage is set 4.096 Volts)
FS_2_048V (full scale voltage is set 2.048 Volts)
FS_1_024V (full scale voltage is set 1.024 Volts)
FS_0_512V (full scale voltage is set 0.512 Volts)
FS_0_256V (full scale voltage is set 0.256 Volts)

Code: Select all

HCADS1xxx.Conv_Mode(Mode);
Sets the conversion mode to either single shot or continuous conversion where:

Mode is the required conversion mode. Valid values are
CONTINUOUS (the device will make continuous measurements of the input)
SINGLE_SHOT (the device will make one measurement of the input and then power down)



Code: Select all

HCADS1xxx.Data_Rate(Rate);
Sets the conversion data rate where:

Rate is the required conversion rate. Valid values for ADS111x devices are
ADS1115_DR_8SPS (conversion rate is set to 8 samples per second)
ADS1115_DR_16SPS (conversion rate is set to 16 samples per second)
ADS1115_DR_32SPS (conversion rate is set to 32 samples per second)
ADS1115_DR_64SPS (conversion rate is set to 64 samples per second)
ADS1115_DR_128SPS (conversion rate is set to 128 samples per second)
ADS1115_DR_250SPS (conversion rate is set to 250 samples per second)
ADS1115_DR_475PS (conversion rate is set to 475 samples per second)
ADS1115_DR_860SPS (conversion rate is set to 860 samples per second)

For the ADS1015 device:

ADS1015_DR_128SPS 0 (conversion rate is set to 128 samples per second)
ADS1015_DR_250SPS 1 (conversion rate is set to 250 samples per second)
ADS1015_DR_490SPS 2 (conversion rate is set to 490 samples per second)
ADS1015_DR_920SPS 3 (conversion rate is set to 920 samples per second)
ADS1015_DR_1600SPS 4 (conversion rate is set to 1600 samples per second)
ADS1015_DR_2400SPS 5 (conversion rate is set to 2400 samples per second)
ADS1015_DR_3300SPS 6 (conversion rate is set to 3300 samples per second)


Code: Select all

HCADS1xxx.Comp_Mode(Mode);
Sets the comparators mode of operation where:

Mode is the required mode. Valid values are
HYSTERESIS (traditional comparator with hysteresis)
WINDOWED (window comparator)


Code: Select all

HCADS1xxx.Comp_Pol(Mode);
Sets the polarity of the ALERT/RDY pin where:

Mode is the required active state of the pin. Valid values are
ACTIVE_LOW (sets pin to active low state)
ACTIVE_HIGH (sets pin to active high state)


Code: Select all

HCADS1xxx.Comp_Lat(Mode);
Controls whether the ALERT/RDY pin latches once asserted or clears once conversions are within the margin of the upper and lower threshold values where:

Mode is latching state of the pin. Valid values are
NON_LATCHING (non-latching comparator)
LATCHING (latching comparator)


Code: Select all

HCADS1xxx.Comp_Que(Mode)
Sets the state of the comparator queue where:

Mode is the required state. Valid values are
ASSERT_AFTER_1_CONV (ALERT/RDY is asserted after 1 conversion exceeding the upper or lower thresholds)
ASSERT_AFTER_2_CONV (ALERT/RDY is asserted after 2 conversions exceeding the upper or lower thresholds)
ASSERT_AFTER_4_CONV (ALERT/RDY is asserted after 4 conversions exceeding the upper or lower thresholds)
DISABLE_COMP (disables the comparator function)


Code: Select all

unsigned int Threshold = HCADS1xxx.Get_Lo_Thresh();
Gets the current value of the lower comparator threshold register.

Returns a 16 bit unsigned integer containing the current threshold value.



Code: Select all

unsigned int Threshold = HCADS1xxx.Get_Hi_Thresh();
Gets the current value of the upper comparator threshold register.

Returns a 16 bit unsigned integer containing the current threshold value.


Code: Select all

HCADS1xxx.Set_Lo_Thresh(Threshold);
Sets the value for the lower comparator threshold register where:

Threshold is a 16 unsigned integer to set the register to.



Code: Select all

HCADS1xxx.Set_Hi_Thresh(Threshold);
Sets the value for the upper comparator threshold register where:

Threshold is a 16 unsigned integer to set the register to.



Image

Code: Select all

/* FILE:    HCADS1xxx_Continuous_Conversion_Example
   DATE:    20/06/16
   VERSION: 0.2
   AUTHOR:  Andrew Davies

   07/06/16 version 0.1: Original version
   20/06/16 version 0.2: Updated to include support for 12Bit ADS1015
   
   Example Arduino sketch for ADS1015, ADS1113, ADS1114, & ADS1115 serial analogue to 
   digital devices created by Hobby Components Ltd (HOBBYCOMPONENTS.COM)
   

This example Arduino sketch demonstrates how to use the HCADS111x library to configure
the ADC device to continuous conversion mode and how to take repeated measurements.
The measurement results will be output to the Arduino's serial port. 

This library and sketch supports the ADS1015, ADS1015, ADS1113, ADS1114, & ADS1115 
devices but has been written in particular for our ADS1015 12bit and ADS1115 16bit
analogue to digital modules (HCMODU0098 & HCMODU0099) 
available from hobbycomponents.com

To use the module connect it to your Arduino as follows:

ADS1015/ADS1115......Uno/Nano
VDD..................5V
GND..................GND
SCL..................A5
SDA..................A4
A0...................To the analogue voltage to be measured (voltage must be between 0 & 6.144V)

The library for this sketch can be downloaded form the software section of our support
forum (forum.hobbycomponents.com).


You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of selling products that
directly compete with Hobby Components Ltd's own range of products.

THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/

/* Include the HCADS1xxx library */
#include "HCADS1xxx.h"

/* Create an instance of the library */
HCADS1xxx HCADS1xxx(ADS1xxx_I2C_ADD_LOW);


void setup() 
{
  /* Initialise the library and serial interface */ 
  Serial.begin(9600);
  HCADS1xxx.Init();

  /* Set the ADC's input to single ended (referenced to GND) and connect it to the 
   * modules pin A0. See the library documentation in our support forum for more 
   * options */
  HCADS1xxx.Input(A0);

  /* Set the devices full scale reference voltage to 6.144V. See library 
   * documentation for other options */
  HCADS1xxx.FullScale(FS_6_144V);
  
  /* Put the device into continuous conversion mode */
  HCADS1xxx.Conv_Mode(CONTINUOUS);
}


void loop() 
{
  int Result;

  /* Get the last measurement from the the device */
  Result = HCADS1xxx.Read();

  /* Output the raw result to the serial port */
  Serial.print("Raw value: ");
  Serial.println(Result);

  /* Divide the result by the ADC resolution to get the actual pin voltage:
   * (Result * (Full Scale Voltage / ADC resolution)) */
  Serial.print("Pin voltage: ");
  Serial.println(Result * (6.144 / (65536 / 2)), 4);
  Serial.println();

  /* Wait a little before taking another measurement so not to flood the serial port */
  delay(200);
}


Image
HCADS1xxx.zip
You do not have the required permissions to view the files attached to this post.

syedamerali
Posts: 5
Joined: Wed Jan 01, 2014 12:28 pm

Re: HCADS1xxx - Library for ADS1113/4/5 16bit ADCs (HCMODU00

Post by syedamerali » Wed Feb 21, 2018 4:29 pm

Not quite sure how to take readings for multiple inputs ....
would it be something like this?

void loop(){
HCADS1xxx.Input(A0);
int a0 = HCADS1xxx.Read();
HCADS1xxx.Input(A1);
int a1 = HCADS1xxx.Read();
etc ....
}

I am using an ads1015 (12bit) - to get voltages for HCADS1xxx.FullScale(FS_4_096V) I had to:
double volts =double(a0)*(2**12/2**16)* 0.002 // where 0.002 is volts/bit for this gain setting

What you have given in the code does not yield the correct result.

Please advise if what I am doing is right.

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCADS1xxx - Library for ADS1113/4/5 16bit ADCs (HCMODU00

Post by andrew » Thu Feb 22, 2018 10:27 am

Not quite sure how to take readings for multiple inputs ....
would it be something like this?

void loop(){
HCADS1xxx.Input(A0);
int a0 = HCADS1xxx.Read();
HCADS1xxx.Input(A1);
int a1 = HCADS1xxx.Read();
etc ....
}
Yes this is correct but if your reading values from different pins in quick succession using continuous conversion mode you'll need to leave sufficient time for the ADC to make a new conversion. You can do this by either inserting a delay between the input with the HCADS1xxx.Input() function and the HCADS1xxx.Read() function, or use the single shot mode.
am using an ads1015 (12bit) - to get voltages for HCADS1xxx.FullScale(FS_4_096V) I had to:
double volts =double(a0)*(2**12/2**16)* 0.002 // where 0.002 is volts/bit for this gain setting

What you have given in the code does not yield the correct result.

If you change the full scale voltage level you'll also need to change the full scale voltage value in the equation like this.

Result * (4.096 / (65536 / 2))

Note that the raw value returned by the ADC for the 12 bit (ADS1015) version is a 2's compliment number and although it is a 12 bit resolution the register is also shifted left by 4 bits to make it a 16 bit number. See page 15 of the ADS1015 datasheet for more information.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

syedamerali
Posts: 5
Joined: Wed Jan 01, 2014 12:28 pm

Re: HCADS1xxx - Library for ADS1113/4/5 16bit ADCs (HCMODU00

Post by syedamerali » Thu Feb 22, 2018 2:13 pm

Thank you for your response.

0.000125 = (4.096/(65536/2)) = 0.002*4096/65536

I was just doing it empirically without reading the documentation.

The example file states:
(Result * (Full Scale Voltage / ADC resolution))
I was plugging in 4096/2 for ADC resolution it should have been 65536/2 because of the 4 bit shift.

I feel the example documentation needs to be updated to reflect this 4 bit shift.

Thank you for making the world a little clearer. I wanted to use this library in preferene to the other one floating out there as this library is so flexible and allows you to change everything to what you want.
regards ....

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCADS1xxx - Library for ADS1113/4/5 16bit ADCs (HCMODU00

Post by andrew » Thu Feb 22, 2018 4:04 pm

Thanks. I've added a note to the description of the read function to make this clearer.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

rudiggi
Posts: 1
Joined: Tue Jan 19, 2021 1:21 pm

Re: HCADS1xxx - Library for ADS1113/4/5 16bit ADCs (HCMODU0098)

Post by rudiggi » Tue Jan 19, 2021 5:15 pm

Hello,

I love this library. It is giving simple access to the functions of the device without trying to assume what I want to do with it. In particular, I want to do a sampling with 3300 SPS with two ADS1015 in parallel for IV-tracing. Now I tested this by doing a measurement every 303 microseconds (using micros() and waiting for the next value). This worked very well. Then I started reading every 50 µs and expected to read 6 identical values in a row. I don't. The values seem to be fluctuating much faster than the sampling frequency. This is too good to be true in a way.

Please see the code for one ADS1015 below. Could you explain this?

I would like to use alert/rdy from each ADS1015 and connect them to two interrupt pins and trigger reading a new result whenever one of the ADS1015 has a new value. How can I do this? Simply set a window that covers the full scale in the window comparator mode such that it will be true every time there is a value? Or do you have a better way?

Kind regards.

Now here is the adapted code that reads a burst of 20 values and displays it afterwards.

/* Include the HCADS1xxx library */
#include "HCADS1xxx.h"

/* Create an instance of the library */
HCADS1xxx HCADS1xxx(ADS1xxx_I2C_ADD_LOW);


void setup()
{
/* Initialise the library and serial interface */
Serial.begin(115200);
HCADS1xxx.Init();

HCADS1xxx.Data_Rate(ADS1015_DR_3300SPS);

/* Set the ADC's input to single ended (referenced to GND) and connect it to the
* modules pin A0. See the library documentation in our support forum for more
* options */
HCADS1xxx.Input(A0_A1);

/* Set the devices full scale reference voltage to 6.144V. See library
* documentation for other options FS_6_144V FS_4_096V FS_2_048V FS_1_024V FS_0_512V FS_0_256V */
HCADS1xxx.FullScale(FS_2_048V);

/* Put the device into continuous conversion mode */
HCADS1xxx.Conv_Mode(CONTINUOUS);
}


void loop()
{
int Result[20];
unsigned long next_micros = micros()+303;
unsigned long prev_micros = next_micros-303;
for (int i=0; i<20; i++){
while (micros()<next_micros){}
next_micros += 50;
/* Get the last measurement from the the device */
Result = HCADS1xxx.Read();
}
Serial.print("time_for_20: ");
Serial.println(next_micros - prev_micros - 303);

/* Output the raw result to the serial port */
for (int i=0; i<20; i++){
Serial.print("Raw value: ");
Serial.println(Result);

/* Divide the result by the ADC resolution to get the actual pin voltage:
* (Result * (Full Scale Voltage / ADC resolution)) */
Serial.print("Pin voltage: ");
Serial.println(Result * (2.048 / (65536 / 2)), 4);
Serial.println();
}

/* Wait a little before taking another measurement so not to flood the serial port */
delay(2000);
}

Can you explain what happens?

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCADS1xxx - Library for ADS1113/4/5 16bit ADCs (HCMODU0098)

Post by andrew » Wed Jan 20, 2021 3:17 pm

Then I started reading every 50 µs and expected to read 6 identical values in a row. I don't. The values seem to be fluctuating much faster than the sampling frequency. This is too good to be true in a way.

Doing a quick scan of your sketch I don't see anything jumping out at me other than you don't seem to be indexing your Result array. I wouldn't expect it to compile like that so is this the correct sketch?


I would like to use alert/rdy from each ADS1015 and connect them to two interrupt pins and trigger reading a new result whenever one of the ADS1015 has a new value. How can I do this? Simply set a window that covers the full scale in the window comparator mode such that it will be true every time there is a value? Or do you have a better way?

Yes it has what is called a conversion ready mode which looking at the datasheet is effectively what you are suggesting. Here is the instructions from the datasheet on how to put it into this mode:

9.3.8 Conversion Ready Pin (ADS1114 and ADS1115 Only)
The ALERT/RDY pin can also be configured as a conversion ready pin. Set the most-significant bit of the Hi_thresh register to 1 and the most-significant bit of Lo_thresh register to 0 to enable the pin as a conversion
ready pin. The COMP_POL bit continues to function as expected. Set the COMP_QUE[1:0] bits to any 2-bit value other than 11 to keep the ALERT/RDY pin enabled, and allow the conversion ready signal to appear at the ALERT/RDY pin output. The COMP_MODE and COMP_LAT bits no longer control any function. When configured as a conversion ready pin, ALERT/RDY continues to require a pullup resistor. The ADS111x provide
an approximately 8-µs conversion ready pulse on the ALERT/RDY pin at the end of each conversion in continuous-conversion mode, as shown in Figure 29. In single-shot mode, the ALERT/RDY pin asserts low at the
end of a conversion if the COMP_POL bit is set to 0.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Post Reply

Return to “Arduino”