Skip to content

Commit dbe2adc

Browse files
author
K. Townsend
committed
Added basic ADS1115 support
1 parent f6b151f commit dbe2adc

File tree

2 files changed

+69
-22
lines changed

2 files changed

+69
-22
lines changed

Adafruit_ADS1x15/Adafruit_ADS1x15.py

+46-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/python
22

33
import time
4+
import smbus
45
from Adafruit_I2C import Adafruit_I2C
56

67
# ===========================================================================
@@ -58,6 +59,15 @@ class ADS1x15:
5859
__ADS1015_REG_CONFIG_DR_2400SPS = 0x00A0 # 2400 samples per second
5960
__ADS1015_REG_CONFIG_DR_3300SPS = 0x00C0 # 3300 samples per second
6061

62+
__ADS1115_REG_CONFIG_DR_8SPS = 0x0000 # 8 samples per second
63+
__ADS1115_REG_CONFIG_DR_16SPS = 0x0020 # 16 samples per second
64+
__ADS1115_REG_CONFIG_DR_32SPS = 0x0040 # 32 samples per second
65+
__ADS1115_REG_CONFIG_DR_64SPS = 0x0060 # 64 samples per second
66+
__ADS1115_REG_CONFIG_DR_128SPS = 0x0080 # 128 samples per second
67+
__ADS1115_REG_CONFIG_DR_250SPS = 0x00A0 # 250 samples per second (default)
68+
__ADS1115_REG_CONFIG_DR_475SPS = 0x00C0 # 475 samples per second
69+
__ADS1115_REG_CONFIG_DR_860SPS = 0x00E0 # 860 samples per second
70+
6171
__ADS1015_REG_CONFIG_CMODE_MASK = 0x0010
6272
__ADS1015_REG_CONFIG_CMODE_TRAD = 0x0000 # Traditional comparator with hysteresis (default)
6373
__ADS1015_REG_CONFIG_CMODE_WINDOW = 0x0010 # Window comparator
@@ -78,6 +88,11 @@ class ADS1x15:
7888

7989
# Constructor
8090
def __init__(self, address=0x48, ic=__IC_ADS1015, debug=False):
91+
# Depending on if you have an old or a new Raspberry Pi, you
92+
# may need to change the I2C bus. Older Pis use SMBus 0,
93+
# whereas new Pis use SMBus 1. If you see an error like:
94+
# 'Error accessing 0x48: Check your I2C address '
95+
# change the SMBus number in the initializer below!
8196
self.i2c = Adafruit_I2C(address)
8297
self.address = address
8398
self.debug = debug
@@ -98,16 +113,24 @@ def readADCSingleEnded(self, channel=0):
98113
print "ADS1x15: Invalid channel specified: %d" % channel
99114
return -1
100115

101-
# Start with default values
102-
config = self.__ADS1015_REG_CONFIG_CQUE_NONE | \
103-
self.__ADS1015_REG_CONFIG_CLAT_NONLAT | \
104-
self.__ADS1015_REG_CONFIG_CPOL_ACTVLOW | \
105-
self.__ADS1015_REG_CONFIG_CMODE_TRAD | \
106-
self.__ADS1015_REG_CONFIG_DR_1600SPS | \
107-
self.__ADS1015_REG_CONFIG_MODE_SINGLE
116+
# Set default values
117+
if (self.ic == self.__IC_ADS1015):
118+
config = self.__ADS1015_REG_CONFIG_CQUE_NONE | \
119+
self.__ADS1015_REG_CONFIG_CLAT_NONLAT | \
120+
self.__ADS1015_REG_CONFIG_CPOL_ACTVLOW | \
121+
self.__ADS1015_REG_CONFIG_CMODE_TRAD | \
122+
self.__ADS1015_REG_CONFIG_DR_1600SPS | \
123+
self.__ADS1015_REG_CONFIG_MODE_SINGLE
124+
else:
125+
config = self.__ADS1015_REG_CONFIG_CQUE_NONE | \
126+
self.__ADS1015_REG_CONFIG_CLAT_NONLAT | \
127+
self.__ADS1015_REG_CONFIG_CPOL_ACTVLOW | \
128+
self.__ADS1015_REG_CONFIG_CMODE_TRAD | \
129+
self.__ADS1115_REG_CONFIG_DR_250SPS | \
130+
self.__ADS1015_REG_CONFIG_MODE_SINGLE
108131

