├── ADS1115_Test_Wiring.png ├── List of public functions.pdf ├── ADS1115_ATtiny_Test_Wiring.png ├── .gitignore ├── src ├── ADS1115_config.h ├── ADS1015_WE.h ├── ADS1115_WE.h └── ADS1115_WE.cpp ├── .vscode └── extensions.json ├── library.properties ├── LICENSE ├── .github └── workflows │ └── main.yml ├── examples ├── Who_Am_I │ └── Who_Am_I.ino ├── Using_two_ADS1115 │ └── Using_two_ADS1115.ino ├── Result_Format_Options │ └── Result_Format_Options.ino ├── Single_Shot_Conv_Ready_Controlled │ └── Single_Shot_Conv_Ready_Controlled.ino ├── Single_Shot │ └── Single_Shot.ino ├── Alert_Window_Mode │ └── Alert_Window_Mode.ino ├── Alert_Window_Mode_with_Latch │ └── Alert_Window_Mode_with_Latch.ino ├── Conv_Ready_Alert_Pin_Controlled │ └── Conv_Ready_Alert_Pin_Controlled.ino ├── Continuous │ └── Continuous.ino ├── Continuous_ADS1015 │ └── Continuous_ADS1015.ino ├── Continuous_ATtiny │ └── Continuous_ATtiny.ino └── Auto_Range │ └── Auto_Range.ino ├── README.md └── keywords.txt /ADS1115_Test_Wiring.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wollewald/ADS1115_WE/HEAD/ADS1115_Test_Wiring.png -------------------------------------------------------------------------------- /List of public functions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wollewald/ADS1115_WE/HEAD/List of public functions.pdf -------------------------------------------------------------------------------- /ADS1115_ATtiny_Test_Wiring.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wollewald/ADS1115_WE/HEAD/ADS1115_ATtiny_Test_Wiring.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .pio 2 | .vscode/.browse.c_cpp.db* 3 | .vscode/c_cpp_properties.json 4 | .vscode/launch.json 5 | .vscode/ipch 6 | -------------------------------------------------------------------------------- /src/ADS1115_config.h: -------------------------------------------------------------------------------- 1 | #ifndef ADS1115_CONFIG_H_ 2 | #define ADS1115_CONFIG_H_ 3 | /* Uncomment the following line to use TinyWireM instead of Wire */ 4 | //#define USE_TINY_WIRE_M_ 5 | #endif -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | // See http://go.microsoft.com/fwlink/?LinkId=827846 3 | // for the documentation about the extensions.json format 4 | "recommendations": [ 5 | "platformio.platformio-ide" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=ADS1115_WE 2 | version=1.5.5 3 | author=Wolfgang Ewald 4 | maintainer=Wolfgang Ewald 5 | sentence=A library for the ADS1115 and the ADS1015 ADC 6 | paragraph=An Arduino library for the 16-bit, 4-channel ADS1115 and the 12-Bit, 4-channel ADS1015 ADC, convenient to use. All features of the ADS1115 are implemented, including alert functions. 7 | category=Signal Input/Output 8 | url=https://github.com/wollewald/ADS1115_WE 9 | architectures=* 10 | includes=ADS1115_WE.h 11 | 12 | -------------------------------------------------------------------------------- /src/ADS1015_WE.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * This is a library for the ADS1115 and ADS1015 A/D Converter 4 | * 5 | * You'll find several example sketches which should enable you to use the library. 6 | * 7 | * You are free to use it, change it or build on it. In case you like it, it would 8 | * be cool if you give it a star. 9 | * 10 | * If you find bugs, please inform me! 11 | * 12 | * Written by Wolfgang (Wolle) Ewald 13 | * https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 14 | * https://wolles-elektronikkiste.de/ads1115 (German) 15 | * 16 | * 17 | ******************************************************************************/ 18 | 19 | #ifndef ADS1015_WE_H 20 | #define ADS1015_WE_H 21 | #include 22 | 23 | #if ARDUINO < 100 24 | #include 25 | #else 26 | #include 27 | #endif 28 | 29 | class ADS1015_WE : public ADS1115_WE { 30 | public: 31 | using ADS1115_WE::ADS1115_WE; 32 | }; 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Wolfgang (Wolle) Ewald 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: PlatformIO CI 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | 8 | runs-on: ubuntu-latest 9 | strategy: 10 | matrix: 11 | example: [examples/Single_Shot/Single_Shot.ino, examples/Continuous/Continuous.ino, examples/Alert_Window_Mode/Alert_Window_Mode.ino] 12 | 13 | steps: 14 | - uses: actions/checkout@v4 15 | - name: Cache pip 16 | uses: actions/cache@v4 17 | with: 18 | path: ~/.cache/pip 19 | key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }} 20 | restore-keys: ${{ runner.os }}-pip- 21 | - name: Cache PlatformIO 22 | uses: actions/cache@v4 23 | with: 24 | path: ~/.platformio 25 | key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }} 26 | - name: Set up Python 27 | uses: actions/setup-python@v5 28 | - name: Install PlatformIO 29 | run: | 30 | python -m pip install --upgrade pip setuptools 31 | pip install --upgrade platformio 32 | - name: Run PlatformIO 33 | run: pio ci --lib="." --board=uno --board=esp-wrover-kit --board=d1_mini 34 | env: 35 | PLATFORMIO_CI_SRC: ${{ matrix.example }} 36 | -------------------------------------------------------------------------------- /examples/Who_Am_I/Who_Am_I.ino: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Example sketch for the ADS1115_WE library 3 | * 4 | * This sketch checks whether you have an ADS1115 or ADS1015 module. The last 5 | * four bits of raw values obtained from an ADS1015 should be zero. Connect A0 6 | * to a voltage different from GND. The sketch also checks how much time is 7 | * needed to perform ten measurements at lowest data rate, which is 128 SPS for 8 | * the ADS1015 and 8 SPS for the ADS1115. 9 | * 10 | * Further information can be found on: 11 | * https://wolles-elektronikkiste.de/ads1115 (German) 12 | * https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 13 | * 14 | ***************************************************************************/ 15 | 16 | #include 17 | #include 18 | #define I2C_ADDRESS 0x48 19 | 20 | ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); 21 | 22 | void setup() { 23 | Wire.begin(); 24 | Serial.begin(9600); 25 | if(!adc.init()){ 26 | Serial.println("ADS1115 not connected!"); 27 | } 28 | adc.setVoltageRange_mV(ADS1115_RANGE_6144); 29 | adc.setCompareChannels(ADS1115_COMP_0_GND); 30 | Serial.println("ADS1115/ADS1015 Example Sketch - Who am I"); 31 | Serial.println("Performing 10 single ended conversions A0 vs. GND:"); 32 | uint16_t checkSum = 0; 33 | for(int i=0; i<10; i++){ 34 | adc.startSingleMeasurement(); 35 | while(adc.isBusy()){} 36 | int16_t raw = adc.getRawResult(); 37 | Serial.println(raw, BIN); 38 | checkSum += raw & 0xF; 39 | } 40 | Serial.println(); 41 | Serial.print("Check Sum (Sum of the last 4 bits): "); 42 | Serial.println(checkSum); 43 | 44 | adc.setConvRate(ADS1115_8_SPS); // = ADS1015_128_SPS = 0x0000 45 | unsigned long startingTime = millis(); 46 | for(int i=0; i<10; i++){ 47 | adc.startSingleMeasurement(); 48 | while(adc.isBusy()){} 49 | } 50 | unsigned long duration = millis() - startingTime; 51 | Serial.print("Time needed for 10 conversions at slowest sample rate [ms]: "); 52 | Serial.println(duration); 53 | Serial.println(); 54 | 55 | if(checkSum && duration > 1000){ 56 | Serial.println("I am an ADS1115!"); 57 | } 58 | else if (!checkSum && duration < 1000){ 59 | Serial.println("I am an ADS1015!"); 60 | } 61 | else { 62 | Serial.println("Sorry, don't know who I am!"); 63 | } 64 | } 65 | 66 | void loop() {} 67 | -------------------------------------------------------------------------------- /examples/Using_two_ADS1115/Using_two_ADS1115.ino: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Example sketch for the ADS1115_WE library 3 | * 4 | * This sketch shows how to use two ADS1115 modules. In order to set the address, 5 | * connect the address pin to: 6 | * 7 | * GND -> 0x48 (or leave unconnected) 8 | * VCC -> 0x49 9 | * SDA -> 0x4A 10 | * SCL -> 0x4B 11 | * 12 | * When you have understood how it works you can easily add two additional ADS1115. 13 | * Of course there is potential to shorten the code, e.g. by setting up the ADCs 14 | * as array. 15 | * 16 | * If you need up to eight ADS1115 modules you can use an ESP32 with its two I2C 17 | * interfaces: 18 | * https://wolles-elektronikkiste.de/en/how-to-use-the-i2c-interfaces-of-the-esp32 19 | * 20 | * If you need up to 32 ADS1115 modules you can use a multiplexer like the TSCA9548A: 21 | * https://wolles-elektronikkiste.de/en/tca9548a-i2c-multiplexer 22 | * 23 | * Or you combine both and control up to 64 ADS1115 modules. 24 | * 25 | * Further information about the ADS1115 can be found on: 26 | * https://wolles-elektronikkiste.de/ads1115 (German) 27 | * https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 28 | * 29 | ***************************************************************************/ 30 | 31 | #include 32 | #include 33 | #define I2C_ADDRESS_1 0x48 34 | #define I2C_ADDRESS_2 0x49 35 | 36 | ADS1115_WE adc_1 = ADS1115_WE(I2C_ADDRESS_1); 37 | ADS1115_WE adc_2 = ADS1115_WE(I2C_ADDRESS_2); 38 | 39 | void setup() { 40 | Wire.begin(); 41 | Serial.begin(9600); 42 | 43 | if(!adc_1.init()){ 44 | Serial.print("ADS1115 No 1 not connected!"); 45 | } 46 | adc_1.setVoltageRange_mV(ADS1115_RANGE_6144); 47 | adc_1.setMeasureMode(ADS1115_CONTINUOUS); 48 | adc_1.setCompareChannels(ADS1115_COMP_0_GND); 49 | 50 | if(!adc_2.init()){ 51 | Serial.print("ADS1115 No 2 not connected!"); 52 | } 53 | adc_2.setVoltageRange_mV(ADS1115_RANGE_6144); 54 | adc_2.setMeasureMode(ADS1115_CONTINUOUS); 55 | adc_2.setCompareChannels(ADS1115_COMP_0_GND); 56 | } 57 | 58 | void loop() { 59 | float voltage = 0.0; 60 | 61 | voltage = adc_1.getResult_V(); 62 | Serial.println("Voltage [V], ADS1115 No 1: "); 63 | Serial.println(voltage); 64 | 65 | voltage = adc_2.getResult_V(); 66 | Serial.println("Voltage [V], ADS1115 No 2: "); 67 | Serial.println(voltage); 68 | 69 | Serial.println("****************************"); 70 | delay(1000); 71 | } 72 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ADS1115_WE 2 | 3 | An Arduino library for the 16-bit, 4-channel ADS1115 and the 12-Bit, 4-channel ADS1015 ADC with gain and alert functions. 4 | 5 | I have have tried to optimize the library for convenience to use. If you try the examples I recommend to start with `Single_Shot.ino`. 6 | 7 | You can find more details here: 8 | 9 | https://wolles-elektronikkiste.de/ads1115 (German) 10 | 11 | https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 12 | 13 | All features of the ADS1115 and ADS1015 are implemented, including alert functions. 14 | 15 | The examples are written for the ADS1115 with one exception, which is Continuous_ADS1015.ino. This shows how to "translate" the sketches 16 | for the ADS1015. Most enum values like ADS1115_RANGE_6144 and ADS1015_RANGE_6144 are even identical. The exceptions are the enum values for 17 | the conversion rate. 18 | 19 | In version 1.4.1 I have implemented the option to use TinyWireM instead of Wire. Therefore the library can be used, for example, on 20 | an ATtiny85. 21 | 22 | If you like the library it would be cool if you can give it a star. If you find bugs, please inform me. 23 | 24 |

Some remarks on the continuous mode

25 | 26 | When you change channels in continuous mode using ``setCompareChannels()``, the current conversion will be completed first before the next 27 | measurement for the new channel will be started. This means you have to wait the time of two conversions before you can be sure that a measured 28 | value of the new channel is available. In contrast to the single shot mode, there is no way to determine when this process is completed. 29 | Therefore I added delays according to the rate that you have set. The disadvantage is that changing channels is a blocking process. 30 | 31 | If you don't want blocking code, you can use the function ``setCompareChannels_nonblock()``. But please be aware that you have to ensure yourself 32 | that the measured value has been obtained from the new channel. 33 | 34 | I recommend using the single shot mode instead, because in this mode you can immediately start a new measurement on the new channel and you can 35 | check whether the current conversion is completed with the ``isBusy()`` function. 36 | 37 |

Beware of fake modules

