├── LICENSE ├── README.md ├── Examples ├── ads1115_using_two_ads1115.py ├── ads1115_who_am_i.py ├── ads1115_result_format_options.py ├── ads1115_single_shot_conv_ready_controlled.py ├── ads1015_continuous.py ├── ads1115_continuous.py ├── ads1115_alert_window_mode.py ├── ads1115_single_shot.py ├── ads1115_alert_window_mode_with_latch.py ├── ads1115_conv_ready_alert_pin_controlled.py └── ads1115_auto_range.py └── ADS1115.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ADS1115_mpy 2 | A MicroPython module for the 16-bit, 4 channel ADS1115 and ADS1015 ADC. All features of the ADS1x15 are implemented, including alert functions. 3 | 4 | I have developed it on the ESP32, latest successful test was with the firmware version v1.24.1 (2024-11-29). 5 | 6 | I have have tried to optimize the module for convenience to use. If you try the examples I recommend 7 | to start with ads1115_single_shot.py. 8 | 9 | All features of the ADS1115 are implemented, including alert functions. 10 | 11 | The module is a translation of my Arduino library ADS1115_WE which you find here on GitHub: 12 | 13 | https://github.com/wollewald/ADS1115_WE 14 | 15 | You can find more information about the ADS1115 in my blog post about the Arduino version: 16 | 17 | https://wolles-elektronikkiste.de/ads1115 (German) 18 | 19 | https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 20 | 21 |

ADS1115 or ADS1015 - which ADC do you really have?

