ADS1115 Module¶
| Since | Origin / Contributor | Maintainer | Source |
|---|---|---|---|
| 2017-04-24 | fetchbot | fetchbot | ads1115.c |
This module provides access to the ADS1115 (16-Bit) and ADS1015 (12-Bit) analog-to-digital converters. Other chips from the same family (ADS1113, ADS1114, ADS1013 and ADS1014) are likely to work. Missing hardware features will be silently ignored.
This module supports multiple devices connected to I²C bus. The devices of different types can be mixed. The addressing of ADS family allows for maximum of 4 devices connected to the same I²C bus.
Caution
The ABSOLUTE MAXIMUM RATINGS for all analog inputs are –0.3V to VDD+0.3V referred to GND.
ads1115.ads1115()¶
Registers ADS1115 (ADS1113, ADS1114) device.
Syntax¶
ads1115.ADS1115(I2C_ID, I2C_ADDR)
Parameters¶
I2C_ID- always 0ADDRESS- I²C address of a deviceads1115.ADDR_GNDads1115.ADDR_VDDads1115.ADDR_SDAads1115.ADDR_SCL
Returns¶
Registered device object
Example¶
local id, sda, scl = 0, 6, 5
i2c.setup(id, sda, scl, i2c.SLOW)
ads1115.reset()
adc1 = ads1115.ads1115(id, ads1115.ADDR_GND)
ads1115.ads1015()¶
Registers ADS1015 (ADS1013, ADS1014) device.
Syntax¶
ads1115.ads1015(I2C_ID, I2C_ADDR)
Parameters¶
I2C_ID- always 0ADDRESS- I²C address of a deviceads1115.ADDR_GNDads1115.ADDR_VDDads1115.ADDR_SDAads1115.ADDR_SCL
Returns¶
Registered device object
Example¶
local id, sda, scl = 0, 6, 5
i2c.setup(id, sda, scl, i2c.SLOW)
ads1115.reset()
adc1 = ads1115.ads1015(id, ads1115.ADDR_VDD)
adc2 = ads1115.ads1115(id, ads1115.ADDR_SDA)
ads1115.reset()¶
Reset all devices connected to I²C interface.
Syntax¶
ads1115.reset()
Parameters¶
none
Returns¶
nil
Example¶
local id, alert_pin, sda, scl = 0, 7, 6, 5
i2c.setup(id, sda, scl, i2c.SLOW)
ads1115.reset()
ADS Device¶
ads1115.device:read()¶
Gets the result stored in the register of a previously issued conversion, e.g. in continuous mode or with a conversion ready interrupt.
Syntax¶
volt, volt_dec, raw, sign = device:read()
Parameters¶
none
Returns¶
voltvoltage in mV (see note below)volt_decvoltage decimal in uV (see note below)adcraw adc register valuesignsign of the result (see note below)
Note
If using float firmware then volt is a floating point number, volt_dec and sign are nil. On an integer firmware, the final value has to be concatenated from volt, volt_dec and sign. On integer firmware volt and volt_dec are always positive, sign can be -1, 0, 1.
Example¶
local id, alert_pin, sda, scl = 0, 7, 6, 5
i2c.setup(id, sda, scl, i2c.SLOW)
ads1115.reset()
adc1 = ads1115.ads1115(id, ads1115.ADDR_GND)
-- continuous mode
adc1:setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_0, ads1115.CONTINUOUS)
-- read adc result with read()
volt, volt_dec, adc, sign = ads1:read()
print(volt, volt_dec, adc, sign)
-- comparator
adc1:setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_0, ads1115.CONTINUOUS, ads1115.COMP_1CONV, 1000, 2000)
local function comparator(level, when)
-- read adc result with read() when threshold reached
gpio.trig(alert_pin)
volt, volt_dec, adc, sign = adc1:read()
print(volt, volt_dec, adc, sign)
end
gpio.mode(alert_pin, gpio.INT)
gpio.trig(alert_pin, "both", comparator)
-- read adc result with read()
volt, volt_dec, adc, sign = ads1115:read()
print(volt, volt_dec, adc, sing)
-- format value in int build
if sign then
-- int build
print(string.format("%s%d.%03d mV", sign >= 0 and "+" or "-", volt, volt_dec))
else
-- float build
-- just use V as it is
end
ads1115.device:setting()¶
Configuration settings for the ADC.
Syntax¶
device:setting(GAIN, SAMPLES, CHANNEL, MODE[, CONVERSION_RDY][, COMPARATOR, THRESHOLD_LOW, THRESHOLD_HI[,COMP_MODE]])
Parameters¶
GAINProgrammable gain amplifierads1115.GAIN_6_144V2/3x Gainads1115.GAIN_4_096V1x Gainads1115.GAIN_2_048V2x Gainads1115.GAIN_1_024V4x Gainads1115.GAIN_0_512V8x Gainads1115.GAIN_0_256V16x Gain
SAMPLESData rate in samples per secondads1115.DR_8SPSADS1115 onlyads1115.DR_16SPSADS1115 onlyads1115.DR_32SPSADS1115 onlyads1115.DR_64SPSADS1115 onlyads1115.DR_128SPSads1115.DR_250SPSads1115.DR_475SPSADS1115 onlyads1115.DR_490SPSADS1015 onlyads1115.DR_860SPSADS1115 onlyads1115.DR_920SPSADS1015 onlyads1115.DR_1600SPSADS1015 onlyads1115.DR_2400SPSADS1015 onlyads1115.DR_3300SPSADS1015 only
CHANNELInput multiplexer for single-ended or differential measurementads1115.SINGLE_0channel 0 to GNDads1115.SINGLE_1channel 1 to GNDads1115.SINGLE_2channel 2 to GNDads1115.SINGLE_3channel 3 to GNDads1115.DIFF_0_1channel 0 to 1ads1115.DIFF_0_3channel 0 to 3ads1115.DIFF_1_3channel 1 to 3ads1115.DIFF_2_3channel 2 to 3
MODEDevice operating modeads1115.SINGLE_SHOTsingle-shot modeads1115.CONTINUOUScontinuous mode
CONVERSION_RDYNumber of conversions after conversion ready asserts (optional)ads1115.CONV_RDY_1ads1115.CONV_RDY_2ads1115.CONV_RDY_4
COMPARATORNumber of conversions after comparator asserts (optional)ads1115.COMP_1CONVads1115.COMP_2CONVads1115.COMP_4CONV
THRESHOLD_LOW0-+ GAIN_MAXin mV for single-ended inputs- GAIN_MAX-+ GAIN_MAXin mV for differential inputs
THRESHOLD_HI0-+ GAIN_MAXin mV for single-ended inputs- GAIN_MAX-+ GAIN_MAXin mV for differential inputs
COMP_MODEComparator modeads1115.CMODE_TRADtraditional comparator mode (with hysteresis)ads1115.CMODE_WINDOWwindow comparator mode
note: Comparator and conversion ready are always configured to non-latching, active low.
Returns¶
nil
Example¶
local id, sda, scl = 0, 6, 5
i2c.setup(id, sda, scl, i2c.SLOW)
ads1115.reset()
adc1 = ads1115.ads1015(id, ads1115.ADDR_GND)
adc1:setting(ads1115.GAIN_6_144V, ads1115.DR_3300SPS, ads1115.SINGLE_0, ads1115.SINGLE_SHOT)
ads1115.device:startread()¶
Starts the ADC reading for single-shot mode and after the conversion is done it will invoke an optional callback function in which the ADC conversion result can be obtained.
Syntax¶
device:startread([CALLBACK])
Parameters¶
CALLBACKcallback function which will be invoked after the adc conversion is donefunction(volt, volt_dec, adc, sign) end
Returns¶
nil
Example¶
local id, alert_pin, sda, scl = 0, 7, 6, 5
i2c.setup(id, sda, scl, i2c.SLOW)
ads1115.reset()
adc1 = ads1115.ads1115(id, ads1115.ADDR_VDD)
-- single shot
adc1:setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_0, ads1115.SINGLE_SHOT)
-- start adc conversion and get result in callback after conversion is ready
adc1:startread(function(volt, volt_dec, adc, sign) print(volt, volt_dec, adc, sign) end)
-- conversion ready
adc1:setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_0, ads1115.SINGLE_SHOT, ads1115.CONV_RDY_1)
local function conversion_ready(level, when)
gpio.trig(alert_pin)
volt, volt_dec, adc, sign = adc1:read()
print(volt, volt_dec, adc, sign)
end
gpio.mode(alert_pin, gpio.INT)
gpio.trig(alert_pin, "down", conversion_ready)
-- start conversion and get result with read() after conversion ready pin asserts
adc1:startread()