109-
# Set PGA/voltage range (1 bit = 3mV using the default range)
110-
config |= self.__ADS1015_REG_CONFIG_PGA_6_144V # +/- 6.144V range
132+
# Set PGA/voltage range to max value (+/- 6.144V)
133+
config |= self.__ADS1015_REG_CONFIG_PGA_6_144V
111134

112135
if channel == 3:
113136
config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_3
@@ -126,17 +149,25 @@ def readADCSingleEnded(self, channel=0):
126149
self.i2c.writeList(self.__ADS1015_REG_POINTER_CONFIG, bytes)
127150

128151
# Wait for the ADC conversion to complete
129-
time.sleep(0.001)
152+
if (self.ic == self.__IC_ADS1015):
153+
time.sleep(0.001)
154+
else:
155+
time.sleep(0.008)
130156

131157
# Read the conversion results
132158
result = self.i2c.readList(self.__ADS1015_REG_POINTER_CONVERT, 2)
133159
if (self.ic == self.__IC_ADS1015):
134-
# Shift right 4 bits for the 12-bit ADS1015
135-
return ( ((result[0] << 8) | (result[1] & 0xFF)) >> 4 )
160+
# Shift right 4 bits for the 12-bit ADS1015
161+
return ( ((result[0] << 8) | (result[1] & 0xFF)) >> 4 )
136162
else:
137-
# Return 16-bit value for the ADS1115
138-
return ( (result[0] << 8) | (result[1] & 0xFF) )
139-
163+
# Return 16-bit value for the ADS1115
164+
# (Take signed values into account as well)
165+
val = (result[0] << 8) | (result[1])
166+
if val > 0x7FFF:
167+
return val - 0xFFFF
168+
else:
169+
return (result[0] << 8) | (result[1])
170+
140171
def readADCDifferential01(self):
141172
"Gets a differential ADC reading from channels 0 and 1"
142173

@@ -148,4 +179,3 @@ def startSingleEndedComparator(self, channel, threshold):
148179

149180
def getLastConversionResults(self):
150181
"Returns the last ADC conversion result"
151-

Adafruit_ADS1x15/ads1015_example.py

+23-6
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,31 @@
55
# ============================================================================
66
# Example Code
77
# ============================================================================
8+
ADS1015 = 0x00 # 12-bit ADC
9+
ADS1115 = 0x01 # 16-bit ADC
810

9-
# Initialise the ADC using the default mode (IC = ADS1015, default address)
10-
adc = ADS1x15()
11+
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
# ToDo: Change the value below depending on which chip you're using!
13+
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
14+
ADS_Current = ADS1115
1115

12-
# Read channel 0 and 1 in single-ended mode (1 bit = 3mV)
16+
# Initialise the ADC using the default mode (use default I2C address)
17+
adc = ADS1x15(ic=ADS_Current)
18+
19+
# Read channel 0 in single-ended mode
1320
result = adc.readADCSingleEnded(0)
14-
print "Channel 0 = %.3f V" % ((result * 3.0) / 1000.0)
21+
if ADS_Current == ADS1015:
22+
# For ADS1015 at max range (+/-6.144V) 1 bit = 3mV (12-bit values)
23+
print "Channel 0 = %.3f V" % (result * 0.003)
24+
else:
25+
# For ADS1115 at max range (+/-6.144V) 1-bit = 0.1875mV (16-bit values)
26+
print "Channel 0 = %.3f V" % (result * 0.0001875)
1527

28+
# Read channel 1 in single-ended mode
1629
result = adc.readADCSingleEnded(1)
17-
print "Channel 1 = %.3f V" % ((result * 3.0) / 1000.0)
18-
30+
if ADS_Current == ADS1015:
31+
# For ADS1015 at max range (+/-6.144V) 1 bit = 3mV (12-bit values)
32+
print "Channel 1 = %.3f V" % (result * 0.003)
33+
else:
34+
# For ADS1115 at max range (+/-6.144V) 1-bit = 0.1875mV (16-bit values)
35+
print "Channel 1 = %.3f V" % (result * 0.0001875)

0 commit comments

Comments
 (0)