22 | 23 | There are many modules which are labelled as ADS1115 which are based on an ADS1015 and the other way round. The main differences are: 24 | 25 | ADS1115: 16-bit resolution / 8 SPS to 860 SPS 26 | 27 | ADS1015: 12-bit resolution / 128 SPS to 3300 SPS 28 | 29 | You might not even notice if have not the one you thought you have. To find out which one you have, you can use ads1115_who_am_i.py. 30 | -------------------------------------------------------------------------------- /Examples/ads1115_using_two_ads1115.py: -------------------------------------------------------------------------------- 1 | ######################################################################################## 2 | # Example program for the ADS1115_mPy module 3 | # 4 | # This program 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 | # 19 | # If you need up to 32 ADS1115 modules you can use a multiplexer like the TSCA9548A. 20 | # 21 | # Or you combine both and control up to 64 ADS1115 modules. 22 | # 23 | # Further information can be found on (currently only for the Arduino version): 24 | # https://wolles-elektronikkiste.de/ads1115 (German) 25 | # https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 26 | # 27 | ######################################################################################## 28 | 29 | from machine import I2C, Pin 30 | from time import sleep 31 | from ADS1115 import * 32 | 33 | I2C_ADDRESS_1 = 0x48 34 | I2C_ADDRESS_2 = 0x49 35 | 36 | i2c = I2C(0) 37 | adc_1 = ADS1115(I2C_ADDRESS_1, i2c=i2c) 38 | adc_2 = ADS1115(I2C_ADDRESS_2, i2c=i2c) 39 | 40 | adc_1.setVoltageRange_mV(ADS1115_RANGE_6144) 41 | adc_1.setMeasureMode(ADS1115_CONTINUOUS) 42 | adc_1.setCompareChannels(ADS1115_COMP_0_GND) 43 | 44 | adc_2.setVoltageRange_mV(ADS1115_RANGE_6144) 45 | adc_2.setMeasureMode(ADS1115_CONTINUOUS) 46 | adc_2.setCompareChannels(ADS1115_COMP_0_GND) 47 | 48 | while True: 49 | voltage = adc_1.getResult_V() 50 | print("Voltage [V], ADS1115 No 1: {:<4.2f}".format(voltage)) 51 | voltage = adc_2.getResult_V() 52 | print("Voltage [V], ADS1115 No 1: {:<4.2f}".format(voltage)) 53 | 54 | print("****************************") 55 | sleep(1) 56 | -------------------------------------------------------------------------------- /Examples/ads1115_who_am_i.py: -------------------------------------------------------------------------------- 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 | from machine import I2C, Pin 17 | import time 18 | from time import sleep 19 | from ADS1115 import * 20 | 21 | ADS1115_ADDRESS = 0x48 22 | 23 | i2c = I2C(0) 24 | adc = ADS1115(ADS1115_ADDRESS, i2c=i2c) 25 | 26 | adc.setVoltageRange_mV(ADS1115_RANGE_6144) 27 | adc.setCompareChannels(ADS1115_COMP_0_GND) 28 | adc.setMeasureMode(ADS1115_SINGLE) 29 | print("ADS1115/ADS1015 Example Sketch - Who am I") 30 | print("Performing 10 single ended conversions A0 vs. GND:"); 31 | print(" "); 32 | #uint16_t checkSum = 0; 33 | 34 | sum = 0 35 | for i in range(10): 36 | adc.startSingleMeasurement() 37 | while adc.isBusy(): 38 | pass 39 | raw = adc.getRawResult() 40 | print(bin(raw)) 41 | sum = sum + raw%8 42 | 43 | print("Check sum of last for bits", sum) 44 | 45 | 46 | adc.setConvRate(ADS1115_8_SPS); # = ADS1015_128_SPS = 0x0000 47 | 48 | startTime = time.ticks_ms() 49 | for i in range(10): 50 | adc.startSingleMeasurement() 51 | while adc.isBusy(): 52 | pass 53 | duration = time.ticks_ms() - startTime 54 | print("Time needed for 10 conversions at slowest sample rate [ms]: ", duration) 55 | 56 | if (sum > 0) and (duration > 1000): 57 | print("I am an ADS1115!"); 58 | elif (sum == 0) and (duration < 1000): 59 | print("I am an ADS1015!"); 60 | else: 61 | print("Sorry, I am not sure. Please run the program again!"); 62 | 63 | while True: 64 | sleep(1) -------------------------------------------------------------------------------- /Examples/ads1115_result_format_options.py: -------------------------------------------------------------------------------- 1 | ##################################################################################### 2 | # Example program for the ADS1115_mPy module 3 | # 4 | # This program shows how to obtain results using different scales / formats. 5 | # 6 | # Further information can be found on (currently only for the Arduino version): 7 | # https://wolles-elektronikkiste.de/ads1115 (German) 8 | # https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 9 | # 10 | ##################################################################################### 11 | 12 | from machine import I2C 13 | from time import sleep 14 | from ADS1115 import * 15 | 16 | ADS1115_ADDRESS = 0x48 17 | 18 | i2c = I2C(0) 19 | adc = ADS1115(ADS1115_ADDRESS, i2c=i2c) 20 | 21 | adc.setVoltageRange_mV(ADS1115_RANGE_6144) 22 | adc.setCompareChannels(ADS1115_COMP_0_GND) 23 | adc.setMeasureMode(ADS1115_CONTINUOUS) 24 | 25 | print("ADS1115 Example Sketch - Results in different scales / formats") 26 | print("All results are for Channel 0 vs. GND") 27 | print() 28 | 29 | while True: 30 | voltageInMillivolt = adc.getResult_mV() 31 | print("{:<33} {:<5.0f}".format("Result in Millivolt [mV]:", voltageInMillivolt)) 32 | voltageInVolt = adc.getResult_V() 33 | print("{:<33} {:<5.2f}".format("Result in Volt [V]:", voltageInVolt)) 34 | 35 | # Get the raw result from the conversion register. The conversion register 36 | # contains the conversion result of the amplified (!) voltage. This means the 37 | # value depends on the voltage as well as on the voltage range. E.g. if the 38 | # voltage range is 6144 mV (ADS1115_RANGE_6144), +32767 is 6144 mV; if the 39 | # range is 4096 mV, +32767 is 4096 mV, and so on. 40 | rawResult = adc.getRawResult() 41 | print("{:<33}".format("Raw Result:"), rawResult) 42 | 43 | # Scaling of the result to a different range: 44 | # The results in the conversion register are in a range of -32767 to +32767 45 | # You might want to receive the result in a different scale, e.g. -1023 to 1023. 46 | # For -1023 to 1023, and if you have chosen e.g. ADS1115_RANGE_4096, 0 Volt would 47 | # give 0 as result and 4096 mV would give 1023. -4096 mV would give -1023. 48 | scaledResult = adc.getResultWithRange(-1023, 1023) 49 | print("{:<33} {:<5.0f}".format("Scaled result:",scaledResult)) 50 | 51 | # Scaling of the result to a different range plus scaling to a voltage range: 52 | # You can use this variant if you also want to scale to a voltage range. E.g. in 53 | # in order to get results equivalent to an Arduino UNO (10 bit, 5000 mV range), you 54 | # would choose getResultWithRange(-1023, 1023, 5000). A difference to the Arduino 55 | # UNO is that you can measure negative voltages. 56 | # You have to ensure that the voltage range you scale to is smaller than the 57 | # measuring voltage range. For this example only ADS1115_RANGE_6144 would cover the 58 | # scale up to 5000 mV. 59 | scaledResultWithMaxVoltage = adc.getResultWithRangeAndMaxVolt(-1023, 1023, 5000) 60 | print("{:<33} {:<5.0f}".format("Scaled result with voltage scale:", scaledResultWithMaxVoltage)) 61 | 62 | # This function returns the voltage range ADS1115_RANGE_XXXX in Millivolt */ 63 | voltRange = adc.getVoltageRange_mV() 64 | print("{:<33}".format("Voltage Range of ADS1115 [mV]:"), voltRange) 65 | print("---------------------------------------") 66 | sleep(2) 67 | -------------------------------------------------------------------------------- /Examples/ads1115_single_shot_conv_ready_controlled.py: -------------------------------------------------------------------------------- 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 (currently only for the Arduino version): 8 | # https://wolles-elektronikkiste.de/ads1115 (German) 9 | # https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 10 | # 11 | ##################################################################################### 12 | 13 | from machine import I2C 14 | from time import sleep 15 | from ADS1115 import * 16 | 17 | ADS1115_ADDRESS = 0x48 18 | 19 | i2c = I2C(0) 20 | adc = ADS1115(ADS1115_ADDRESS, i2c=i2c) 21 | 22 | # Set the voltage range of the ADC to adjust the gain: 23 | # Please note that you must not apply more than VDD + 0.3V to the input pins! 24 | # ADS1115_RANGE_6144 -> +/- 6144 mV 25 | # ADS1115_RANGE_4096 -> +/- 4096 mV 26 | # ADS1115_RANGE_2048 -> +/- 2048 mV (default) 27 | # ADS1115_RANGE_1024 -> +/- 1024 mV 28 | # ADS1115_RANGE_0512 -> +/- 512 mV 29 | # ADS1115_RANGE_0256 -> +/- 256 mV 30 | adc.setVoltageRange_mV(ADS1115_RANGE_6144) 31 | 32 | # Set the inputs to be compared: 33 | # ADS1115_COMP_0_1 -> compares 0 with 1 (default) 34 | # ADS1115_COMP_0_3 -> compares 0 with 3 35 | # ADS1115_COMP_1_3 -> compares 1 with 3 36 | # ADS1115_COMP_2_3 -> compares 2 with 3 37 | # ADS1115_COMP_0_GND -> compares 0 with GND 38 | # ADS1115_COMP_1_GND -> compares 1 with GND 39 | # ADS1115_COMP_2_GND -> compares 2 with GND 40 | # ADS1115_COMP_3_GND -> compares 3 with GND 41 | adc.setCompareChannels(ADS1115_COMP_0_GND) 42 | 43 | # Set number of conversions after which the alert pin will assert 44 | # - or you can disable the alert: 45 | # ADS1115_ASSERT_AFTER_1 -> after 1 conversion 46 | # ADS1115_ASSERT_AFTER_2 -> after 2 conversions 47 | # ADS1115_ASSERT_AFTER_4 -> after 4 conversions 48 | # ADS1115_DISABLE_ALERT -> disable comparator / alert pin (default) 49 | # adc.setAlertPinMode(ADS1115_ASSERT_AFTER_1) 50 | 51 | # Set the conversion rate in SPS (samples per second) 52 | # Options should be self-explaining: 53 | # ADS1115_8_SPS 54 | # ADS1115_16_SPS 55 | # ADS1115_32_SPS 56 | # ADS1115_64_SPS 57 | # ADS1115_128_SPS (default) 58 | # ADS1115_250_SPS 59 | # ADS1115_475_SPS 60 | # ADS1115_860_SPS 61 | adc.setConvRate(ADS1115_8_SPS) 62 | 63 | # Set continuous or single shot mode: 64 | # ADS1115_CONTINUOUS -> continuous mode 65 | # ADS1115_SINGLE -> single shot mode (default) 66 | adc.setMeasureMode(ADS1115_SINGLE) 67 | 68 | # Choose maximum limit or maximum and minimum alert limit (window) in volts - alert pin will 69 | # assert when measured values are beyond the maximum limit or outside the window 70 | # Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 71 | # In max limit mode the minimum value is the limit where the alert pin assertion will be 72 | # be cleared (if not latched). 73 | # ADS1115_MAX_LIMIT 74 | # ADS1115_WINDOW 75 | # adc.setAlertModeAndLimit_V(ADS1115_MAX_LIMIT, 3.0, 1.5) 76 | 77 | # Enable or disable latch. If latch is enabled the alert pin will assert until the 78 | # conversion register is read (getResult functions). If disabled the alert pin assertion 79 | # will be cleared with next value within limits. 80 | # ADS1115_LATCH_DISABLED (default) 81 | # ADS1115_LATCH_ENABLED 82 | # adc.setAlertLatch(ADS1115_LATCH_ENABLED) 83 | 84 | # Sets the alert pin polarity if active: 85 | # ADS1115_ACT_LOW -> active low (default) 86 | # ADS1115_ACT_HIGH -> active high 87 | # adc.setAlertPol(ADS1115_ACT_LOW) 88 | 89 | # With this function the alert pin will assert, when a conversion is ready. 90 | # In order to deactivate, use the setAlertLimit_V function 91 | # adc.setAlertPinToConversionReady() 92 | 93 | print("ADS1115 Example Sketch - Single Shot, Conversion Ready (isBusy) controlled") 94 | print(" ") 95 | 96 | while True: 97 | for i in range(32): # counter is 32, conversion rate is 8 SPS --> 4s 98 | adc.startSingleMeasurement(); 99 | while adc.isBusy(): 100 | pass 101 | 102 | voltage = adc.getResult_V() # alternative: getResult_mV for Millivolt 103 | print("Channel 0 vs GND [V]: {:<4.2f}".format(voltage)) 104 | print("-------------------------------") 105 | -------------------------------------------------------------------------------- /Examples/ads1015_continuous.py: -------------------------------------------------------------------------------- 1 | ##################################################################################### 2 | # Example program for the ADS1015_mPy module 3 | # 4 | # This program shows how to use the ADS1015 in continuous mode. 5 | # 6 | # Further information can be found on (currently only for the Arduino version): 7 | # https://wolles-elektronikkiste.de/ADS1115 (German) 8 | # https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 9 | # 10 | ##################################################################################### 11 | 12 | from machine import I2C, Pin 13 | from time import sleep 14 | from ADS1115 import * 15 | 16 | ADS1015_ADDRESS = 0x48 17 | 18 | i2c = I2C(0) 19 | adc = ADS1015(ADS1015_ADDRESS, i2c=i2c) 20 | 21 | # ADS1015_RANGE_6144 -> +/- 6144 mV 22 | # ADS1015_RANGE_4096 -> +/- 4096 mV 23 | # ADS1015_RANGE_2048 -> +/- 2048 mV (default) 24 | # ADS1015_RANGE_1024 -> +/- 1024 mV 25 | # ADS1015_RANGE_0512 -> +/- 512 mV 26 | # ADS1015_RANGE_0256 -> +/- 256 mV 27 | adc.setVoltageRange_mV(ADS1015_RANGE_6144) 28 | 29 | # ADS1015_COMP_0_1 -> compares 0 with 1 (default) 30 | # ADS1015_COMP_0_3 -> compares 0 with 3 31 | # ADS1015_COMP_1_3 -> compares 1 with 3 32 | # ADS1015_COMP_2_3 -> compares 2 with 3 33 | # ADS1015_COMP_0_GND -> compares 0 with GND 34 | # ADS1015_COMP_1_GND -> compares 1 with GND 35 | # ADS1015_COMP_2_GND -> compares 2 with GND 36 | # ADS1015_COMP_3_GND -> compares 3 with GND 37 | #adc.setCompareChannels(ADS1015_COMP_0_GND) 38 | 39 | # ADS1015_ASSERT_AFTER_1 -> after 1 conversion 40 | # ADS1015_ASSERT_AFTER_2 -> after 2 conversions 41 | # ADS1015_ASSERT_AFTER_4 -> after 4 conversions 42 | # ADS1015_DISABLE_ALERT -> disable comparator / alert pin (default) 43 | #adc.setAlertPinMode(ADS1015_ASSERT_AFTER_1) 44 | 45 | # ADS1015_128_SPS 46 | # ADS1015_250_SPS 47 | # ADS1015_490_SPS 48 | # ADS1015_920_SPS 49 | # ADS1015_1600_SPS (default) 50 | # ADS1015_2400_SPS 51 | # ADS1015_3300_SPS 52 | adc.setConvRate(ADS1015_1600_SPS) 53 | 54 | # ADS1015_CONTINUOUS -> continuous mode 55 | # ADS1015_SINGLE -> single shot mode (default) 56 | adc.setMeasureMode(ADS1015_CONTINUOUS) 57 | 58 | # Choose maximum limit or maximum and minimum alert limit (window) in Volt - alert pin will 59 | # assert when measured values are beyond the maximum limit or outside the window 60 | # Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 61 | # In max limit mode the minimum value is the limit where the alert pin assertion will be 62 | # cleared (if not latched) 63 | # 64 | # ADS1015_MAX_LIMIT 65 | # ADS1015_WINDOW 66 | #adc.setAlertModeAndLimit_V(ADS1015_MAX_LIMIT, 3.0, 1.5) 67 | 68 | # Enable or disable latch. If latch is enabled the alert pin will assert until the 69 | # conversion register is read (getResult functions). If disabled the alert pin assertion will be 70 | # cleared with next value within limits. 71 | # 72 | # ADS1015_LATCH_DISABLED (default) 73 | # ADS1015_LATCH_ENABLED 74 | #adc.setAlertLatch(ADS1015_LATCH_ENABLED) 75 | 76 | # Sets the alert pin polarity if active: 77 | # 78 | # ADS1015_ACT_LOW -> active low (default) 79 | # ADS1015_ACT_HIGH -> active high 80 | #adc.setAlertPol(ADS1015_ACT_LOW) 81 | 82 | # With this function the alert pin will assert, when a conversion is ready. 83 | # In order to deactivate, use the setAlertLimit_V function 84 | #adc.setAlertPinToConversionReady() 85 | 86 | print("ADS1015 Example Sketch - Continuous Mode") 87 | print("All values in volts") 88 | print("") 89 | 90 | # If you change the compare channels you can immediately read values from the conversion 91 | # register, although they might belong to the former channel if no precautions are taken. 92 | # It takes about the time needed for two conversions to get the correct data. In single 93 | # shot mode you can use the isBusy() function to wait for data from the new channel. This 94 | # does not work in continuous mode. 95 | # To solve this issue the library adds a delay after change of channels if you are in contunuous 96 | # mode. The length of the delay is adjusted to the conversion rate. But be aware that the output 97 | # rate will be much lower that the conversion rate if you change channels frequently. 98 | 99 | def readChannel(channel): 100 | adc.setCompareChannels(channel) 101 | voltage = adc.getResult_V() 102 | return voltage 103 | 104 | while True: 105 | voltage0 = readChannel(ADS1015_COMP_0_GND) 106 | voltage1 = readChannel(ADS1015_COMP_1_GND) 107 | voltage2 = readChannel(ADS1015_COMP_2_GND) 108 | voltage3 = readChannel(ADS1015_COMP_3_GND) 109 | print("0: {:<8.2f} 1: {:<8.2f} 2: {:<8.2f} 3: {:<8.2f}".format(voltage0, voltage1, voltage2, voltage3)) 110 | sleep(1) 111 | -------------------------------------------------------------------------------- /Examples/ads1115_continuous.py: -------------------------------------------------------------------------------- 1 | ##################################################################################### 2 | # Example program for the ADS1115_mPy module 3 | # 4 | # This program shows how to use the ADS1115 in continuous mode. 5 | # 6 | # Further information can be found on (currently only for the Arduino version): 7 | # https://wolles-elektronikkiste.de/ads1115 (German) 8 | # https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 9 | # 10 | ##################################################################################### 11 | 12 | from machine import I2C 13 | from time import sleep 14 | from ADS1115 import * 15 | 16 | ADS1115_ADDRESS = 0x48 17 | 18 | i2c = I2C(0) 19 | adc = ADS1115(ADS1115_ADDRESS, i2c=i2c) 20 | 21 | # ADS1115_RANGE_6144 -> +/- 6144 mV 22 | # ADS1115_RANGE_4096 -> +/- 4096 mV 23 | # ADS1115_RANGE_2048 -> +/- 2048 mV (default) 24 | # ADS1115_RANGE_1024 -> +/- 1024 mV 25 | # ADS1115_RANGE_0512 -> +/- 512 mV 26 | # ADS1115_RANGE_0256 -> +/- 256 mV 27 | adc.setVoltageRange_mV(ADS1115_RANGE_6144) 28 | 29 | # ADS1115_COMP_0_1 -> compares 0 with 1 (default) 30 | # ADS1115_COMP_0_3 -> compares 0 with 3 31 | # ADS1115_COMP_1_3 -> compares 1 with 3 32 | # ADS1115_COMP_2_3 -> compares 2 with 3 33 | # ADS1115_COMP_0_GND -> compares 0 with GND 34 | # ADS1115_COMP_1_GND -> compares 1 with GND 35 | # ADS1115_COMP_2_GND -> compares 2 with GND 36 | # ADS1115_COMP_3_GND -> compares 3 with GND 37 | #adc.setCompareChannels(ADS1115_COMP_0_GND) 38 | 39 | # ADS1115_ASSERT_AFTER_1 -> after 1 conversion 40 | # ADS1115_ASSERT_AFTER_2 -> after 2 conversions 41 | # ADS1115_ASSERT_AFTER_4 -> after 4 conversions 42 | # ADS1115_DISABLE_ALERT -> disable comparator / alert pin (default) 43 | #adc.setAlertPinMode(ADS1115_ASSERT_AFTER_1) 44 | 45 | # ADS1115_8_SPS 46 | # ADS1115_16_SPS 47 | # ADS1115_32_SPS 48 | # ADS1115_64_SPS 49 | # ADS1115_128_SPS (default) 50 | # ADS1115_250_SPS 51 | # ADS1115_475_SPS 52 | # ADS1115_860_SPS 53 | adc.setConvRate(ADS1115_128_SPS) 54 | 55 | # ADS1115_CONTINUOUS -> continuous mode 56 | # ADS1115_SINGLE -> single shot mode (default) 57 | adc.setMeasureMode(ADS1115_CONTINUOUS) 58 | 59 | # Choose maximum limit or maximum and minimum alert limit (window) in Volt - alert pin will 60 | # assert when measured values are beyond the maximum limit or outside the window 61 | # Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 62 | # In max limit mode the minimum value is the limit where the alert pin assertion will be 63 | # cleared (if not latched) 64 | # 65 | # ADS1115_MAX_LIMIT 66 | # ADS1115_WINDOW 67 | #adc.setAlertModeAndLimit_V(ADS1115_MAX_LIMIT, 3.0, 1.5) 68 | 69 | # Enable or disable latch. If latch is enabled the alert pin will assert until the 70 | # conversion register is read (getResult functions). If disabled the alert pin assertion will be 71 | # cleared with next value within limits. 72 | # 73 | # ADS1115_LATCH_DISABLED (default) 74 | # ADS1115_LATCH_ENABLED 75 | #adc.setAlertLatch(ADS1115_LATCH_ENABLED) 76 | 77 | # Sets the alert pin polarity if active: 78 | # 79 | # ADS1115_ACT_LOW -> active low (default) 80 | # ADS1115_ACT_HIGH -> active high 81 | #adc.setAlertPol(ADS1115_ACT_LOW) 82 | 83 | # With this function the alert pin will assert, when a conversion is ready. 84 | # In order to deactivate, use the setAlertLimit_V function 85 | #adc.setAlertPinToConversionReady() 86 | 87 | print("ADS1115 Example Sketch - Continuous Mode") 88 | print("All values in volts") 89 | print("") 90 | 91 | # If you change the compare channels you can immediately read values from the conversion 92 | # register, although they might belong to the former channel if no precautions are taken. 93 | # It takes about the time needed for two conversions to get the correct data. In single 94 | # shot mode you can use the isBusy() function to wait for data from the new channel. This 95 | # does not work in continuous mode. 96 | # To solve this issue the library adds a delay after change of channels if you are in contunuous 97 | # mode. The length of the delay is adjusted to the conversion rate. But be aware that the output 98 | # rate will be much lower that the conversion rate if you change channels frequently. 99 | 100 | def readChannel(channel): 101 | adc.setCompareChannels(channel) 102 | voltage = adc.getResult_V() 103 | return voltage 104 | 105 | while True: 106 | voltage0 = readChannel(ADS1115_COMP_0_GND) 107 | voltage1 = readChannel(ADS1115_COMP_1_GND) 108 | voltage2 = readChannel(ADS1115_COMP_2_GND) 109 | voltage3 = readChannel(ADS1115_COMP_3_GND) 110 | print("0: {:<8.2f} 1: {:<8.2f} 2: {:<8.2f} 3: {:<8.2f}".format(voltage0, voltage1, voltage2, voltage3)) 111 | sleep(1) 112 | -------------------------------------------------------------------------------- /Examples/ads1115_alert_window_mode.py: -------------------------------------------------------------------------------- 1 | ##################################################################################### 2 | # Example program for the ADS1115_mPy module 3 | 4 | # This program 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 (currently only for the Arduino version): 9 | # https://wolles-elektronikkiste.de/ads1115 (German) 10 | # https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 11 | # 12 | ##################################################################################### 13 | 14 | from machine import I2C, Pin 15 | from time import sleep 16 | from ADS1115 import * 17 | 18 | ADS1115_ADDRESS = 0x48 19 | 20 | i2c = I2C(0) 21 | adc = ADS1115(ADS1115_ADDRESS, i2c=i2c) 22 | 23 | alertPin = Pin(17, Pin.IN, Pin.PULL_DOWN) 24 | led = Pin(16, Pin.OUT) 25 | alert = False 26 | 27 | def alert_handler(alertPin): 28 | global alert 29 | alert = True 30 | 31 | alertPin.irq(trigger=Pin.IRQ_RISING, handler=alert_handler) 32 | 33 | # Set the voltage range of the ADC to adjust the gain: 34 | # Please note that you must not apply more than VDD + 0.3V to the input pins! 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 | adc.setVoltageRange_mV(ADS1115_RANGE_6144) 42 | 43 | # Set the inputs to be compared: 44 | # ADS1115_COMP_0_1 -> compares 0 with 1 (default) 45 | # ADS1115_COMP_0_3 -> compares 0 with 3 46 | # ADS1115_COMP_1_3 -> compares 1 with 3 47 | # ADS1115_COMP_2_3 -> compares 2 with 3 48 | # ADS1115_COMP_0_GND -> compares 0 with GND 49 | # ADS1115_COMP_1_GND -> compares 1 with GND 50 | # ADS1115_COMP_2_GND -> compares 2 with GND 51 | # ADS1115_COMP_3_GND -> compares 3 with GND 52 | adc.setCompareChannels(ADS1115_COMP_0_GND) 53 | 54 | # Set number of conversions after which the alert pin will assert 55 | # - or you can disable the alert: 56 | # ADS1115_ASSERT_AFTER_1 -> after 1 conversion 57 | # ADS1115_ASSERT_AFTER_2 -> after 2 conversions 58 | # ADS1115_ASSERT_AFTER_4 -> after 4 conversions 59 | # ADS1115_DISABLE_ALERT -> disable comparator / alert pin (default) 60 | adc.setAlertPinMode(ADS1115_ASSERT_AFTER_1) 61 | 62 | # Set the conversion rate in SPS (samples per second) 63 | # Options should be self-explaining: 64 | # ADS1115_8_SPS 65 | # ADS1115_16_SPS 66 | # ADS1115_32_SPS 67 | # ADS1115_64_SPS 68 | # ADS1115_128_SPS (default) 69 | # ADS1115_250_SPS 70 | # ADS1115_475_SPS 71 | # ADS1115_860_SPS 72 | #adc.setConvRate(ADS1115_8_SPS) 73 | 74 | # Set continuous or single shot mode: 75 | # ADS1115_CONTINUOUS -> continuous mode 76 | # ADS1115_SINGLE -> single shot mode (default) 77 | adc.setMeasureMode(ADS1115_CONTINUOUS) 78 | 79 | # Choose maximum limit or maximum and minimum alert limit (window) in volts - alert pin will 80 | # assert when measured values are beyond the maximum limit or outside the window 81 | # Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 82 | # In max limit mode the minimum value is the limit where the alert pin assertion will be 83 | # be cleared (if not latched). 84 | # ADS1115_MAX_LIMIT 85 | # ADS1115_WINDOW 86 | adc.setAlertModeAndLimit_V(ADS1115_WINDOW, 3.0, 1.5) 87 | 88 | # Enable or disable latch. If latch is enabled the alert pin will assert until the 89 | # conversion register is read (getResult functions). If disabled the alert pin assertion 90 | # will be cleared with next value within limits. 91 | # ADS1115_LATCH_DISABLED (default) 92 | # ADS1115_LATCH_ENABLED 93 | # adc.setAlertLatch(ADS1115_LATCH_ENABLED) 94 | 95 | # Sets the alert pin polarity if active: 96 | # ADS1115_ACT_LOW -> active low (default) 97 | # ADS1115_ACT_HIGH -> active high 98 | adc.setAlertPol(ADS1115_ACT_HIGH) 99 | 100 | # With this function the alert pin will assert, when a conversion is ready. 101 | # In order to deactivate, use the setAlertLimit_V function 102 | #adc.setAlertPinToConversionReady() 103 | 104 | print("ADS1115 Example Sketch - uses the Alert Pin / Window Mode") 105 | print(" ") 106 | print("Waiting for Value out of Limit") 107 | 108 | while True: 109 | if alert: 110 | voltage = adc.getResult_V() 111 | print("Voltage [V]: {:<4.2f}".format(voltage)) 112 | led.value(1) 113 | sleep(1) 114 | led.value(0) 115 | alert = False -------------------------------------------------------------------------------- /Examples/ads1115_single_shot.py: -------------------------------------------------------------------------------- 1 | ##################################################################################### 2 | # Example program for the ADS1115_mPy module 3 | # 4 | # This program shows how to use the ADS1115 in single shot mode. 5 | # 6 | # Further information can be found on (currently only for the Arduino version): 7 | # https://wolles-elektronikkiste.de/ads1115 (German) 8 | # https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 9 | # 10 | ##################################################################################### 11 | 12 | from machine import I2C 13 | from time import sleep 14 | from ADS1115 import * 15 | 16 | ADS1115_ADDRESS = 0x48 17 | 18 | i2c = I2C(0) 19 | adc = ADS1115(ADS1115_ADDRESS, i2c=i2c) 20 | 21 | # Set the voltage range of the ADC to adjust the gain: 22 | # Please note that you must not apply more than VDD + 0.3V to the input pins! 23 | # ADS1115_RANGE_6144 -> +/- 6144 mV 24 | # ADS1115_RANGE_4096 -> +/- 4096 mV 25 | # ADS1115_RANGE_2048 -> +/- 2048 mV (default) 26 | # ADS1115_RANGE_1024 -> +/- 1024 mV 27 | # ADS1115_RANGE_0512 -> +/- 512 mV 28 | # ADS1115_RANGE_0256 -> +/- 256 mV 29 | adc.setVoltageRange_mV(ADS1115_RANGE_6144) 30 | 31 | # Set the inputs to be compared: 32 | # ADS1115_COMP_0_1 -> compares 0 with 1 (default) 33 | # ADS1115_COMP_0_3 -> compares 0 with 3 34 | # ADS1115_COMP_1_3 -> compares 1 with 3 35 | # ADS1115_COMP_2_3 -> compares 2 with 3 36 | # ADS1115_COMP_0_GND -> compares 0 with GND 37 | # ADS1115_COMP_1_GND -> compares 1 with GND 38 | # ADS1115_COMP_2_GND -> compares 2 with GND 39 | # ADS1115_COMP_3_GND -> compares 3 with GND 40 | adc.setCompareChannels(ADS1115_COMP_0_GND) 41 | 42 | # Set number of conversions after which the alert pin will assert 43 | # - or you can disable the alert: 44 | # ADS1115_ASSERT_AFTER_1 -> after 1 conversion 45 | # ADS1115_ASSERT_AFTER_2 -> after 2 conversions 46 | # ADS1115_ASSERT_AFTER_4 -> after 4 conversions 47 | # ADS1115_DISABLE_ALERT -> disable comparator / alert pin (default) 48 | # adc.setAlertPinMode(ADS1115_ASSERT_AFTER_1); //uncomment if you want to change the default 49 | 50 | # Set the conversion rate in SPS (samples per second) 51 | # Options should be self-explaining: 52 | # ADS1115_8_SPS 53 | # ADS1115_16_SPS 54 | # ADS1115_32_SPS 55 | # ADS1115_64_SPS 56 | # ADS1115_128_SPS (default) 57 | # ADS1115_250_SPS 58 | # ADS1115_475_SPS 59 | # ADS1115_860_SPS 60 | # adc.setConvRate(ADS1115_128_SPS) # uncomment if you want to change the default 61 | 62 | # Set continuous or single shot mode: 63 | # ADS1115_CONTINUOUS -> continuous mode 64 | # ADS1115_SINGLE -> single shot mode (default) 65 | adc.setMeasureMode(ADS1115_SINGLE) 66 | 67 | # Choose maximum limit or maximum and minimum alert limit (window) in volts - alert pin will 68 | # assert when measured values are beyond the maximum limit or outside the window 69 | # Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 70 | # In max limit mode the minimum value is the limit where the alert pin assertion will be 71 | # be cleared (if not latched). 72 | # ADS1115_MAX_LIMIT 73 | # ADS1115_WINDOW 74 | # adc.setAlertModeAndLimit_V(ADS1115_MAX_LIMIT, 3.0, 1.5) 75 | 76 | # Enable or disable latch. If latch is enabled the alert pin will assert until the 77 | # conversion register is read (getResult functions). If disabled the alert pin assertion 78 | # will be cleared with next value within limits. 79 | # ADS1115_LATCH_DISABLED (default) 80 | # ADS1115_LATCH_ENABLED 81 | # adc.setAlertLatch(ADS1115_LATCH_ENABLED) 82 | 83 | # Sets the alert pin polarity if active: 84 | # ADS1115_ACT_LOW -> active low (default) 85 | # ADS1115_ACT_HIGH -> active high 86 | # adc.setAlertPol(ADS1115_ACT_LOW) 87 | 88 | # With this function the alert pin will assert, when a conversion is ready. 89 | # In order to deactivate, use the setAlertLimit_V function 90 | # adc.setAlertPinToConversionReady() 91 | 92 | def readChannel(channel): 93 | adc.setCompareChannels(channel) 94 | adc.startSingleMeasurement() 95 | while adc.isBusy(): 96 | pass 97 | voltage = adc.getResult_V() 98 | return voltage 99 | 100 | print("ADS1115 Example Sketch - Single Shot Mode") 101 | print("Channel / Voltage [V]: ") 102 | print(" ") 103 | 104 | while True: 105 | voltage = readChannel(ADS1115_COMP_0_GND) 106 | print("Channel 0: {:<4.2f}".format(voltage)) 107 | voltage = readChannel(ADS1115_COMP_1_GND) 108 | print("Channel 1: {:<4.2f}".format(voltage)) 109 | voltage = readChannel(ADS1115_COMP_2_GND) 110 | print("Channel 2: {:<4.2f}".format(voltage)) 111 | voltage = readChannel(ADS1115_COMP_3_GND) 112 | print("Channel 3: {:<4.2f}".format(voltage)) 113 | print("---------------") 114 | sleep(1) 115 | -------------------------------------------------------------------------------- /Examples/ads1115_alert_window_mode_with_latch.py: -------------------------------------------------------------------------------- 1 | ##################################################################################### 2 | # Example program for the ADS1115_mPy module 3 | # 4 | # This program shows how you can use the alert pin with the latch function. The 5 | # only difference to ads1115_alert_window_mode.py is that latch is enabled (line 95) 6 | # and that the alert needs to be cleared (line 118). 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 (currently only for the Arduino version): 11 | # https://wolles-elektronikkiste.de/ads1115 (German) 12 | # https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 13 | # 14 | ##################################################################################### 15 | 16 | from machine import I2C, Pin 17 | from time import sleep 18 | from ADS1115 import * 19 | 20 | ADS1115_ADDRESS = 0x48 21 | 22 | i2c = I2C(0) 23 | adc = ADS1115(ADS1115_ADDRESS, i2c=i2c) 24 | 25 | alertPin = Pin(17, Pin.IN, Pin.PULL_DOWN) 26 | led = Pin(16, Pin.OUT) 27 | alert = False 28 | 29 | def alert_handler(alertPin): 30 | global alert 31 | alert = True 32 | 33 | alertPin.irq(trigger=Pin.IRQ_RISING, handler=alert_handler) 34 | 35 | # Set the voltage range of the ADC to adjust the gain: 36 | # Please note that you must not apply more than VDD + 0.3V to the input pins! 37 | # ADS1115_RANGE_6144 -> +/- 6144 mV 38 | # ADS1115_RANGE_4096 -> +/- 4096 mV 39 | # ADS1115_RANGE_2048 -> +/- 2048 mV (default) 40 | # ADS1115_RANGE_1024 -> +/- 1024 mV 41 | # ADS1115_RANGE_0512 -> +/- 512 mV 42 | # ADS1115_RANGE_0256 -> +/- 256 mV 43 | adc.setVoltageRange_mV(ADS1115_RANGE_6144) 44 | 45 | # Set the inputs to be compared: 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 | adc.setCompareChannels(ADS1115_COMP_0_GND) 55 | 56 | # Set number of conversions after which the alert pin will assert 57 | # - or you can disable the alert: 58 | # ADS1115_ASSERT_AFTER_1 -> after 1 conversion 59 | # ADS1115_ASSERT_AFTER_2 -> after 2 conversions 60 | # ADS1115_ASSERT_AFTER_4 -> after 4 conversions 61 | # ADS1115_DISABLE_ALERT -> disable comparator / alert pin (default) 62 | adc.setAlertPinMode(ADS1115_ASSERT_AFTER_1) 63 | 64 | # Set the conversion rate in SPS (samples per second) 65 | # Options should be self-explaining: 66 | # ADS1115_8_SPS 67 | # ADS1115_16_SPS 68 | # ADS1115_32_SPS 69 | # ADS1115_64_SPS 70 | # ADS1115_128_SPS (default) 71 | # ADS1115_250_SPS 72 | # ADS1115_475_SPS 73 | # ADS1115_860_SPS 74 | #adc.setConvRate(ADS1115_8_SPS) 75 | 76 | # Set continuous or single shot mode: 77 | # ADS1115_CONTINUOUS -> continuous mode 78 | # ADS1115_SINGLE -> single shot mode (default) 79 | adc.setMeasureMode(ADS1115_CONTINUOUS) 80 | 81 | # Choose maximum limit or maximum and minimum alert limit (window) in volts - alert pin will 82 | # assert when measured values are beyond the maximum limit or outside the window 83 | # Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 84 | # In max limit mode the minimum value is the limit where the alert pin assertion will be 85 | # be cleared (if not latched). 86 | # ADS1115_MAX_LIMIT 87 | # ADS1115_WINDOW 88 | adc.setAlertModeAndLimit_V(ADS1115_WINDOW, 3.0, 1.5) 89 | 90 | # Enable or disable latch. If latch is enabled the alert pin will assert until the 91 | # conversion register is read (getResult functions). If disabled the alert pin assertion 92 | # will be cleared with next value within limits. 93 | # ADS1115_LATCH_DISABLED (default) 94 | # ADS1115_LATCH_ENABLED 95 | adc.setAlertLatch(ADS1115_LATCH_ENABLED) 96 | 97 | # Sets the alert pin polarity if active: 98 | # ADS1115_ACT_LOW -> active low (default) 99 | # ADS1115_ACT_HIGH -> active high 100 | adc.setAlertPol(ADS1115_ACT_HIGH) 101 | 102 | # With this function the alert pin will assert, when a conversion is ready. 103 | # In order to deactivate, use the setAlertLimit_V function 104 | #adc.setAlertPinToConversionReady() 105 | 106 | print("ADS1115 Example Sketch - uses the Alert Pin / Window Mode / Latch enabled") 107 | print(" ") 108 | print("Waiting for Value out of Limit") 109 | 110 | while True: 111 | if alert: 112 | voltage = adc.getResult_V(); 113 | print("Voltage [V]: {:<4.2f}".format(voltage)) 114 | led.value(1) 115 | sleep(1) 116 | led.value(0) 117 | alert = False 118 | adc.clearAlert() # clears the interrupt (alternatively use getResult_V) -------------------------------------------------------------------------------- /Examples/ads1115_conv_ready_alert_pin_controlled.py: -------------------------------------------------------------------------------- 1 | ##################################################################################### 2 | # Example program for the ADS1115_mPy module 3 | # 4 | # This program 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 (currently only for the Arduino version): 10 | # https://wolles-elektronikkiste.de/ads1115 (German) 11 | # https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 12 | # 13 | ##################################################################################### 14 | 15 | from machine import I2C, Pin 16 | from time import sleep 17 | from ADS1115 import * 18 | 19 | ADS1115_ADDRESS = 0x48 20 | 21 | i2c = I2C(0) 22 | adc = ADS1115(ADS1115_ADDRESS, i2c=i2c) 23 | 24 | alertPin = Pin(17, Pin.IN, Pin.PULL_DOWN) 25 | alert = False 26 | 27 | def alert_handler(alertPin): 28 | global alert 29 | alert = True 30 | 31 | alertPin.irq(trigger=Pin.IRQ_RISING, handler=alert_handler) 32 | 33 | # Set the voltage range of the ADC to adjust the gain: 34 | # Please note that you must not apply more than VDD + 0.3V to the input pins! 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 | adc.setVoltageRange_mV(ADS1115_RANGE_6144) 42 | 43 | # Set the inputs to be compared: 44 | # ADS1115_COMP_0_1 -> compares 0 with 1 (default) 45 | # ADS1115_COMP_0_3 -> compares 0 with 3 46 | # ADS1115_COMP_1_3 -> compares 1 with 3 47 | # ADS1115_COMP_2_3 -> compares 2 with 3 48 | # ADS1115_COMP_0_GND -> compares 0 with GND 49 | # ADS1115_COMP_1_GND -> compares 1 with GND 50 | # ADS1115_COMP_2_GND -> compares 2 with GND 51 | # ADS1115_COMP_3_GND -> compares 3 with GND 52 | adc.setCompareChannels(ADS1115_COMP_0_GND) 53 | 54 | # Set number of conversions after which the alert pin will assert 55 | # - or you can disable the alert: 56 | # ADS1115_ASSERT_AFTER_1 -> after 1 conversion 57 | # ADS1115_ASSERT_AFTER_2 -> after 2 conversions 58 | # ADS1115_ASSERT_AFTER_4 -> after 4 conversions 59 | # ADS1115_DISABLE_ALERT -> disable comparator / alert pin (default) 60 | adc.setAlertPinMode(ADS1115_ASSERT_AFTER_1) 61 | 62 | # Set the conversion rate in SPS (samples per second) 63 | # Options should be self-explaining: 64 | # ADS1115_8_SPS 65 | # ADS1115_16_SPS 66 | # ADS1115_32_SPS 67 | # ADS1115_64_SPS 68 | # ADS1115_128_SPS (default) 69 | # ADS1115_250_SPS 70 | # ADS1115_475_SPS 71 | # ADS1115_860_SPS 72 | adc.setConvRate(ADS1115_8_SPS) 73 | 74 | # Set continuous or single shot mode: 75 | # ADS1115_CONTINUOUS -> continuous mode 76 | # ADS1115_SINGLE -> single shot mode (default) 77 | adc.setMeasureMode(ADS1115_SINGLE) 78 | 79 | # Choose maximum limit or maximum and minimum alert limit (window) in volts - alert pin will 80 | # assert when measured values are beyond the maximum limit or outside the window 81 | # Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 82 | # In max limit mode the minimum value is the limit where the alert pin assertion will be 83 | # be cleared (if not latched). 84 | # ADS1115_MAX_LIMIT 85 | # ADS1115_WINDOW 86 | # adc.setAlertModeAndLimit_V(ADS1115_MAX_LIMIT, 3.0, 1.5) 87 | 88 | # Enable or disable latch. If latch is enabled the alert pin will assert until the 89 | # conversion register is read (getResult functions). If disabled the alert pin assertion 90 | # will be cleared with next value within limits. 91 | # ADS1115_LATCH_DISABLED (default) 92 | # ADS1115_LATCH_ENABLED 93 | # adc.setAlertLatch(ADS1115_LATCH_ENABLED) 94 | 95 | # Sets the alert pin polarity if active: 96 | # ADS1115_ACT_LOW -> active low (default) 97 | # ADS1115_ACT_HIGH -> active high 98 | adc.setAlertPol(ADS1115_ACT_HIGH) 99 | 100 | # With this function the alert pin will assert, when a conversion is ready. 101 | # In order to deactivate, use the setAlertLimit_V function 102 | adc.setAlertPinToConversionReady() 103 | 104 | print("ADS1115 Example Sketch - Single Shot, Conversion Ready Alert Pin controlled"); 105 | print() 106 | 107 | # In this example I measure 32 times before the result is output. This is only to slow down 108 | # the output rate. You don't have to read 32 times, of course.I want to show that the output 109 | # rate is controlled by the conversion ready alerts and not by a sleep. 110 | 111 | counter = [0] 112 | while True: 113 | if alert: 114 | counter[0] += 1 115 | alert = False 116 | if counter[0]==32: # counter is 32, conversion rate is 8 SPS --> 4s 117 | voltage = adc.getResult_V() 118 | print("Channel 0 vs GND [V]: {:<4.2f}".format(voltage)) 119 | print("-------------------------------") 120 | counter[0] = 0 121 | adc.startSingleMeasurement() 122 | -------------------------------------------------------------------------------- /Examples/ads1115_auto_range.py: -------------------------------------------------------------------------------- 1 | ##################################################################################### 2 | # Example program for the ADS1115_mPy module 3 | # 4 | # This program shows how to use the Auto Range function of the AD1115_mPy module. 5 | # 6 | # Further information can be found on (currently only for the Arduino version): 7 | # https://wolles-elektronikkiste.de/ads1115 (German) 8 | # https://wolles-elektronikkiste.de/en/ads1115-a-d-converter-with-amplifier (English) 9 | # 10 | ##################################################################################### 11 | 12 | from machine import I2C 13 | from time import sleep 14 | from ADS1115 import * 15 | 16 | ADS1115_ADDRESS = 0x48 17 | 18 | i2c = I2C(0) 19 | adc = ADS1115(ADS1115_ADDRESS, i2c=i2c) 20 | 21 | # Set the voltage range of the ADC to adjust the gain: 22 | # Please note that you must not apply more than VDD + 0.3V to the input pins! 23 | # ADS1115_RANGE_6144 -> +/- 6144 mV 24 | # ADS1115_RANGE_4096 -> +/- 4096 mV 25 | # ADS1115_RANGE_2048 -> +/- 2048 mV (default) 26 | # ADS1115_RANGE_1024 -> +/- 1024 mV 27 | # ADS1115_RANGE_0512 -> +/- 512 mV 28 | # ADS1115_RANGE_0256 -> +/- 256 mV 29 | adc.setVoltageRange_mV(ADS1115_RANGE_6144) 30 | 31 | # Set the inputs to be compared: 32 | # ADS1115_COMP_0_1 -> compares 0 with 1 (default) 33 | # ADS1115_COMP_0_3 -> compares 0 with 3 34 | # ADS1115_COMP_1_3 -> compares 1 with 3 35 | # ADS1115_COMP_2_3 -> compares 2 with 3 36 | # ADS1115_COMP_0_GND -> compares 0 with GND 37 | # ADS1115_COMP_1_GND -> compares 1 with GND 38 | # ADS1115_COMP_2_GND -> compares 2 with GND 39 | # ADS1115_COMP_3_GND -> compares 3 with GND 40 | adc.setCompareChannels(ADS1115_COMP_0_GND) 41 | 42 | # Set number of conversions after which the alert pin will assert 43 | # - or you can disable the alert: 44 | # ADS1115_ASSERT_AFTER_1 -> after 1 conversion 45 | # ADS1115_ASSERT_AFTER_2 -> after 2 conversions 46 | # ADS1115_ASSERT_AFTER_4 -> after 4 conversions 47 | # ADS1115_DISABLE_ALERT -> disable comparator / alert pin (default) 48 | # adc.setAlertPinMode(ADS1115_ASSERT_AFTER_1) 49 | 50 | # Set the conversion rate in SPS (samples per second) 51 | # Options should be self-explaining: 52 | # ADS1115_8_SPS 53 | # ADS1115_16_SPS 54 | # ADS1115_32_SPS 55 | # ADS1115_64_SPS 56 | # ADS1115_128_SPS (default) 57 | # ADS1115_250_SPS 58 | # ADS1115_475_SPS 59 | # ADS1115_860_SPS 60 | adc.setConvRate(ADS1115_128_SPS) 61 | 62 | # Set continuous or single shot mode: 63 | # ADS1115_CONTINUOUS -> continuous mode 64 | # ADS1115_SINGLE -> single shot mode (default) 65 | adc.setMeasureMode(ADS1115_CONTINUOUS) 66 | 67 | # Choose maximum limit or maximum and minimum alert limit (window) in volts - alert pin will 68 | # assert when measured values are beyond the maximum limit or outside the window 69 | # Upper limit first: setAlertLimit_V(MODE, maximum, minimum) 70 | # In max limit mode the minimum value is the limit where the alert pin assertion will be 71 | # be cleared (if not latched). 72 | # ADS1115_MAX_LIMIT 73 | # ADS1115_WINDOW 74 | # adc.setAlertModeAndLimit_V(ADS1115_MAX_LIMIT, 3.0, 1.5) 75 | 76 | # Enable or disable latch. If latch is enabled the alert pin will assert until the 77 | # conversion register is read (getResult functions). If disabled the alert pin assertion 78 | # will be cleared with next value within limits. 79 | # ADS1115_LATCH_DISABLED (default) 80 | # ADS1115_LATCH_ENABLED 81 | # adc.setAlertLatch(ADS1115_LATCH_ENABLED) 82 | 83 | # Sets the alert pin polarity if active: 84 | # ADS1115_ACT_LOW -> active low (default) 85 | # ADS1115_ACT_HIGH -> active high 86 | # adc.setAlertPol(ADS1115_ACT_LOW) 87 | 88 | # With this function the alert pin will assert, when a conversion is ready. 89 | # In order to deactivate, use the setAlertLimit_V function 90 | # adc.setAlertPinToConversionReady() 91 | 92 | # Enable or disable permanent automatic range selection mode. If enabled, the range will 93 | # change if the measured values are outside of 30-80% of the maximum value of the current 94 | # range. 95 | # !!! Use EITHER this function once OR setAutoRange() whenever needed (see below) !!! 96 | adc.setPermanentAutoRangeMode(True) 97 | 98 | def readChannel(channel): 99 | adc.setCompareChannels(channel) 100 | 101 | # setAutoRange() switches to the highest range (+/- 6144 mV), measures the current 102 | # voltage and then switches to the lowest range where the current value is still 103 | # below 80% of the maximum value of the range. The function is only suitable if you 104 | # expect stable or slowly changing voltages. setAutoRange needs roughly the time you 105 | # would need for three conversions. 106 | # If the ADS115 is in single shot mode, setAutoRange() will switch into continuous 107 | # mode to measure a value and switch back again. 108 | # !!! Use EITHER this function whenever needed OR setPermanentAutoRangeMode(true) once !!! 109 | #adc.setAutoRange() 110 | 111 | voltage = adc.getResult_V() 112 | voltRange = adc.getVoltageRange_mV() # this is just to show that the range is changing with changing voltages 113 | print("Range: +/-", voltRange, "[mV], Voltage [V]: {:<4.2f}".format(voltage)) 114 | 115 | 116 | print("ADS1115 Example Sketch - Continuous Mode with Auto Range") 117 | print("") 118 | 119 | while True: 120 | print("Channel 0 -", end=" ") 121 | readChannel(ADS1115_COMP_0_GND) 122 | 123 | print("Channel 1 -", end=" ") 124 | readChannel(ADS1115_COMP_1_GND) 125 | 126 | print("Channel 2 -", end=" ") 127 | readChannel(ADS1115_COMP_2_GND) 128 | 129 | print("Channel 3 -", end=" ") 130 | readChannel(ADS1115_COMP_3_GND) 131 | 132 | print("-------------------------------") 133 | sleep(2) 134 | 135 | 136 | 137 | 138 | -------------------------------------------------------------------------------- /ADS1115.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import struct 3 | from time import sleep_ms 4 | __ADS1115_CONV_REG = 0x00 #Conversion Register 5 | __ADS1115_CONFIG_REG = 0x01 #Configuration Register 6 | __ADS1115_LO_THRESH_REG = 0x02 #Low Threshold Register 7 | __ADS1115_HI_THRESH_REG = 0x03 #High Threshold Register 8 | 9 | __ADS1115_DEFAULT_ADDR = 0x48 10 | __ADS1115_REG_RESET_VAL = 0x8583 11 | __ADS1115_REG_FACTOR = 0x7FFF 12 | 13 | __ADS1115_BUSY = 0x0000 14 | __ADS1115_START_ISREADY = 0x8000 15 | 16 | __ADS1115_COMP_INC = 0x1000 17 | 18 | ADS1115_ASSERT_AFTER_1 = 0x0000 19 | ADS1115_ASSERT_AFTER_2 = 0x0001 20 | ADS1115_ASSERT_AFTER_4 = 0x0002 21 | ADS1115_DISABLE_ALERT = 0x0003 22 | ADS1015_ASSERT_AFTER_1 = ADS1115_ASSERT_AFTER_1 23 | ADS1015_ASSERT_AFTER_2 = ADS1115_ASSERT_AFTER_2 24 | ADS1015_ASSERT_AFTER_4 = ADS1115_ASSERT_AFTER_4 25 | ADS1015_DISABLE_ALERT = ADS1115_DISABLE_ALERT 26 | 27 | 28 | ADS1115_LATCH_DISABLED = 0x0000 29 | ADS1115_LATCH_ENABLED = 0x0004 30 | ADS1015_LATCH_DISABLED = 0x0000 31 | ADS1015_LATCH_ENABLED = 0x0004 32 | 33 | ADS1115_ACT_LOW = 0x0000 34 | ADS1115_ACT_HIGH = 0x0008 35 | ADS1015_ACT_LOW = ADS1115_ACT_LOW 36 | ADS1015_ACT_HIGH = ADS1115_ACT_HIGH 37 | 38 | ADS1115_MAX_LIMIT = 0x0000 39 | ADS1115_WINDOW = 0x0010 40 | ADS1015_MAX_LIMIT = ADS1115_MAX_LIMIT 41 | ADS1015_WINDOW = ADS1115_WINDOW 42 | 43 | ADS1115_8_SPS = 0x0000 44 | ADS1115_16_SPS = 0x0020 45 | ADS1115_32_SPS = 0x0040 46 | ADS1115_64_SPS = 0x0060 47 | ADS1115_128_SPS = 0x0080 48 | ADS1115_250_SPS = 0x00A0 49 | ADS1115_475_SPS = 0x00C0 50 | ADS1115_860_SPS = 0x00E0 51 | ADS1015_128_SPS = ADS1115_8_SPS 52 | ADS1015_250_SPS = ADS1115_16_SPS 53 | ADS1015_490_SPS = ADS1115_32_SPS 54 | ADS1015_920_SPS = ADS1115_64_SPS 55 | ADS1015_1600_SPS = ADS1115_128_SPS 56 | ADS1015_2400_SPS = ADS1115_250_SPS 57 | ADS1015_3300_SPS = ADS1115_475_SPS 58 | ADS1015_3300_SPS_2 = ADS1115_860_SPS 59 | 60 | ADS1115_RANGE_6144 = 0x0000 61 | ADS1115_RANGE_4096 = 0x0200 62 | ADS1115_RANGE_2048 = 0x0400 63 | ADS1115_RANGE_1024 = 0x0600 64 | ADS1115_RANGE_0512 = 0x0800 65 | ADS1115_RANGE_0256 = 0x0A00 66 | ADS1015_RANGE_6144 = ADS1115_RANGE_6144 67 | ADS1015_RANGE_4096 = ADS1115_RANGE_4096 68 | ADS1015_RANGE_2048 = ADS1115_RANGE_2048 69 | ADS1015_RANGE_1024 = ADS1115_RANGE_1024 70 | ADS1015_RANGE_0512 = ADS1115_RANGE_0512 71 | ADS1015_RANGE_0256 = ADS1115_RANGE_0256 72 | 73 | ADS1115_COMP_0_1 = 0x0000 74 | ADS1115_COMP_0_3 = 0x1000 75 | ADS1115_COMP_1_3 = 0x2000 76 | ADS1115_COMP_2_3 = 0x3000 77 | ADS1115_COMP_0_GND = 0x4000 78 | ADS1115_COMP_1_GND = 0x5000 79 | ADS1115_COMP_2_GND = 0x6000 80 | ADS1115_COMP_3_GND = 0x7000 81 | ADS1015_COMP_0_1 = ADS1115_COMP_0_1 82 | ADS1015_COMP_0_3 = ADS1115_COMP_0_3 83 | ADS1015_COMP_1_3 = ADS1115_COMP_1_3 84 | ADS1015_COMP_2_3 = ADS1115_COMP_2_3 85 | ADS1015_COMP_0_GND = ADS1115_COMP_0_GND 86 | ADS1015_COMP_1_GND = ADS1115_COMP_1_GND 87 | ADS1015_COMP_2_GND = ADS1115_COMP_2_GND 88 | ADS1015_COMP_3_GND = ADS1115_COMP_3_GND 89 | 90 | ADS1115_CONTINUOUS = 0x0000 91 | ADS1115_SINGLE = 0x0100 92 | ADS1015_CONTINUOUS = ADS1115_CONTINUOUS 93 | ADS1015_SINGLE = ADS1115_SINGLE 94 | 95 | class ADS1115(object): 96 | __autoRangeMode = False 97 | __voltageRange = 2048 98 | __measureMode = ADS1115_SINGLE 99 | 100 | def __init__(self, address = __ADS1115_DEFAULT_ADDR, i2c = None): 101 | self.__address = address 102 | if i2c is None: 103 | try: 104 | i2c = I2C(0) 105 | except: 106 | i2c = I2C() 107 | self.__i2c = i2c 108 | try: 109 | self.reset() 110 | except OSError: # I2C bus error: 111 | raise ValueError("Can't connect to the ADS1115. Check wiring, address, etc.") 112 | 113 | self.setVoltageRange_mV(ADS1115_RANGE_2048) 114 | self.__writeADS1115(__ADS1115_LO_THRESH_REG, 0x8000) 115 | self.__writeADS1115(__ADS1115_HI_THRESH_REG, 0x7FFF) 116 | self.__measureMode = ADS1115_SINGLE 117 | self.__autoRangeMode = False 118 | 119 | def setAlertPinMode(self, mode): 120 | currentConfReg = self.__getConfReg() 121 | currentConfReg &= ~(0x8003) 122 | currentConfReg |= mode 123 | self.__setConfReg(currentConfReg) 124 | 125 | def setAlertLatch(self, latch): 126 | currentConfReg = self.__getConfReg() 127 | currentConfReg &= ~(0x8004) 128 | currentConfReg |= latch 129 | self.__setConfReg(currentConfReg) 130 | 131 | def setAlertPol(self, polarity): 132 | currentConfReg = self.__getConfReg() 133 | currentConfReg &= ~(0x8008) 134 | currentConfReg |= polarity 135 | self.__setConfReg(currentConfReg) 136 | 137 | def setAlertModeAndLimit_V(self, mode, hiThres, loThres): 138 | currentConfReg = self.__getConfReg() 139 | currentConfReg &= ~(0x8010) 140 | currentConfReg |= mode 141 | self.__setConfReg(currentConfReg) 142 | alertLimit = self.__calcLimit(hiThres) 143 | self.__writeADS1115(__ADS1115_HI_THRESH_REG, alertLimit) 144 | alertLimit = self.__calcLimit(loThres) 145 | self.__writeADS1115(__ADS1115_LO_THRESH_REG, alertLimit) 146 | 147 | def __calcLimit(self, rawLimit): 148 | limit = int((rawLimit * __ADS1115_REG_FACTOR / self.__voltageRange) * 1000) 149 | if limit > 32767: 150 | limit -= 65536 151 | return limit 152 | 153 | def reset(self): 154 | return self.__setConfReg(__ADS1115_REG_RESET_VAL) 155 | 156 | def setVoltageRange_mV(self, newRange): 157 | currentVoltageRange = self.__voltageRange 158 | currentConfReg = self.__getConfReg() 159 | currentRange = (currentConfReg >> 9) & 7 160 | currentAlertPinMode = currentConfReg & 3 161 | 162 | self.setMeasureMode(ADS1115_SINGLE) 163 | 164 | if newRange == ADS1115_RANGE_6144: 165 | self.__voltageRange = 6144; 166 | elif newRange == ADS1115_RANGE_4096: 167 | self.__voltageRange = 4096; 168 | elif newRange == ADS1115_RANGE_2048: 169 | self.__voltageRange = 2048; 170 | elif newRange == ADS1115_RANGE_1024: 171 | self.__voltageRange = 1024; 172 | elif newRange == ADS1115_RANGE_0512: 173 | self.__voltageRange = 512; 174 | elif newRange == ADS1115_RANGE_0256: 175 | self.__voltageRange = 256; 176 | 177 | if (currentRange != newRange) and (currentAlertPinMode != ADS1115_DISABLE_ALERT): 178 | alertLimit = self.__readADS1115(__ADS1115_HI_THRESH_REG) 179 | alertLimit = alertLimit * (currentVoltageRange / self.__voltageRange) 180 | self.__writeADS1115(__ADS1115_HI_THRESH_REG, alertLimit) 181 | alertLimit = self.__readADS1115(__ADS1115_LO_THRESH_REG) 182 | alertLimit = alertLimit * (currentVoltageRange / self.__voltageRange) 183 | self.__writeADS1115(__ADS1115_LO_THRESH_REG, alertLimit) 184 | 185 | currentConfReg &= ~(0x8E00) 186 | currentConfReg |= newRange 187 | self.__setConfReg(currentConfReg) 188 | rate = self.__getConvRate() 189 | self.__delayAccToRate(rate) 190 | 191 | def setAutoRange(self): 192 | currentConfReg = self.__getConfReg() 193 | self.setVoltageRange_mV(ADS1115_RANGE_6144) 194 | 195 | if self.__measureMode == ADS1115_SINGLE: 196 | self.setMeasureMode(ADS1115_CONTINUOUS) 197 | convRate = self.__getConvRate() 198 | self.__delayAccToRate(convRate) 199 | 200 | rawResult = abs(self.__getConvReg()) 201 | optRange = ADS1115_RANGE_6144 202 | 203 | if rawResult < 1093: 204 | optRange = ADS1115_RANGE_0256 205 | elif rawResult < 2185: 206 | optRange = ADS1115_RANGE_0512 207 | elif rawResult < 4370: 208 | optRange = ADS1115_RANGE_1024 209 | elif rawResult < 8738: 210 | optRange = ADS1115_RANGE_2048 211 | elif rawResult < 17476: 212 | optRange = ADS1115_RANGE_4096 213 | 214 | self.__setConfReg(currentConfReg) 215 | self.setVoltageRange_mV(optRange) 216 | 217 | def setPermanentAutoRangeMode(self, autoMode): 218 | if autoMode: 219 | self.__autoRangeMode = True 220 | else: 221 | self.__autoRangeMode = False 222 | 223 | def setMeasureMode(self, mMode): 224 | currentConfReg = self.__getConfReg() 225 | self.__measureMode = mMode 226 | currentConfReg &= ~(0x8100) 227 | currentConfReg |= mMode 228 | self.__setConfReg(currentConfReg) 229 | 230 | def setCompareChannels(self, compChannels): 231 | currentConfReg = self.__getConfReg() 232 | currentConfReg &= ~(0xF000) 233 | currentConfReg |= compChannels 234 | self.__setConfReg(currentConfReg) 235 | 236 | if not (currentConfReg & 0x0100): # if not single shot mode 237 | convRate = self.__getConvRate() 238 | for i in range(2): 239 | self.__delayAccToRate(convRate) 240 | 241 | def setSingleChannel(self, channel): 242 | if channel >= 4: 243 | return 244 | self.setCompareChannels((ADS1115_COMP_0_GND + ADS1115_COMP_INC) * channel) 245 | 246 | def isBusy(self): 247 | currentConfReg = self.__getConfReg() 248 | return not((currentConfReg>>15) & 1) 249 | 250 | def startSingleMeasurement(self): 251 | currentConfReg = self.__getConfReg() 252 | currentConfReg |= (1 << 15) 253 | self.__setConfReg(currentConfReg) 254 | 255 | def getResult_V(self): 256 | return self.getResult_mV()/1000 257 | 258 | def getResult_mV(self): 259 | rawResult = self.getRawResult() 260 | return rawResult * self.__voltageRange / __ADS1115_REG_FACTOR 261 | 262 | def getRawResult(self): 263 | rawResult = self.__getConvReg() 264 | 265 | if self.__autoRangeMode: 266 | if (abs(rawResult) > 26214) and (self.__voltageRange != 6144): # 80% 267 | self.setAutoRange() 268 | rawResult = self.__getConvReg() 269 | elif (abs(rawResult) < 9800) and (self.__voltageRange != 256): # 30% 270 | self.setAutoRange() 271 | rawResult = self.__getConvReg() 272 | 273 | return rawResult 274 | 275 | def __getConvReg(self): 276 | rawResult = self.__readADS1115(__ADS1115_CONV_REG) 277 | if rawResult > 32767: 278 | rawResult -= 65536 279 | return rawResult 280 | 281 | def getResultWithRange(self, minLimit, maxLimit): 282 | rawResult = self.getRawResult() 283 | result = rawResult * (maxLimit - minLimit) / 65536 284 | return result 285 | 286 | def getResultWithRangeAndMaxVolt(self, minLimit, maxLimit, maxMillivolt): 287 | result = self.getResultWithRange(minLimit, maxLimit) 288 | result = result * self.__voltageRange / maxMillivolt 289 | return result 290 | 291 | def getVoltageRange_mV(self): 292 | return self.__voltageRange 293 | 294 | def setAlertPinToConversionReady(self): 295 | self.__writeADS1115(__ADS1115_LO_THRESH_REG, (0<<15)) 296 | self.__writeADS1115(__ADS1115_HI_THRESH_REG, (1<<15)) 297 | 298 | def clearAlert(self): 299 | self.__readADS1115(__ADS1115_CONV_REG) 300 | 301 | def __setConfReg(self, regVal): 302 | self.__writeADS1115(__ADS1115_CONFIG_REG, regVal) 303 | 304 | def __getConfReg(self): 305 | return self.__readADS1115(__ADS1115_CONFIG_REG) 306 | 307 | def __getConvRate(self): 308 | currentConfReg = self.__getConfReg() 309 | return (currentConfReg & 0xE0) 310 | 311 | def setConvRate(self, rate): 312 | currentConfReg = self.__getConfReg() 313 | currentConfReg &= ~(0x80E0) 314 | currentConfReg |= rate 315 | self.__setConfReg(currentConfReg) 316 | 317 | def __delayAccToRate(self, rate): 318 | if rate == ADS1115_8_SPS: 319 | sleep_ms(130) 320 | elif rate == ADS1115_16_SPS: 321 | sleep_ms(65) 322 | elif rate == ADS1115_32_SPS: 323 | sleep_ms(32) 324 | elif rate == ADS1115_64_SPS: 325 | sleep_ms(16) 326 | elif rate == ADS1115_128_SPS: 327 | sleep_ms(8) 328 | elif rate == ADS1115_250_SPS: 329 | sleep_ms(4) 330 | elif rate == ADS1115_475_SPS: 331 | sleep_ms(3) 332 | elif rate == ADS1115_860_SPS: 333 | sleep_ms(2) 334 | 335 | def __writeADS1115(self, reg, val): 336 | self.__i2c.writeto_mem(self.__address, reg, self.__toBytearray(val)) 337 | 338 | def __readADS1115(self, reg): 339 | regVal = self.__i2c.readfrom_mem(self.__address, reg, 2) 340 | return self.__bytesToInt(regVal) 341 | 342 | def __toBytearray(self, intVal): 343 | # return bytearray(intVal.to_bytes(2, 'big')) 344 | return struct.pack('>i',intVal)[2:] 345 | 346 | def __bytesToInt(self, bytesToConvert): 347 | intVal = int.from_bytes(bytesToConvert, 'big') # "big" = MSB at beginning 348 | return intVal 349 | 350 | class ADS1015(ADS1115): 351 | def __init__(self, address = __ADS1115_DEFAULT_ADDR, i2c = None): 352 | super().__init__(address, i2c) 353 | --------------------------------------------------------------------------------