Skip to content

Commit

Permalink
Merge pull request #61 from caternuson/volts
Browse files Browse the repository at this point in the history
Add volts and other tweaks
  • Loading branch information
caternuson authored Mar 26, 2021
2 parents d0e1f3c + 2a79680 commit 2822d1e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 37 deletions.
68 changes: 41 additions & 27 deletions Adafruit_ADS1X15.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ uint16_t Adafruit_ADS1X15::getDataRate() { return m_dataRate; }
@return the ADC reading
*/
/**************************************************************************/
uint16_t Adafruit_ADS1X15::readADC_SingleEnded(uint8_t channel) {
int16_t Adafruit_ADS1X15::readADC_SingleEnded(uint8_t channel) {
if (channel > 3) {
return 0;
}
Expand Down Expand Up @@ -156,8 +156,7 @@ uint16_t Adafruit_ADS1X15::readADC_SingleEnded(uint8_t channel) {
;

// Read the conversion results
// Shift 12-bit results right 4 bits for the ADS1015
return readRegister(ADS1X15_REG_POINTER_CONVERT) >> m_bitShift;
return getLastConversionResults();
}

/**************************************************************************/
Expand Down Expand Up @@ -199,18 +198,7 @@ int16_t Adafruit_ADS1X15::readADC_Differential_0_1() {
;

// Read the conversion results
uint16_t res = readRegister(ADS1X15_REG_POINTER_CONVERT) >> m_bitShift;
if (m_bitShift == 0) {
return (int16_t)res;
} else {
// Shift 12-bit results right 4 bits for the ADS1015,
// making sure we keep the sign bit intact
if (res > 0x07FF) {
// negative number - extend the sign to 16th bit
res |= 0xF000;
}
return (int16_t)res;
}
return getLastConversionResults();
}

/**************************************************************************/
Expand Down Expand Up @@ -252,18 +240,7 @@ int16_t Adafruit_ADS1X15::readADC_Differential_2_3() {
;

// Read the conversion results
uint16_t res = readRegister(ADS1X15_REG_POINTER_CONVERT) >> m_bitShift;
if (m_bitShift == 0) {
return (int16_t)res;
} else {
// Shift 12-bit results right 4 bits for the ADS1015,
// making sure we keep the sign bit intact
if (res > 0x07FF) {
// negative number - extend the sign to 16th bit
res |= 0xF000;
}
return (int16_t)res;
}
return getLastConversionResults();
}

/**************************************************************************/
Expand Down Expand Up @@ -345,6 +322,43 @@ int16_t Adafruit_ADS1X15::getLastConversionResults() {
}
}

/**************************************************************************/
/*!
@brief Returns true if conversion is complete, false otherwise.
@param counts the ADC reading in raw counts
@return the ADC reading in volts
*/
/**************************************************************************/
float Adafruit_ADS1X15::computeVolts(int16_t counts) {
// see data sheet Table 3
float fsRange;
switch (m_gain) {
case GAIN_TWOTHIRDS:
fsRange = 6.144f;
break;
case GAIN_ONE:
fsRange = 4.096f;
break;
case GAIN_TWO:
fsRange = 2.048f;
break;
case GAIN_FOUR:
fsRange = 1.024f;
break;
case GAIN_EIGHT:
fsRange = 0.512f;
break;
case GAIN_SIXTEEN:
fsRange = 0.256f;
break;
default:
fsRange = 0.0f;
}
return counts * (fsRange / (32768 >> m_bitShift));
}

/**************************************************************************/
/*!
@brief Returns true if conversion is complete, false otherwise.
Expand Down
3 changes: 2 additions & 1 deletion Adafruit_ADS1X15.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,12 @@ class Adafruit_ADS1X15 {

public:
void begin(uint8_t i2c_addr = ADS1X15_ADDRESS, TwoWire *wire = &Wire);
uint16_t readADC_SingleEnded(uint8_t channel);
int16_t readADC_SingleEnded(uint8_t channel);
int16_t readADC_Differential_0_1();
int16_t readADC_Differential_2_3();
void startComparator_SingleEnded(uint8_t channel, int16_t threshold);
int16_t getLastConversionResults();
float computeVolts(int16_t counts);
void setGain(adsGain_t gain);
adsGain_t getGain();
void setDataRate(uint16_t rate);
Expand Down
21 changes: 14 additions & 7 deletions examples/singleended/singleended.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <Adafruit_ADS1X15.h>

// Adafruit_ADS1115 ads; /* Use this for the 16-bit version */
//Adafruit_ADS1115 ads; /* Use this for the 16-bit version */
Adafruit_ADS1015 ads; /* Use this for the 12-bit version */

void setup(void)
Expand Down Expand Up @@ -30,16 +30,23 @@ void setup(void)
void loop(void)
{
int16_t adc0, adc1, adc2, adc3;
float volts0, volts1, volts2, volts3;

adc0 = ads.readADC_SingleEnded(0);
adc1 = ads.readADC_SingleEnded(1);
adc2 = ads.readADC_SingleEnded(2);
adc3 = ads.readADC_SingleEnded(3);
Serial.print("AIN0: "); Serial.println(adc0);
Serial.print("AIN1: "); Serial.println(adc1);
Serial.print("AIN2: "); Serial.println(adc2);
Serial.print("AIN3: "); Serial.println(adc3);
Serial.println(" ");


volts0 = ads.computeVolts(adc0);
volts1 = ads.computeVolts(adc1);
volts2 = ads.computeVolts(adc2);
volts3 = ads.computeVolts(adc3);

Serial.println("-----------------------------------------------------------");
Serial.print("AIN0: "); Serial.print(adc0); Serial.print(" "); Serial.print(volts0); Serial.println("V");
Serial.print("AIN1: "); Serial.print(adc1); Serial.print(" "); Serial.print(volts1); Serial.println("V");
Serial.print("AIN2: "); Serial.print(adc2); Serial.print(" "); Serial.print(volts2); Serial.println("V");
Serial.print("AIN3: "); Serial.print(adc3); Serial.print(" "); Serial.print(volts3); Serial.println("V");

delay(1000);
}
5 changes: 4 additions & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ readADC_Differential_0_1 KEYWORD2
readADC_Differential_2_3 KEYWORD2
startComparator_SingleEnded KEYWORD2
getLastConversionResults KEYWORD2
computeVolts KEYWORD2
setGain KEYWORD2
getGain KEYWORD2
getGain KEYWORD2
setDataRate KEYWORD2
getDataRate KEYWORD2
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Adafruit ADS1X15
version=2.0.1
version=2.1.0
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=Arduino library for ADS1015/1115 ADCs.
Expand Down

0 comments on commit 2822d1e

Please sign in to comment.