我有一个 ESP8266 (ESP-12) NodeMCU V3,带有 2 个相同的传感器 TSL2561。 如果我想获得两个值,我做的对吗?
status = tsl2561.init(4, 5, tsl2561.ADDRESS_FLOAT, tsl2561.PACKAGE_T_FN_CL)
if status == tsl2561.TSL2561_OK then
first = tsl2561.getlux()
end
status = tsl2561.init(4, 5, tsl2561.ADDRESS_GND, tsl2561.PACKAGE_T_FN_CL)
if status == tsl2561.TSL2561_OK then
second = tsl2561.getlux()
end
我还没试过,我正在等待速卖通的传感器。
每个 TSL2561 都可以配置为使用三个 i2c 地址之一。请参阅数据表:https://cdn-shop.adafruit.com/datasheets/TSL2561.pdf
如果您在 Adafruit 或类似板上订购了传感器,那么将会有一个“addr”引脚,您可以将其连接到 Gnd、Vdd 或保持浮动以设置该板的地址。很明显,Nodemcu 软件将这些地址识别为 tsl2561.ADDRESS_GND 等。
假设您计划将两个传感器放在同一个 i2c 总线上(一个简单、明智的选择)。或者,您可以运行两个独立的 i2c 总线,在这种情况下,您不需要更改任一传感器的 i2c 地址。
作为可能的解决方案,可以使用 i2c 模块重写
local SCLPIN = 1 -- pin 1 / D1 / GPIO5
local SDAPIN = 2 -- pin 2 / D2 / GPIO4
local TSL1_ADDR = 0x29 -- soldered to GND
local TSL2_ADDR = 0x39 -- not soldered (FLOAT)
local I2C_ID = 0 -- the only bus
function writebyte(bus, device, reg, val)
i2c.start(bus)
i2c.address(bus, device, i2c.TRANSMITTER)
i2c.write(bus, reg)
i2c.write(bus, val)
i2c.stop(bus)
end
function readbyte(bus, device, reg)
i2c.start(bus)
i2c.address(bus, device, i2c.TRANSMITTER)
i2c.write(bus, reg)
i2c.stop(bus)
i2c.start(bus)
i2c.address(bus, device, i2c.RECEIVER)
b = i2c.read(bus, 1)
i2c.stop(bus)
return string.byte(b)
end
function loop()
-- light1
writebyte(I2C_ID, TSL1_ADDR, 0x80, 0x03) -- enable
writebyte(I2C_ID, TSL1_ADDR, 0x81, 0x02) -- set timing 402ms & gain 1x
writebyte(I2C_ID, TSL1_ADDR, 0x80, 0x00) -- disable
tmr.delay(1000)
writebyte(I2C_ID, TSL1_ADDR, 0x80, 0x03) -- enable
tmr.delay(500000)
c = readbyte(I2C_ID, TSL1_ADDR, 0x8C) -- read 0 channel (fullspectrum low)
d = readbyte(I2C_ID, TSL1_ADDR, 0x8D) -- read 0 channel (fullspectrum high)
l1=c+d*256
-- light2
writebyte(I2C_ID, TSL2_ADDR, 0x80, 0x03) -- enable
writebyte(I2C_ID, TSL2_ADDR, 0x81, 0x02) -- set timing 402ms & gain 1x
writebyte(I2C_ID, TSL2_ADDR, 0x80, 0x00) -- disable
tmr.delay(1000)
writebyte(I2C_ID, TSL2_ADDR, 0x80, 0x03) -- enable
tmr.delay(500000)
c = readbyte(I2C_ID, TSL2_ADDR, 0x8C) -- read 0 channel (fullspectrum low)
d = readbyte(I2C_ID, TSL2_ADDR, 0x8D) -- read 0 channel (fullspectrum high)
l2=c+d*256
makesomethingwith(l1,l2)
end