Skip to content

Commit 2a79680

Browse files
committed
add volts and other tweaks
1 parent d0e1f3c commit 2a79680

File tree

5 files changed

+62
-37
lines changed

5 files changed

+62
-37
lines changed

Adafruit_ADS1X15.cpp

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ uint16_t Adafruit_ADS1X15::getDataRate() { return m_dataRate; }
110110
@return the ADC reading
111111
*/
112112
/**************************************************************************/
113-
uint16_t Adafruit_ADS1X15::readADC_SingleEnded(uint8_t channel) {
113+
int16_t Adafruit_ADS1X15::readADC_SingleEnded(uint8_t channel) {
114114
if (channel > 3) {
115115
return 0;
116116
}
@@ -156,8 +156,7 @@ uint16_t Adafruit_ADS1X15::readADC_SingleEnded(uint8_t channel) {
156156
;
157157

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

163162
/**************************************************************************/
@@ -199,18 +198,7 @@ int16_t Adafruit_ADS1X15::readADC_Differential_0_1() {
199198
;
200199

201200
// Read the conversion results
202-
uint16_t res = readRegister(ADS1X15_REG_POINTER_CONVERT) >> m_bitShift;
203-
if (m_bitShift == 0) {
204-
return (int16_t)res;
205-
} else {
206-
// Shift 12-bit results right 4 bits for the ADS1015,
207-
// making sure we keep the sign bit intact
208-
if (res > 0x07FF) {
209-
// negative number - extend the sign to 16th bit
210-
res |= 0xF000;
211-
}
212-
return (int16_t)res;
213-
}
201+
return getLastConversionResults();
214202
}
215203

216204
/**************************************************************************/
@@ -252,18 +240,7 @@ int16_t Adafruit_ADS1X15::readADC_Differential_2_3() {
252240
;
253241

254242
// Read the conversion results
255-
uint16_t res = readRegister(ADS1X15_REG_POINTER_CONVERT) >> m_bitShift;
256-
if (m_bitShift == 0) {
257-
return (int16_t)res;
258-
} else {
259-
// Shift 12-bit results right 4 bits for the ADS1015,
260-
// making sure we keep the sign bit intact
261-
if (res > 0x07FF) {
262-
// negative number - extend the sign to 16th bit
263-
res |= 0xF000;
264-
}
265-
return (int16_t)res;
266-
}
243+
return getLastConversionResults();
267244
}
268245

269246
/**************************************************************************/
@@ -345,6 +322,43 @@ int16_t Adafruit_ADS1X15::getLastConversionResults() {
345322
}
346323
}
347324

325+
/**************************************************************************/
326+
/*!
327+
@brief Returns true if conversion is complete, false otherwise.
328+
329+
@param counts the ADC reading in raw counts
330+
331+
@return the ADC reading in volts
332+
*/
333+
/**************************************************************************/
334+
float Adafruit_ADS1X15::computeVolts(int16_t counts) {
335+
// see data sheet Table 3
336+
float fsRange;
337+
switch (m_gain) {
338+
case GAIN_TWOTHIRDS:
339+
fsRange = 6.144f;
340+
break;
341+
case GAIN_ONE:
342+
fsRange = 4.096f;
343+
break;
344+
case GAIN_TWO:
345+
fsRange = 2.048f;
346+
break;
347+
case GAIN_FOUR:
348+
fsRange = 1.024f;
349+
break;
350+
case GAIN_EIGHT:
351+
fsRange = 0.512f;
352+
break;
353+
case GAIN_SIXTEEN:
354+
fsRange = 0.256f;
355+
break;
356+
default:
357+
fsRange = 0.0f;
358+
}
359+
return counts * (fsRange / (32768 >> m_bitShift));
360+
}
361+
348362
/**************************************************************************/
349363
/*!
350364
@brief Returns true if conversion is complete, false otherwise.

Adafruit_ADS1X15.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,12 @@ class Adafruit_ADS1X15 {
148148

149149
public:
150150
void begin(uint8_t i2c_addr = ADS1X15_ADDRESS, TwoWire *wire = &Wire);
151-
uint16_t readADC_SingleEnded(uint8_t channel);
151+
int16_t readADC_SingleEnded(uint8_t channel);
152152
int16_t readADC_Differential_0_1();
153153
int16_t readADC_Differential_2_3();
154154
void startComparator_SingleEnded(uint8_t channel, int16_t threshold);
155155
int16_t getLastConversionResults();
156+
float computeVolts(int16_t counts);
156157
void setGain(adsGain_t gain);
157158
adsGain_t getGain();
158159
void setDataRate(uint16_t rate);

examples/singleended/singleended.ino

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <Adafruit_ADS1X15.h>
22

3-
// Adafruit_ADS1115 ads; /* Use this for the 16-bit version */
3+
//Adafruit_ADS1115 ads; /* Use this for the 16-bit version */
44
Adafruit_ADS1015 ads; /* Use this for the 12-bit version */
55

66
void setup(void)
@@ -30,16 +30,23 @@ void setup(void)
3030
void loop(void)
3131
{
3232
int16_t adc0, adc1, adc2, adc3;
33+
float volts0, volts1, volts2, volts3;
3334

3435
adc0 = ads.readADC_SingleEnded(0);
3536
adc1 = ads.readADC_SingleEnded(1);
3637
adc2 = ads.readADC_SingleEnded(2);
3738
adc3 = ads.readADC_SingleEnded(3);
38-
Serial.print("AIN0: "); Serial.println(adc0);
39-
Serial.print("AIN1: "); Serial.println(adc1);
40-
Serial.print("AIN2: "); Serial.println(adc2);
41-
Serial.print("AIN3: "); Serial.println(adc3);
42-
Serial.println(" ");
43-
39+
40+
volts0 = ads.computeVolts(adc0);
41+
volts1 = ads.computeVolts(adc1);
42+
volts2 = ads.computeVolts(adc2);
43+
volts3 = ads.computeVolts(adc3);
44+
45+
Serial.println("-----------------------------------------------------------");
46+
Serial.print("AIN0: "); Serial.print(adc0); Serial.print(" "); Serial.print(volts0); Serial.println("V");
47+
Serial.print("AIN1: "); Serial.print(adc1); Serial.print(" "); Serial.print(volts1); Serial.println("V");
48+
Serial.print("AIN2: "); Serial.print(adc2); Serial.print(" "); Serial.print(volts2); Serial.println("V");
49+
Serial.print("AIN3: "); Serial.print(adc3); Serial.print(" "); Serial.print(volts3); Serial.println("V");
50+
4451
delay(1000);
4552
}

keywords.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ readADC_Differential_0_1 KEYWORD2
66
readADC_Differential_2_3 KEYWORD2
77
startComparator_SingleEnded KEYWORD2
88
getLastConversionResults KEYWORD2
9+
computeVolts KEYWORD2
910
setGain KEYWORD2
10-
getGain KEYWORD2
11+
getGain KEYWORD2
12+
setDataRate KEYWORD2
13+
getDataRate KEYWORD2

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Adafruit ADS1X15
2-
version=2.0.1
2+
version=2.1.0
33
author=Adafruit
44
maintainer=Adafruit <info@adafruit.com>
55
sentence=Arduino library for ADS1015/1115 ADCs.

0 commit comments

Comments
 (0)