本文产品淘宝链接:https://shop401594091.taobao.com
先贴代码,demo如下:
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)
-- comparator
adc1:setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_1, 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)
tmr.alarm(5,100,1,function()
-- read adc result with read()
volt, volt_dec, adc, sign = adc1:read()
print(volt, volt_dec, adc, sing)
end)
-----------------------------分割线-----------------------------------------------
结论一:
local id=0,不是指输入模拟量的id,是i2c的输入必须为0.ADS1115要使用A1或者A2\A3需要配置寄存器地址,demo如下:
adc1 = ads1115.ads1115(0, ads1115.ADDR_GND)
adc1:setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_1, ads1115.CONTINUOUS)
注意,在nodemcu中ads1115.SINGLE_1表示A1口,同理ads1115.SINGLE_0表示A0口,亲测可用。
结论耳:
官网API里的函数实例有大量错误,常见的是把变量adc1写混成ads1,导致编译时报错。
留下QQ为了帮助更多其他人:1252595878
附图:
参考文章:
https://blog.csdn.net/Elaine_up/article/details/78052212
---------------------------分割线---------------------------
2018.5.18补充关于提示:attempt to call field 'reset' (a nil value)的问题
原因先查一下固件里包含ads1115模块。
还有个原因我是自己编译的固件(不是通过邮箱获取的)会出现这个问题,至今没解决,有大佬解决了还请下方留言指点一二
2018.5.28补充关于ads1117不能同时获取多路数据的问题
dome如下:
tmr.alarm(5,1000,1,function()
-- read adc result with read()
adc1:setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_0, ads1115.CONTINUOUS, ads1115.COMP_1CONV, 1000, 2000)
volt, volt_dec, adc, sign = adc1:read()
print("1:"..volt, volt_dec, adc, sing)
end)
tmr.alarm(4,1000,1,function()
-- read adc result with read()
adc1:setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_1, ads1115.CONTINUOUS, ads1115.COMP_1CONV, 1000, 2000)
volt, volt_dec, adc, sign = adc1:read()
print("2:"..volt, volt_dec, adc, sing)
end)
2018.9.5补充:
上面写的是用定时器获取多路采集电压,下面写个不用定时器的。可在一个函数里随便获取多路ads电压
adc1:setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_0, ads1115.SINGLE_SHOT, ads1115.COMP_1CONV, 1000, 2000)
adc1:startread(
function(volt, volt_dec, adc, sign)
print("wendu1:"..volt)
adc1:setting(ads1115.GAIN_6_144V, ads1115.DR_128SPS, ads1115.SINGLE_1, ads1115.SINGLE_SHOT, ads1115.COMP_1CONV, 1000, 2000)
adc1:startread(
function(volt2, volt_dec2, adc2, sign2)
print("wendu2:"..volt2)
if volt > volt2 then
voltdata = volt - volt2
elseif volt <= volt2 then
voltdata = volt2 - volt
end
print("wencha:"..voltdata)
if voltdata < 100 then
local vlan=volt
wdjishu = wdjishu + 1
numswd[wdjishu] = vlan
else
print("wendu-cuowu:"..voltdata)
TcpConnect:send("{'theme':'wencha','type':'"..ATVdata.wendutype.."','value':'"..voltdata.."','pmentid':'"..ATVdata.wenduID.."'}")
end
end)
end)
注意adc1:startread(函数里面是获取到的数据,我是获取了2路温度进行对比,所以套了2层adc1:startread()
参考资料: