├── keywords.txt ├── library.properties ├── .github ├── workflows │ └── githubci.yml ├── PULL_REQUEST_TEMPLATE.md └── ISSUE_TEMPLATE.md ├── README.md ├── license.txt ├── examples ├── comparator │ └── comparator.ino ├── differential │ └── differential.ino ├── nonblocking │ └── nonblocking.ino ├── singleended │ └── singleended.ino └── continuous │ └── continuous.ino ├── Adafruit_ADS1X15.h └── Adafruit_ADS1X15.cpp /keywords.txt: -------------------------------------------------------------------------------- 1 | Adafruit_ADS1015 KEYWORD1 2 | Adafruit_ADS1115 KEYWORD1 3 | begin KEYWORD2 4 | readADC_SingleEnded KEYWORD2 5 | readADC_Differential_0_1 KEYWORD2 6 | readADC_Differential_2_3 KEYWORD2 7 | startComparator_SingleEnded KEYWORD2 8 | getLastConversionResults KEYWORD2 9 | getFsRange KEYWORD1 10 | computeVolts KEYWORD2 11 | setGain KEYWORD2 12 | getGain KEYWORD2 13 | setDataRate KEYWORD2 14 | getDataRate KEYWORD2 -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Adafruit ADS1X15 2 | version=2.6.0 3 | author=Adafruit 4 | maintainer=Adafruit 5 | sentence=Arduino library for ADS1015/1115 ADCs. 6 | paragraph=Arduino library for ADS1015/1115 12/16-bit Differential or Single-Ended ADCs with PGA and Comparator 7 | category=Signal Input/Output 8 | url=https://github.com/adafruit/Adafruit_ADS1X15 9 | architectures=* 10 | depends=Adafruit BusIO 11 | -------------------------------------------------------------------------------- /.github/workflows/githubci.yml: -------------------------------------------------------------------------------- 1 | name: Arduino Library CI 2 | 3 | on: [pull_request, push, repository_dispatch] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/setup-python@v4 11 | with: 12 | python-version: '3.x' 13 | - uses: actions/checkout@v3 14 | - uses: actions/checkout@v3 15 | with: 16 | repository: adafruit/ci-arduino 17 | path: ci 18 | 19 | - name: pre-install 20 | run: bash ci/actions_install.sh 21 | 22 | - name: test platforms 23 | run: python3 ci/build_platform.py main_platforms 24 | 25 | - name: clang 26 | run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r . 27 | 28 | - name: doxygen 29 | env: 30 | GH_REPO_TOKEN: ${{ secrets.GH_REPO_TOKEN }} 31 | PRETTYNAME : "Adafruit ADS1X15 ADC Driver Library" 32 | run: bash ci/doxy_gen_and_deploy.sh 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Adafruit_ADS1015 ![Build Status](https://github.com/adafruit/Adafruit_ADS1X15/workflows/Arduino%20Library%20CI/badge.svg)[![Documentation](https://github.com/adafruit/ci-arduino/blob/master/assets/doxygen_badge.svg)](http://adafruit.github.io/Adafruit_ADS1X15/html/index.html) 2 | 3 | 4 | Driver for TI's ADS1X15: 12 and 16-bit Differential or Single-Ended ADC with PGA and Comparator 5 | 6 | ## Info 7 | 8 | This family of ADCs provide 4 single-ended or 2 differential channels. 9 | Each has a programmable gain amplifier from 2/3 up to 16x. Available 10 | in 12 or 16 bit versions: 11 | 12 | * [ADS1015 12-bit ADC](https://www.adafruit.com/product/1083) 13 | * [ADS1115 16-bit ADC](https://www.adafruit.com/product/1085) 14 | 15 | The chip's fairly small so it comes on a breakout board with ferrites to keep the AVDD and AGND quiet. Interfacing is done via I2C. The address can be changed to one of four options (see the datasheet table 5) so you can have up to 4 ADS1x15's connected on a single 2-wire I2C bus for 16 single ended inputs. 16 | 17 | Adafruit invests time and resources providing this open source code, please 18 | support Adafruit and open-source hardware by purchasing products from 19 | [Adafruit](https://www.adafruit.com)! 20 | 21 | ## License 22 | 23 | BSD license, all text above must be included in any redistribution. 24 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for creating a pull request to contribute to Adafruit's GitHub code! 2 | Before you open the request please review the following guidelines and tips to 3 | help it be more easily integrated: 4 | 5 | - **Describe the scope of your change--i.e. what the change does and what parts 6 | of the code were modified.** This will help us understand any risks of integrating 7 | the code. 8 | 9 | - **Describe any known limitations with your change.** For example if the change 10 | doesn't apply to a supported platform of the library please mention it. 11 | 12 | - **Please run any tests or examples that can exercise your modified code.** We 13 | strive to not break users of the code and running tests/examples helps with this 14 | process. 15 | 16 | Thank you again for contributing! We will try to test and integrate the change 17 | as soon as we can, but be aware we have many GitHub repositories to manage and 18 | can't immediately respond to every request. There is no need to bump or check in 19 | on a pull request (it will clutter the discussion of the request). 20 | 21 | Also don't be worried if the request is closed or not integrated--sometimes the 22 | priorities of Adafruit's GitHub code (education, ease of use) might not match the 23 | priorities of the pull request. Don't fret, the open source community thrives on 24 | forks and GitHub makes it easy to keep your changes in a forked repo. 25 | 26 | After reviewing the guidelines above you can delete this text from the pull request. 27 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | Software License Agreement (BSD License) 2 | 3 | Copyright (c) 2012, Adafruit Industries 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 1. Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 2. Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in the 12 | documentation and/or other materials provided with the distribution. 13 | 3. Neither the name of the copyright holders nor the 14 | names of its contributors may be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 18 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 21 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /examples/comparator/comparator.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Adafruit_ADS1115 ads; /* Use this for the 16-bit version */ 4 | Adafruit_ADS1015 ads; /* Use this for the 12-bit version */ 5 | 6 | void setup(void) 7 | { 8 | Serial.begin(9600); 9 | Serial.println("Hello!"); 10 | 11 | Serial.println("Single-ended readings from AIN0 with >3.0V comparator"); 12 | Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)"); 13 | Serial.println("Comparator Threshold: 1000 (3.000V)"); 14 | 15 | // The ADC input range (or gain) can be changed via the following 16 | // functions, but be careful never to exceed VDD +0.3V max, or to 17 | // exceed the upper and lower limits if you adjust the input range! 18 | // ADS1015 ADS1115 19 | // ------- ------- 20 | // ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default) 21 | // ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV 22 | // ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV 23 | // ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV 24 | // ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV 25 | // ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV 26 | 27 | if (!ads.begin()) { 28 | Serial.println("Failed to initialize ADS."); 29 | while (1); 30 | } 31 | // Setup 3V comparator on channel 0 32 | ads.startComparator_SingleEnded(0, 1000); 33 | } 34 | 35 | void loop(void) 36 | { 37 | int16_t adc0; 38 | 39 | // Comparator will only de-assert after a read 40 | adc0 = ads.getLastConversionResults(); 41 | Serial.print("AIN0: "); Serial.println(adc0); 42 | 43 | delay(100); 44 | } 45 | -------------------------------------------------------------------------------- /examples/differential/differential.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Adafruit_ADS1115 ads; /* Use this for the 16-bit version */ 4 | Adafruit_ADS1015 ads; /* Use this for the 12-bit version */ 5 | 6 | void setup(void) 7 | { 8 | Serial.begin(9600); 9 | Serial.println("Hello!"); 10 | 11 | Serial.println("Getting differential reading from AIN0 (P) and AIN1 (N)"); 12 | Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)"); 13 | 14 | // The ADC input range (or gain) can be changed via the following 15 | // functions, but be careful never to exceed VDD +0.3V max, or to 16 | // exceed the upper and lower limits if you adjust the input range! 17 | // ADS1015 ADS1115 18 | // ------- ------- 19 | // ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default) 20 | // ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV 21 | // ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV 22 | // ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV 23 | // ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV 24 | // ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV 25 | 26 | if (!ads.begin()) { 27 | Serial.println("Failed to initialize ADS."); 28 | while (1); 29 | }} 30 | 31 | void loop(void) 32 | { 33 | int16_t results; 34 | 35 | /* Be sure to update this value based on the IC and the gain settings! */ 36 | float multiplier = 3.0F; /* ADS1015 @ +/- 6.144V gain (12-bit results) */ 37 | //float multiplier = 0.1875F; /* ADS1115 @ +/- 6.144V gain (16-bit results) */ 38 | 39 | results = ads.readADC_Differential_0_1(); 40 | 41 | Serial.print("Differential: "); Serial.print(results); Serial.print("("); Serial.print(results * multiplier); Serial.println("mV)"); 42 | 43 | delay(1000); 44 | } 45 | -------------------------------------------------------------------------------- /examples/nonblocking/nonblocking.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Adafruit_ADS1115 ads; /* Use this for the 16-bit version */ 4 | Adafruit_ADS1015 ads; /* Use this for the 12-bit version */ 5 | 6 | void setup(void) 7 | { 8 | Serial.begin(9600); 9 | Serial.println("Hello!"); 10 | 11 | Serial.println("Getting differential reading from AIN0 (P) and AIN1 (N)"); 12 | Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)"); 13 | 14 | // The ADC input range (or gain) can be changed via the following 15 | // functions, but be careful never to exceed VDD +0.3V max, or to 16 | // exceed the upper and lower limits if you adjust the input range! 17 | // ADS1015 ADS1115 18 | // ------- ------- 19 | // ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default) 20 | // ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV 21 | // ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV 22 | // ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV 23 | // ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV 24 | // ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV 25 | 26 | if (!ads.begin()) { 27 | Serial.println("Failed to initialize ADS."); 28 | while (1); 29 | } 30 | 31 | // Start the first conversion. 32 | ads.startADCReading(ADS1X15_REG_CONFIG_MUX_DIFF_0_1, /*continuous=*/false); 33 | } 34 | 35 | void loop(void) 36 | { 37 | // If we don't have new data, skip this iteration. 38 | if (!ads.conversionComplete()) { 39 | return; 40 | } 41 | 42 | int16_t results = ads.getLastConversionResults(); 43 | 44 | Serial.print("Differential: "); Serial.print(results); Serial.print("("); Serial.print(ads.computeVolts(results)); Serial.println("V)"); 45 | 46 | // Start another conversion. 47 | ads.startADCReading(ADS1X15_REG_CONFIG_MUX_DIFF_0_1, /*continuous=*/false); 48 | 49 | delay(1000); 50 | } 51 | -------------------------------------------------------------------------------- /examples/singleended/singleended.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //Adafruit_ADS1115 ads; /* Use this for the 16-bit version */ 4 | Adafruit_ADS1015 ads; /* Use this for the 12-bit version */ 5 | 6 | void setup(void) 7 | { 8 | Serial.begin(9600); 9 | Serial.println("Hello!"); 10 | 11 | Serial.println("Getting single-ended readings from AIN0..3"); 12 | Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)"); 13 | 14 | // The ADC input range (or gain) can be changed via the following 15 | // functions, but be careful never to exceed VDD +0.3V max, or to 16 | // exceed the upper and lower limits if you adjust the input range! 17 | // ADS1015 ADS1115 18 | // ------- ------- 19 | // ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default) 20 | // ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV 21 | // ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV 22 | // ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV 23 | // ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV 24 | // ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV 25 | 26 | if (!ads.begin()) { 27 | Serial.println("Failed to initialize ADS."); 28 | while (1); 29 | } 30 | } 31 | 32 | void loop(void) 33 | { 34 | int16_t adc0, adc1, adc2, adc3; 35 | float volts0, volts1, volts2, volts3; 36 | 37 | adc0 = ads.readADC_SingleEnded(0); 38 | adc1 = ads.readADC_SingleEnded(1); 39 | adc2 = ads.readADC_SingleEnded(2); 40 | adc3 = ads.readADC_SingleEnded(3); 41 | 42 | volts0 = ads.computeVolts(adc0); 43 | volts1 = ads.computeVolts(adc1); 44 | volts2 = ads.computeVolts(adc2); 45 | volts3 = ads.computeVolts(adc3); 46 | 47 | Serial.println("-----------------------------------------------------------"); 48 | Serial.print("AIN0: "); Serial.print(adc0); Serial.print(" "); Serial.print(volts0); Serial.println("V"); 49 | Serial.print("AIN1: "); Serial.print(adc1); Serial.print(" "); Serial.print(volts1); Serial.println("V"); 50 | Serial.print("AIN2: "); Serial.print(adc2); Serial.print(" "); Serial.print(volts2); Serial.println("V"); 51 | Serial.print("AIN3: "); Serial.print(adc3); Serial.print(" "); Serial.print(volts3); Serial.println("V"); 52 | 53 | delay(1000); 54 | } 55 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Thank you for opening an issue on an Adafruit Arduino library repository. To 2 | improve the speed of resolution please review the following guidelines and 3 | common troubleshooting steps below before creating the issue: 4 | 5 | - **Do not use GitHub issues for troubleshooting projects and issues.** Instead use 6 | the forums at http://forums.adafruit.com to ask questions and troubleshoot why 7 | something isn't working as expected. In many cases the problem is a common issue 8 | that you will more quickly receive help from the forum community. GitHub issues 9 | are meant for known defects in the code. If you don't know if there is a defect 10 | in the code then start with troubleshooting on the forum first. 11 | 12 | - **If following a tutorial or guide be sure you didn't miss a step.** Carefully 13 | check all of the steps and commands to run have been followed. Consult the 14 | forum if you're unsure or have questions about steps in a guide/tutorial. 15 | 16 | - **For Arduino projects check these very common issues to ensure they don't apply**: 17 | 18 | - For uploading sketches or communicating with the board make sure you're using 19 | a **USB data cable** and **not** a **USB charge-only cable**. It is sometimes 20 | very hard to tell the difference between a data and charge cable! Try using the 21 | cable with other devices or swapping to another cable to confirm it is not 22 | the problem. 23 | 24 | - **Be sure you are supplying adequate power to the board.** Check the specs of 25 | your board and plug in an external power supply. In many cases just 26 | plugging a board into your computer is not enough to power it and other 27 | peripherals. 28 | 29 | - **Double check all soldering joints and connections.** Flakey connections 30 | cause many mysterious problems. See the [guide to excellent soldering](https://learn.adafruit.com/adafruit-guide-excellent-soldering/tools) for examples of good solder joints. 31 | 32 | - **Ensure you are using an official Arduino or Adafruit board.** We can't 33 | guarantee a clone board will have the same functionality and work as expected 34 | with this code and don't support them. 35 | 36 | If you're sure this issue is a defect in the code and checked the steps above 37 | please fill in the following fields to provide enough troubleshooting information. 38 | You may delete the guideline and text above to just leave the following details: 39 | 40 | - Arduino board: **INSERT ARDUINO BOARD NAME/TYPE HERE** 41 | 42 | - Arduino IDE version (found in Arduino -> About Arduino menu): **INSERT ARDUINO 43 | VERSION HERE** 44 | 45 | - List the steps to reproduce the problem below (if possible attach a sketch or 46 | copy the sketch code in too): **LIST REPRO STEPS BELOW** 47 | -------------------------------------------------------------------------------- /examples/continuous/continuous.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Adafruit_ADS1115 ads; /* Use this for the 16-bit version */ 4 | Adafruit_ADS1015 ads; /* Use this for the 12-bit version */ 5 | 6 | // Pin connected to the ALERT/RDY signal for new sample notification. 7 | constexpr int READY_PIN = 3; 8 | 9 | // This is required on ESP32 to put the ISR in IRAM. Define as 10 | // empty for other platforms. Be careful - other platforms may have 11 | // other requirements. 12 | #ifndef IRAM_ATTR 13 | #define IRAM_ATTR 14 | #endif 15 | 16 | volatile bool new_data = false; 17 | void IRAM_ATTR NewDataReadyISR() { 18 | new_data = true; 19 | } 20 | 21 | void setup(void) 22 | { 23 | Serial.begin(9600); 24 | Serial.println("Hello!"); 25 | 26 | Serial.println("Getting differential reading from AIN0 (P) and AIN1 (N)"); 27 | Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)"); 28 | 29 | // The ADC input range (or gain) can be changed via the following 30 | // functions, but be careful never to exceed VDD +0.3V max, or to 31 | // exceed the upper and lower limits if you adjust the input range! 32 | // ADS1015 ADS1115 33 | // ------- ------- 34 | // ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default) 35 | // ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV 36 | // ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV 37 | // ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV 38 | // ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV 39 | // ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV 40 | 41 | if (!ads.begin()) { 42 | Serial.println("Failed to initialize ADS."); 43 | while (1); 44 | } 45 | 46 | pinMode(READY_PIN, INPUT); 47 | // With default COMP_POL=0, get a rising edge every time a new sample is ready. 48 | attachInterrupt(digitalPinToInterrupt(READY_PIN), NewDataReadyISR, RISING); 49 | 50 | // Start continuous conversions. 51 | ads.startADCReading(ADS1X15_REG_CONFIG_MUX_DIFF_0_1, /*continuous=*/true); 52 | } 53 | 54 | void loop(void) 55 | { 56 | // If we don't have new data, skip this iteration. 57 | if (!new_data) { 58 | return; 59 | } 60 | 61 | int16_t results = ads.getLastConversionResults(); 62 | 63 | Serial.print("Differential: "); Serial.print(results); Serial.print("("); Serial.print(ads.computeVolts(results)); Serial.println("V)"); 64 | 65 | new_data = false; 66 | 67 | // In a real application we probably don't want to do a delay here if we are doing interrupt-based sampling, but we have a delay 68 | // in this example to avoid writing too much data to the serial port. 69 | delay(1000); 70 | } 71 | -------------------------------------------------------------------------------- /Adafruit_ADS1X15.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file Adafruit_ADS1X15.h 4 | 5 | This is a library for the Adafruit ADS1X15 ADC breakout boards. 6 | 7 | Adafruit invests time and resources providing this open source code, 8 | please support Adafruit and open-source hardware by purchasing 9 | products from Adafruit! 10 | 11 | Written by Kevin "KTOWN" Townsend for Adafruit Industries. 12 | 13 | BSD license, all text here must be included in any redistribution 14 | */ 15 | /**************************************************************************/ 16 | #ifndef __ADS1X15_H__ 17 | #define __ADS1X15_H__ 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | /*========================================================================= 24 | I2C ADDRESS/BITS 25 | -----------------------------------------------------------------------*/ 26 | #define ADS1X15_ADDRESS (0x48) ///< 1001 000 (ADDR = GND) 27 | /*=========================================================================*/ 28 | 29 | /*========================================================================= 30 | POINTER REGISTER 31 | -----------------------------------------------------------------------*/ 32 | #define ADS1X15_REG_POINTER_MASK (0x03) ///< Point mask 33 | #define ADS1X15_REG_POINTER_CONVERT (0x00) ///< Conversion 34 | #define ADS1X15_REG_POINTER_CONFIG (0x01) ///< Configuration 35 | #define ADS1X15_REG_POINTER_LOWTHRESH (0x02) ///< Low threshold 36 | #define ADS1X15_REG_POINTER_HITHRESH (0x03) ///< High threshold 37 | /*=========================================================================*/ 38 | 39 | /*========================================================================= 40 | CONFIG REGISTER 41 | -----------------------------------------------------------------------*/ 42 | #define ADS1X15_REG_CONFIG_OS_MASK (0x8000) ///< OS Mask 43 | #define ADS1X15_REG_CONFIG_OS_SINGLE \ 44 | (0x8000) ///< Write: Set to start a single-conversion 45 | #define ADS1X15_REG_CONFIG_OS_BUSY \ 46 | (0x0000) ///< Read: Bit = 0 when conversion is in progress 47 | #define ADS1X15_REG_CONFIG_OS_NOTBUSY \ 48 | (0x8000) ///< Read: Bit = 1 when device is not performing a conversion 49 | 50 | #define ADS1X15_REG_CONFIG_MUX_MASK (0x7000) ///< Mux Mask 51 | #define ADS1X15_REG_CONFIG_MUX_DIFF_0_1 \ 52 | (0x0000) ///< Differential P = AIN0, N = AIN1 (default) 53 | #define ADS1X15_REG_CONFIG_MUX_DIFF_0_3 \ 54 | (0x1000) ///< Differential P = AIN0, N = AIN3 55 | #define ADS1X15_REG_CONFIG_MUX_DIFF_1_3 \ 56 | (0x2000) ///< Differential P = AIN1, N = AIN3 57 | #define ADS1X15_REG_CONFIG_MUX_DIFF_2_3 \ 58 | (0x3000) ///< Differential P = AIN2, N = AIN3 59 | #define ADS1X15_REG_CONFIG_MUX_SINGLE_0 (0x4000) ///< Single-ended AIN0 60 | #define ADS1X15_REG_CONFIG_MUX_SINGLE_1 (0x5000) ///< Single-ended AIN1 61 | #define ADS1X15_REG_CONFIG_MUX_SINGLE_2 (0x6000) ///< Single-ended AIN2 62 | #define ADS1X15_REG_CONFIG_MUX_SINGLE_3 (0x7000) ///< Single-ended AIN3 63 | 64 | constexpr uint16_t MUX_BY_CHANNEL[] = { 65 | ADS1X15_REG_CONFIG_MUX_SINGLE_0, ///< Single-ended AIN0 66 | ADS1X15_REG_CONFIG_MUX_SINGLE_1, ///< Single-ended AIN1 67 | ADS1X15_REG_CONFIG_MUX_SINGLE_2, ///< Single-ended AIN2 68 | ADS1X15_REG_CONFIG_MUX_SINGLE_3 ///< Single-ended AIN3 69 | }; ///< MUX config by channel 70 | 71 | #define ADS1X15_REG_CONFIG_PGA_MASK (0x0E00) ///< PGA Mask 72 | #define ADS1X15_REG_CONFIG_PGA_6_144V (0x0000) ///< +/-6.144V range = Gain 2/3 73 | #define ADS1X15_REG_CONFIG_PGA_4_096V (0x0200) ///< +/-4.096V range = Gain 1 74 | #define ADS1X15_REG_CONFIG_PGA_2_048V \ 75 | (0x0400) ///< +/-2.048V range = Gain 2 (default) 76 | #define ADS1X15_REG_CONFIG_PGA_1_024V (0x0600) ///< +/-1.024V range = Gain 4 77 | #define ADS1X15_REG_CONFIG_PGA_0_512V (0x0800) ///< +/-0.512V range = Gain 8 78 | #define ADS1X15_REG_CONFIG_PGA_0_256V (0x0A00) ///< +/-0.256V range = Gain 16 79 | 80 | #define ADS1X15_REG_CONFIG_MODE_MASK (0x0100) ///< Mode Mask 81 | #define ADS1X15_REG_CONFIG_MODE_CONTIN (0x0000) ///< Continuous conversion mode 82 | #define ADS1X15_REG_CONFIG_MODE_SINGLE \ 83 | (0x0100) ///< Power-down single-shot mode (default) 84 | 85 | #define ADS1X15_REG_CONFIG_RATE_MASK (0x00E0) ///< Data Rate Mask 86 | 87 | #define ADS1X15_REG_CONFIG_CMODE_MASK (0x0010) ///< CMode Mask 88 | #define ADS1X15_REG_CONFIG_CMODE_TRAD \ 89 | (0x0000) ///< Traditional comparator with hysteresis (default) 90 | #define ADS1X15_REG_CONFIG_CMODE_WINDOW (0x0010) ///< Window comparator 91 | 92 | #define ADS1X15_REG_CONFIG_CPOL_MASK (0x0008) ///< CPol Mask 93 | #define ADS1X15_REG_CONFIG_CPOL_ACTVLOW \ 94 | (0x0000) ///< ALERT/RDY pin is low when active (default) 95 | #define ADS1X15_REG_CONFIG_CPOL_ACTVHI \ 96 | (0x0008) ///< ALERT/RDY pin is high when active 97 | 98 | #define ADS1X15_REG_CONFIG_CLAT_MASK \ 99 | (0x0004) ///< Determines if ALERT/RDY pin latches once asserted 100 | #define ADS1X15_REG_CONFIG_CLAT_NONLAT \ 101 | (0x0000) ///< Non-latching comparator (default) 102 | #define ADS1X15_REG_CONFIG_CLAT_LATCH (0x0004) ///< Latching comparator 103 | 104 | #define ADS1X15_REG_CONFIG_CQUE_MASK (0x0003) ///< CQue Mask 105 | #define ADS1X15_REG_CONFIG_CQUE_1CONV \ 106 | (0x0000) ///< Assert ALERT/RDY after one conversions 107 | #define ADS1X15_REG_CONFIG_CQUE_2CONV \ 108 | (0x0001) ///< Assert ALERT/RDY after two conversions 109 | #define ADS1X15_REG_CONFIG_CQUE_4CONV \ 110 | (0x0002) ///< Assert ALERT/RDY after four conversions 111 | #define ADS1X15_REG_CONFIG_CQUE_NONE \ 112 | (0x0003) ///< Disable the comparator and put ALERT/RDY in high state (default) 113 | /*=========================================================================*/ 114 | 115 | /** Gain settings */ 116 | typedef enum { 117 | GAIN_TWOTHIRDS = ADS1X15_REG_CONFIG_PGA_6_144V, 118 | GAIN_ONE = ADS1X15_REG_CONFIG_PGA_4_096V, 119 | GAIN_TWO = ADS1X15_REG_CONFIG_PGA_2_048V, 120 | GAIN_FOUR = ADS1X15_REG_CONFIG_PGA_1_024V, 121 | GAIN_EIGHT = ADS1X15_REG_CONFIG_PGA_0_512V, 122 | GAIN_SIXTEEN = ADS1X15_REG_CONFIG_PGA_0_256V 123 | } adsGain_t; 124 | 125 | /** Data rates */ 126 | #define RATE_ADS1015_128SPS (0x0000) ///< 128 samples per second 127 | #define RATE_ADS1015_250SPS (0x0020) ///< 250 samples per second 128 | #define RATE_ADS1015_490SPS (0x0040) ///< 490 samples per second 129 | #define RATE_ADS1015_920SPS (0x0060) ///< 920 samples per second 130 | #define RATE_ADS1015_1600SPS (0x0080) ///< 1600 samples per second (default) 131 | #define RATE_ADS1015_2400SPS (0x00A0) ///< 2400 samples per second 132 | #define RATE_ADS1015_3300SPS (0x00C0) ///< 3300 samples per second 133 | 134 | #define RATE_ADS1115_8SPS (0x0000) ///< 8 samples per second 135 | #define RATE_ADS1115_16SPS (0x0020) ///< 16 samples per second 136 | #define RATE_ADS1115_32SPS (0x0040) ///< 32 samples per second 137 | #define RATE_ADS1115_64SPS (0x0060) ///< 64 samples per second 138 | #define RATE_ADS1115_128SPS (0x0080) ///< 128 samples per second (default) 139 | #define RATE_ADS1115_250SPS (0x00A0) ///< 250 samples per second 140 | #define RATE_ADS1115_475SPS (0x00C0) ///< 475 samples per second 141 | #define RATE_ADS1115_860SPS (0x00E0) ///< 860 samples per second 142 | 143 | /**************************************************************************/ 144 | /*! 145 | @brief Sensor driver for the Adafruit ADS1X15 ADC breakouts. 146 | */ 147 | /**************************************************************************/ 148 | class Adafruit_ADS1X15 { 149 | protected: 150 | // Instance-specific properties 151 | Adafruit_I2CDevice *m_i2c_dev; ///< I2C bus device 152 | uint8_t m_bitShift; ///< bit shift amount 153 | adsGain_t m_gain; ///< ADC gain 154 | uint16_t m_dataRate; ///< Data rate 155 | 156 | public: 157 | bool begin(uint8_t i2c_addr = ADS1X15_ADDRESS, TwoWire *wire = &Wire); 158 | int16_t readADC_SingleEnded(uint8_t channel); 159 | int16_t readADC_Differential_0_1(); 160 | int16_t readADC_Differential_0_3(); 161 | int16_t readADC_Differential_1_3(); 162 | int16_t readADC_Differential_2_3(); 163 | void startComparator_SingleEnded(uint8_t channel, int16_t threshold); 164 | int16_t getLastConversionResults(); 165 | float getFsRange(); 166 | float computeVolts(int16_t counts); 167 | void setGain(adsGain_t gain); 168 | adsGain_t getGain(); 169 | void setDataRate(uint16_t rate); 170 | uint16_t getDataRate(); 171 | 172 | void startADCReading(uint16_t mux, bool continuous); 173 | 174 | bool conversionComplete(); 175 | 176 | private: 177 | void writeRegister(uint8_t reg, uint16_t value); 178 | uint16_t readRegister(uint8_t reg); 179 | uint8_t buffer[3]; 180 | }; 181 | 182 | /**************************************************************************/ 183 | /*! 184 | @brief Sensor driver for the Adafruit ADS1015 ADC breakout. 185 | */ 186 | /**************************************************************************/ 187 | class Adafruit_ADS1015 : public Adafruit_ADS1X15 { 188 | public: 189 | Adafruit_ADS1015(); 190 | }; 191 | 192 | /**************************************************************************/ 193 | /*! 194 | @brief Sensor driver for the Adafruit ADS1115 ADC breakout. 195 | */ 196 | /**************************************************************************/ 197 | class Adafruit_ADS1115 : public Adafruit_ADS1X15 { 198 | public: 199 | Adafruit_ADS1115(); 200 | }; 201 | 202 | #endif 203 | -------------------------------------------------------------------------------- /Adafruit_ADS1X15.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************/ 2 | /*! 3 | @file Adafruit_ADS1X15.cpp 4 | @author K.Townsend (Adafruit Industries) 5 | 6 | @mainpage Adafruit ADS1X15 ADC Breakout Driver 7 | 8 | @section intro_sec Introduction 9 | 10 | This is a library for the Adafruit ADS1X15 ADC breakout boards. 11 | 12 | Adafruit invests time and resources providing this open source code, 13 | please support Adafruit and open-source hardware by purchasing 14 | products from Adafruit! 15 | 16 | @section author Author 17 | 18 | Written by Kevin "KTOWN" Townsend for Adafruit Industries. 19 | 20 | @section HISTORY 21 | 22 | v1.0 - First release 23 | v1.1 - Added ADS1115 support - W. Earl 24 | v2.0 - Refactor - C. Nelson 25 | 26 | @section license License 27 | 28 | BSD license, all text here must be included in any redistribution 29 | */ 30 | /**************************************************************************/ 31 | #include "Adafruit_ADS1X15.h" 32 | 33 | /**************************************************************************/ 34 | /*! 35 | @brief Instantiates a new ADS1015 class w/appropriate properties 36 | */ 37 | /**************************************************************************/ 38 | Adafruit_ADS1015::Adafruit_ADS1015() { 39 | m_bitShift = 4; 40 | m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */ 41 | m_dataRate = RATE_ADS1015_1600SPS; 42 | } 43 | 44 | /**************************************************************************/ 45 | /*! 46 | @brief Instantiates a new ADS1115 class w/appropriate properties 47 | */ 48 | /**************************************************************************/ 49 | Adafruit_ADS1115::Adafruit_ADS1115() { 50 | m_bitShift = 0; 51 | m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */ 52 | m_dataRate = RATE_ADS1115_128SPS; 53 | } 54 | 55 | /**************************************************************************/ 56 | /*! 57 | @brief Sets up the HW (reads coefficients values, etc.) 58 | 59 | @param i2c_addr I2C address of device 60 | @param wire I2C bus 61 | 62 | @return true if successful, otherwise false 63 | */ 64 | /**************************************************************************/ 65 | bool Adafruit_ADS1X15::begin(uint8_t i2c_addr, TwoWire *wire) { 66 | m_i2c_dev = new Adafruit_I2CDevice(i2c_addr, wire); 67 | return m_i2c_dev->begin(); 68 | } 69 | 70 | /**************************************************************************/ 71 | /*! 72 | @brief Sets the gain and input voltage range 73 | 74 | @param gain gain setting to use 75 | */ 76 | /**************************************************************************/ 77 | void Adafruit_ADS1X15::setGain(adsGain_t gain) { m_gain = gain; } 78 | 79 | /**************************************************************************/ 80 | /*! 81 | @brief Gets a gain and input voltage range 82 | 83 | @return the gain setting 84 | */ 85 | /**************************************************************************/ 86 | adsGain_t Adafruit_ADS1X15::getGain() { return m_gain; } 87 | 88 | /**************************************************************************/ 89 | /*! 90 | @brief Sets the data rate 91 | 92 | @param rate the data rate to use 93 | */ 94 | /**************************************************************************/ 95 | void Adafruit_ADS1X15::setDataRate(uint16_t rate) { m_dataRate = rate; } 96 | 97 | /**************************************************************************/ 98 | /*! 99 | @brief Gets the current data rate 100 | 101 | @return the data rate 102 | */ 103 | /**************************************************************************/ 104 | uint16_t Adafruit_ADS1X15::getDataRate() { return m_dataRate; } 105 | 106 | /**************************************************************************/ 107 | /*! 108 | @brief Gets a single-ended ADC reading from the specified channel 109 | 110 | @param channel ADC channel to read 111 | 112 | @return the ADC reading 113 | */ 114 | /**************************************************************************/ 115 | int16_t Adafruit_ADS1X15::readADC_SingleEnded(uint8_t channel) { 116 | if (channel > 3) { 117 | return 0; 118 | } 119 | 120 | startADCReading(MUX_BY_CHANNEL[channel], /*continuous=*/false); 121 | 122 | // Wait for the conversion to complete 123 | while (!conversionComplete()) 124 | ; 125 | 126 | // Read the conversion results 127 | return getLastConversionResults(); 128 | } 129 | 130 | /**************************************************************************/ 131 | /*! 132 | @brief Reads the conversion results, measuring the voltage 133 | difference between the P (AIN0) and N (AIN1) input. Generates 134 | a signed value since the difference can be either 135 | positive or negative. 136 | 137 | @return the ADC reading 138 | */ 139 | /**************************************************************************/ 140 | int16_t Adafruit_ADS1X15::readADC_Differential_0_1() { 141 | startADCReading(ADS1X15_REG_CONFIG_MUX_DIFF_0_1, /*continuous=*/false); 142 | 143 | // Wait for the conversion to complete 144 | while (!conversionComplete()) 145 | ; 146 | 147 | // Read the conversion results 148 | return getLastConversionResults(); 149 | } 150 | 151 | /**************************************************************************/ 152 | /*! 153 | @brief Reads the conversion results, measuring the voltage 154 | difference between the P (AIN0) and N (AIN3) input. Generates 155 | a signed value since the difference can be either 156 | positive or negative. 157 | @return the ADC reading 158 | */ 159 | /**************************************************************************/ 160 | int16_t Adafruit_ADS1X15::readADC_Differential_0_3() { 161 | startADCReading(ADS1X15_REG_CONFIG_MUX_DIFF_0_3, /*continuous=*/false); 162 | 163 | // Wait for the conversion to complete 164 | while (!conversionComplete()) 165 | ; 166 | 167 | // Read the conversion results 168 | return getLastConversionResults(); 169 | } 170 | 171 | /**************************************************************************/ 172 | /*! 173 | @brief Reads the conversion results, measuring the voltage 174 | difference between the P (AIN1) and N (AIN3) input. Generates 175 | a signed value since the difference can be either 176 | positive or negative. 177 | @return the ADC reading 178 | */ 179 | /**************************************************************************/ 180 | int16_t Adafruit_ADS1X15::readADC_Differential_1_3() { 181 | startADCReading(ADS1X15_REG_CONFIG_MUX_DIFF_1_3, /*continuous=*/false); 182 | 183 | // Wait for the conversion to complete 184 | while (!conversionComplete()) 185 | ; 186 | 187 | // Read the conversion results 188 | return getLastConversionResults(); 189 | } 190 | 191 | /**************************************************************************/ 192 | /*! 193 | @brief Reads the conversion results, measuring the voltage 194 | difference between the P (AIN2) and N (AIN3) input. Generates 195 | a signed value since the difference can be either 196 | positive or negative. 197 | 198 | @return the ADC reading 199 | */ 200 | /**************************************************************************/ 201 | int16_t Adafruit_ADS1X15::readADC_Differential_2_3() { 202 | startADCReading(ADS1X15_REG_CONFIG_MUX_DIFF_2_3, /*continuous=*/false); 203 | 204 | // Wait for the conversion to complete 205 | while (!conversionComplete()) 206 | ; 207 | 208 | // Read the conversion results 209 | return getLastConversionResults(); 210 | } 211 | 212 | /**************************************************************************/ 213 | /*! 214 | @brief Sets up the comparator to operate in basic mode, causing the 215 | ALERT/RDY pin to assert (go from high to low) when the ADC 216 | value exceeds the specified threshold. 217 | 218 | This will also set the ADC in continuous conversion mode. 219 | 220 | @param channel ADC channel to use 221 | @param threshold comparator threshold 222 | */ 223 | /**************************************************************************/ 224 | void Adafruit_ADS1X15::startComparator_SingleEnded(uint8_t channel, 225 | int16_t threshold) { 226 | // Start with default values 227 | uint16_t config = 228 | ADS1X15_REG_CONFIG_CQUE_1CONV | // Comparator enabled and asserts on 1 229 | // match 230 | ADS1X15_REG_CONFIG_CLAT_LATCH | // Latching mode 231 | ADS1X15_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) 232 | ADS1X15_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) 233 | ADS1X15_REG_CONFIG_MODE_CONTIN | // Continuous conversion mode 234 | ADS1X15_REG_CONFIG_MODE_CONTIN; // Continuous conversion mode 235 | 236 | // Set PGA/voltage range 237 | config |= m_gain; 238 | 239 | // Set data rate 240 | config |= m_dataRate; 241 | 242 | config |= MUX_BY_CHANNEL[channel]; 243 | 244 | // Set the high threshold register 245 | // Shift 12-bit results left 4 bits for the ADS1015 246 | writeRegister(ADS1X15_REG_POINTER_HITHRESH, threshold << m_bitShift); 247 | 248 | // Write config register to the ADC 249 | writeRegister(ADS1X15_REG_POINTER_CONFIG, config); 250 | } 251 | 252 | /**************************************************************************/ 253 | /*! 254 | @brief In order to clear the comparator, we need to read the 255 | conversion results. This function reads the last conversion 256 | results without changing the config value. 257 | 258 | @return the last ADC reading 259 | */ 260 | /**************************************************************************/ 261 | int16_t Adafruit_ADS1X15::getLastConversionResults() { 262 | // Read the conversion results 263 | uint16_t res = readRegister(ADS1X15_REG_POINTER_CONVERT) >> m_bitShift; 264 | if (m_bitShift == 0) { 265 | return (int16_t)res; 266 | } else { 267 | // Shift 12-bit results right 4 bits for the ADS1015, 268 | // making sure we keep the sign bit intact 269 | if (res > 0x07FF) { 270 | // negative number - extend the sign to 16th bit 271 | res |= 0xF000; 272 | } 273 | return (int16_t)res; 274 | } 275 | } 276 | 277 | /**************************************************************************/ 278 | /*! 279 | @brief Return the current fs range for the configured gain 280 | 281 | @return the fsRange for the configured gain, or zero. 282 | Zero should not be possible thereby indicating error 283 | */ 284 | /**************************************************************************/ 285 | float Adafruit_ADS1X15::getFsRange() { 286 | // see data sheet Table 3 287 | switch (m_gain) { 288 | case GAIN_TWOTHIRDS: 289 | return 6.144f; 290 | break; 291 | case GAIN_ONE: 292 | return 4.096f; 293 | break; 294 | case GAIN_TWO: 295 | return 2.048f; 296 | break; 297 | case GAIN_FOUR: 298 | return 1.024f; 299 | break; 300 | case GAIN_EIGHT: 301 | return 0.512f; 302 | break; 303 | case GAIN_SIXTEEN: 304 | return 0.256f; 305 | break; 306 | default: 307 | return 0.0f; 308 | } 309 | } 310 | 311 | /**************************************************************************/ 312 | /*! 313 | @brief Compute volts for the given raw counts. 314 | 315 | @param counts the ADC reading in raw counts 316 | 317 | @return the ADC reading in volts 318 | */ 319 | /**************************************************************************/ 320 | float Adafruit_ADS1X15::computeVolts(int16_t counts) { 321 | return counts * (getFsRange() / (32768 >> m_bitShift)); 322 | } 323 | 324 | /**************************************************************************/ 325 | /*! 326 | @brief Non-blocking start conversion function 327 | 328 | Call getLastConversionResults() once conversionComplete() returns true. 329 | In continuous mode, getLastConversionResults() will always return the 330 | latest result. 331 | ALERT/RDY pin is set to RDY mode, and a 8us pulse is generated every 332 | time new data is ready. 333 | 334 | @param mux mux field value 335 | @param continuous continuous if set, otherwise single shot 336 | */ 337 | /**************************************************************************/ 338 | void Adafruit_ADS1X15::startADCReading(uint16_t mux, bool continuous) { 339 | // Start with default values 340 | uint16_t config = 341 | ADS1X15_REG_CONFIG_CQUE_1CONV | // Set CQUE to any value other than 342 | // None so we can use it in RDY mode 343 | ADS1X15_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) 344 | ADS1X15_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) 345 | ADS1X15_REG_CONFIG_CMODE_TRAD; // Traditional comparator (default val) 346 | 347 | if (continuous) { 348 | config |= ADS1X15_REG_CONFIG_MODE_CONTIN; 349 | } else { 350 | config |= ADS1X15_REG_CONFIG_MODE_SINGLE; 351 | } 352 | 353 | // Set PGA/voltage range 354 | config |= m_gain; 355 | 356 | // Set data rate 357 | config |= m_dataRate; 358 | 359 | // Set channels 360 | config |= mux; 361 | 362 | // Set 'start single-conversion' bit 363 | config |= ADS1X15_REG_CONFIG_OS_SINGLE; 364 | 365 | // Write config register to the ADC 366 | writeRegister(ADS1X15_REG_POINTER_CONFIG, config); 367 | 368 | // Set ALERT/RDY to RDY mode. 369 | writeRegister(ADS1X15_REG_POINTER_HITHRESH, 0x8000); 370 | writeRegister(ADS1X15_REG_POINTER_LOWTHRESH, 0x0000); 371 | } 372 | 373 | /**************************************************************************/ 374 | /*! 375 | @brief Returns true if conversion is complete, false otherwise. 376 | 377 | @return True if conversion is complete, false otherwise. 378 | */ 379 | /**************************************************************************/ 380 | bool Adafruit_ADS1X15::conversionComplete() { 381 | return (readRegister(ADS1X15_REG_POINTER_CONFIG) & 0x8000) != 0; 382 | } 383 | 384 | /**************************************************************************/ 385 | /*! 386 | @brief Writes 16-bits to the specified destination register 387 | 388 | @param reg register address to write to 389 | @param value value to write to register 390 | */ 391 | /**************************************************************************/ 392 | void Adafruit_ADS1X15::writeRegister(uint8_t reg, uint16_t value) { 393 | buffer[0] = reg; 394 | buffer[1] = value >> 8; 395 | buffer[2] = value & 0xFF; 396 | m_i2c_dev->write(buffer, 3); 397 | } 398 | 399 | /**************************************************************************/ 400 | /*! 401 | @brief Read 16-bits from the specified destination register 402 | 403 | @param reg register address to read from 404 | 405 | @return 16 bit register value read 406 | */ 407 | /**************************************************************************/ 408 | uint16_t Adafruit_ADS1X15::readRegister(uint8_t reg) { 409 | buffer[0] = reg; 410 | m_i2c_dev->write(buffer, 1); 411 | m_i2c_dev->read(buffer, 2); 412 | return ((buffer[0] << 8) | buffer[1]); 413 | } 414 | --------------------------------------------------------------------------------