1
1
#!/usr/bin/python
2
2
3
3
import time
4
+ import smbus
4
5
from Adafruit_I2C import Adafruit_I2C
5
6
6
7
# ===========================================================================
@@ -58,6 +59,15 @@ class ADS1x15:
58
59
__ADS1015_REG_CONFIG_DR_2400SPS = 0x00A0 # 2400 samples per second
59
60
__ADS1015_REG_CONFIG_DR_3300SPS = 0x00C0 # 3300 samples per second
60
61
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
+
61
71
__ADS1015_REG_CONFIG_CMODE_MASK = 0x0010
62
72
__ADS1015_REG_CONFIG_CMODE_TRAD = 0x0000 # Traditional comparator with hysteresis (default)
63
73
__ADS1015_REG_CONFIG_CMODE_WINDOW = 0x0010 # Window comparator
@@ -78,6 +88,11 @@ class ADS1x15:
78
88
79
89
# Constructor
80
90
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!
81
96
self .i2c = Adafruit_I2C (address )
82
97
self .address = address
83
98
self .debug = debug
@@ -98,16 +113,24 @@ def readADCSingleEnded(self, channel=0):
98
113
print "ADS1x15: Invalid channel specified: %d" % channel
99
114
return - 1
100
115
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
108
131
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
111
134
112
135
if channel == 3 :
113
136
config |= self .__ADS1015_REG_CONFIG_MUX_SINGLE_3
@@ -126,17 +149,25 @@ def readADCSingleEnded(self, channel=0):
126
149
self .i2c .writeList (self .__ADS1015_REG_POINTER_CONFIG , bytes )
127
150
128
151
# 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 )
130
156
131
157
# Read the conversion results
132
158
result = self .i2c .readList (self .__ADS1015_REG_POINTER_CONVERT , 2 )
133
159
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 )
136
162
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
+
140
171
def readADCDifferential01 (self ):
141
172
"Gets a differential ADC reading from channels 0 and 1"
142
173
@@ -148,4 +179,3 @@ def startSingleEndedComparator(self, channel, threshold):
148
179
149
180
def getLastConversionResults (self ):
150
181
"Returns the last ADC conversion result"
151
-
0 commit comments