upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
Public Types | Public Member Functions | Protected Member Functions

API for ADS1115. More...

Detailed Description

The ADS1113, ADS1114, and ADS1115 are precision analog-to-digital converters (ADCs) with 16 bits of resolution offered in an ultra-small, leadless QFN-10 package or an MSOP-10 package. The ADS1113/4/5 are designed with precision, power, and ease of implementation in mind. The ADS1113/4/5 feature an onboard reference and oscillator. Data is transferred via an I2C-compatible serial interface; four I2C slave addresses can be selected. The ADS1113/4/5 operate from a single power supply ranging from 2.0V to 5.5V. The ADS1113/4/5 can perform conversions at rates up to 860 samples per second (SPS). An onboard PGA is available on the ADS1114 and ADS1115 that offers input ranges from the supply to as low as +/- 256mV, allowing both large and small signals to be measured with high resolution. The ADS1115 also features an input multiplexer (MUX) that provides two differential or four single-ended inputs. The ADS1113/4/5 operate either in continuous conversion mode or a single-shot mode that automatically powers down after a conversion and greatly reduces current consumption during idle periods. The ADS1113/4/5 are specified from -40 deg C to +125 deg C.

Tested with DIYMall ADS1115 board. Also available from Adafruit: https://www.adafruit.com/products/1085

