ESP8266 用node mcu开发ADS1115 IC模块总结


先贴代码,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


附图:





参考文章:

Elaine_up的博客

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)


每个定时器获取一路数据。





猜你喜欢

转载自blog.csdn.net/qq_16855605/article/details/80092123