upm  1.3.0
Sensor/Actuator repository for libmraa (v1.7.0)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Public Types | Public Member Functions | Protected Member Functions
ADS1015 Class Reference

API for ADS1015. More...

Detailed Description

The ADS1013, ADS1014, and ADS1015 are precision analog-to-digital converters (ADCs) with 12 bits of resolution offered in an ultra-small, leadless QFN-10 package or an MSOP-10 package. The ADS1013/4/5 are designed with precision, power, and ease of implementation in mind. The ADS1013/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 ADS1013/4/5 operate from a single power supply ranging from 2.0V to 5.5V. The ADS1013/4/5 can perform conversions at rates up to 3300 samples per second (SPS). An onboard PGA is available on the ADS1014 and ADS1015 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 ADS1015 also features an input multiplexer (MUX) that provides two differential or four single-ended inputs. The ADS1013/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 ADS1013/4/5 are specified from -40 deg C to +125 deg C.

Tested with Adafriut ADS1015 board: https://www.adafruit.com/products/1083

ads1015.jpg
//Select the device you are testing here and adjust case 6 for the correct sample rates.
//upm::ADS1015 *ads = new upm::ADS1015(1);
upm::ADS1115 *ads = new upm::ADS1115(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 );
delete ads;
long id = 0; // Sample number
string fileName = "./ads1015.data"; // Output filename
ofstream f;
// Initialize and configure the ADS1015
ADS1015 *ads1015 = new upm::ADS1015(0, 0x48);
// Put the ADC into differential mode for pins A0 and A1
ads1015->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
ads1015->setGain(ADS1X15::GAIN_TWO);
// Set the sample rate to 3300 samples per second (max) and turn on continuous
// sampling
ads1015->setSPS(ADS1015::SPS_3300);
ads1015->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++ << " " << ads1015->getLastSample() << endl;
usleep(1000);
}
// Clean-up and exit
timer.join();
f.close();
delete ads1015;
} catch (ios_base::failure &e) {
cout << "Failed to write to file: " << e.what() << endl;
return 1;
}
cout << "Wrote " << id << " samples to file: " << fileName << endl;
// Simple example of using IADC to determine
// which sensor is present and return its name.
// IADC is then used to get readings from sensor
upm::IADC* getADC()
{
upm::IADC* adc = NULL;
try {
adc = new upm::ADS1015(EDISON_I2C_BUS);
mraa::Gpio gpio(EDISON_GPIO_SI7005_CS);
gpio.dir(mraa::DIR_OUT_HIGH);
return adc;
} catch (std::exception& e) {
std::cerr << "ADS1015: " << e.what() << std::endl;
}
return adc;
}
int main ()
{
upm::IADC* adc = getADC();
if (adc == NULL) {
std::cout << "ADC not detected" << std::endl;
return 1;
}
std::cout << "ADC " << adc->getModuleName() << " detected. " ;
std::cout << adc->getNumInputs() << " inputs available" << std::endl;
while (true) {
for (unsigned int i=0; i<adc->getNumInputs(); ++i) {
std::cout << "Input " << i;
try {
float voltage = adc->getVoltage(i);
std::cout << ": Voltage = " << voltage << "V" << std::endl;
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
}
}
sleep(1);
}
delete adc;
return 0;
}

Public Types

enum  ADSSAMPLERATE {
  SPS_128 = ADS1015_DR_128SPS, SPS_250 = ADS1015_DR_250SPS, SPS_490 = ADS1015_DR_490SPS, SPS_920 = ADS1015_DR_920SPS,
  SPS_1600 = ADS1015_DR_1600SPS, SPS_2400 = ADS1015_DR_2400SPS, SPS_3300 = ADS1015_DR_3300SPS
}
 uint16_t enum containing values representing the sample rate of the device.
 
typedef enum
upm::ADS1015::ADSSAMPLERATE 
ADSSAMPLERATE
 
- 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

 ADS1015 (int bus, uint8_t address=0x48, float vref=ADS1015_VREF)
 
 ~ADS1015 ()
 
void setSPS (ADSSAMPLERATE rate=SPS_1600)
 
unsigned int getNumInputs ()
 
uint16_t getRawValue (unsigned int input)
 
float getVoltage (unsigned int input)
 
unsigned int getResolutionInBits ()
 
const char * getModuleName ()
 
- 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

ADS1015 ( int  bus,
uint8_t  address = 0x48,
float  vref = ADS1015_VREF 
)

ADS1015 constructor

This constructor includes a vref parameter that can be used to set gain so it matches full scale value of input

Parameters
busi2c bus the sensor is attached to.
addressOptional device address. Default is 0x48.
vrefOptional reference (i.e. half full swing) voltage. Default is 2.048V

Here is the call graph for this function:

~ADS1015 ( )

ADS1X15 destructor

Member Function Documentation

void setSPS ( ADSSAMPLERATE  rate = SPS_1600)

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
unsigned int getNumInputs ( )
virtual

Get number of inputs

Returns
number of inputs

Implements IADC.

uint16_t getRawValue ( unsigned int  input)
virtual

Read current value for current single ended analogue input

Returns
current conversion value

Implements IADC.

float getVoltage ( unsigned int  input)
virtual

Read current voltage for current single ended analogue input

Returns
current voltage

Implements IADC.

Here is the call graph for this function:

unsigned int getResolutionInBits ( )
virtual

Read current voltage for current single ended analogue input

Returns
current voltage

Implements IADC.

const char * getModuleName ( )
virtual

Returns module name

Returns
modulename as const char*

Implements IModuleStatus.

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

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