ads1115.jpg
// Select the device you are testing here and adjust case 6 for the correct
// sample rates.
upm::ADS1115 ads(1, 0x49);
float inputVoltage;
int ans;
do {
cout << endl;
cout << "1 - get Conversion \t";
cout << "2 - get last conversion" << endl;
cout << "3 - get Gain \t\t";
cout << "4 - set Gain" << endl;
;
cout << "5 - get Data Rate \t";
cout << "6 - set Data Rate" << endl;
cout << "7 - Set Upper Threshold \t";
cout << "8 - Set Lower Threshold \t";
cout << "9 - Display Thresholds \t";
cout << "10 - Set Default Thresholds \t";
cout << "11 - Set conversion ready" << endl;
cout << "12 - get Comp Que \t";
cout << "13 - set Comp Que" << endl;
cout << "14 - get Comp Pol \t";
cout << "15 - set Comp Pol" << endl;
cout << "16 - get Comp mode \t";
cout << "17 - set Comp mode " << endl;
cout << "18 - get Comp Latch\t";
cout << "19 - set Comp Latch " << endl;
cout << "20 - get Continuous \t";
cout << "21 - set Continuous \t" << endl;
cout << "-1 - exit" << endl;
cout << "Enter a command: ";
cin >> command;
switch (command) {
case 2:
cout << ads.getLastSample() << endl;
break;
case 3:
cout << std::hex << ads.getGain() << endl;
break;
case 5:
cout << std::hex << ads.getSPS() << endl;
break;
case 4:
int gain;
ADS1015::ADSGAIN set_gain;
cout << "select one of the following:" << endl;
cout << "1 -> gain 2/3 \t 2 -> gain1 \t 3 -> gain 2" << endl;
cout << "4 -> gain 4 \t 5 -> gain 8 \t 6 -> gain 15" << endl;
cin >> gain;
switch (gain) {
case 1:
set_gain = ADS1X15::GAIN_TWOTHIRDS;
break;
case 2:
set_gain = ADS1X15::GAIN_ONE;
break;
case 3:
set_gain = ADS1X15::GAIN_TWO;
break;
case 4:
set_gain = ADS1X15::GAIN_FOUR;
break;
case 5:
set_gain = ADS1X15::GAIN_EIGHT;
break;
case 6:
set_gain = ADS1X15::GAIN_SIXTEEN;
break;
default:
set_gain = ADS1X15::GAIN_ONE;
}
ads.setGain(set_gain);
break;
case 6:
int rate;
/*ADS1015::ADSDATARATE set_rate;
cout << "select one of the following:" << endl;
cout << "1 -> SPS_120 \t 2 -> SPS_250 \t 3 -> SPS_490 \t 4 -> SPS_920" <<
endl;
cout << "5 -> SPS_1600 \t 6 -> SPS_2400 \t 7 -> SPS_3300" << endl;
cin >> rate;
switch(rate){
case 1:
set_rate = ADS1015::SPS_128;
break;
case 2:
set_rate = ADS1015::SPS_250;
break;
case 3:
set_rate = ADS1015::SPS_490;
break;
case 4:
set_rate = ADS1015::SPS_920;
break;
case 5:
set_rate = ADS1015::SPS_1600;
break;
case 6:
set_rate = ADS1015::SPS_2400;
break;
case 7:
set_rate = ADS1015::SPS_3300;
break;
default:
set_rate = ADS1015::SPS_1600;
} */
ADS1115::ADSDATARATE set_rate;
cout << "select one of the following:" << endl;
cout << "1 -> SPS_8 \t 2 -> SPS_16 \t 3 -> SPS_32 \t 4 -> SPS_64" << endl;
cout << "5 -> SPS_128 \t 6 -> SPS_250 \t 7 -> SPS_475 \t 8-> SPS_860" << endl;
cin >> rate;
switch (rate) {
case 1:
set_rate = ADS1115::SPS_8;
break;
case 2:
set_rate = ADS1115::SPS_16;
break;
case 3:
set_rate = ADS1115::SPS_32;
break;
case 4:
set_rate = ADS1115::SPS_64;
break;
case 5:
set_rate = ADS1115::SPS_128;
break;
case 6:
set_rate = ADS1115::SPS_250;
break;
case 7:
set_rate = ADS1115::SPS_475;
break;
case 8:
set_rate = ADS1115::SPS_860;
break;
default:
set_rate = ADS1115::SPS_128;
}
ads.setSPS(set_rate);
break;
case 1:
int mode;
cout << "select one of the following:" << endl;
cout << "1 -> MUX_0_1 \t 2 -> MUX_0_3 \t 3 -> MUX_1_3 \t 4 -> MUX_2_3" << endl;
cout << "5 -> SINGLE_0 \t 6 -> SINGLE_1 \t 7 -> SINGLE_2 \t 8 -> "
"SINGLE_3"
<< endl;
cin >> mode;
switch (mode) {
case 1:
set_mode = ADS1X15::DIFF_0_1;
break;
case 2:
set_mode = ADS1X15::DIFF_0_3;
break;
case 3:
set_mode = ADS1X15::DIFF_1_3;
break;
case 4:
set_mode = ADS1X15::DIFF_2_3;
break;
case 5:
set_mode = ADS1X15::SINGLE_0;
break;
case 6:
set_mode = ADS1X15::SINGLE_1;
break;
case 7:
set_mode = ADS1X15::SINGLE_2;
break;
case 8:
set_mode = ADS1X15::SINGLE_3;
break;
default:
set_mode = ADS1X15::DIFF_0_1;
break;
}
cout << ads.getSample(set_mode) << endl;
break;
case 7:
cout << " enter a float value: ";
cin >> inputVoltage;
ads.setThresh(ADS1115::THRESH_HIGH, inputVoltage);
break;
case 8:
cout << " enter a float value: ";
cin >> inputVoltage;
ads.setThresh(ADS1115::THRESH_LOW, inputVoltage);
break;
case 9:
cout << "Upper " << ads.getThresh(ADS1X15::THRESH_HIGH) << endl;
cout << "Lower " << ads.getThresh(ADS1X15::THRESH_LOW) << endl;
break;
case 10:
ads.setThresh(ADS1115::THRESH_DEFAULT);
break;
case 11:
ads.setThresh(ADS1015::CONVERSION_RDY);
break;
case 12:
cout << ads.getCompQue() << endl;
break;
case 13:
int que;
cout << "select one of the following:" << endl;
cout << "1 -> CQUE_1CONV \t 2 -> CQUE_2CONV \t 3 -> CQUE_3CONV \t 4 -> "
"CQUE_NONE"
<< endl;
cin >> que;
switch (que) {
case 1:
ads.setCompQue(ADS1X15::CQUE_1CONV);
break;
case 2:
ads.setCompQue(ADS1X15::CQUE_2CONV);
break;
case 3:
ads.setCompQue(ADS1X15::CQUE_4CONV);
break;
case 4:
default:
ads.setCompQue(ADS1X15::CQUE_NONE);
break;
}
break;
case 14:
cout << ads.getCompPol() << endl;
break;
case 15:
cout << "select one of the following:" << endl;
cout << "1 -> active high \t 2 -> active low" << endl;
cin >> ans;
if (ans == 1)
ads.setCompPol(true);
else
ads.setCompPol(false);
break;
case 16:
cout << ads.getCompMode() << endl;
break;
case 17:
cout << "select one of the following:" << endl;
cout << "1 -> Window \t 2 -> Traditional (default)" << endl;
cin >> ans;
if (ans == 1)
ads.setCompMode(true);
else
ads.setCompMode();
break;
case 18:
cout << ads.getCompLatch() << endl;
break;
case 19:
cout << "select one of the following:" << endl;
cout << "1 -> Latching \t 2 -> Non-latching (default)" << endl;
cin >> ans;
if (ans == 1)
ads.setCompLatch(true);
else
ads.setCompLatch();
break;
case 20:
cout << ads.getContinuous() << endl;
break;
case 21:
cout << "select one of the following:" << endl;
cout << "1 -> Power Down (default) \t 2 -> Continuous" << endl;
cin >> ans;
if (ans == 1)
ads.setContinuous(true);
else
ads.setContinuous();
break;
case -1:
break;
default:
break;
}
} while (command != -1);
long id = 0; // Sample number
string fileName = "./ads1115.data"; // Output filename
ofstream f;
// Initialize and configure the ADS1115 for the SM-24 Geophone
// There are two ADS1115 chips on the DFRobot Joule Shield on the same I2C bus
// - 0x48 gives access to pins A0 - A3
// - 0x49 gives access to pins A4 - A7
ADS1115 ads1115(0, 0x48);
// Put the ADC into differential mode for pins A0 and A1,
// the SM-24 Geophone is connected to these pins
ads1115.getSample(ADS1X15::DIFF_0_1);
// Set the gain based on expected VIN range to -/+ 2.048 V
// Can be adjusted based on application to as low as -/+ 0.256 V, see API
// documentation for details
ads1115.setGain(ADS1X15::GAIN_TWO);
// Set the sample rate to 860 samples per second (max) and turn on continuous
// sampling
ads1115.setSPS(ADS1115::SPS_860);
ads1115.setContinuous(true);
// Enable exceptions from the output stream
f.exceptions(ofstream::failbit | ofstream::badbit);
// Open the file
try {
f.open(fileName);
// Output formatting
f.setf(ios_base::fixed);
f.precision(7);
// Start the thread that will stop logging after 10 seconds
thread timer(stop);
// Read sensor data and write it to the output file every ms
while (running) {
f << id++ << " " << ads1115.getLastSample() << endl;
upm_delay_us(1000);
}
// Clean-up and exit
timer.join();
f.close();
} catch (ios_base::failure& e) {
cout << "Failed to write to file: " << e.what() << endl;
return 1;
}
cout << "Wrote " << id << " samples to file: " << fileName << endl;