38 | 39 | There are ADS1115 modules which use ADS1015 ICs and also there are ADS1015 modules which are based on ADS1115 ICs. In theory you should 40 | recognize the IC by its label which is "BRPI" for the ADS1015 and "BOGI" for the ADS1115. But I have even found ADS1115 ICs labeled with 41 | "BRPI" which is definitely a fake. The difference between the ADS1115 and the ADS1015 is a) the 16-bit vs. 12-bit resolution an b) the speed. 42 | 43 | This example is almost funny: 44 | 45 | ![ADS1015_absurd](https://github.com/wollewald/ADS1115_WE/assets/41305162/756f4cd6-4f7d-497a-b742-76fae73d99aa) 46 | 47 | If you want to find out what you really have on on your module, then try the example sketch "Who_Am_I.ino". Do not change anything apart from 48 | the I2C address if necessary. 49 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For ADS1115_WE 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | ADS1115_WE KEYWORD1 10 | ADS1015_WE KEYWORD1 11 | 12 | # ENUM TYPES 13 | ADS1115_COMP_QUE KEYWORD1 14 | ADS1115_LATCH KEYWORD1 15 | ADS1115_ALERT_POL KEYWORD1 16 | ADS1115_COMP_MODE KEYWORD1 17 | ADS1115_CONV_RATE KEYWORD1 18 | ADS1115_MEASURE_MODE KEYWORD1 19 | ADS1115_RANGE KEYWORD1 20 | ADS1115_MUX KEYWORD1 21 | ADS1015_MUX KEYWORD1 22 | ADS1115_STATUS_OR_START KEYWORD1 23 | 24 | 25 | ####################################### 26 | # Methods and Functions (KEYWORD2) 27 | ####################################### 28 | 29 | reset KEYWORD2 30 | init KEYWORD2 31 | isDisconnected KEYWORD2 32 | setAlertPinMode KEYWORD2 33 | setAlertLatch KEYWORD2 34 | setAlertPol KEYWORD2 35 | setAlertModeAndLimit_V KEYWORD2 36 | setConvRate KEYWORD2 37 | getConvRate KEYWORD2 38 | setMeasureMode KEYWORD2 39 | setVoltageRange_mV KEYWORD2 40 | setAutoRange KEYWORD2 41 | setCompareChannels KEYWORD2 42 | setCompareChannels_nonblock KEYWORD2 43 | isBusy KEYWORD2 44 | startSingleMeasurement KEYWORD2 45 | getResult_V KEYWORD2 46 | getResult_mV KEYWORD2 47 | getRawResult KEYWORD2 48 | getResultWithRange KEYWORD2 49 | getVoltageRange_mV KEYWORD2 50 | setAlertPinToConversionReady KEYWORD2 51 | clearAlert KEYWORD2 52 | 53 | ####################################### 54 | # Constants (LITERAL1) 55 | ####################################### 56 | ADS1115_CONV_REG LITERAL1 57 | ADS1115_CONFIG_REG LITERAL1 58 | ADS1115_LO_THRESH_REG LITERAL1 59 | ADS1115_HI_THRESH_REG LITERAL1 60 | ADS1115_REG_FACTOR LITERAL1 61 | ADS1115_REG_RESET_VAL LITERAL1 62 | 63 | # ENUM VALUES 64 | ADS1115_ASSERT_AFTER_1 LITERAL1 65 | ADS1115_ASSERT_AFTER_2 LITERAL1 66 | ADS1115_ASSERT_AFTER_4 LITERAL1 67 | ADS1115_DISABLE_ALERT LITERAL1 68 | ADS1115_LATCH_DISABLED LITERAL1 69 | ADS1115_LATCH_ENABLED LITERAL1 70 | ADS1115_ACT_LOW LITERAL1 71 | ADS1115_ACT_HIGH LITERAL1 72 | ADS1115_MAX_LIMIT LITERAL1 73 | ADS1115_WINDOW LITERAL1 74 | ADS1115_8_SPS LITERAL1 75 | ADS1115_16_SPS LITERAL1 76 | ADS1115_32_SPS LITERAL1 77 | ADS1115_64_SPS LITERAL1 78 | ADS1115_128_SPS LITERAL1 79 | ADS1115_250_SPS LITERAL1 80 | ADS1115_475_SPS LITERAL1 81 | ADS1115_860_SPS LITERAL1 82 | ADS1115_CONTINUOUS LITERAL1 83 | ADS1115_CONTINOUS LITERAL1 84 | ADS1115_SINGLE LITERAL1 85 | ADS1115_RANGE_6144 LITERAL1 86 | ADS1115_RANGE_4096 LITERAL1 87 | ADS1115_RANGE_2048 LITERAL1 88 | ADS1115_RANGE_1024 LITERAL1 89 | ADS1115_RANGE_0512 LITERAL1 90 | ADS1115_RANGE_0256 LITERAL1 91 | ADS1115_COMP_0_1 LITERAL1 92 | ADS1115_COMP_0_3 LITERAL1 93 | ADS1115_COMP_1_3 LITERAL1 94 | ADS1115_COMP_2_3 LITERAL1 95 | ADS1115_COMP_0_GND LITERAL1 96 | ADS1115_COMP_1_GND LITERAL1 97 | ADS1115_COMP_2_GND LITERAL1 98 | ADS1115_COMP_3_GND LITERAL1 99 | ADS1115_BUSY LITERAL1 100 | ADS1015_START_ISREADY LITERAL1 101 | ADS1015_ASSERT_AFTER_1 LITERAL1 102 | ADS1015_ASSERT_AFTER_2 LITERAL1 103 | ADS1015_ASSERT_AFTER_4 LITERAL1 104 | ADS1015_DISABLE_ALERT LITERAL1 105 | ADS1015_LATCH_DISABLED LITERAL1 106 | ADS1015_LATCH_ENABLED LITERAL1 107 | ADS1015_ACT_LOW LITERAL1 108 | ADS1015_ACT_HIGH LITERAL1 109 | ADS1015_MAX_LIMIT LITERAL1 110 | ADS1015_WINDOW LITERAL1 111 | ADS1015_8_SPS LITERAL1 112 | ADS1015_16_SPS LITERAL1 113 | ADS1015_32_SPS LITERAL1 114 | ADS1015_64_SPS LITERAL1 115 | ADS1015_128_SPS LITERAL1 116 | ADS1015_250_SPS LITERAL1 117 | ADS1015_475_SPS LITERAL1 118 | ADS1015_860_SPS LITERAL1 119 | ADS1015_CONTINUOUS LITERAL1 120 | ADS1015_CONTINOUS LITERAL1 121 | ADS1015_SINGLE LITERAL1 122 | ADS1015_RANGE_6144 LITERAL1 123 | ADS1015_RANGE_4096 LITERAL1 124 | ADS1015_RANGE_2048 LITERAL1 125 | ADS1015_RANGE_1024 LITERAL1 126 | ADS1015_RANGE_0512 LITERAL1 127 | ADS1015_RANGE_0256 LITERAL1 128 | ADS1015_COMP_0_1 LITERAL1 129 | ADS1015_COMP_0_3 LITERAL1 130 | ADS1015_COMP_1_3 LITERAL1 131 | ADS1015_COMP_2_3 LITERAL1 132 | ADS1015_COMP_0_GND LITERAL1 133 | ADS1015_COMP_1_GND LITERAL1 134 | ADS1015_COMP_2_GND LITERAL1 135 | ADS1015_COMP_3_GND LITERAL1 136 | ADS1015_BUSY LITERAL1 137 | ADS1015_START_ISREADY LITERAL1 138 | -------------------------------------------------------------------------------- /examples/Result_Format_Options/Result_Format_Options.ino: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Example sketch for the ADS1115_WE library 3 | * 4 | * This sketch shows how to obtain results using different scales / formats. 5 | * 6 | * Further information can be found on: 7 | * https://wolles-elektronikkiste.de/ads1115 (German) 8 | * https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 9 | * 10 | ***************************************************************************/ 11 | 12 | #include 13 | #include 14 | #define I2C_ADDRESS 0x48 15 | 16 | /* There are several ways to create your ADS1115_WE object: 17 | * ADS1115_WE adc = ADS1115_WE(); -> uses Wire / I2C Address = 0x48 18 | * ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); -> uses Wire / I2C_ADDRESS 19 | * ADS1115_WE adc = ADS1115_WE(&Wire); -> you can pass any TwoWire object / I2C Address = 0x48 20 | * ADS1115_WE adc = ADS1115_WE(&Wire, I2C_ADDRESS); -> all together 21 | */ 22 | ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); 23 | 24 | void setup() { 25 | Wire.begin(); 26 | Serial.begin(9600); 27 | if(!adc.init()){ 28 | Serial.println("ADS1115 not connected!"); 29 | } 30 | 31 | adc.setVoltageRange_mV(ADS1115_RANGE_6144); //comment line/change parameter to change range 32 | adc.setCompareChannels(ADS1115_COMP_0_1); //comment line/change parameter to change channel 33 | adc.setMeasureMode(ADS1115_CONTINUOUS); //comment line/change parameter to change mode 34 | 35 | Serial.println("ADS1115 Example Sketch - Results in different scales / formats"); 36 | Serial.println("All results are for channel 0 vs. GND"); 37 | Serial.println(); 38 | } 39 | 40 | void loop() { 41 | 42 | /* Output in Volt or in Millivolt */ 43 | 44 | float voltageInMillivolt = adc.getResult_mV(); 45 | Serial.print("Result in Millivolt [mV]: "); 46 | Serial.println(voltageInMillivolt); 47 | 48 | float voltageInVolt = adc.getResult_V(); 49 | Serial.print("Result in Volt [V]: "); 50 | Serial.println(voltageInVolt); 51 | 52 | /* Get the raw result from the conversion register. The conversion register 53 | * contains the conversion result of the amplified (!) voltage. This means the 54 | * value depends on the voltage as well as on the voltage range. E.g. if the 55 | * voltage range is 6144 mV (ADS1115_RANGE_6144), +32768 is 6144 mV; if the 56 | * range is 4096 mV, +32768 is 4096 mV, and so on. To be exact: +32768 would 57 | * the range value, but the register max.value is 32767. 58 | */ 59 | int rawResult = adc.getRawResult(); 60 | Serial.print("Raw Result : "); 61 | Serial.println(rawResult); 62 | 63 | /* Scaling of the result to a different range: 64 | * The results in the conversion register are in a range of -32768 to +32767 65 | * You might want to receive the result in a different scale, e.g. -1024 to 1023. 66 | * For -1024 to 1023, and if you have chosen e.g. ADS1115_RANGE_4096, 0 Volt would 67 | * give 0 as result and 4096 * (32767/32768) mV would give 1023. -4096 mV would 68 | * give -1024. 69 | */ 70 | int scaledResult = adc.getResultWithRange(-1024, 1023); 71 | Serial.print("Scaled result : "); 72 | Serial.println(scaledResult); 73 | 74 | /* Scaling of the result to a different range plus scaling to a voltage range: 75 | * You can use this variant if you also want to scale to a voltage range. E.g. in 76 | * in order to get results equivalent to an Arduino UNO (10 bit, 5000 mV range), you 77 | * would choose getResultWithRange(-1024, 1023, 5000). A difference to the Arduino 78 | * UNO is that you can measure negative voltages. 79 | * You have to ensure that the voltage range you scale to is smaller than the 80 | * measuring voltage range. For this example only ADS1115_RANGE_6144 would cover the 81 | * scale up to 5000 mV. 82 | */ 83 | int scaledResultWithMaxVoltage = adc.getResultWithRange(-1024, 1023, 5000); 84 | Serial.print("Scaled result with voltage scale : "); 85 | Serial.println(scaledResultWithMaxVoltage); 86 | 87 | /* This function returns the voltage range ADS1115_RANGE_XXXX in Millivolt */ 88 | unsigned int voltRange = adc.getVoltageRange_mV(); 89 | Serial.print("Voltage Range of ADS1115 [mV]: "); 90 | Serial.println(voltRange); 91 | 92 | Serial.println("-------------------------------"); 93 | delay(2000); 94 | } 95 | -------------------------------------------------------------------------------- /examples/Single_Shot_Conv_Ready_Controlled/Single_Shot_Conv_Ready_Controlled.ino: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Example sketch for the ADS1115_WE library 3 | * 4 | * This sketch shows how you can use the conversion ready bit with the isBusy function. 5 | * This does not work in the continuous mode. 6 | * 7 | * Further information can be found on: 8 | * https://wolles-elektronikkiste.de/ads1115 (German) 9 | * https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 10 | * 11 | ***************************************************************************/ 12 | 13 | #include 14 | #include 15 | #define I2C_ADDRESS 0x48 16 | 17 | /* There are several ways to create your ADS1115_WE object: 18 | * ADS1115_WE adc = ADS1115_WE(); -> uses Wire / I2C Address = 0x48 19 | * ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); -> uses Wire / I2C_ADDRESS 20 | * ADS1115_WE adc = ADS1115_WE(&Wire); -> you can pass any TwoWire object / I2C Address = 0x48 21 | * ADS1115_WE adc = ADS1115_WE(&Wire, I2C_ADDRESS); -> all together 22 | */ 23 | ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); 24 | 25 | void setup() { 26 | Wire.begin(); 27 | Serial.begin(9600); 28 | if(!adc.init()){ 29 | Serial.println("ADS1115 not connected!"); 30 | } 31 | 32 | /* Set the voltage range of the ADC to adjust the gain 33 | * Please note that you must not apply more than VDD + 0.3V to the input pins! 34 | * 35 | * ADS1115_RANGE_6144 -> +/- 6144 mV 36 | * ADS1115_RANGE_4096 -> +/- 4096 mV 37 | * ADS1115_RANGE_2048 -> +/- 2048 mV (default) 38 | * ADS1115_RANGE_1024 -> +/- 1024 mV 39 | * ADS1115_RANGE_0512 -> +/- 512 mV 40 | * ADS1115_RANGE_0256 -> +/- 256 mV 41 | */ 42 | adc.setVoltageRange_mV(ADS1115_RANGE_6144); //comment line/change parameter to change range 43 | 44 | /* Set the inputs to be compared 45 | * 46 | * ADS1115_COMP_0_1 -> compares 0 with 1 (default) 47 | * ADS1115_COMP_0_3 -> compares 0 with 3 48 | * ADS1115_COMP_1_3 -> compares 1 with 3 49 | * ADS1115_COMP_2_3 -> compares 2 with 3 50 | * ADS1115_COMP_0_GND -> compares 0 with GND 51 | * ADS1115_COMP_1_GND -> compares 1 with GND 52 | * ADS1115_COMP_2_GND -> compares 2 with GND 53 | * ADS1115_COMP_3_GND -> compares 3 with GND 54 | */ 55 | adc.setCompareChannels(ADS1115_COMP_0_GND); //comment line/change parameter to change channels 56 | 57 | /* Set number of conversions after which the alert pin will assert 58 | * - or you can disable the alert 59 | * 60 | * ADS1115_ASSERT_AFTER_1 -> after 1 conversion 61 | * ADS1115_ASSERT_AFTER_2 -> after 2 conversions 62 | * ADS1115_ASSERT_AFTER_3 -> after 3 conversions 63 | * ADS1115_DISABLE_ALERT -> disable comparator / alert pin (default) 64 | */ 65 | //adc.setAlertPinMode(ADS1115_ASSERT_AFTER_1); //uncomment if you want to change the default 66 | 67 | /* Set the conversion rate in SPS (samples per second) 68 | * Options should be self-explaining: 69 | * 70 | * ADS1115_8_SPS 71 | * ADS1115_16_SPS 72 | * ADS1115_32_SPS 73 | * ADS1115_64_SPS 74 | * ADS1115_128_SPS (default) 75 | * ADS1115_250_SPS 76 | * ADS1115_475_SPS 77 | * ADS1115_860_SPS 78 | */ 79 | adc.setConvRate(ADS1115_8_SPS); //comment line/change parameter to change SPS 80 | 81 | /* Set continuous or single shot mode: 82 | * 83 | * ADS1115_CONTINUOUS -> continuous mode 84 | * ADS1115_SINGLE -> single shot mode (default) 85 | * 86 | * Continuous mode does not work with conversion ready (isBusy), but it works with the 87 | * conversion ready alert pin. Confusing, but that's a property of the ADS1115 and not 88 | * a property of the library. 89 | */ 90 | // adc.setMeasureMode(ADS1115_CONTINUOUS); // continuous mode does not work with conversion ready (isBusy) 91 | 92 | /* Choose maximum limit or maximum and minimum alert limit (window) in volts - alert pin will 93 | * assert when measured values are beyond the maximum limit or outside the window 94 | * Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 95 | * In max limit mode the minimum value is the limit where the alert pin assertion will be cleared (if 96 | * not latched) 97 | * 98 | * ADS1115_MAX_LIMIT 99 | * ADS1115_WINDOW 100 | * 101 | */ 102 | //adc.setAlertModeAndLimit_V(ADS1115_MAX_LIMIT, 3.0, 1.5); //uncomment if you want to change the default 103 | 104 | /* Enable or disable latch. If latch is enabled the alert pin will assert until the 105 | * conversion register is read (getResult functions). If disabled the alert pin assertion will be 106 | * cleared with next value within limits. 107 | * 108 | * ADS1115_LATCH_DISABLED (default) 109 | * ADS1115_LATCH_ENABLED 110 | */ 111 | //adc.setAlertLatch(ADS1115_LATCH_ENABLED); //uncomment if you want to change the default 112 | 113 | /* Sets the alert pin polarity if active: 114 | * 115 | * ADS1115_ACT_LOW -> active low (default) 116 | * ADS1115_ACT_HIGH -> active high 117 | */ 118 | //adc.setAlertPol(ADS1115_ACT_LOW); //uncomment if you want to change the default 119 | 120 | /* With this function the alert pin will assert, when a conversion is ready. 121 | * In order to deactivate, use the setAlertLimit_V function 122 | */ 123 | //adc.setAlertPinToConversionReady(); //uncomment if you want to change the default 124 | 125 | Serial.println("ADS1115 Example Sketch - Single Shot, Conversion Ready (isBusy) controlled"); 126 | Serial.println(); 127 | } 128 | 129 | void loop() { 130 | float voltage = 0.0; 131 | for(int i=0; i<32; i++){ // counter is 32, conversion rate is 8 SPS --> 4s 132 | adc.startSingleMeasurement(); 133 | while(adc.isBusy()){delay(0);} 134 | } 135 | voltage = adc.getResult_V(); // alternative: getResult_mV for Millivolt 136 | Serial.print("Channel 0 vs GND [V]: "); 137 | Serial.println(voltage); 138 | Serial.println("-------------------------------"); 139 | } 140 | -------------------------------------------------------------------------------- /examples/Single_Shot/Single_Shot.ino: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Example sketch for the ADS1115_WE library 3 | * 4 | * This sketch shows how to use the ADS1115 in single shot mode. 5 | * 6 | * Further information can be found on: 7 | * https://wolles-elektronikkiste.de/ads1115 (German) 8 | * https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 9 | * 10 | ***************************************************************************/ 11 | 12 | #include 13 | #include 14 | #define I2C_ADDRESS 0x48 15 | 16 | /* There are several ways to create your ADS1115_WE object: 17 | * ADS1115_WE adc = ADS1115_WE(); -> uses Wire / I2C Address = 0x48 18 | * ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); -> uses Wire / I2C_ADDRESS 19 | * ADS1115_WE adc = ADS1115_WE(&Wire); -> you can pass any TwoWire object / I2C Address = 0x48 20 | * ADS1115_WE adc = ADS1115_WE(&Wire, I2C_ADDRESS); -> all together 21 | */ 22 | ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); 23 | 24 | void setup() { 25 | Wire.begin(); 26 | Serial.begin(9600); 27 | if(!adc.init()){ 28 | Serial.println("ADS1115 not connected!"); 29 | } 30 | 31 | /* Set the voltage range of the ADC to adjust the gain 32 | * Please note that you must not apply more than VDD + 0.3V to the input pins! 33 | * 34 | * ADS1115_RANGE_6144 -> +/- 6144 mV 35 | * ADS1115_RANGE_4096 -> +/- 4096 mV 36 | * ADS1115_RANGE_2048 -> +/- 2048 mV (default) 37 | * ADS1115_RANGE_1024 -> +/- 1024 mV 38 | * ADS1115_RANGE_0512 -> +/- 512 mV 39 | * ADS1115_RANGE_0256 -> +/- 256 mV 40 | */ 41 | adc.setVoltageRange_mV(ADS1115_RANGE_6144); //comment line/change parameter to change range 42 | 43 | /* Set the inputs to be compared 44 | * 45 | * ADS1115_COMP_0_1 -> compares 0 with 1 (default) 46 | * ADS1115_COMP_0_3 -> compares 0 with 3 47 | * ADS1115_COMP_1_3 -> compares 1 with 3 48 | * ADS1115_COMP_2_3 -> compares 2 with 3 49 | * ADS1115_COMP_0_GND -> compares 0 with GND 50 | * ADS1115_COMP_1_GND -> compares 1 with GND 51 | * ADS1115_COMP_2_GND -> compares 2 with GND 52 | * ADS1115_COMP_3_GND -> compares 3 with GND 53 | */ 54 | //adc.setCompareChannels(ADS1115_COMP_0_GND); //uncomment if you want to change the default 55 | 56 | /* Set number of conversions after which the alert pin will assert 57 | * - or you can disable the alert 58 | * 59 | * ADS1115_ASSERT_AFTER_1 -> after 1 conversion 60 | * ADS1115_ASSERT_AFTER_2 -> after 2 conversions 61 | * ADS1115_ASSERT_AFTER_4 -> after 4 conversions 62 | * ADS1115_DISABLE_ALERT -> disable comparator / alert pin (default) 63 | */ 64 | //adc.setAlertPinMode(ADS1115_ASSERT_AFTER_1); //uncomment if you want to change the default 65 | 66 | /* Set the conversion rate in SPS (samples per second) 67 | * Options should be self-explaining: 68 | * 69 | * ADS1115_8_SPS 70 | * ADS1115_16_SPS 71 | * ADS1115_32_SPS 72 | * ADS1115_64_SPS 73 | * ADS1115_128_SPS (default) 74 | * ADS1115_250_SPS 75 | * ADS1115_475_SPS 76 | * ADS1115_860_SPS 77 | */ 78 | //adc.setConvRate(ADS1115_128_SPS); //uncomment if you want to change the default 79 | 80 | /* Set continuous or single shot mode: 81 | * 82 | * ADS1115_CONTINUOUS -> continuous mode 83 | * ADS1115_SINGLE -> single shot mode (default) 84 | */ 85 | //adc.setMeasureMode(ADS1115_CONTINUOUS); //uncomment if you want to change the default 86 | 87 | /* Choose maximum limit or maximum and minimum alert limit (window) in volts - alert pin will 88 | * assert when measured values are beyond the maximum limit or outside the window 89 | * Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 90 | * In max limit mode the minimum value is the limit where the alert pin assertion will be 91 | * be cleared (if not latched) 92 | * 93 | * ADS1115_MAX_LIMIT 94 | * ADS1115_WINDOW 95 | * 96 | */ 97 | //adc.setAlertModeAndLimit_V(ADS1115_MAX_LIMIT, 3.0, 1.5); //uncomment if you want to change the default 98 | 99 | /* Enable or disable latch. If latch is enabled the alert pin will assert until the 100 | * conversion register is read (getResult functions). If disabled the alert pin assertion 101 | * will be cleared with next value within limits. 102 | * 103 | * ADS1115_LATCH_DISABLED (default) 104 | * ADS1115_LATCH_ENABLED 105 | */ 106 | //adc.setAlertLatch(ADS1115_LATCH_ENABLED); //uncomment if you want to change the default 107 | 108 | /* Sets the alert pin polarity if active: 109 | * 110 | * ADS1115_ACT_LOW -> active low (default) 111 | * ADS1115_ACT_HIGH -> active high 112 | */ 113 | //adc.setAlertPol(ADS1115_ACT_LOW); //uncomment if you want to change the default 114 | 115 | /* With this function the alert pin will assert, when a conversion is ready. 116 | * In order to deactivate, use the setAlertLimit_V function 117 | */ 118 | //adc.setAlertPinToConversionReady(); //uncomment if you want to change the default 119 | 120 | Serial.println("ADS1115 Example Sketch - Single Shot Mode"); 121 | Serial.println("Channel / Voltage [V]: "); 122 | Serial.println(); 123 | } 124 | 125 | void loop() { 126 | float voltage = 0.0; 127 | 128 | Serial.print("0: "); 129 | voltage = readChannel(ADS1115_COMP_0_GND); 130 | Serial.print(voltage); 131 | 132 | Serial.print(", 1: "); 133 | voltage = readChannel(ADS1115_COMP_1_GND); 134 | Serial.print(voltage); 135 | 136 | Serial.print(", 2: "); 137 | voltage = readChannel(ADS1115_COMP_2_GND); 138 | Serial.print(voltage); 139 | 140 | Serial.print(", 3: "); 141 | voltage = readChannel(ADS1115_COMP_3_GND); 142 | Serial.println(voltage); 143 | 144 | delay(1000); 145 | } 146 | 147 | float readChannel(ADS1115_MUX channel) { 148 | float voltage = 0.0; 149 | adc.setCompareChannels(channel); 150 | adc.startSingleMeasurement(); 151 | while(adc.isBusy()){delay(0);} 152 | voltage = adc.getResult_V(); // alternative: getResult_mV for Millivolt 153 | return voltage; 154 | } 155 | -------------------------------------------------------------------------------- /examples/Alert_Window_Mode/Alert_Window_Mode.ino: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Example sketch for the ADS1115_WE library 3 | * 4 | * This sketch shows how you can use the alert pin to compare with limits. I 5 | * have chosen the window mode. You can also try the max limit (traditional) 6 | * mode. You can change the mode with the function setAlertModeAndLimit_V 7 | * 8 | * Further information can be found on: 9 | * https://wolles-elektronikkiste.de/ads1115 (German) 10 | * https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 11 | * 12 | ***************************************************************************/ 13 | 14 | #include 15 | #include 16 | #define I2C_ADDRESS 0x48 17 | volatile int interruptPin = 2; 18 | int ledPin = 10; 19 | volatile bool outOfLimit = false; 20 | 21 | /* There are several ways to create your ADS1115_WE object: 22 | * ADS1115_WE adc = ADS1115_WE(); -> uses Wire / I2C Address = 0x48 23 | * ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); -> uses Wire / I2C_ADDRESS 24 | * ADS1115_WE adc = ADS1115_WE(&Wire); -> you can pass any TwoWire object / I2C Address = 0x48 25 | * ADS1115_WE adc = ADS1115_WE(&Wire, I2C_ADDRESS); -> all together 26 | */ 27 | ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); 28 | 29 | void setup() { 30 | Wire.begin(); 31 | Serial.begin(9600); 32 | 33 | pinMode(interruptPin, INPUT_PULLUP); 34 | pinMode(ledPin, OUTPUT); 35 | digitalWrite(ledPin, LOW); 36 | 37 | if(!adc.init()){ 38 | Serial.println("ADS1115 not connected!"); 39 | } 40 | 41 | /* Set the voltage range of the ADC to adjust the gain 42 | * Please note that you must not apply more than VDD + 0.3V to the input pins! 43 | * 44 | * ADS1115_RANGE_6144 -> +/- 6144 mV 45 | * ADS1115_RANGE_4096 -> +/- 4096 mV 46 | * ADS1115_RANGE_2048 -> +/- 2048 mV (default) 47 | * ADS1115_RANGE_1024 -> +/- 1024 mV 48 | * ADS1115_RANGE_0512 -> +/- 512 mV 49 | * ADS1115_RANGE_0256 -> +/- 256 mV 50 | */ 51 | adc.setVoltageRange_mV(ADS1115_RANGE_6144); //comment line/change parameter to change range 52 | 53 | /* Set the inputs to be compared 54 | * 55 | * ADS1115_COMP_0_1 -> compares 0 with 1 (default) 56 | * ADS1115_COMP_0_3 -> compares 0 with 3 57 | * ADS1115_COMP_1_3 -> compares 1 with 3 58 | * ADS1115_COMP_2_3 -> compares 2 with 3 59 | * ADS1115_COMP_0_GND -> compares 0 with GND 60 | * ADS1115_COMP_1_GND -> compares 1 with GND 61 | * ADS1115_COMP_2_GND -> compares 2 with GND 62 | * ADS1115_COMP_3_GND -> compares 3 with GND 63 | */ 64 | adc.setCompareChannels(ADS1115_COMP_0_GND); //comment line/change parameter to change range 65 | 66 | /* Set number of conversions out of limit after which alert pin asserts 67 | * - or you can disable the alert (including conversion ready alert) 68 | * 69 | * ADS1115_ASSERT_AFTER_1 -> after 1 conversion 70 | * ADS1115_ASSERT_AFTER_2 -> after 2 conversions 71 | * ADS1115_ASSERT_AFTER_4 -> after 4 conversions 72 | * ADS1115_DISABLE_ALERT -> disable comparator / alert pin (default) 73 | */ 74 | adc.setAlertPinMode(ADS1115_ASSERT_AFTER_1); // alternative: ...AFTER_2 or 4. If you disable this sketch does not work 75 | 76 | /* Set the conversion rate in SPS (samples per second) 77 | * Options should be self-explaining: 78 | * 79 | * ADS1115_8_SPS 80 | * ADS1115_16_SPS 81 | * ADS1115_32_SPS 82 | * ADS1115_64_SPS 83 | * ADS1115_128_SPS (default) 84 | * ADS1115_250_SPS 85 | * ADS1115_475_SPS 86 | * ADS1115_860_SPS 87 | */ 88 | //adc.setConvRate(ADS1115_8_SPS); //uncomment if you want to change the default 89 | 90 | /* Set continuous or single shot mode: 91 | * 92 | * ADS1115_CONTINUOUS -> continuous mode 93 | * ADS1115_SINGLE -> single shot mode (default) 94 | */ 95 | adc.setMeasureMode(ADS1115_CONTINUOUS); //comment or change you want to change to single shot 96 | 97 | /* Choose maximum limit or maximum and minimum alert limit (window) in volts - alert pin will 98 | * assert when measured values are beyond the maximum limit or outside the window 99 | * Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 100 | * In max limit mode the minimum value is the limit where the alert pin assertion is cleared 101 | * again (if not latched!) 102 | * 103 | * ADS1115_MAX_LIMIT 104 | * ADS1115_WINDOW 105 | * 106 | */ 107 | adc.setAlertModeAndLimit_V(ADS1115_WINDOW, 3.0, 1.5); //you can change modes / limits 108 | 109 | /* Enable or disable latch. If latch is enabled the alert pin will assert until the 110 | * conversion register is read (getResult functions). If disabled the alert pin assertion 111 | * will be cleared with next value within limits. 112 | * 113 | * ADS1115_LATCH_DISABLED (default) 114 | * ADS1115_LATCH_ENABLED 115 | */ 116 | //adc.setAlertLatch(ADS1115_LATCH_ENABLED); //uncomment if you want to change the default 117 | 118 | /* Sets the alert pin polarity if active: 119 | * 120 | * ADS1115_ACT_LOW -> active low (default) 121 | * ADS1115_ACT_HIGH -> active high 122 | */ 123 | //adc.setAlertPol(ADS1115_ACT_LOW); //uncomment if you want to change the default 124 | 125 | /* With this function the alert pin will assert, when a conversion is ready. 126 | * In order to deactivate, use the setAlertLimit_V function 127 | */ 128 | //adc.setAlertPinToConversionReady(); //uncomment if you want to change the default 129 | 130 | Serial.println("ADS1115 Example Sketch - uses the Alert Pin / Window Mode"); 131 | Serial.println(); 132 | Serial.println("Waiting for Value out of Limit"); 133 | attachInterrupt(digitalPinToInterrupt(interruptPin), outOfLimitAlert, FALLING); 134 | } 135 | 136 | void loop() { 137 | float voltage = 0.0; 138 | if(outOfLimit){ 139 | voltage = adc.getResult_V(); 140 | Serial.print("Voltage [V]: "); 141 | Serial.println(voltage); 142 | digitalWrite(ledPin,HIGH); 143 | delay(1000); 144 | digitalWrite(ledPin,LOW); 145 | outOfLimit = false; 146 | attachInterrupt(digitalPinToInterrupt(interruptPin), outOfLimitAlert, FALLING); 147 | } 148 | } 149 | 150 | void outOfLimitAlert(){ 151 | detachInterrupt(interruptPin); 152 | outOfLimit = true; 153 | } 154 | -------------------------------------------------------------------------------- /examples/Alert_Window_Mode_with_Latch/Alert_Window_Mode_with_Latch.ino: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Example sketch for the ADS1115_WE library 3 | * 4 | * This sketch shows how you can use the alert pin with the latch function. The 5 | * only difference to Alert_Window_Mode.ino is that latch is enabled (line 113) 6 | * and that the alert needs to be cleared (line 144). Try and see the difference. 7 | * As an alternative to the unlatchAlertPin function you can use getResult_V. 8 | * Internally clearAlert just performs a read of the conversion register. 9 | * 10 | * Further information can be found on: 11 | * https://wolles-elektronikkiste.de/ads1115 (German) 12 | * https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 13 | * 14 | ***************************************************************************/ 15 | 16 | #include 17 | #include 18 | #define I2C_ADDRESS 0x48 19 | volatile int interruptPin = 2; 20 | int ledPin = 10; 21 | volatile bool outOfLimit = false; 22 | 23 | /* There are several ways to create your ADS1115_WE object: 24 | * ADS1115_WE adc = ADS1115_WE(); -> uses Wire / I2C Address = 0x48 25 | * ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); -> uses Wire / I2C_ADDRESS 26 | * ADS1115_WE adc = ADS1115_WE(&Wire); -> you can pass any TwoWire object / I2C Address = 0x48 27 | * ADS1115_WE adc = ADS1115_WE(&Wire, I2C_ADDRESS); -> all together 28 | */ 29 | ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); 30 | 31 | void setup() { 32 | Wire.begin(); 33 | Serial.begin(9600); 34 | 35 | pinMode(interruptPin, INPUT_PULLUP); 36 | pinMode(ledPin, OUTPUT); 37 | digitalWrite(ledPin, LOW); 38 | 39 | if(!adc.init()){ 40 | Serial.println("ADS1115 not connected!"); 41 | } 42 | 43 | /* Set the voltage range of the ADC to adjust the gain 44 | * Please note that you must not apply more than VDD + 0.3V to the input pins! 45 | * 46 | * ADS1115_RANGE_6144 -> +/- 6144 mV 47 | * ADS1115_RANGE_4096 -> +/- 4096 mV 48 | * ADS1115_RANGE_2048 -> +/- 2048 mV (default) 49 | * ADS1115_RANGE_1024 -> +/- 1024 mV 50 | * ADS1115_RANGE_0512 -> +/- 512 mV 51 | * ADS1115_RANGE_0256 -> +/- 256 mV 52 | */ 53 | adc.setVoltageRange_mV(ADS1115_RANGE_6144); //comment line/change parameter to change range 54 | 55 | /* Set the inputs to be compared 56 | * 57 | * ADS1115_COMP_0_1 -> compares 0 with 1 (default) 58 | * ADS1115_COMP_0_3 -> compares 0 with 3 59 | * ADS1115_COMP_1_3 -> compares 1 with 3 60 | * ADS1115_COMP_2_3 -> compares 2 with 3 61 | * ADS1115_COMP_0_GND -> compares 0 with GND 62 | * ADS1115_COMP_1_GND -> compares 1 with GND 63 | * ADS1115_COMP_2_GND -> compares 2 with GND 64 | * ADS1115_COMP_3_GND -> compares 3 with GND 65 | */ 66 | adc.setCompareChannels(ADS1115_COMP_0_GND); //uncomment if you want to change the default 67 | 68 | /* Set number of conversions out of limit after which the alert pin will assert 69 | * - or you can disable the alert (including conversion ready alert) 70 | * 71 | * ADS1115_ASSERT_AFTER_1 -> after 1 conversion 72 | * ADS1115_ASSERT_AFTER_2 -> after 2 conversions 73 | * ADS1115_ASSERT_AFTER_4 -> after 4 conversions 74 | * ADS1115_DISABLE_ALERT -> disable comparator / alert pin (default) 75 | */ 76 | adc.setAlertPinMode(ADS1115_ASSERT_AFTER_1); // you can also choose ...AFTER_2 or 4 for this sketch 77 | 78 | /* Set the conversion rate in SPS (samples per second) 79 | * Options should be self-explaining: 80 | * 81 | * ADS1115_8_SPS 82 | * ADS1115_16_SPS 83 | * ADS1115_32_SPS 84 | * ADS1115_64_SPS 85 | * ADS1115_128_SPS (default) 86 | * ADS1115_250_SPS 87 | * ADS1115_475_SPS 88 | * ADS1115_860_SPS 89 | */ 90 | //adc.setConvRate(ADS1115_8_SPS); //uncomment if you want to change the default 91 | 92 | /* Set continuous or single shot mode: 93 | * 94 | * ADS1115_CONTINUOUS -> continuous mode 95 | * ADS1115_SINGLE -> single shot mode (default) 96 | */ 97 | adc.setMeasureMode(ADS1115_CONTINUOUS); //comment or change if you want to change single shot 98 | 99 | /* Choose maximum limit or maximum and minimum alert limit (window) in volts - alert pin will 100 | * assert when measured values are beyond the maximum limit or outside the window 101 | * Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 102 | * In max limit mode the minimum value is the limit where the alert pin assertion will be cleared 103 | * again (if not latched!) 104 | * 105 | * ADS1115_MAX_LIMIT 106 | * ADS1115_WINDOW 107 | * 108 | */ 109 | adc.setAlertModeAndLimit_V(ADS1115_WINDOW, 3.0, 1.5); //uncomment if you want to change the default 110 | 111 | /* Enable or disable latch. If latch is enabled the alert pin will be active until the 112 | * conversion register is read (getResult functions). If disabled the alert pin assertion 113 | * will be cleared with next value within limits. 114 | * 115 | * ADS1115_LATCH_DISABLED (default) 116 | * ADS1115_LATCH_ENABLED 117 | */ 118 | adc.setAlertLatch(ADS1115_LATCH_ENABLED); //uncomment if you want to change the default 119 | 120 | /* Sets the alert pin polarity if active: 121 | * 122 | * ADS1115_ACT_LOW -> active low (default) 123 | * ADS1115_ACT_HIGH -> active high 124 | */ 125 | //adc.setAlertPol(ADS1115_ACT_LOW); //uncomment if you want to change the default 126 | 127 | /* With this function the alert pin will assert, when a conversion is ready. 128 | * In order to deactivate, use the setAlertLimit_V function 129 | */ 130 | //adc.setAlertPinToConversionReady(); //uncomment if you want to change the default 131 | 132 | Serial.println("ADS1115 Example Sketch - uses the Alert Pin / Window Mode / Latch enabled"); 133 | Serial.println(); 134 | Serial.println("Waiting for Value out of Limit"); 135 | attachInterrupt(digitalPinToInterrupt(interruptPin), outOfLimitAlert, FALLING); 136 | } 137 | 138 | void loop() { 139 | float voltage = 0.0; 140 | if(outOfLimit){ 141 | voltage = adc.getResult_V(); 142 | Serial.print("Voltage [V]: "); 143 | Serial.println(voltage); 144 | digitalWrite(ledPin,HIGH); 145 | delay(1000); 146 | digitalWrite(ledPin,LOW); 147 | outOfLimit = false; 148 | attachInterrupt(digitalPinToInterrupt(interruptPin), outOfLimitAlert, FALLING); 149 | adc.clearAlert(); // unlatches the alert Pin (alternatively use getResult_V) 150 | } 151 | } 152 | 153 | void outOfLimitAlert(){ 154 | detachInterrupt(interruptPin); 155 | outOfLimit = true; 156 | } 157 | -------------------------------------------------------------------------------- /examples/Conv_Ready_Alert_Pin_Controlled/Conv_Ready_Alert_Pin_Controlled.ino: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Example sketch for the ADS1115_WE library 3 | * 4 | * This sketch shows how you can use the alert pin as conversion ready alert pin. 5 | * It works in continuous mode as well as in single shot mode. 6 | * Please note that you need to enable the alert pin with setAlertPinMode. Choose any 7 | * parameter except ADS1115_DISABLE_ALERT. 8 | * 9 | * Further information can be found on: 10 | * https://wolles-elektronikkiste.de/ads1115 (German) 11 | * https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 12 | * 13 | ***************************************************************************/ 14 | 15 | #include 16 | #include 17 | #define I2C_ADDRESS 0x48 18 | int interruptPin = 2; 19 | volatile bool convReady = false; 20 | 21 | /* There are several ways to create your ADS1115_WE object: 22 | * ADS1115_WE adc = ADS1115_WE(); -> uses Wire / I2C Address = 0x48 23 | * ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); -> uses Wire / I2C_ADDRESS 24 | * ADS1115_WE adc = ADS1115_WE(&Wire); -> you can pass any TwoWire object / I2C Address = 0x48 25 | * ADS1115_WE adc = ADS1115_WE(&Wire, I2C_ADDRESS); -> all together 26 | */ 27 | ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); 28 | 29 | void setup() { 30 | Wire.begin(); 31 | Serial.begin(9600); 32 | pinMode(interruptPin, INPUT_PULLUP); 33 | if(!adc.init()){ 34 | Serial.println("ADS1115 not connected!"); 35 | } 36 | 37 | /* Set the voltage range of the ADC to adjust the gain 38 | * Please note that you must not apply more than VDD + 0.3V to the input pins! 39 | * 40 | * ADS1115_RANGE_6144 -> +/- 6144 mV 41 | * ADS1115_RANGE_4096 -> +/- 4096 mV 42 | * ADS1115_RANGE_2048 -> +/- 2048 mV (default) 43 | * ADS1115_RANGE_1024 -> +/- 1024 mV 44 | * ADS1115_RANGE_0512 -> +/- 512 mV 45 | * ADS1115_RANGE_0256 -> +/- 256 mV 46 | */ 47 | adc.setVoltageRange_mV(ADS1115_RANGE_6144); //comment line/change parameter to change range 48 | 49 | /* Set the inputs to be compared 50 | * 51 | * ADS1115_COMP_0_1 -> compares 0 with 1 (default) 52 | * ADS1115_COMP_0_3 -> compares 0 with 3 53 | * ADS1115_COMP_1_3 -> compares 1 with 3 54 | * ADS1115_COMP_2_3 -> compares 2 with 3 55 | * ADS1115_COMP_0_GND -> compares 0 with GND 56 | * ADS1115_COMP_1_GND -> compares 1 with GND 57 | * ADS1115_COMP_2_GND -> compares 2 with GND 58 | * ADS1115_COMP_3_GND -> compares 3 with GND 59 | */ 60 | adc.setCompareChannels(ADS1115_COMP_0_GND); //comment line/change parameter to change channels 61 | 62 | /* Set number of conversions out of limit after which the alert pin will assert 63 | * - or you can disable the alert (including conversion ready alert) 64 | * 65 | * ADS1115_ASSERT_AFTER_1 -> after 1 conversion 66 | * ADS1115_ASSERT_AFTER_2 -> after 2 conversions 67 | * ADS1115_ASSERT_AFTER_4 -> after 4 conversions 68 | * ADS1115_DISABLE_ALERT -> disable comparator / alert pin (default) 69 | */ 70 | adc.setAlertPinMode(ADS1115_ASSERT_AFTER_1); //needed in this sketch to enable alert pin (doesn't matter if you choose after 1,2 or 4) 71 | 72 | /* Set the conversion rate in SPS (samples per second) 73 | * Options should be self-explaining: 74 | * 75 | * ADS1115_8_SPS 76 | * ADS1115_16_SPS 77 | * ADS1115_32_SPS 78 | * ADS1115_64_SPS 79 | * ADS1115_128_SPS (default) 80 | * ADS1115_250_SPS 81 | * ADS1115_475_SPS 82 | * ADS1115_860_SPS 83 | */ 84 | adc.setConvRate(ADS1115_8_SPS); //comment line/change parameter to change SPS 85 | 86 | /* Set continuous or single shot mode: 87 | * 88 | * ADS1115_CONTINUOUS -> continuous mode 89 | * ADS1115_SINGLE -> single shot mode (default) 90 | */ 91 | //adc.setMeasureMode(ADS1115_CONTINUOUS); // the conversion ready alert pin also works in continuous mode 92 | 93 | /* Choose maximum limit or maximum and minimum alert limit (window) in volts - alert pin will 94 | * assert when measured values are beyond the maximum limit or outside the window 95 | * Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 96 | * In max limit mode the minimum value is the limit where the alert pin assertion will be cleared 97 | * (if not latched) 98 | * 99 | * ADS1115_MAX_LIMIT 100 | * ADS1115_WINDOW 101 | * 102 | */ 103 | //adc.setAlertModeAndLimit_V(ADS1115_MAX_LIMIT, 3.0, 1.5); //uncomment if you want to change the default 104 | 105 | /* Enable or disable latch. If latch is enabled the alert pin will assert until the 106 | * conversion register is read (getResult functions). If disabled the alert pin assertion 107 | * will be cleared with next value within limits. 108 | * 109 | * ADS1115_LATCH_DISABLED (default) 110 | * ADS1115_LATCH_ENABLED 111 | */ 112 | //adc.setAlertLatch(ADS1115_LATCH_ENABLED); //uncomment if you want to change the default 113 | 114 | /* Sets the alert pin polarity if active: 115 | * 116 | * ADS1115_ACT_LOW -> active low (default) 117 | * ADS1115_ACT_HIGH -> active high 118 | */ 119 | //adc.setAlertPol(ADS1115_ACT_LOW); //uncomment if you want to change the default 120 | 121 | /* With this function the alert pin assert, when a conversion is ready. 122 | * In order to deactivate, use the setAlertLimit_V function 123 | */ 124 | adc.setAlertPinToConversionReady(); //needed for this sketch 125 | 126 | Serial.println("ADS1115 Example Sketch - Single Shot, Conversion Ready Alert Pin controlled"); 127 | Serial.println(); 128 | attachInterrupt(digitalPinToInterrupt(interruptPin), convReadyAlert, FALLING); 129 | adc.startSingleMeasurement(); 130 | } 131 | 132 | /* In this example I measure 32 times before the result is output. This is only to slow down 133 | * the output rate. I want to show that the output rate is controlled by the conversion ready 134 | * signals and not by a delay. 135 | */ 136 | void loop() { 137 | float voltage = 0.0; 138 | static int counter = 0; 139 | if(convReady){ 140 | counter++; 141 | convReady = false; 142 | if(counter==32){ // counter is 32, conversion rate is 8 SPS --> 4s 143 | voltage = adc.getResult_V(); // alternative: getResult_mV for Millivolt 144 | Serial.print("Channel 0 vs GND [V]: "); 145 | Serial.println(voltage); 146 | Serial.println("-------------------------------"); 147 | counter = 0; 148 | } 149 | adc.startSingleMeasurement(); 150 | } 151 | } 152 | 153 | void convReadyAlert(){ 154 | convReady = true; 155 | } 156 | -------------------------------------------------------------------------------- /examples/Continuous/Continuous.ino: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Example sketch for the ADS1115_WE library 3 | * 4 | * This sketch shows how to use the ADS1115 in continuous mode. 5 | * 6 | * Further information can be found on: 7 | * https://wolles-elektronikkiste.de/ads1115 (German) 8 | * https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 9 | * 10 | ***************************************************************************/ 11 | 12 | #include 13 | #include 14 | #define I2C_ADDRESS 0x48 15 | 16 | /* There are several ways to create your ADS1115_WE object: 17 | * ADS1115_WE adc = ADS1115_WE(); -> uses Wire / I2C Address = 0x48 18 | * ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); -> uses Wire / I2C_ADDRESS 19 | * ADS1115_WE adc = ADS1115_WE(&Wire); -> you can pass any TwoWire object / I2C Address = 0x48 20 | * ADS1115_WE adc = ADS1115_WE(&Wire, I2C_ADDRESS); -> all together 21 | */ 22 | ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); 23 | 24 | void setup() { 25 | Wire.begin(); 26 | Serial.begin(9600); 27 | if(!adc.init()){ 28 | Serial.println("ADS1115 not connected!"); 29 | } 30 | 31 | /* Set the voltage range of the ADC to adjust the gain 32 | * Please note that you must not apply more than VDD + 0.3V to the input pins! 33 | * 34 | * ADS1115_RANGE_6144 -> +/- 6144 mV 35 | * ADS1115_RANGE_4096 -> +/- 4096 mV 36 | * ADS1115_RANGE_2048 -> +/- 2048 mV (default) 37 | * ADS1115_RANGE_1024 -> +/- 1024 mV 38 | * ADS1115_RANGE_0512 -> +/- 512 mV 39 | * ADS1115_RANGE_0256 -> +/- 256 mV 40 | */ 41 | adc.setVoltageRange_mV(ADS1115_RANGE_6144); //comment line/change parameter to change range 42 | 43 | /* Set the inputs to be compared 44 | * 45 | * ADS1115_COMP_0_1 -> compares 0 with 1 (default) 46 | * ADS1115_COMP_0_3 -> compares 0 with 3 47 | * ADS1115_COMP_1_3 -> compares 1 with 3 48 | * ADS1115_COMP_2_3 -> compares 2 with 3 49 | * ADS1115_COMP_0_GND -> compares 0 with GND 50 | * ADS1115_COMP_1_GND -> compares 1 with GND 51 | * ADS1115_COMP_2_GND -> compares 2 with GND 52 | * ADS1115_COMP_3_GND -> compares 3 with GND 53 | */ 54 | adc.setCompareChannels(ADS1115_COMP_0_GND); //comment line/change parameter to change channel 55 | 56 | /* Set number of conversions after which the alert pin asserts 57 | * - or you can disable the alert 58 | * 59 | * ADS1115_ASSERT_AFTER_1 -> after 1 conversion 60 | * ADS1115_ASSERT_AFTER_2 -> after 2 conversions 61 | * ADS1115_ASSERT_AFTER_4 -> after 4 conversions 62 | * ADS1115_DISABLE_ALERT -> disable comparator / alert pin (default) 63 | */ 64 | //adc.setAlertPinMode(ADS1115_ASSERT_AFTER_1); //uncomment if you want to change the default 65 | 66 | /* Set the conversion rate in SPS (samples per second) 67 | * Options should be self-explaining: 68 | * 69 | * ADS1115_8_SPS 70 | * ADS1115_16_SPS 71 | * ADS1115_32_SPS 72 | * ADS1115_64_SPS 73 | * ADS1115_128_SPS (default) 74 | * ADS1115_250_SPS 75 | * ADS1115_475_SPS 76 | * ADS1115_860_SPS 77 | */ 78 | // adc.setConvRate(ADS1115_8_SPS); //uncomment if you want to change the default 79 | 80 | /* Set continuous or single shot mode: 81 | * 82 | * ADS1115_CONTINUOUS -> continuous mode 83 | * ADS1115_SINGLE -> single shot mode (default) 84 | */ 85 | adc.setMeasureMode(ADS1115_CONTINUOUS); //comment line/change parameter to change mode 86 | 87 | /* Choose maximum limit or maximum and minimum alert limit (window) in Volt - alert pin will 88 | * assert when measured values are beyond the maximum limit or outside the window 89 | * Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 90 | * In max limit mode the minimum value is the limit where the alert pin assertion will be 91 | * cleared (if not latched) 92 | * 93 | * ADS1115_MAX_LIMIT 94 | * ADS1115_WINDOW 95 | * 96 | */ 97 | //adc.setAlertModeAndLimit_V(ADS1115_MAX_LIMIT, 3.0, 1.5); //uncomment if you want to change the default 98 | 99 | /* Enable or disable latch. If latch is enabled the alert pin will assert until the 100 | * conversion register is read (getResult functions). If disabled the alert pin assertion will be 101 | * cleared with next value within limits. 102 | * 103 | * ADS1115_LATCH_DISABLED (default) 104 | * ADS1115_LATCH_ENABLED 105 | */ 106 | //adc.setAlertLatch(ADS1115_LATCH_ENABLED); //uncomment if you want to change the default 107 | 108 | /* Sets the alert pin polarity if active: 109 | * 110 | * ADS1115_ACT_LOW -> active low (default) 111 | * ADS1115_ACT_HIGH -> active high 112 | */ 113 | //adc.setAlertPol(ADS1115_ACT_LOW); //uncomment if you want to change the default 114 | 115 | /* With this function the alert pin will assert, when a conversion is ready. 116 | * In order to deactivate, use the setAlertLimit_V function 117 | */ 118 | //adc.setAlertPinToConversionReady(); //uncomment if you want to change the default 119 | 120 | Serial.println("ADS1115 Example Sketch - Continuous Mode"); 121 | Serial.println("All values in volts"); 122 | Serial.println(); 123 | } 124 | 125 | /* If you change the compare channels you can immediately read values from the conversion 126 | * register, although they might belong to the former channel if no precautions are taken. 127 | * It takes up to the time needed for two conversions to get data of the new channel. In single 128 | * shot mode you can use the isBusy() function to wait for fresh data. This does not work in 129 | * continuous mode. 130 | * To solve this issue the library adds a delay after change of channels if you are in contunuous 131 | * mode. The length of the delay is adjusted to the conversion rate. But be aware that the output 132 | * rate will be much lower that the conversion rate if you change channels frequently. 133 | * If you don't want to block the sketch, use setCompareChannels_nonblock() instead of 134 | * setCompareChannels(). 135 | */ 136 | 137 | void loop() { 138 | float voltage = 0.0; 139 | 140 | Serial.print("0: "); 141 | voltage = readChannel(ADS1115_COMP_0_GND); 142 | Serial.print(voltage); 143 | 144 | Serial.print(", 1: "); 145 | voltage = readChannel(ADS1115_COMP_1_GND); 146 | Serial.print(voltage); 147 | 148 | Serial.print(", 2: "); 149 | voltage = readChannel(ADS1115_COMP_2_GND); 150 | Serial.print(voltage); 151 | 152 | Serial.print(", 3: "); 153 | voltage = readChannel(ADS1115_COMP_3_GND); 154 | Serial.println(voltage); 155 | 156 | delay(1000); 157 | } 158 | 159 | float readChannel(ADS1115_MUX channel) { 160 | float voltage = 0.0; 161 | adc.setCompareChannels(channel); 162 | // setCompareChannels_nonblock(channel); 163 | voltage = adc.getResult_V(); // alternative: getResult_mV for Millivolt 164 | return voltage; 165 | } 166 | -------------------------------------------------------------------------------- /examples/Continuous_ADS1015/Continuous_ADS1015.ino: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Example sketch for the ADS1015_WE library 3 | * 4 | * This sketch shows how to use the ADS1015 in continuous mode. 5 | * 6 | * Further information can be found on: 7 | * https://wolles-elektronikkiste.de/ads1115 (German) 8 | * https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 9 | * 10 | ***************************************************************************/ 11 | 12 | #include 13 | #include 14 | #define I2C_ADDRESS 0x48 15 | 16 | /* There are several ways to create your ADS1015_WE object: 17 | * ADS1015_WE adc = ADS1015_WE(); -> uses Wire / I2C Address = 0x48 18 | * ADS1015_WE adc = ADS1015_WE(I2C_ADDRESS); -> uses Wire / I2C_ADDRESS 19 | * ADS1015_WE adc = ADS1015_WE(&Wire); -> you can pass any TwoWire object / I2C Address = 0x48 20 | * ADS1015_WE adc = ADS1015_WE(&Wire, I2C_ADDRESS); -> all together 21 | */ 22 | ADS1015_WE adc = ADS1015_WE(I2C_ADDRESS); 23 | 24 | void setup() { 25 | bool useADS1015 = true; 26 | Wire.begin(); 27 | Serial.begin(9600); 28 | if(!adc.init(useADS1015)){ // passing true will tell the lib that an ADS1015 is used 29 | Serial.println("ADS1015 not connected!"); 30 | } 31 | 32 | /* Set the voltage range of the ADC to adjust the gain 33 | * Please note that you must not apply more than VDD + 0.3V to the input pins! 34 | * 35 | * ADS1015_RANGE_6144 -> +/- 6144 mV 36 | * ADS1015_RANGE_4096 -> +/- 4096 mV 37 | * ADS1015_RANGE_2048 -> +/- 2048 mV (default) 38 | * ADS1015_RANGE_1024 -> +/- 1024 mV 39 | * ADS1015_RANGE_0512 -> +/- 512 mV 40 | * ADS1015_RANGE_0256 -> +/- 256 mV 41 | */ 42 | adc.setVoltageRange_mV(ADS1015_RANGE_6144); //comment line/change parameter to change range 43 | 44 | /* Set the inputs to be compared 45 | * 46 | * ADS1015_COMP_0_1 -> compares 0 with 1 (default) 47 | * ADS1015_COMP_0_3 -> compares 0 with 3 48 | * ADS1015_COMP_1_3 -> compares 1 with 3 49 | * ADS1015_COMP_2_3 -> compares 2 with 3 50 | * ADS1015_COMP_0_GND -> compares 0 with GND 51 | * ADS1015_COMP_1_GND -> compares 1 with GND 52 | * ADS1015_COMP_2_GND -> compares 2 with GND 53 | * ADS1015_COMP_3_GND -> compares 3 with GND 54 | */ 55 | adc.setCompareChannels(ADS1015_COMP_0_GND); //comment line/change parameter to change channel 56 | 57 | /* Set number of conversions after which the alert pin asserts 58 | * - or you can disable the alert 59 | * 60 | * ADS1015_ASSERT_AFTER_1 -> after 1 conversion 61 | * ADS1015_ASSERT_AFTER_2 -> after 2 conversions 62 | * ADS1015_ASSERT_AFTER_4 -> after 4 conversions 63 | * ADS1015_DISABLE_ALERT -> disable comparator / alert pin (default) 64 | */ 65 | //adc.setAlertPinMode(ADS1015_ASSERT_AFTER_1); //uncomment if you want to change the default 66 | 67 | /* Set the conversion rate in SPS (samples per second) 68 | * Options should be self-explaining: 69 | * 70 | * ADS1015_128_SPS 71 | * ADS1015_250_SPS 72 | * ADS1015_490_SPS 73 | * ADS1015_920_SPS 74 | * ADS1015_1600_SPS (default) 75 | * ADS1015_2400_SPS 76 | * ADS1015_3300_SPS 77 | */ 78 | //adc.setConvRate(ADS1015_3300_SPS); //uncomment if you want to change the default 79 | 80 | /* Set continuous or single shot mode: 81 | * 82 | * ADS1015_CONTINUOUS -> continuous mode 83 | * ADS1015_SINGLE -> single shot mode (default) 84 | */ 85 | adc.setMeasureMode(ADS1015_CONTINUOUS); //comment line/change parameter to change mode 86 | 87 | /* Choose maximum limit or maximum and minimum alert limit (window) in Volt - alert pin will 88 | * assert when measured values are beyond the maximum limit or outside the window 89 | * Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 90 | * In max limit mode the minimum value is the limit where the alert pin assertion will be 91 | * cleared (if not latched) 92 | * 93 | * ADS1015_MAX_LIMIT 94 | * ADS1015_WINDOW 95 | * 96 | */ 97 | //adc.setAlertModeAndLimit_V(ADS1015_MAX_LIMIT, 3.0, 1.5); //uncomment if you want to change the default 98 | 99 | /* Enable or disable latch. If latch is enabled the alert pin will assert until the 100 | * conversion register is read (getResult functions). If disabled the alert pin assertion will be 101 | * cleared with next value within limits. 102 | * 103 | * ADS1015_LATCH_DISABLED (default) 104 | * ADS1015_LATCH_ENABLED 105 | */ 106 | //adc.setAlertLatch(ADS1015_LATCH_ENABLED); //uncomment if you want to change the default 107 | 108 | /* Sets the alert pin polarity if active: 109 | * 110 | * ADS1015_ACT_LOW -> active low (default) 111 | * ADS1015_ACT_HIGH -> active high 112 | */ 113 | //adc.setAlertPol(ADS1015_ACT_LOW); //uncomment if you want to change the default 114 | 115 | /* With this function the alert pin will assert, when a conversion is ready. 116 | * In order to deactivate, use the setAlertLimit_V function 117 | */ 118 | //adc.setAlertPinToConversionReady(); //uncomment if you want to change the default 119 | 120 | Serial.println("ADS1015 Example Sketch - Continuous Mode"); 121 | Serial.println("All values in volts"); 122 | Serial.println(); 123 | } 124 | 125 | /* If you change the compare channels you can immediately read values from the conversion 126 | * register, although they might belong to the former channel if no precautions are taken. 127 | * It takes up to the time needed for two conversions to get data of the new channel. In single 128 | * shot mode you can use the isBusy() function to wait for fresh data. This does not work in 129 | * continuous mode. 130 | * To solve this issue the library adds a delay after change of channels if you are in contunuous 131 | * mode. The length of the delay is adjusted to the conversion rate. But be aware that the output 132 | * rate will be much lower that the conversion rate if you change channels frequently. 133 | * If you don't want to block the sketch, use setCompareChannels_nonblock() instead of 134 | * setCompareChannels(). 135 | */ 136 | 137 | void loop() { 138 | float voltage = 0.0; 139 | 140 | Serial.print("0: "); 141 | voltage = readChannel(ADS1015_COMP_0_GND); 142 | Serial.print(voltage); 143 | 144 | Serial.print(", 1: "); 145 | voltage = readChannel(ADS1015_COMP_1_GND); 146 | Serial.print(voltage); 147 | 148 | Serial.print(", 2: "); 149 | voltage = readChannel(ADS1015_COMP_2_GND); 150 | Serial.print(voltage); 151 | 152 | Serial.print(", 3: "); 153 | voltage = readChannel(ADS1015_COMP_3_GND); 154 | Serial.println(voltage); 155 | 156 | delay(1000); 157 | } 158 | 159 | float readChannel(ADS1015_MUX channel) { 160 | float voltage = 0.0; 161 | adc.setCompareChannels(channel); 162 | // setCompareChannels_nonblock(channel); 163 | voltage = adc.getResult_V(); // alternative: getResult_mV for Millivolt 164 | return voltage; 165 | } 166 | -------------------------------------------------------------------------------- /examples/Continuous_ATtiny/Continuous_ATtiny.ino: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Example sketch for the ADS1115_WE library 3 | * 4 | * This sketch shows how to use the ADS1115 module with the TinyWireM library: 5 | * https://github.com/adafruit/TinyWireM 6 | * This allows you to run the ADS1115_WE library on an ATtiny85, for example. 7 | * 8 | * For this specific sketch you also need library Tiny4KOLED to display the 9 | * measured values on a SSD1306 OLED display: 10 | * https://github.com/datacute/Tiny4kOLED 11 | * 12 | * Further information can be found on: 13 | * https://wolles-elektronikkiste.de/ads1115 (German) 14 | * https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 15 | * 16 | ***************************************************************************/ 17 | 18 | /* !!!! IN ORDER TO USE TinyWireM YOU NEED TO UNCOMMENT #define USE_TINY_WIRE_M_ !!!! 19 | * !!!! IN ADS1115_config.h WHICH YOU FIND IN THE libraries/ADS1115_WE/src FOLDER !!!! !!!! 20 | */ 21 | #include 22 | #include 23 | #include 24 | #define ADS1115_I2C_ADDR 0x48 25 | uint8_t width = 128; 26 | uint8_t height = 64; 27 | 28 | /* There are two ways to create your ADS1115_WE object: 29 | * ADS1115_WE adc = ADS1115_WE() -> uses Wire / I2C Address = 0x48 30 | * ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS) -> uses Wire / I2C_ADDRESS 31 | */ 32 | ADS1115_WE adc = ADS1115_WE(ADS1115_I2C_ADDR); 33 | 34 | void setup() { 35 | TinyWireM.begin(); 36 | oled.begin(width, height, sizeof(tiny4koled_init_128x64br), tiny4koled_init_128x64br); 37 | oled.clear(); 38 | oled.setFont(FONT6X8); 39 | oled.on(); 40 | 41 | if(!adc.init()){ 42 | oled.print("ADS1115 not connected"); 43 | while(1){} 44 | } 45 | else{ 46 | oled.print("ADS1115 connected"); 47 | delay(1000); 48 | oled.clear(); 49 | } 50 | 51 | /* Set the voltage range of the ADC to adjust the gain 52 | * Please note that you must not apply more than VDD + 0.3V to the input pins! 53 | * 54 | * ADS1115_RANGE_6144 -> +/- 6144 mV 55 | * ADS1115_RANGE_4096 -> +/- 4096 mV 56 | * ADS1115_RANGE_2048 -> +/- 2048 mV (default) 57 | * ADS1115_RANGE_1024 -> +/- 1024 mV 58 | * ADS1115_RANGE_0512 -> +/- 512 mV 59 | * ADS1115_RANGE_0256 -> +/- 256 mV 60 | */ 61 | adc.setVoltageRange_mV(ADS1115_RANGE_6144); //comment line/change parameter to change range 62 | 63 | /* Set the inputs to be compared 64 | * 65 | * ADS1115_COMP_0_1 -> compares 0 with 1 (default) 66 | * ADS1115_COMP_0_3 -> compares 0 with 3 67 | * ADS1115_COMP_1_3 -> compares 1 with 3 68 | * ADS1115_COMP_2_3 -> compares 2 with 3 69 | * ADS1115_COMP_0_GND -> compares 0 with GND 70 | * ADS1115_COMP_1_GND -> compares 1 with GND 71 | * ADS1115_COMP_2_GND -> compares 2 with GND 72 | * ADS1115_COMP_3_GND -> compares 3 with GND 73 | */ 74 | adc.setCompareChannels(ADS1115_COMP_0_GND); //comment line/change parameter to change channel 75 | 76 | /* Set number of conversions after which the alert pin asserts 77 | * - or you can disable the alert 78 | * 79 | * ADS1115_ASSERT_AFTER_1 -> after 1 conversion 80 | * ADS1115_ASSERT_AFTER_2 -> after 2 conversions 81 | * ADS1115_ASSERT_AFTER_4 -> after 4 conversions 82 | * ADS1115_DISABLE_ALERT -> disable comparator / alert pin (default) 83 | */ 84 | //adc.setAlertPinMode(ADS1115_ASSERT_AFTER_1); //uncomment if you want to change the default 85 | 86 | /* Set the conversion rate in SPS (samples per second) 87 | * Options should be self-explaining: 88 | * 89 | * ADS1115_8_SPS 90 | * ADS1115_16_SPS 91 | * ADS1115_32_SPS 92 | * ADS1115_64_SPS 93 | * ADS1115_128_SPS (default) 94 | * ADS1115_250_SPS 95 | * ADS1115_475_SPS 96 | * ADS1115_860_SPS 97 | */ 98 | // adc.setConvRate(ADS1115_8_SPS); //uncomment if you want to change the default 99 | 100 | /* Set continuous or single shot mode: 101 | * 102 | * ADS1115_CONTINUOUS -> continuous mode 103 | * ADS1115_SINGLE -> single shot mode (default) 104 | */ 105 | adc.setMeasureMode(ADS1115_CONTINUOUS); //comment line/change parameter to change mode 106 | 107 | /* Choose maximum limit or maximum and minimum alert limit (window) in Volt - alert pin will 108 | * assert when measured values are beyond the maximum limit or outside the window 109 | * Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 110 | * In max limit mode the minimum value is the limit where the alert pin assertion will be 111 | * cleared (if not latched) 112 | * 113 | * ADS1115_MAX_LIMIT 114 | * ADS1115_WINDOW 115 | * 116 | */ 117 | //adc.setAlertModeAndLimit_V(ADS1115_MAX_LIMIT, 3.0, 1.5); //uncomment if you want to change the default 118 | 119 | /* Enable or disable latch. If latch is enabled the alert pin will assert until the 120 | * conversion register is read (getResult functions). If disabled the alert pin assertion will be 121 | * cleared with next value within limits. 122 | * 123 | * ADS1115_LATCH_DISABLED (default) 124 | * ADS1115_LATCH_ENABLED 125 | */ 126 | //adc.setAlertLatch(ADS1115_LATCH_ENABLED); //uncomment if you want to change the default 127 | 128 | /* Sets the alert pin polarity if active: 129 | * 130 | * ADS1115_ACT_LOW -> active low (default) 131 | * ADS1115_ACT_HIGH -> active high 132 | */ 133 | //adc.setAlertPol(ADS1115_ACT_LOW); //uncomment if you want to change the default 134 | 135 | /* With this function the alert pin will assert, when a conversion is ready. 136 | * In order to deactivate, use the setAlertLimit_V function 137 | */ 138 | //adc.setAlertPinToConversionReady(); //uncomment if you want to change the default 139 | } 140 | 141 | /* If you change the compare channels you can immediately read values from the conversion 142 | * register, although they might belong to the former channel if no precautions are taken. 143 | * It takes about the time needed for two conversions to get the correct data. In single 144 | * shot mode you can use the isBusy() function to wait for data from the new channel. This 145 | * does not work in continuous mode. 146 | * To solve this issue the library adds a delay after change of channels if you are in contunuous 147 | * mode. The length of the delay is adjusted to the conversion rate. But be aware that the output 148 | * rate will be much lower that the conversion rate if you change channels frequently. 149 | */ 150 | 151 | void loop() { 152 | float voltage = 0.0; 153 | 154 | adc.setCompareChannels(ADS1115_COMP_0_GND); 155 | voltage = adc.getResult_V(); 156 | oled.setCursor(0,0); 157 | oled.print("Channel 0 [V]: "); 158 | oled.print(voltage); 159 | oled.clearToEOL(); 160 | 161 | adc.setCompareChannels(ADS1115_COMP_1_GND); 162 | voltage = adc.getResult_V(); 163 | oled.setCursor(0,2); 164 | oled.print("Channel 1 [V]: "); 165 | oled.print(voltage); 166 | oled.clearToEOL(); 167 | 168 | adc.setCompareChannels(ADS1115_COMP_2_GND); 169 | voltage = adc.getResult_V(); 170 | oled.setCursor(0,4); 171 | oled.print("Channel 2 [V]: "); 172 | oled.print(voltage); 173 | oled.clearToEOL(); 174 | 175 | adc.setCompareChannels(ADS1115_COMP_3_GND); 176 | voltage = adc.getResult_V(); 177 | oled.setCursor(0,6); 178 | oled.print("Channel 3 [V]: "); 179 | oled.print(voltage); 180 | oled.clearToEOL(); 181 | 182 | delay(2000); 183 | } 184 | -------------------------------------------------------------------------------- /examples/Auto_Range/Auto_Range.ino: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * Example sketch for the ADS1115_WE library 3 | * 4 | * This sketch shows how to use the Auto Range function of the AD1115_WE library. 5 | * 6 | * Further information can be found on: 7 | * https://wolles-elektronikkiste.de/ads1115 (German) 8 | * https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 9 | * 10 | ***************************************************************************/ 11 | 12 | #include 13 | #include 14 | #define I2C_ADDRESS 0x48 15 | 16 | /* There are several ways to create your ADS1115_WE object: 17 | * ADS1115_WE adc = ADS1115_WE(); -> uses Wire / I2C Address = 0x48 18 | * ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); -> uses Wire / I2C_ADDRESS 19 | * ADS1115_WE adc = ADS1115_WE(&Wire); -> you can pass any TwoWire object / I2C Address = 0x48 20 | * ADS1115_WE adc = ADS1115_WE(&Wire, I2C_ADDRESS); -> all together 21 | */ 22 | ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS); 23 | 24 | void setup() { 25 | Wire.begin(); 26 | Serial.begin(9600); 27 | if(!adc.init()){ 28 | Serial.println("ADS1115 not connected!"); 29 | } 30 | 31 | /* Set the voltage range of the ADC to adjust the gain 32 | * Please note that you must not apply more than VDD + 0.3V to the input pins! 33 | * 34 | * ADS1115_RANGE_6144 -> +/- 6144 mV 35 | * ADS1115_RANGE_4096 -> +/- 4096 mV 36 | * ADS1115_RANGE_2048 -> +/- 2048 mV (default) 37 | * ADS1115_RANGE_1024 -> +/- 1024 mV 38 | * ADS1115_RANGE_0512 -> +/- 512 mV 39 | * ADS1115_RANGE_0256 -> +/- 256 mV 40 | */ 41 | adc.setVoltageRange_mV(ADS1115_RANGE_6144); //comment line/change parameter to change range 42 | 43 | /* Set the inputs to be compared 44 | * 45 | * ADS1115_COMP_0_1 -> compares 0 with 1 (default) 46 | * ADS1115_COMP_0_3 -> compares 0 with 3 47 | * ADS1115_COMP_1_3 -> compares 1 with 3 48 | * ADS1115_COMP_2_3 -> compares 2 with 3 49 | * ADS1115_COMP_0_GND -> compares 0 with GND 50 | * ADS1115_COMP_1_GND -> compares 1 with GND 51 | * ADS1115_COMP_2_GND -> compares 2 with GND 52 | * ADS1115_COMP_3_GND -> compares 3 with GND 53 | */ 54 | adc.setCompareChannels(ADS1115_COMP_0_GND); //comment line/change parameter to change channel 55 | 56 | /* Set number of conversions after which the alert pin asserts 57 | * - or you can disable the alert 58 | * 59 | * ADS1115_ASSERT_AFTER_1 -> after 1 conversion 60 | * ADS1115_ASSERT_AFTER_2 -> after 2 conversions 61 | * ADS1115_ASSERT_AFTER_4 -> after 4 conversions 62 | * ADS1115_DISABLE_ALERT -> disable comparator / alert pin (default) 63 | */ 64 | //adc.setAlertPinMode(ADS1115_ASSERT_AFTER_1); //uncomment if you want to change the default 65 | 66 | /* Set the conversion rate in SPS (samples per second) 67 | * Options should be self-explaining: 68 | * 69 | * ADS1115_8_SPS 70 | * ADS1115_16_SPS 71 | * ADS1115_32_SPS 72 | * ADS1115_64_SPS 73 | * ADS1115_128_SPS (default) 74 | * ADS1115_250_SPS 75 | * ADS1115_475_SPS 76 | * ADS1115_860_SPS 77 | */ 78 | adc.setConvRate(ADS1115_64_SPS); //uncomment if you want to change the default 79 | 80 | /* Set continuous or single shot mode: 81 | * 82 | * ADS1115_CONTINUOUS -> continuous mode 83 | * ADS1115_SINGLE -> single shot mode (default) 84 | */ 85 | adc.setMeasureMode(ADS1115_CONTINUOUS); //comment line/change parameter to change mode 86 | 87 | /* Choose maximum limit or maximum and minimum alert limit (window) in Volt - alert pin will 88 | * assert when measured values are beyond the maximum limit or outside the window 89 | * Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 90 | * In max limit mode the minimum value is the limit where the alert pin assertion will be 91 | * cleared (if not latched) 92 | * 93 | * ADS1115_MAX_LIMIT 94 | * ADS1115_WINDOW 95 | * 96 | */ 97 | //adc.setAlertModeAndLimit_V(ADS1115_MAX_LIMIT, 3.0, 1.5); //uncomment if you want to change the default 98 | 99 | /* Enable or disable latch. If latch is enabled the alert pin will assert until the 100 | * conversion register is read (getResult functions). If disabled the alert pin assertion will be 101 | * cleared with next value within limits. 102 | * 103 | * ADS1115_LATCH_DISABLED (default) 104 | * ADS1115_LATCH_ENABLED 105 | */ 106 | //adc.setAlertLatch(ADS1115_LATCH_ENABLED); //uncomment if you want to change the default 107 | 108 | /* Sets the alert pin polarity if active: 109 | * 110 | * ADS1115_ACT_LOW -> active low (default) 111 | * ADS1115_ACT_HIGH -> active high 112 | */ 113 | //adc.setAlertPol(ADS1115_ACT_LOW); //uncomment if you want to change the default 114 | 115 | /* With this function the alert pin will assert, when a conversion is ready. 116 | * In order to deactivate, use the setAlertLimit_V function 117 | */ 118 | //adc.setAlertPinToConversionReady(); //uncomment if you want to change the default 119 | 120 | /* Enable or disable permanent automatic range selection mode. If enabled, the range will 121 | * change if the measured values are outside of 30-80% of the maximum value of the current 122 | * range. 123 | * !!! Use EITHER this function once OR setAutoRange() whenever needed (see below) !!! 124 | */ 125 | adc.setPermanentAutoRangeMode(true); 126 | 127 | Serial.println("ADS1115 Example Sketch - Continuous Mode with Auto Range"); 128 | Serial.println(); 129 | } 130 | 131 | void loop() { 132 | Serial.print("Channel 0 - "); 133 | readChannel(ADS1115_COMP_0_GND); 134 | 135 | Serial.print("Channel 1 - "); 136 | readChannel(ADS1115_COMP_1_GND); 137 | 138 | Serial.print("Channel 2 - "); 139 | readChannel(ADS1115_COMP_2_GND); 140 | 141 | Serial.print("Channel 3 - "); 142 | readChannel(ADS1115_COMP_3_GND); 143 | 144 | Serial.println("-------------------------------"); 145 | delay(1000); 146 | } 147 | 148 | void readChannel(ADS1115_MUX channel) { 149 | float voltage = 0.0; 150 | adc.setCompareChannels(channel); 151 | 152 | /* setAutoRange() switches to the highest range (+/- 6144 mV), measures the current 153 | * voltage and then switches to the lowest range where the current value is still 154 | * below 80% of the maximum value of the range. The function is only suitable if you 155 | * expect stable or slowly changing voltages. setAutoRange needs roughly the time you 156 | * would need for three conversions. 157 | * If the ADS115 is in single shot mode, setAutoRange() will switch into continuous 158 | * mode to measure a value and switch back again. 159 | * !!! Use EITHER this function whenever needed OR setPermanentAutoRangeMode(true) once !!! 160 | */ 161 | //adc.setAutoRange(); //use either this or setPermanentAutoRangeMode(true) 162 | 163 | voltage = adc.getResult_V(); // alternative: getResult_mV for Millivolt 164 | printVoltageRange(); // this is just to show that the range is changing with changing voltages 165 | Serial.println(voltage); 166 | } 167 | 168 | void printVoltageRange(){ 169 | unsigned int voltageRange = adc.getVoltageRange_mV(); 170 | Serial.print("Range: "); 171 | 172 | switch(voltageRange){ 173 | case 6144: 174 | Serial.print("+/- 6144 mV, Voltage [V]: "); 175 | break; 176 | case 4096: 177 | Serial.print("+/- 4096 mV, Voltage [V]: "); 178 | break; 179 | case 2048: 180 | Serial.print("+/- 2048 mV, Voltage [V]: "); 181 | break; 182 | case 1024: 183 | Serial.print("+/- 1024 mV, Voltage [V]: "); 184 | break; 185 | case 512: 186 | Serial.print("+/- 512 mV, Voltage [V]: "); 187 | break; 188 | case 256: 189 | Serial.print("+/- 256 mV, Voltage [V]: "); 190 | break; 191 | default: 192 | Serial.println("Something went wrong"); 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /src/ADS1115_WE.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * This is a library for the ADS1115 and ADS1015 A/D Converter 4 | * 5 | * You'll find several example sketches which should enable you to use the library. 6 | * 7 | * You are free to use it, change it or build on it. In case you like it, it would 8 | * be cool if you give it a star. 9 | * 10 | * If you find bugs, please inform me! 11 | * 12 | * Written by Wolfgang (Wolle) Ewald 13 | * https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 14 | * https://wolles-elektronikkiste.de/ads1115 (German) 15 | * 16 | * 17 | ******************************************************************************/ 18 | 19 | #ifndef ADS1115_WE_H_ 20 | #define ADS1115_WE_H_ 21 | 22 | #if (ARDUINO >= 100) 23 | #include "Arduino.h" 24 | #else 25 | #include "WProgram.h" 26 | #endif 27 | #include "ADS1115_config.h" 28 | 29 | #ifdef USE_TINY_WIRE_M_ 30 | #include 31 | #endif 32 | #ifndef USE_TINY_WIRE_M_ 33 | #include 34 | #endif 35 | 36 | typedef enum ADS1115_COMP_QUE { 37 | ADS1115_ASSERT_AFTER_1 = 0x0000, 38 | ADS1115_ASSERT_AFTER_2 = 0x0001, 39 | ADS1115_ASSERT_AFTER_4 = 0x0002, 40 | ADS1115_DISABLE_ALERT = 0x0003, 41 | ADS1015_ASSERT_AFTER_1 = ADS1115_ASSERT_AFTER_1, 42 | ADS1015_ASSERT_AFTER_2 = ADS1115_ASSERT_AFTER_2, 43 | ADS1015_ASSERT_AFTER_4 = ADS1115_ASSERT_AFTER_4, 44 | ADS1015_DISABLE_ALERT = ADS1115_DISABLE_ALERT 45 | } compQue; 46 | 47 | typedef enum ADS1115_LATCH { 48 | ADS1115_LATCH_DISABLED = 0x0000, 49 | ADS1115_LATCH_ENABLED = 0x0004, 50 | ADS1015_LATCH_DISABLED = ADS1115_LATCH_DISABLED, 51 | ADS1015_LATCH_ENABLED = ADS1115_LATCH_ENABLED 52 | } latch; 53 | 54 | typedef enum ADS1115_ALERT_POL { 55 | ADS1115_ACT_LOW = 0x0000, 56 | ADS1115_ACT_HIGH = 0x0008, 57 | ADS1015_ACT_LOW = ADS1115_ACT_LOW, 58 | ADS1015_ACT_HIGH = ADS1115_ACT_HIGH 59 | } alertPol; 60 | 61 | typedef enum ADS1115_COMP_MODE{ 62 | ADS1115_MAX_LIMIT = 0x0000, 63 | ADS1115_WINDOW = 0x0010, 64 | ADS1015_MAX_LIMIT = ADS1115_MAX_LIMIT, 65 | ADS1015_WINDOW = ADS1115_WINDOW 66 | } compMode; 67 | 68 | typedef enum ADS1115_CONV_RATE{ 69 | ADS1115_8_SPS = 0x0000, 70 | ADS1115_16_SPS = 0x0020, 71 | ADS1115_32_SPS = 0x0040, 72 | ADS1115_64_SPS = 0x0060, 73 | ADS1115_128_SPS = 0x0080, 74 | ADS1115_250_SPS = 0x00A0, 75 | ADS1115_475_SPS = 0x00C0, 76 | ADS1115_860_SPS = 0x00E0, 77 | ADS1015_128_SPS = ADS1115_8_SPS, 78 | ADS1015_250_SPS = ADS1115_16_SPS, 79 | ADS1015_490_SPS = ADS1115_32_SPS, 80 | ADS1015_920_SPS = ADS1115_64_SPS, 81 | ADS1015_1600_SPS = ADS1115_128_SPS, 82 | ADS1015_2400_SPS = ADS1115_250_SPS, 83 | ADS1015_3300_SPS = ADS1115_475_SPS, 84 | ADS1015_3300_SPS_2 = ADS1115_860_SPS 85 | } convRate; 86 | 87 | typedef enum ADS1115_MEASURE_MODE{ 88 | ADS1115_CONTINUOUS = 0x0000, 89 | ADS1115_SINGLE = 0x0100, 90 | ADS1015_CONTINUOUS = ADS1115_CONTINUOUS, 91 | ADS1015_SINGLE = ADS1115_SINGLE 92 | } measureMode; 93 | 94 | typedef enum ADS1115_RANGE{ 95 | ADS1115_RANGE_6144 = 0x0000, 96 | ADS1115_RANGE_4096 = 0x0200, 97 | ADS1115_RANGE_2048 = 0x0400, 98 | ADS1115_RANGE_1024 = 0x0600, 99 | ADS1115_RANGE_0512 = 0x0800, 100 | ADS1115_RANGE_0256 = 0x0A00, 101 | ADS1015_RANGE_6144 = ADS1115_RANGE_6144, 102 | ADS1015_RANGE_4096 = ADS1115_RANGE_4096, 103 | ADS1015_RANGE_2048 = ADS1115_RANGE_2048, 104 | ADS1015_RANGE_1024 = ADS1115_RANGE_1024, 105 | ADS1015_RANGE_0512 = ADS1115_RANGE_0512, 106 | ADS1015_RANGE_0256 = ADS1115_RANGE_0256 107 | } range; 108 | 109 | typedef enum ADS1115_MUX{ 110 | ADS1115_COMP_0_1 = 0x0000, 111 | ADS1115_COMP_0_3 = 0x1000, 112 | ADS1115_COMP_1_3 = 0x2000, 113 | ADS1115_COMP_2_3 = 0x3000, 114 | ADS1115_COMP_0_GND = 0x4000, 115 | ADS1115_COMP_1_GND = 0x5000, 116 | ADS1115_COMP_2_GND = 0x6000, 117 | ADS1115_COMP_3_GND = 0x7000, 118 | ADS1015_COMP_0_1 = ADS1115_COMP_0_1, 119 | ADS1015_COMP_0_3 = ADS1115_COMP_0_3, 120 | ADS1015_COMP_1_3 = ADS1115_COMP_1_3, 121 | ADS1015_COMP_2_3 = ADS1115_COMP_2_3, 122 | ADS1015_COMP_0_GND = ADS1115_COMP_0_GND, 123 | ADS1015_COMP_1_GND = ADS1115_COMP_1_GND, 124 | ADS1015_COMP_2_GND = ADS1115_COMP_2_GND, 125 | ADS1015_COMP_3_GND = ADS1115_COMP_3_GND 126 | } mux; 127 | 128 | #define ADS1115_COMP_INC 0x1000 // increment to next channel 129 | #define ADS1015_MUX ADS1115_MUX 130 | 131 | typedef enum ADS1115_STATUS_OR_START{ 132 | ADS1115_BUSY = 0x0000, 133 | ADS1115_START_ISREADY = 0x8000, 134 | ADS1015_BUSY = ADS1115_BUSY, 135 | ADS1015_START_ISREADY = ADS1115_START_ISREADY 136 | } statusOrStart; 137 | 138 | 139 | class ADS1115_WE 140 | { 141 | public: 142 | /* registers */ 143 | static constexpr uint8_t ADS1115_CONV_REG {0x00}; // Conversion Register 144 | static constexpr uint8_t ADS1115_CONFIG_REG {0x01}; // Configuration Register 145 | static constexpr uint8_t ADS1115_LO_THRESH_REG {0x02}; // Low Threshold Register 146 | static constexpr uint8_t ADS1115_HI_THRESH_REG {0x03}; // High Threshold Register 147 | 148 | /* other */ 149 | static constexpr uint16_t ADS1115_REG_FACTOR {32768}; 150 | static constexpr uint16_t ADS1115_REG_RESET_VAL {0x8583}; 151 | 152 | #ifndef USE_TINY_WIRE_M_ 153 | ADS1115_WE(const uint8_t addr = 0x48) : _wire{&Wire}, i2cAddress{addr} {} 154 | ADS1115_WE(TwoWire *w, const uint8_t addr = 0x48) : _wire{w}, i2cAddress{addr} {} 155 | #else 156 | ADS1115_WE(const uint8_t addr = 0x48) : i2cAddress{addr} {} 157 | #endif 158 | 159 | void reset(); 160 | bool init(bool ads1015 = false); 161 | uint8_t isDisconnected(); 162 | 163 | /* Set number of conversions after which the alert pin will be active 164 | * - or you can disable the alert 165 | * 166 | * ADS1115_ASSERT_AFTER_1 -> after 1 conversion 167 | * ADS1115_ASSERT_AFTER_2 -> after 2 conversions 168 | * ADS1115_ASSERT_AFTER_4 -> after 4 conversions 169 | * ADS1115_DISABLE_ALERT -> disable comparator // alert pin (default) 170 | */ 171 | void setAlertPinMode(ADS1115_COMP_QUE mode); 172 | 173 | /* Enable or disable latch. If latch is enabled the alarm pin will be active until the 174 | * conversion register is read (getResult functions). If disabled the alarm pin will be 175 | * deactivated with next value within limits. 176 | * 177 | * ADS1115_LATCH_DISABLED (default) 178 | * ADS1115_LATCH_ENABLED 179 | */ 180 | void setAlertLatch(ADS1115_LATCH latch); 181 | 182 | /* Sets the alert pin polarity if active: 183 | * 184 | * Enable or disable latch. If latch is enabled the alarm pin will be active until the 185 | * conversion register is read (getResult functions). If disabled the alarm pin will be 186 | * deactivated with next value within limits. 187 | * 188 | * ADS1115_ACT_LOW -> active low (default) 189 | * ADS1115_ACT_HIGH -> active high 190 | */ 191 | void setAlertPol(ADS1115_ALERT_POL polarity); 192 | 193 | /* Choose maximum limit or maximum and minimum alert limit (window)in Volt - alert pin will 194 | * be active when measured values are beyond the maximum limit or outside the window 195 | * Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 196 | * In max limit mode the minimum value is the limit where the alert pin will be deactivated (if 197 | * not latched) 198 | * 199 | * ADS1115_MAX_LIMIT 200 | * ADS1115_WINDOW 201 | */ 202 | void setAlertModeAndLimit_V(ADS1115_COMP_MODE mode, float hithres, float lothres); 203 | 204 | /* Set the conversion rate in SPS (samples per second) 205 | * Options should be self-explaining: 206 | * 207 | * ADS1115_8_SPS 208 | * ADS1115_16_SPS 209 | * ADS1115_32_SPS 210 | * ADS1115_64_SPS 211 | * ADS1115_128_SPS (default) 212 | * ADS1115_250_SPS 213 | * ADS1115_475_SPS 214 | * ADS1115_860_SPS 215 | */ 216 | void setConvRate(ADS1115_CONV_RATE rate); 217 | 218 | /* returns the conversion rate */ 219 | convRate getConvRate(); 220 | 221 | /* Set continuous or single shot mode: 222 | * 223 | * ADS1115_CONTINUOUS -> continuous mode 224 | * ADS1115_SINGLE -> single shot mode (default) 225 | */ 226 | void setMeasureMode(ADS1115_MEASURE_MODE mode); 227 | 228 | /* Set the voltage range of the ADC to adjust the gain: 229 | * Please note that you must not apply more than VDD + 0.3V to the input pins! 230 | * 231 | * ADS1115_RANGE_6144 -> +/- 6144 mV 232 | * ADS1115_RANGE_4096 -> +/- 4096 mV 233 | * ADS1115_RANGE_2048 -> +/- 2048 mV (default) 234 | * ADS1115_RANGE_1024 -> +/- 1024 mV 235 | * ADS1115_RANGE_0512 -> +/- 512 mV 236 | * ADS1115_RANGE_0256 -> +/- 256 mV 237 | */ 238 | void setVoltageRange_mV(ADS1115_RANGE range); 239 | 240 | /* Set the voltage range automatically 241 | * 1) changes into maximum range and continuous mode 242 | * 2) measures the voltage 243 | * 3) chooses the smallest range in which the measured voltage is <80% 244 | * of the range's maximum 245 | * 4) switches back to single shot mode if it was in this mode before 246 | * 247 | * Please be aware that the procedure takes the the time needed for several conversions. 248 | * You should ony use it in case you expect stable or slowly changing voltages. 249 | */ 250 | void setAutoRange(); 251 | 252 | /* Set the automatic voltage range permanantly, but the range will only be changed if the 253 | * measured value is outside 30 - 80% of the maximum value of the current range. 254 | * Therefore this method is faster than setAutoRange(). 255 | */ 256 | void setPermanentAutoRangeMode(bool autoMode); 257 | 258 | /* Set the inputs to be compared 259 | * 260 | * ADS1115_COMP_0_1 -> compares 0 with 1 (default) 261 | * ADS1115_COMP_0_3 -> compares 0 with 3 262 | * ADS1115_COMP_1_3 -> compares 1 with 3 263 | * ADS1115_COMP_2_3 -> compares 2 with 3 264 | * ADS1115_COMP_0_GND -> compares 0 with GND 265 | * ADS1115_COMP_1_GND -> compares 1 with GND 266 | * ADS1115_COMP_2_GND -> compares 2 with GND 267 | * ADS1115_COMP_3_GND -> compares 3 with GND 268 | */ 269 | void setCompareChannels(ADS1115_MUX mux); 270 | 271 | /* Set to channel (0-3) in single ended mode in a non blocking way without delay 272 | */ 273 | void setCompareChannels_nonblock(ADS1115_MUX mux); 274 | 275 | /* Set to channel (0-3) in single ended mode 276 | */ 277 | void setSingleChannel(size_t channel); 278 | 279 | bool isBusy(); 280 | void startSingleMeasurement(); 281 | float getResult_V(); 282 | float getResult_mV(); 283 | 284 | /* Get the raw result from the conversion register: 285 | * The conversion register contains the conversion result of the amplified (!) 286 | * voltage. This means the value depends on the voltage as well as on the 287 | * voltage range. E.g. if the voltage range is 6144 mV (ADS1115_RANGE_6144), 288 | * +32767 is 6144 mV; if the range is 4096 mV, +32767 is 4096 mV, and so on. 289 | */ 290 | int16_t getRawResult(); 291 | 292 | /* Scaling of the result to a different range: 293 | * The results in the conversion register are in a range of -32767 to +32767 294 | * You might want to receive the result in a different scale, e.g. -1023 to 1023. 295 | * For -1023 to 1023, and if you have chosen e.g. ADS1115_RANGE_4096, 0 Volt would 296 | * give 0 as result and 4096 mV would give 1023. -4096 mV would give -1023. 297 | */ 298 | int16_t getResultWithRange(int16_t min, int16_t max); 299 | 300 | /* Scaling of the result to a different range plus scaling to a voltage range: 301 | * You can use this variant if you also want to scale to a voltage range. E.g. in 302 | * in order to get results equivalent to an Arduino UNO (10 bit, 5000 mV range), you 303 | * would choose getResultWithRange(-1023, 1023, 5000). A difference to the Arduino 304 | * UNO is that you can measure negative voltages. 305 | * You have to ensure that the voltage range you scale to is smaller than the 306 | * measuring voltage range. 307 | */ 308 | int16_t getResultWithRange(int16_t min, int16_t max, int16_t maxVoltage); 309 | 310 | /* This function returns the voltage range ADS1115_RANGE_XXXX in Millivolt */ 311 | uint16_t getVoltageRange_mV(); 312 | 313 | /* With this function the alert pin will be active, when a conversion is ready. 314 | * In order to deactivate, use the setAlertLimit_V function 315 | */ 316 | void setAlertPinToConversionReady(); 317 | void clearAlert(); 318 | 319 | 320 | protected: 321 | #ifndef USE_TINY_WIRE_M_ 322 | TwoWire *_wire; 323 | #endif 324 | bool useADS1015; 325 | uint16_t voltageRange; 326 | ADS1115_MEASURE_MODE deviceMeasureMode; 327 | uint8_t i2cAddress; 328 | bool autoRangeMode; 329 | void delayAccToRate(convRate cr); 330 | int16_t calcLimit(float rawLimit); 331 | uint8_t writeRegister(uint8_t reg, uint16_t val); 332 | uint16_t readRegister(uint8_t reg); 333 | }; 334 | #endif 335 | 336 | -------------------------------------------------------------------------------- /src/ADS1115_WE.cpp: -------------------------------------------------------------------------------- 1 | /***************************************** 2 | * This is a library for the ADS1115 and ADS1015 A/D Converter 3 | * 4 | * You'll find an example which should enable you to use the library. 5 | * 6 | * You are free to use it, change it or build on it. In case you like 7 | * it, it would be cool if you give it a star. 8 | * 9 | * If you find bugs, please inform me! 10 | * 11 | * Written by Wolfgang (Wolle) Ewald 12 | * https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 13 | * https://wolles-elektronikkiste.de/ads1115 (German) 14 | * 15 | *******************************************/ 16 | 17 | #include "ADS1115_WE.h" 18 | 19 | void ADS1115_WE::reset(){ 20 | #ifndef USE_TINY_WIRE_M_ 21 | _wire->beginTransmission(0); 22 | _wire->write(0x06); 23 | _wire->endTransmission(); 24 | #else 25 | TinyWireM.beginTransmission(0); 26 | TinyWireM.send(0x06); 27 | TinyWireM.endTransmission(); 28 | #endif 29 | } 30 | 31 | bool ADS1115_WE::init(bool ads1015){ 32 | useADS1015 = ads1015; 33 | 34 | if(isDisconnected()){ 35 | return 0; 36 | } 37 | writeRegister(ADS1115_CONFIG_REG, ADS1115_REG_RESET_VAL); 38 | setVoltageRange_mV(ADS1115_RANGE_2048); 39 | writeRegister(ADS1115_LO_THRESH_REG, 0x8000); 40 | writeRegister(ADS1115_HI_THRESH_REG, 0x7FFF); 41 | deviceMeasureMode = ADS1115_SINGLE; 42 | autoRangeMode = false; 43 | return 1; 44 | } 45 | 46 | uint8_t ADS1115_WE::isDisconnected(){ 47 | #ifndef USE_TINY_WIRE_M_ 48 | _wire->beginTransmission(i2cAddress); 49 | uint8_t success = _wire->endTransmission(); 50 | #else 51 | TinyWireM.beginTransmission(i2cAddress); 52 | uint8_t success = TinyWireM.endTransmission(); 53 | #endif 54 | return success; 55 | } 56 | 57 | void ADS1115_WE::setAlertPinMode(ADS1115_COMP_QUE mode){ 58 | uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); 59 | currentConfReg &= ~(0x8003); 60 | currentConfReg |= mode; 61 | writeRegister(ADS1115_CONFIG_REG, currentConfReg); 62 | } 63 | 64 | void ADS1115_WE::setAlertLatch(ADS1115_LATCH latch){ 65 | uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); 66 | currentConfReg &= ~(0x8004); 67 | currentConfReg |= latch; 68 | writeRegister(ADS1115_CONFIG_REG, currentConfReg); 69 | } 70 | 71 | void ADS1115_WE::setAlertPol(ADS1115_ALERT_POL polarity){ 72 | uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); 73 | currentConfReg &= ~(0x8008); 74 | currentConfReg |= polarity; 75 | writeRegister(ADS1115_CONFIG_REG, currentConfReg); 76 | } 77 | 78 | void ADS1115_WE::setAlertModeAndLimit_V(ADS1115_COMP_MODE mode, float hiThres, float loThres){ 79 | uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); 80 | currentConfReg &= ~(0x8010); 81 | currentConfReg |= mode; 82 | writeRegister(ADS1115_CONFIG_REG, currentConfReg); 83 | int16_t alertLimit = calcLimit(hiThres); 84 | writeRegister(ADS1115_HI_THRESH_REG, alertLimit); 85 | alertLimit = calcLimit(loThres); 86 | writeRegister(ADS1115_LO_THRESH_REG, alertLimit); 87 | 88 | } 89 | 90 | void ADS1115_WE::setConvRate(ADS1115_CONV_RATE rate){ 91 | uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); 92 | currentConfReg &= ~(0x80E0); 93 | currentConfReg |= rate; 94 | writeRegister(ADS1115_CONFIG_REG, currentConfReg); 95 | } 96 | 97 | convRate ADS1115_WE::getConvRate(){ 98 | uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); 99 | return (convRate)(currentConfReg & 0xE0); 100 | } 101 | 102 | void ADS1115_WE::setMeasureMode(ADS1115_MEASURE_MODE mode){ 103 | uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); 104 | deviceMeasureMode = mode; 105 | currentConfReg &= ~(0x8100); 106 | currentConfReg |= mode; 107 | writeRegister(ADS1115_CONFIG_REG, currentConfReg); 108 | } 109 | 110 | void ADS1115_WE::setVoltageRange_mV(ADS1115_RANGE range){ 111 | uint16_t currentVoltageRange = voltageRange; 112 | uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); 113 | uint16_t currentRange = (currentConfReg >> 9) & 7; 114 | uint16_t currentAlertPinMode = currentConfReg & 3; 115 | 116 | setMeasureMode(ADS1115_SINGLE); 117 | 118 | switch(range){ 119 | case ADS1115_RANGE_6144: 120 | voltageRange = 6144; 121 | break; 122 | case ADS1115_RANGE_4096: 123 | voltageRange = 4096; 124 | break; 125 | case ADS1115_RANGE_2048: 126 | voltageRange = 2048; 127 | break; 128 | case ADS1115_RANGE_1024: 129 | voltageRange = 1024; 130 | break; 131 | case ADS1115_RANGE_0512: 132 | voltageRange = 512; 133 | break; 134 | case ADS1115_RANGE_0256: 135 | voltageRange = 256; 136 | break; 137 | } 138 | 139 | if ((currentRange != range) && (currentAlertPinMode != ADS1115_DISABLE_ALERT)){ 140 | int16_t alertLimit = readRegister(ADS1115_HI_THRESH_REG); 141 | alertLimit = alertLimit * (currentVoltageRange * 1.0 / voltageRange); 142 | writeRegister(ADS1115_HI_THRESH_REG, alertLimit); 143 | 144 | alertLimit = readRegister(ADS1115_LO_THRESH_REG); 145 | alertLimit = alertLimit * (currentVoltageRange * 1.0 / voltageRange); 146 | writeRegister(ADS1115_LO_THRESH_REG, alertLimit); 147 | } 148 | 149 | currentConfReg &= ~(0x8E00); 150 | currentConfReg |= range; 151 | writeRegister(ADS1115_CONFIG_REG, currentConfReg); 152 | convRate rate = getConvRate(); 153 | delayAccToRate(rate); 154 | } 155 | 156 | void ADS1115_WE::setAutoRange(){ 157 | uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); 158 | setVoltageRange_mV(ADS1115_RANGE_6144); 159 | 160 | if(deviceMeasureMode == ADS1115_SINGLE){ 161 | setMeasureMode(ADS1115_CONTINUOUS); 162 | convRate rate = getConvRate(); 163 | delayAccToRate(rate); 164 | } 165 | 166 | int16_t rawResult = abs(readRegister(ADS1115_CONV_REG)); 167 | int16_t rawResultCopy = rawResult; 168 | if(rawResultCopy == -32768){ 169 | rawResultCopy++; 170 | } 171 | rawResultCopy = abs(rawResultCopy); 172 | 173 | range optRange = ADS1115_RANGE_6144; 174 | 175 | if(rawResultCopy < 1093){ 176 | optRange = ADS1115_RANGE_0256; 177 | } 178 | else if(rawResultCopy < 2185){ 179 | optRange = ADS1115_RANGE_0512; 180 | } 181 | else if(rawResultCopy < 4370){ 182 | optRange = ADS1115_RANGE_1024; 183 | } 184 | else if(rawResultCopy < 8738){ 185 | optRange = ADS1115_RANGE_2048; 186 | } 187 | else if(rawResultCopy < 17476){ 188 | optRange = ADS1115_RANGE_4096; 189 | } 190 | 191 | writeRegister(ADS1115_CONFIG_REG, currentConfReg); 192 | setVoltageRange_mV(optRange); 193 | } 194 | 195 | void ADS1115_WE::setPermanentAutoRangeMode(bool autoMode){ 196 | if(autoMode){ 197 | autoRangeMode = true; 198 | } 199 | else{ 200 | autoRangeMode = false; 201 | } 202 | } 203 | 204 | void ADS1115_WE::delayAccToRate(convRate cr){ 205 | if(!useADS1015){ 206 | switch(cr){ 207 | case ADS1115_8_SPS: 208 | delay(130); 209 | break; 210 | case ADS1115_16_SPS: 211 | delay(65); 212 | break; 213 | case ADS1115_32_SPS: 214 | delay(32); 215 | break; 216 | case ADS1115_64_SPS: 217 | delay(16); 218 | break; 219 | case ADS1115_128_SPS: 220 | delay(8); 221 | break; 222 | case ADS1115_250_SPS: 223 | delay(4); 224 | break; 225 | case ADS1115_475_SPS: 226 | delay(3); 227 | break; 228 | case ADS1115_860_SPS: 229 | delay(2); 230 | break; 231 | } 232 | } 233 | else{ 234 | switch(cr){ 235 | case ADS1015_128_SPS: 236 | delay(8); 237 | break; 238 | case ADS1015_250_SPS: 239 | delay(5); 240 | break; 241 | case ADS1015_490_SPS: 242 | delay(2); 243 | break; 244 | case ADS1015_920_SPS: 245 | delay(1); 246 | break; 247 | case ADS1015_1600_SPS: 248 | delayMicroseconds(675); 249 | break; 250 | case ADS1015_2400_SPS: 251 | delayMicroseconds(450); 252 | break; 253 | case ADS1015_3300_SPS: 254 | delayMicroseconds(330); 255 | break; 256 | case ADS1015_3300_SPS_2: 257 | delayMicroseconds(330); 258 | break; 259 | } 260 | } 261 | } 262 | 263 | void ADS1115_WE::setCompareChannels(ADS1115_MUX mux){ 264 | uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); 265 | currentConfReg &= ~(0xF000); 266 | currentConfReg |= (mux); 267 | writeRegister(ADS1115_CONFIG_REG, currentConfReg); 268 | 269 | if(!(currentConfReg & 0x0100)){ // => if not single shot mode 270 | convRate rate = getConvRate(); 271 | for(int i=0; i<2; i++){ // waiting time for two measurements 272 | delayAccToRate(rate); 273 | } 274 | } 275 | } 276 | 277 | void ADS1115_WE::setCompareChannels_nonblock(ADS1115_MUX mux){ 278 | uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); 279 | currentConfReg &= ~(0xF000); 280 | currentConfReg |= (mux); 281 | writeRegister(ADS1115_CONFIG_REG, currentConfReg); 282 | } 283 | 284 | void ADS1115_WE::setSingleChannel(size_t channel) { 285 | if (channel >= 4) 286 | return; 287 | setCompareChannels((ADS1115_MUX)(ADS1115_COMP_0_GND + ADS1115_COMP_INC*channel)); 288 | } 289 | 290 | bool ADS1115_WE::isBusy(){ 291 | uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); 292 | if(deviceMeasureMode == ADS1115_SINGLE){ 293 | return (!(currentConfReg>>15) & 1); 294 | } 295 | else return 0; 296 | } 297 | 298 | 299 | void ADS1115_WE::startSingleMeasurement(){ 300 | uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG); 301 | currentConfReg |= (1 << 15); 302 | writeRegister(ADS1115_CONFIG_REG, currentConfReg); 303 | } 304 | 305 | 306 | float ADS1115_WE::getResult_V(){ 307 | float result = getResult_mV(); 308 | result /= 1000; 309 | return result; 310 | } 311 | 312 | float ADS1115_WE::getResult_mV(){ 313 | int16_t rawResult = getRawResult(); 314 | float result = (rawResult * 1.0 / ADS1115_REG_FACTOR) * voltageRange; 315 | return result; 316 | } 317 | 318 | int16_t ADS1115_WE::getRawResult(){ 319 | int16_t rawResult = readRegister(ADS1115_CONV_REG); 320 | if(autoRangeMode){ 321 | int16_t rawResultCopy = rawResult; 322 | if(rawResultCopy == -32768){ 323 | rawResultCopy++; 324 | } 325 | rawResultCopy = abs(rawResultCopy); 326 | if((rawResultCopy > 26214) && (voltageRange != 6144)){ // 80% 327 | setAutoRange(); 328 | rawResult = readRegister(ADS1115_CONV_REG); 329 | } 330 | else if((rawResultCopy < 9800) && (voltageRange != 256)){ //30% 331 | setAutoRange(); 332 | rawResult = readRegister(ADS1115_CONV_REG); 333 | } 334 | } 335 | return rawResult; 336 | } 337 | 338 | int16_t ADS1115_WE::getResultWithRange(int16_t min, int16_t max){ 339 | int16_t rawResult = getRawResult(); 340 | int16_t result = map(rawResult, -32768, 32767, min, max); 341 | return result; 342 | } 343 | 344 | int16_t ADS1115_WE::getResultWithRange(int16_t min, int16_t max, int16_t maxMillivolt){ 345 | int16_t result = getResultWithRange(min, max); 346 | result = static_cast((1.0 * result * voltageRange / maxMillivolt) + 0.5); 347 | return result; 348 | } 349 | 350 | uint16_t ADS1115_WE::getVoltageRange_mV(){ 351 | return voltageRange; 352 | } 353 | 354 | void ADS1115_WE::setAlertPinToConversionReady(){ 355 | writeRegister(ADS1115_LO_THRESH_REG, (0<<15)); 356 | writeRegister(ADS1115_HI_THRESH_REG, (1<<15)); 357 | } 358 | 359 | void ADS1115_WE::clearAlert(){ 360 | readRegister(ADS1115_CONV_REG); 361 | } 362 | 363 | /************************************************ 364 | private functions 365 | *************************************************/ 366 | 367 | int16_t ADS1115_WE::calcLimit(float rawLimit){ 368 | int16_t limit = static_cast((rawLimit * ADS1115_REG_FACTOR / voltageRange)*1000); 369 | return limit; 370 | } 371 | 372 | uint8_t ADS1115_WE::writeRegister(uint8_t reg, uint16_t val){ 373 | uint8_t lVal = val & 255; 374 | uint8_t hVal = val >> 8; 375 | #ifndef USE_TINY_WIRE_M_ 376 | _wire->beginTransmission(i2cAddress); 377 | _wire->write(reg); 378 | _wire->write(hVal); 379 | _wire->write(lVal); 380 | return _wire->endTransmission(); 381 | #else 382 | TinyWireM.beginTransmission(i2cAddress); 383 | TinyWireM.send(reg); 384 | TinyWireM.send(hVal); 385 | TinyWireM.send(lVal); 386 | return TinyWireM.endTransmission(); 387 | #endif 388 | 389 | } 390 | 391 | uint16_t ADS1115_WE::readRegister(uint8_t reg){ 392 | uint8_t MSByte = 0, LSByte = 0; 393 | uint16_t regValue = 0; 394 | #ifndef USE_TINY_WIRE_M_ 395 | _wire->beginTransmission(i2cAddress); 396 | _wire->write(reg); 397 | _wire->endTransmission(false); 398 | _wire->requestFrom(i2cAddress,static_cast(2)); 399 | if(_wire->available()){ 400 | MSByte = _wire->read(); 401 | LSByte = _wire->read(); 402 | } 403 | #else 404 | TinyWireM.beginTransmission(i2cAddress); 405 | TinyWireM.send(reg); 406 | TinyWireM.endTransmission(); 407 | TinyWireM.requestFrom(i2cAddress,static_cast(2)); 408 | MSByte = TinyWireM.receive(); 409 | LSByte = TinyWireM.receive(); 410 | #endif 411 | regValue = (MSByte<<8) + LSByte; 412 | return regValue; 413 | } 414 | --------------------------------------------------------------------------------