Difference between revisions of "ADS1115 16-Bit ADC Module SKU: CQRADS1115"
(→Connections and Examples) |
(→Connections and Examples) |
||
Line 23: | Line 23: | ||
=='''Connections and Examples'''== | =='''Connections and Examples'''== | ||
[[image:ADC Module-3.jpg|thumb|1000px| center]] | [[image:ADC Module-3.jpg|thumb|1000px| center]] | ||
+ | ---- | ||
+ | =='''Specification'''== | ||
+ | '''Sensor Specifications''' | ||
+ | *Supply Voltage(VCC): 3.3V to 5.0V | ||
+ | *Analog Signal Detection Range: 0 to VCC | ||
+ | *Number of Analog Channels: 4 | ||
+ | *ADC Bits: 16 Bit | ||
+ | *Operating Current: 2mA to 3mA (Not include sensor module) | ||
+ | *Interface Type: Ocean I2C | ||
+ | *Interface Level: High 3.3V, Low 0V | ||
+ | *Product Size: 32mm * 32mm(1.26in * 1.26in) | ||
+ | '''Ocean Interface Wire Specifications''' | ||
+ | *Cable specifications: 22AWG | ||
+ | *Material: Silicone | ||
+ | *Withstand Voltage: Less Than 50V | ||
+ | *Withstand Current: Less Than 1000MA | ||
+ | *Length: 21cm | ||
+ | *Line Sequence: Black-Negative Power Supply, Red-Positive Power Supply, Green-SDA, Blue-SCL. | ||
+ | |||
+ | =='''Test Code for Arduino'''== | ||
+ | |||
+ | Please down the [[Media: ADS1115_Library.zip|ADS1115 Library]] and install it. | ||
+ | |||
+ | Description of the I2C Address in this Library: | ||
+ | |||
+ | ADS1115_IIC_ADDRESS0:0x48 | ||
+ | |||
+ | ADS1115_IIC_ADDRESS1:0x49 | ||
+ | |||
+ | <pre> | ||
+ | /* | ||
+ | * file ADS1115_ReadVoltage.ino | ||
+ | * | ||
+ | * | ||
+ | * connect ADS1115 I2C interface with your board (please reference board compatibility) | ||
+ | * | ||
+ | * The voltage value read by A0 A1 A2 A3 is printed through the serial port. | ||
+ | * | ||
+ | */ | ||
+ | #include <Wire.h> | ||
+ | #include <ADS1115.h> | ||
+ | |||
+ | ADS1115 ads; | ||
+ | |||
+ | void setup(void) | ||
+ | { | ||
+ | Serial.begin(115200); | ||
+ | ads.setAddr_ADS1115(ADS1115_IIC_ADDRESS0); // 0x48 | ||
+ | ads.setGain(eGAIN_TWOTHIRDS); // 2/3x gain | ||
+ | ads.setMode(eMODE_SINGLE); // single-shot mode | ||
+ | ads.setRate(eRATE_128); // 128SPS (default) | ||
+ | ads.setOSMode(eOSMODE_SINGLE); // Set to start a single-conversion | ||
+ | ads.init(); | ||
+ | } | ||
+ | |||
+ | void loop(void) | ||
+ | { | ||
+ | if (ads.checkADS1115()) | ||
+ | { | ||
+ | int16_t adc0, adc1, adc2, adc3; | ||
+ | adc0 = ads.readVoltage(0); | ||
+ | Serial.print("A0:"); | ||
+ | Serial.print(adc0); | ||
+ | Serial.print("mV, "); | ||
+ | adc1 = ads.readVoltage(1); | ||
+ | Serial.print("A1:"); | ||
+ | Serial.print(adc1); | ||
+ | Serial.print("mV, "); | ||
+ | adc2 = ads.readVoltage(2); | ||
+ | Serial.print("A2:"); | ||
+ | Serial.print(adc2); | ||
+ | Serial.print("mV, "); | ||
+ | adc3 = ads.readVoltage(3); | ||
+ | Serial.print("A3:"); | ||
+ | Serial.print(adc3); | ||
+ | Serial.println("mV"); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | Serial.println("ADS1115 Disconnected!"); | ||
+ | } | ||
+ | |||
+ | delay(1000); | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | =='''Test Code for Raspberry Pi'''== | ||
+ | File: '''CQRobot_ADS1115.py''' | ||
+ | |||
+ | ---- | ||
+ | <pre> | ||
+ | import smbus | ||
+ | import time | ||
+ | |||
+ | # Get I2C bus | ||
+ | bus = smbus.SMBus(1) | ||
+ | |||
+ | # I2C address of the device | ||
+ | ADS1115_IIC_ADDRESS0 = 0x48 | ||
+ | ADS1115_IIC_ADDRESS1 = 0x49 | ||
+ | |||
+ | # ADS1115 Register Map | ||
+ | ADS1115_REG_POINTER_CONVERT = 0x00 # Conversion register | ||
+ | ADS1115_REG_POINTER_CONFIG = 0x01 # Configuration register | ||
+ | ADS1115_REG_POINTER_LOWTHRESH = 0x02 # Lo_thresh register | ||
+ | ADS1115_REG_POINTER_HITHRESH = 0x03 # Hi_thresh register | ||
+ | |||
+ | # ADS1115 Configuration Register | ||
+ | ADS1115_REG_CONFIG_OS_NOEFFECT = 0x00 # No effect | ||
+ | ADS1115_REG_CONFIG_OS_SINGLE = 0x80 # Begin a single conversion | ||
+ | ADS1115_REG_CONFIG_MUX_DIFF_0_1 = 0x00 # Differential P = AIN0, N = AIN1 (default) | ||
+ | ADS1115_REG_CONFIG_MUX_DIFF_0_3 = 0x10 # Differential P = AIN0, N = AIN3 | ||
+ | ADS1115_REG_CONFIG_MUX_DIFF_1_3 = 0x20 # Differential P = AIN1, N = AIN3 | ||
+ | ADS1115_REG_CONFIG_MUX_DIFF_2_3 = 0x30 # Differential P = AIN2, N = AIN3 | ||
+ | ADS1115_REG_CONFIG_MUX_SINGLE_0 = 0x40 # Single-ended P = AIN0, N = GND | ||
+ | ADS1115_REG_CONFIG_MUX_SINGLE_1 = 0x50 # Single-ended P = AIN1, N = GND | ||
+ | ADS1115_REG_CONFIG_MUX_SINGLE_2 = 0x60 # Single-ended P = AIN2, N = GND | ||
+ | ADS1115_REG_CONFIG_MUX_SINGLE_3 = 0x70 # Single-ended P = AIN3, N = GND | ||
+ | ADS1115_REG_CONFIG_PGA_6_144V = 0x00 # +/-6.144V range = Gain 2/3 | ||
+ | ADS1115_REG_CONFIG_PGA_4_096V = 0x02 # +/-4.096V range = Gain 1 | ||
+ | ADS1115_REG_CONFIG_PGA_2_048V = 0x04 # +/-2.048V range = Gain 2 (default) | ||
+ | ADS1115_REG_CONFIG_PGA_1_024V = 0x06 # +/-1.024V range = Gain 4 | ||
+ | ADS1115_REG_CONFIG_PGA_0_512V = 0x08 # +/-0.512V range = Gain 8 | ||
+ | ADS1115_REG_CONFIG_PGA_0_256V = 0x0A # +/-0.256V range = Gain 16 | ||
+ | ADS1115_REG_CONFIG_MODE_CONTIN = 0x00 # Continuous conversion mode | ||
+ | ADS1115_REG_CONFIG_MODE_SINGLE = 0x01 # Power-down single-shot mode (default) | ||
+ | ADS1115_REG_CONFIG_DR_8SPS = 0x00 # 8 samples per second | ||
+ | ADS1115_REG_CONFIG_DR_16SPS = 0x20 # 16 samples per second | ||
+ | ADS1115_REG_CONFIG_DR_32SPS = 0x40 # 32 samples per second | ||
+ | ADS1115_REG_CONFIG_DR_64SPS = 0x60 # 64 samples per second | ||
+ | ADS1115_REG_CONFIG_DR_128SPS = 0x80 # 128 samples per second (default) | ||
+ | ADS1115_REG_CONFIG_DR_250SPS = 0xA0 # 250 samples per second | ||
+ | ADS1115_REG_CONFIG_DR_475SPS = 0xC0 # 475 samples per second | ||
+ | ADS1115_REG_CONFIG_DR_860SPS = 0xE0 # 860 samples per second | ||
+ | ADS1115_REG_CONFIG_CMODE_TRAD = 0x00 # Traditional comparator with hysteresis (default) | ||
+ | ADS1115_REG_CONFIG_CMODE_WINDOW = 0x10 # Window comparator | ||
+ | ADS1115_REG_CONFIG_CPOL_ACTVLOW = 0x00 # ALERT/RDY pin is low when active (default) | ||
+ | ADS1115_REG_CONFIG_CPOL_ACTVHI = 0x08 # ALERT/RDY pin is high when active | ||
+ | ADS1115_REG_CONFIG_CLAT_NONLAT = 0x00 # Non-latching comparator (default) | ||
+ | ADS1115_REG_CONFIG_CLAT_LATCH = 0x04 # Latching comparator | ||
+ | ADS1115_REG_CONFIG_CQUE_1CONV = 0x00 # Assert ALERT/RDY after one conversions | ||
+ | ADS1115_REG_CONFIG_CQUE_2CONV = 0x01 # Assert ALERT/RDY after two conversions | ||
+ | ADS1115_REG_CONFIG_CQUE_4CONV = 0x02 # Assert ALERT/RDY after four conversions | ||
+ | ADS1115_REG_CONFIG_CQUE_NONE = 0x03 # Disable the comparator and put ALERT/RDY in high state (default) | ||
+ | |||
+ | mygain=0x02 | ||
+ | coefficient=0.125 | ||
+ | addr_G=ADS1115_IIC_ADDRESS0 | ||
+ | class ADS1115(): | ||
+ | def setGain(self,gain): | ||
+ | global mygain | ||
+ | global coefficient | ||
+ | mygain=gain | ||
+ | if mygain == ADS1115_REG_CONFIG_PGA_6_144V: | ||
+ | coefficient = 0.1875 | ||
+ | elif mygain == ADS1115_REG_CONFIG_PGA_4_096V: | ||
+ | coefficient = 0.125 | ||
+ | elif mygain == ADS1115_REG_CONFIG_PGA_2_048V: | ||
+ | coefficient = 0.0625 | ||
+ | elif mygain == ADS1115_REG_CONFIG_PGA_1_024V: | ||
+ | coefficient = 0.03125 | ||
+ | elif mygain == ADS1115_REG_CONFIG_PGA_0_512V: | ||
+ | coefficient = 0.015625 | ||
+ | elif mygain == ADS1115_REG_CONFIG_PGA_0_256V: | ||
+ | coefficient = 0.0078125 | ||
+ | else: | ||
+ | coefficient = 0.125 | ||
+ | def setAddr_ADS1115(self,addr): | ||
+ | global addr_G | ||
+ | addr_G=addr | ||
+ | def setChannel(self,channel): | ||
+ | global mygain | ||
+ | """Select the Channel user want to use from 0-3 | ||
+ | For Single-ended Output | ||
+ | 0 : AINP = AIN0 and AINN = GND | ||
+ | 1 : AINP = AIN1 and AINN = GND | ||
+ | 2 : AINP = AIN2 and AINN = GND | ||
+ | 3 : AINP = AIN3 and AINN = GND | ||
+ | For Differential Output | ||
+ | 0 : AINP = AIN0 and AINN = AIN1 | ||
+ | 1 : AINP = AIN0 and AINN = AIN3 | ||
+ | 2 : AINP = AIN1 and AINN = AIN3 | ||
+ | 3 : AINP = AIN2 and AINN = AIN3""" | ||
+ | self.channel = channel | ||
+ | while self.channel > 3 : | ||
+ | self.channel = 0 | ||
+ | |||
+ | return self.channel | ||
+ | |||
+ | def setSingle(self): | ||
+ | global addr_G | ||
+ | if self.channel == 0: | ||
+ | CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_SINGLE_0 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE] | ||
+ | elif self.channel == 1: | ||
+ | CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_SINGLE_1 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE] | ||
+ | elif self.channel == 2: | ||
+ | CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_SINGLE_2 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE] | ||
+ | elif self.channel == 3: | ||
+ | CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_SINGLE_3 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE] | ||
+ | |||
+ | bus.write_i2c_block_data(addr_G, ADS1115_REG_POINTER_CONFIG, CONFIG_REG) | ||
+ | |||
+ | def setDifferential(self): | ||
+ | global addr_G | ||
+ | if self.channel == 0: | ||
+ | CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_DIFF_0_1 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE] | ||
+ | elif self.channel == 1: | ||
+ | CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_DIFF_0_3 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE] | ||
+ | elif self.channel == 2: | ||
+ | CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_DIFF_1_3 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE] | ||
+ | elif self.channel == 3: | ||
+ | CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_DIFF_2_3 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE] | ||
+ | |||
+ | bus.write_i2c_block_data(addr_G, ADS1115_REG_POINTER_CONFIG, CONFIG_REG) | ||
+ | |||
+ | def readValue(self): | ||
+ | """Read data back from ADS1115_REG_POINTER_CONVERT(0x00), 2 bytes | ||
+ | raw_adc MSB, raw_adc LSB""" | ||
+ | global coefficient | ||
+ | global addr_G | ||
+ | data = bus.read_i2c_block_data(addr_G, ADS1115_REG_POINTER_CONVERT, 2) | ||
+ | |||
+ | # Convert the data | ||
+ | raw_adc = data[0] * 256 + data[1] | ||
+ | |||
+ | if raw_adc > 32767: | ||
+ | raw_adc -= 65535 | ||
+ | raw_adc = int(float(raw_adc)*coefficient) | ||
+ | return {'r' : raw_adc} | ||
+ | |||
+ | def readVoltage(self,channel): | ||
+ | self.setChannel(channel) | ||
+ | self.setSingle() | ||
+ | time.sleep(0.1) | ||
+ | return self.readValue() | ||
+ | |||
+ | def ComparatorVoltage(self,channel): | ||
+ | self.setChannel(channel) | ||
+ | self.setDifferential() | ||
+ | time.sleep(0.1) | ||
+ | return self.readValue() | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | |||
+ | File: '''ADS1115_ReadVoltage.py''' | ||
+ | |||
+ | ---- | ||
+ | |||
+ | <pre> | ||
+ | import sys | ||
+ | sys.path.append('../') | ||
+ | import time | ||
+ | from CQRobot_ADS1115 import ADS1115 | ||
+ | ADS1115_REG_CONFIG_PGA_6_144V = 0x00 # 6.144V range = Gain 2/3 | ||
+ | ADS1115_REG_CONFIG_PGA_4_096V = 0x02 # 4.096V range = Gain 1 | ||
+ | ADS1115_REG_CONFIG_PGA_2_048V = 0x04 # 2.048V range = Gain 2 (default) | ||
+ | ADS1115_REG_CONFIG_PGA_1_024V = 0x06 # 1.024V range = Gain 4 | ||
+ | ADS1115_REG_CONFIG_PGA_0_512V = 0x08 # 0.512V range = Gain 8 | ||
+ | ADS1115_REG_CONFIG_PGA_0_256V = 0x0A # 0.256V range = Gain 16 | ||
+ | ads1115 = ADS1115() | ||
+ | |||
+ | while True : | ||
+ | #Set the IIC address | ||
+ | ads1115.setAddr_ADS1115(0x48) | ||
+ | #Sets the gain and input voltage range. | ||
+ | ads1115.setGain(ADS1115_REG_CONFIG_PGA_6_144V) | ||
+ | #Get the Digital Value of Analog of selected channel | ||
+ | adc1 = ads1115.readVoltage(1) | ||
+ | time.sleep(0.2) | ||
+ | print(" A1:%dmV "%(adc1['r'])) | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | '''Run the code''' | ||
+ | |||
+ | Note: Make sure that the I2C address from its default (0x48); | ||
+ | |||
+ | Note: Enable "Interfacing Options - P5 I2C" by typing the below command in the terminal: | ||
+ | |||
+ | <pre> | ||
+ | sudo raspi-config | ||
+ | </pre> | ||
+ | |||
+ | Run the code: | ||
+ | <pre> | ||
+ | cd TS1728 | ||
+ | ls | ||
+ | sudo python3 CQRobot_ADS1115.py | ||
+ | cd ADS1115_ReadVoltage | ||
+ | ls | ||
+ | sudo python3 ADS1115_ReadVoltage.py | ||
+ | </pre> | ||
+ | |||
+ | [[File:ADS1115_cmd.png]] | ||
---- | ---- |
Latest revision as of 13:09, 22 June 2021
Contents
Description
The ADS1115 16-bit AD conversion module can accurately collect and convert analog signals. Through this AD module, the Raspberry Pi main control board can easily use a variety of analog Arduino series sensors to measure various signals. Perceive this world.
Raspberry Pi is a common main control board with powerful functions. However, the pins of the Raspberry Pi main control board can only read digital signals. If you need to read the signals of analog sensors or devices, you can only connect an analog-to-digital conversion (ADC) module, which is not convenient to use.
Ocean: ADS1115 16-bit AD conversion module is optimized for Raspberry Pi and Arduino series analog sensors, which greatly reduces the difficulty of using Arduino series analog sensors on the Raspberry Pi main control board. It is plug and play, no soldering, and very convenient. . The Arduino series of sensors are numerous and rich in types, which can meet the needs of the Raspberry Pi main control board for various sensors.
This module uses the 16-bit ADC chip ADS1115, which supports a wide voltage supply from 3.3V to 5V. The chip has a precision reference voltage and programmable gain adjustment (PGA), so it can be used for weak signals or signals with large changes. Accurate acquisition and conversion, so this module is also suitable for all kinds of occasions where the main control board needs to accurately collect analog signals.
One AD module can read 4 channels of analog signals. Due to the onboard I2C address selection switch, it supports the cascade of 2 AD modules and reads 8 channels of analog signals, which is sufficient to meet the needs of various scenarios .
Features
- Support 3.3V to 5.0V Wide Supply Voltage, 3.3V I2C digital level signal, optimized for Raspberry Pi.
- Ocean I2C Interface, plug and play, 22AWG environmentally friendly high temperature resistant silicone wire, JST 4-Pin to DuPont female single head, wire length 210mm.
- On-board I2C address selection switch, supports cascade of two ADC modules.
- Color coded 3pin headers, plug and play with Arduino analog sensors.
- Comes with development resources and manual (examples for Raspberry Pi/Arduino)
Interface Description and Size
Connections and Examples
Specification
Sensor Specifications
- Supply Voltage(VCC): 3.3V to 5.0V
- Analog Signal Detection Range: 0 to VCC
- Number of Analog Channels: 4
- ADC Bits: 16 Bit
- Operating Current: 2mA to 3mA (Not include sensor module)
- Interface Type: Ocean I2C
- Interface Level: High 3.3V, Low 0V
- Product Size: 32mm * 32mm(1.26in * 1.26in)
Ocean Interface Wire Specifications
- Cable specifications: 22AWG
- Material: Silicone
- Withstand Voltage: Less Than 50V
- Withstand Current: Less Than 1000MA
- Length: 21cm
- Line Sequence: Black-Negative Power Supply, Red-Positive Power Supply, Green-SDA, Blue-SCL.
Test Code for Arduino
Please down the ADS1115 Library and install it.
Description of the I2C Address in this Library:
ADS1115_IIC_ADDRESS0:0x48
ADS1115_IIC_ADDRESS1:0x49
/* * file ADS1115_ReadVoltage.ino * * * connect ADS1115 I2C interface with your board (please reference board compatibility) * * The voltage value read by A0 A1 A2 A3 is printed through the serial port. * */ #include <Wire.h> #include <ADS1115.h> ADS1115 ads; void setup(void) { Serial.begin(115200); ads.setAddr_ADS1115(ADS1115_IIC_ADDRESS0); // 0x48 ads.setGain(eGAIN_TWOTHIRDS); // 2/3x gain ads.setMode(eMODE_SINGLE); // single-shot mode ads.setRate(eRATE_128); // 128SPS (default) ads.setOSMode(eOSMODE_SINGLE); // Set to start a single-conversion ads.init(); } void loop(void) { if (ads.checkADS1115()) { int16_t adc0, adc1, adc2, adc3; adc0 = ads.readVoltage(0); Serial.print("A0:"); Serial.print(adc0); Serial.print("mV, "); adc1 = ads.readVoltage(1); Serial.print("A1:"); Serial.print(adc1); Serial.print("mV, "); adc2 = ads.readVoltage(2); Serial.print("A2:"); Serial.print(adc2); Serial.print("mV, "); adc3 = ads.readVoltage(3); Serial.print("A3:"); Serial.print(adc3); Serial.println("mV"); } else { Serial.println("ADS1115 Disconnected!"); } delay(1000); }
Test Code for Raspberry Pi
File: CQRobot_ADS1115.py
import smbus import time # Get I2C bus bus = smbus.SMBus(1) # I2C address of the device ADS1115_IIC_ADDRESS0 = 0x48 ADS1115_IIC_ADDRESS1 = 0x49 # ADS1115 Register Map ADS1115_REG_POINTER_CONVERT = 0x00 # Conversion register ADS1115_REG_POINTER_CONFIG = 0x01 # Configuration register ADS1115_REG_POINTER_LOWTHRESH = 0x02 # Lo_thresh register ADS1115_REG_POINTER_HITHRESH = 0x03 # Hi_thresh register # ADS1115 Configuration Register ADS1115_REG_CONFIG_OS_NOEFFECT = 0x00 # No effect ADS1115_REG_CONFIG_OS_SINGLE = 0x80 # Begin a single conversion ADS1115_REG_CONFIG_MUX_DIFF_0_1 = 0x00 # Differential P = AIN0, N = AIN1 (default) ADS1115_REG_CONFIG_MUX_DIFF_0_3 = 0x10 # Differential P = AIN0, N = AIN3 ADS1115_REG_CONFIG_MUX_DIFF_1_3 = 0x20 # Differential P = AIN1, N = AIN3 ADS1115_REG_CONFIG_MUX_DIFF_2_3 = 0x30 # Differential P = AIN2, N = AIN3 ADS1115_REG_CONFIG_MUX_SINGLE_0 = 0x40 # Single-ended P = AIN0, N = GND ADS1115_REG_CONFIG_MUX_SINGLE_1 = 0x50 # Single-ended P = AIN1, N = GND ADS1115_REG_CONFIG_MUX_SINGLE_2 = 0x60 # Single-ended P = AIN2, N = GND ADS1115_REG_CONFIG_MUX_SINGLE_3 = 0x70 # Single-ended P = AIN3, N = GND ADS1115_REG_CONFIG_PGA_6_144V = 0x00 # +/-6.144V range = Gain 2/3 ADS1115_REG_CONFIG_PGA_4_096V = 0x02 # +/-4.096V range = Gain 1 ADS1115_REG_CONFIG_PGA_2_048V = 0x04 # +/-2.048V range = Gain 2 (default) ADS1115_REG_CONFIG_PGA_1_024V = 0x06 # +/-1.024V range = Gain 4 ADS1115_REG_CONFIG_PGA_0_512V = 0x08 # +/-0.512V range = Gain 8 ADS1115_REG_CONFIG_PGA_0_256V = 0x0A # +/-0.256V range = Gain 16 ADS1115_REG_CONFIG_MODE_CONTIN = 0x00 # Continuous conversion mode ADS1115_REG_CONFIG_MODE_SINGLE = 0x01 # Power-down single-shot mode (default) ADS1115_REG_CONFIG_DR_8SPS = 0x00 # 8 samples per second ADS1115_REG_CONFIG_DR_16SPS = 0x20 # 16 samples per second ADS1115_REG_CONFIG_DR_32SPS = 0x40 # 32 samples per second ADS1115_REG_CONFIG_DR_64SPS = 0x60 # 64 samples per second ADS1115_REG_CONFIG_DR_128SPS = 0x80 # 128 samples per second (default) ADS1115_REG_CONFIG_DR_250SPS = 0xA0 # 250 samples per second ADS1115_REG_CONFIG_DR_475SPS = 0xC0 # 475 samples per second ADS1115_REG_CONFIG_DR_860SPS = 0xE0 # 860 samples per second ADS1115_REG_CONFIG_CMODE_TRAD = 0x00 # Traditional comparator with hysteresis (default) ADS1115_REG_CONFIG_CMODE_WINDOW = 0x10 # Window comparator ADS1115_REG_CONFIG_CPOL_ACTVLOW = 0x00 # ALERT/RDY pin is low when active (default) ADS1115_REG_CONFIG_CPOL_ACTVHI = 0x08 # ALERT/RDY pin is high when active ADS1115_REG_CONFIG_CLAT_NONLAT = 0x00 # Non-latching comparator (default) ADS1115_REG_CONFIG_CLAT_LATCH = 0x04 # Latching comparator ADS1115_REG_CONFIG_CQUE_1CONV = 0x00 # Assert ALERT/RDY after one conversions ADS1115_REG_CONFIG_CQUE_2CONV = 0x01 # Assert ALERT/RDY after two conversions ADS1115_REG_CONFIG_CQUE_4CONV = 0x02 # Assert ALERT/RDY after four conversions ADS1115_REG_CONFIG_CQUE_NONE = 0x03 # Disable the comparator and put ALERT/RDY in high state (default) mygain=0x02 coefficient=0.125 addr_G=ADS1115_IIC_ADDRESS0 class ADS1115(): def setGain(self,gain): global mygain global coefficient mygain=gain if mygain == ADS1115_REG_CONFIG_PGA_6_144V: coefficient = 0.1875 elif mygain == ADS1115_REG_CONFIG_PGA_4_096V: coefficient = 0.125 elif mygain == ADS1115_REG_CONFIG_PGA_2_048V: coefficient = 0.0625 elif mygain == ADS1115_REG_CONFIG_PGA_1_024V: coefficient = 0.03125 elif mygain == ADS1115_REG_CONFIG_PGA_0_512V: coefficient = 0.015625 elif mygain == ADS1115_REG_CONFIG_PGA_0_256V: coefficient = 0.0078125 else: coefficient = 0.125 def setAddr_ADS1115(self,addr): global addr_G addr_G=addr def setChannel(self,channel): global mygain """Select the Channel user want to use from 0-3 For Single-ended Output 0 : AINP = AIN0 and AINN = GND 1 : AINP = AIN1 and AINN = GND 2 : AINP = AIN2 and AINN = GND 3 : AINP = AIN3 and AINN = GND For Differential Output 0 : AINP = AIN0 and AINN = AIN1 1 : AINP = AIN0 and AINN = AIN3 2 : AINP = AIN1 and AINN = AIN3 3 : AINP = AIN2 and AINN = AIN3""" self.channel = channel while self.channel > 3 : self.channel = 0 return self.channel def setSingle(self): global addr_G if self.channel == 0: CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_SINGLE_0 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE] elif self.channel == 1: CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_SINGLE_1 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE] elif self.channel == 2: CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_SINGLE_2 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE] elif self.channel == 3: CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_SINGLE_3 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE] bus.write_i2c_block_data(addr_G, ADS1115_REG_POINTER_CONFIG, CONFIG_REG) def setDifferential(self): global addr_G if self.channel == 0: CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_DIFF_0_1 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE] elif self.channel == 1: CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_DIFF_0_3 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE] elif self.channel == 2: CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_DIFF_1_3 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE] elif self.channel == 3: CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_DIFF_2_3 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE] bus.write_i2c_block_data(addr_G, ADS1115_REG_POINTER_CONFIG, CONFIG_REG) def readValue(self): """Read data back from ADS1115_REG_POINTER_CONVERT(0x00), 2 bytes raw_adc MSB, raw_adc LSB""" global coefficient global addr_G data = bus.read_i2c_block_data(addr_G, ADS1115_REG_POINTER_CONVERT, 2) # Convert the data raw_adc = data[0] * 256 + data[1] if raw_adc > 32767: raw_adc -= 65535 raw_adc = int(float(raw_adc)*coefficient) return {'r' : raw_adc} def readVoltage(self,channel): self.setChannel(channel) self.setSingle() time.sleep(0.1) return self.readValue() def ComparatorVoltage(self,channel): self.setChannel(channel) self.setDifferential() time.sleep(0.1) return self.readValue()
File: ADS1115_ReadVoltage.py
import sys sys.path.append('../') import time from CQRobot_ADS1115 import ADS1115 ADS1115_REG_CONFIG_PGA_6_144V = 0x00 # 6.144V range = Gain 2/3 ADS1115_REG_CONFIG_PGA_4_096V = 0x02 # 4.096V range = Gain 1 ADS1115_REG_CONFIG_PGA_2_048V = 0x04 # 2.048V range = Gain 2 (default) ADS1115_REG_CONFIG_PGA_1_024V = 0x06 # 1.024V range = Gain 4 ADS1115_REG_CONFIG_PGA_0_512V = 0x08 # 0.512V range = Gain 8 ADS1115_REG_CONFIG_PGA_0_256V = 0x0A # 0.256V range = Gain 16 ads1115 = ADS1115() while True : #Set the IIC address ads1115.setAddr_ADS1115(0x48) #Sets the gain and input voltage range. ads1115.setGain(ADS1115_REG_CONFIG_PGA_6_144V) #Get the Digital Value of Analog of selected channel adc1 = ads1115.readVoltage(1) time.sleep(0.2) print(" A1:%dmV "%(adc1['r']))
Run the code
Note: Make sure that the I2C address from its default (0x48);
Note: Enable "Interfacing Options - P5 I2C" by typing the below command in the terminal:
sudo raspi-config
Run the code:
cd TS1728 ls sudo python3 CQRobot_ADS1115.py cd ADS1115_ReadVoltage ls sudo python3 ADS1115_ReadVoltage.py