Public Types

enum  ADSDATARATE {
  SPS_8 = ADS1115_DR_8SPS, SPS_16 = ADS1115_DR_16SPS, SPS_32 = ADS1115_DR_32SPS, SPS_64 = ADS1115_DR_64SPS,
  SPS_128 = ADS1115_DR_128SPS, SPS_250 = ADS1115_DR_250SPS, SPS_475 = ADS1115_DR_475SPS, SPS_860 = ADS1115_DR_860SPS
}
 
typedef enum upm::ADS1115::ADSDATARATE ADSDATARATE
 
- Public Types inherited from ADS1X15
enum  ADSGAIN {
  GAIN_TWOTHIRDS = ADS1X15_PGA_6_144V, GAIN_ONE = ADS1X15_PGA_4_096V, GAIN_TWO = ADS1X15_PGA_2_048V, GAIN_FOUR = ADS1X15_PGA_1_024V,
  GAIN_EIGHT = ADS1X15_PGA_0_512V, GAIN_SIXTEEN = ADS1X15_PGA_0_256V
}
 uint16_t enum containing values for setting gain for ADS1X15 devices.
 
enum  ADSMUXMODE {
  DIFF_0_1 = ADS1X15_MUX_DIFF_0_1, DIFF_0_3 = ADS1X15_MUX_DIFF_0_3, DIFF_1_3 = ADS1X15_MUX_DIFF_1_3, DIFF_2_3 = ADS1X15_MUX_DIFF_2_3,
  SINGLE_0 = ADS1X15_MUX_SINGLE_0, SINGLE_1 = ADS1X15_MUX_SINGLE_1, SINGLE_2 = ADS1X15_MUX_SINGLE_2, SINGLE_3 = ADS1X15_MUX_SINGLE_3
}
 uint16_t enum containing values used for selecting ADS1X15 read operations.
 
enum  ADSCOMP { CQUE_1CONV = 0x0000, CQUE_2CONV = 0x0001, CQUE_4CONV = 0x0002, CQUE_NONE = 0x0003 }
 uint16_t enum containing values for setting ADS1X15 comparator queue modes.
 
enum  ADSTHRESH { THRESH_LOW = ADS1X15_REG_POINTER_LOWTHRESH, THRESH_HIGH = ADS1X15_REG_POINTER_HITHRESH, CONVERSION_RDY = 0x04, THRESH_DEFAULT = 0x05 }
 uint8_t enum containing register addresses used for setting HI and LOW threshold values as well as setting conversion ready and set to default.
 
enum  ADSSAMPLERATE { SPS_DEFAULT = 0x0080 }
 uint16_t enum containing values representing the sample rate of the device. Will be overridden in subclass
 
typedef enum upm::ADS1X15::ADSGAIN ADSGAIN
 
typedef enum upm::ADS1X15::ADSMUXMODE ADSMUXMODE
 
typedef enum upm::ADS1X15::ADSCOMP ADSCOMP
 
typedef enum upm::ADS1X15::ADSTHRESH ADSTHRESH
 
typedef enum upm::ADS1X15::ADSSAMPLERATE ADSSAMPLERATE
 

Public Member Functions

 ADS1115 (int bus, uint8_t address=0x48)
 
 ~ADS1115 ()
 
void setSPS (ADSDATARATE rate=ADS1115::SPS_128)
 
- Public Member Functions inherited from ADS1X15
 ADS1X15 (int bus, uint8_t address)
 
virtual ~ADS1X15 ()
 
std::string name ()
 
float getLastSample (int reg=ADS1X15_REG_POINTER_CONVERT)
 
float getSample (ADSMUXMODE mode=ADS1X15::DIFF_0_1)
 
ADSGAIN getGain ()
 
void setGain (ADSGAIN gain=ADS1X15::GAIN_TWO)
 
ADSSAMPLERATE getSPS (void)
 
virtual void setSPS (ADSSAMPLERATE rate)
 
bool getCompMode (void)
 
void setCompMode (bool mode=false)
 
bool getCompPol (void)
 
void setCompPol (bool mode=false)
 
bool getCompLatch (void)
 
void setCompLatch (bool mode=false)
 
ADSCOMP getCompQue (void)
 
void setCompQue (ADSCOMP mode=ADS1X15::CQUE_NONE)
 
bool getContinuous (void)
 
void setContinuous (bool mode=false)
 
float getThresh (ADSTHRESH reg=THRESH_DEFAULT)
 
void setThresh (ADSTHRESH reg=THRESH_DEFAULT, float value=0.0)
 

Protected Member Functions

float getMultiplier (void)
 
void setDelay (void)
 
- Protected Member Functions inherited from ADS1X15
void getCurrentConfig ()
 
void updateConfigRegister (uint16_t update, bool read=false)
 
uint16_t swapWord (uint16_t value)
 

Additional Inherited Members

- Protected Attributes inherited from ADS1X15
std::string m_name
 
float m_conversionDelay
 
uint8_t m_bitShift
 
uint16_t m_config_reg
 
mraa::I2c * i2c
 

Constructor & Destructor Documentation

ADS1115 ( int  bus,
uint8_t  address = 0x48 
)

ADS1X15 constructor

Parameters
busi2c bus the sensor is attached to.
addressDevice address. Default is 0x48.
~ADS1115 ( )

ADS1X15 destructor

Member Function Documentation

void setSPS ( ADSDATARATE  rate = ADS1115::SPS_128)

Sets the sample rate of the device. This function needs to be overridden in subclasses as the ADS1115 and ADS1015 have different sample rates.

Parameters
rateADSSAMPLERATE enum

Here is the call graph for this function:

Inheritance diagram for ADS1115:
Inheritance graph
[legend]
Collaboration diagram for ADS1115:
Collaboration graph
[legend]

The documentation for this class was generated from the following files: