hamishcunningham / fishy-wifi

Scripts, notes and the odd subaquatic gizmo for the ESP8266 and what-have-you.
GNU Affero General Public License v3.0
25 stars 13 forks source link

Lux auto-ranging #6

Closed DrRob closed 6 years ago

DrRob commented 9 years ago

I'm using my TSL2561 outside in a greenhouse, and with the integration time set to 402ms the levels were hitting the end of the scale (65535), even if I turned off the 16x gain.

I could have just switched to 101ms to get the full range of brightness, but then the maximum output value becomes 37177, so some granularity is lost.

So instead I've changed the code to try four different combinations of gain on/off and integration time, and choose the first that isn't out of range. It's a bit slower, but I'm only sampling once every five minutes.

What do you think?

function M.getchannels()
  local rc0,rc1 = nil,nil
  local dev_addr = M.TSL2561_ADDR_FLOAT 
  i2c.setup(busid, SDA_PIN , SCL_PIN, i2c.SLOW)

  -- 16 = fixed-point fudge factor

  -- nom,denom = 16 * (322/322), 16
  settimegain(dev_addr, M.TSL2561_INTEGRATIONTIME_402MS, M.TSL2561_GAIN_16X)

  enable(dev_addr)
  tmr.delay(500000)
  local ch0,ch1 = getFullLuminosity(dev_addr)
  disable(dev_addr)

  if rc0 == nil and ch0 < 65535 then
    rc0 = ch0
  end
  if rc1 == nil and ch1 < 65535 then
    rc1 = ch1
  end

  if ( rc0 == nil or rc1 == nil) then
    -- nom,denom = 16 * (322/81), 16
    -- nom,denom = 322,81
    settimegain(dev_addr, M.TSL2561_INTEGRATIONTIME_101MS, M.TSL2561_GAIN_16X)

    enable(dev_addr)
    tmr.delay(500000)
    ch0,ch1 = getFullLuminosity(dev_addr)
    disable(dev_addr)

    if rc0 == nil and ch0 < 37177 then
      rc0 = ch0*322/81
    end
    if rc1 == nil and ch1 < 37177 then
      rc1 = ch1*322/81
    end

    if ( rc0 == nil or rc1 == nil) then
      -- nom,denom = 16 * (322/322), 1
      -- nom,denom = 16,1
      settimegain(dev_addr, M.TSL2561_INTEGRATIONTIME_402MS, M.TSL2561_GAIN_1X)

      enable(dev_addr)
      tmr.delay(500000)
      ch0,ch1 = getFullLuminosity(dev_addr)
      disable(dev_addr)

      if rc0 == nil and ch0 < 65535 then
        rc0 = ch0*16
      end
      if rc1 == nil and ch1 < 65535 then
        rc1 = ch1*16
      end

      if ( rc0 == nil or rc1 == nil) then
        -- nom,denom = 16 * (322/81), 1
        -- nom,denom = 5152,81
        settimegain(dev_addr, M.TSL2561_INTEGRATIONTIME_101MS, M.TSL2561_GAIN_1X)

        enable(dev_addr)
        tmr.delay(500000)
        ch0,ch1 = getFullLuminosity(dev_addr)
        disable(dev_addr)

        if rc0 == nil then
          rc0 = ch0*5152/81
        end
        if rc1 == nil then
          rc1 = ch1*5152/81
        end
      end
    end
  end
  return rc0,rc1
end

In theory this outputs values in the range 0 to 2,364,640 although even with a bright torch focused directly on the sensor I couldn't get the numbers anywhere near that high.

ps. Watch out for the TSL2561_GAIN_1X constant that's been renamed from TSL2561_GAIN_0X

pps. I didn't bother with the 13.7ms integration time - see my explanation here: http://ideasandbox.blogspot.co.uk/2015/06/tsl2561-light-sensor-ranges.html

hamishcunningham commented 9 years ago

hi

I suspect the lux code has no parent at present (though I'm cc'ing Gareth to check)

in which case the floor is yours :-)

(i.e. I'll accept any pull requests you care to make)

sorry not to be more helpful; IIRR that code was ported from an arduino sketch and then attention moved elsewhere :-(

best

h

On 21 June 2015 at 19:51, DrRob notifications@github.com wrote:

I'm using my TSL2561 outside in a greenhouse, and with the integration time set to 402ms the levels were hitting the end of the scale (65535), even if I turned off the 16x gain.

I could have just switched to 101ms to get the full range of brightness, but then the maximum output value becomes 37177, so some granularity is lost.

So instead I've changed the code to try four different combinations of gain on/off and integration time, and choose the first that isn't out of range. It's a bit slower, but I'm only sampling once every five minutes.

What do you think?

function M.getchannels() local rc0,rc1 = nil,nil local dev_addr = M.TSL2561_ADDR_FLOAT i2c.setup(busid, SDA_PIN , SCL_PIN, i2c.SLOW)

-- 16 = fixed-point fudge factor

-- nom,denom = 16 * (322/322), 16 settimegain(dev_addr, M.TSL2561_INTEGRATIONTIME_402MS, M.TSL2561_GAIN_16X)

enable(dev_addr) tmr.delay(500000) local ch0,ch1 = getFullLuminosity(dev_addr) disable(dev_addr)

if rc0 == nil and ch0 < 65535 then rc0 = ch0 end if rc1 == nil and ch1 < 65535 then rc1 = ch1 end

if ( rc0 == nil or rc1 == nil) then -- nom,denom = 16 * (322/81), 16 -- nom,denom = 322,81 settimegain(dev_addr, M.TSL2561_INTEGRATIONTIME_101MS, M.TSL2561_GAIN_16X)

enable(dev_addr)
tmr.delay(500000)
ch0,ch1 = getFullLuminosity(dev_addr)
disable(dev_addr)

if rc0 == nil and ch0 < 37177 then
  rc0 = ch0*322/81
end
if rc1 == nil and ch1 < 37177 then
  rc1 = ch1*322/81
end

if ( rc0 == nil or rc1 == nil) then
  -- nom,denom = 16 * (322/322), 1
  -- nom,denom = 16,1
  settimegain(dev_addr, M.TSL2561_INTEGRATIONTIME_402MS, M.TSL2561_GAIN_1X)

  enable(dev_addr)
  tmr.delay(500000)
  ch0,ch1 = getFullLuminosity(dev_addr)
  disable(dev_addr)

  if rc0 == nil and ch0 < 65535 then
    rc0 = ch0*16
  end
  if rc1 == nil and ch1 < 65535 then
    rc1 = ch1*16
  end

  if ( rc0 == nil or rc1 == nil) then
    -- nom,denom = 16 * (322/81), 1
    -- nom,denom = 5152,81
    settimegain(dev_addr, M.TSL2561_INTEGRATIONTIME_101MS, M.TSL2561_GAIN_1X)

    enable(dev_addr)
    tmr.delay(500000)
    ch0,ch1 = getFullLuminosity(dev_addr)
    disable(dev_addr)

    if rc0 == nil then
      rc0 = ch0*5152/81
    end
    if rc1 == nil then
      rc1 = ch1*5152/81
    end
  end
end

end return rc0,rc1 end

In theory this outputs values in the range 0 to 2,364,640 although even with a bright torch focused directly on the sensor I couldn't get the numbers anywhere near that high.

ps. Watch out for the TSL2561_GAIN_1X constant that's been renamed from TSL2561_GAIN_0X

pps. I didn't bother with the 13.7ms integration time - see my explanation here: http://ideasandbox.blogspot.co.uk/2015/06/tsl2561-light-sensor-ranges.html

— Reply to this email directly or view it on GitHub https://github.com/hamishcunningham/fishy-wifi/issues/6.

Hamish Cunningham Professor of Computer Science, University of Sheffield, UK +44 7920 765 455 https://twitter.com/@HCunningham hamish@gate.ac.uk https://pi.gate.ac.uk https://hamish.gate.ac.uk https://gate.ac.uk

DrRob commented 9 years ago

Hi,

I finally had time to learn how git pull requests work, and have done it.

I hope it's ok.

Rob.

On 22 June 2015 at 21:34, Hamish Cunningham notifications@github.com wrote:

hi

I suspect the lux code has no parent at present (though I'm cc'ing Gareth to check)

in which case the floor is yours :-)

(i.e. I'll accept any pull requests you care to make)

sorry not to be more helpful; IIRR that code was ported from an arduino sketch and then attention moved elsewhere :-(

best

h

On 21 June 2015 at 19:51, DrRob notifications@github.com wrote:

I'm using my TSL2561 outside in a greenhouse, and with the integration time set to 402ms the levels were hitting the end of the scale (65535), even if I turned off the 16x gain.

I could have just switched to 101ms to get the full range of brightness, but then the maximum output value becomes 37177, so some granularity is lost.

So instead I've changed the code to try four different combinations of gain on/off and integration time, and choose the first that isn't out of range. It's a bit slower, but I'm only sampling once every five minutes.

What do you think?

function M.getchannels() local rc0,rc1 = nil,nil local dev_addr = M.TSL2561_ADDR_FLOAT i2c.setup(busid, SDA_PIN , SCL_PIN, i2c.SLOW)

-- 16 = fixed-point fudge factor

-- nom,denom = 16 * (322/322), 16 settimegain(dev_addr, M.TSL2561_INTEGRATIONTIME_402MS, M.TSL2561_GAIN_16X)

enable(dev_addr) tmr.delay(500000) local ch0,ch1 = getFullLuminosity(dev_addr) disable(dev_addr)

if rc0 == nil and ch0 < 65535 then rc0 = ch0 end if rc1 == nil and ch1 < 65535 then rc1 = ch1 end

if ( rc0 == nil or rc1 == nil) then -- nom,denom = 16 * (322/81), 16 -- nom,denom = 322,81 settimegain(dev_addr, M.TSL2561_INTEGRATIONTIME_101MS, M.TSL2561_GAIN_16X)

enable(dev_addr) tmr.delay(500000) ch0,ch1 = getFullLuminosity(dev_addr) disable(dev_addr)

if rc0 == nil and ch0 < 37177 then rc0 = ch0_322/81 end if rc1 == nil and ch1 < 37177 then rc1 = ch1_322/81 end

if ( rc0 == nil or rc1 == nil) then -- nom,denom = 16 * (322/322), 1 -- nom,denom = 16,1 settimegain(dev_addr, M.TSL2561_INTEGRATIONTIME_402MS, M.TSL2561_GAIN_1X)

enable(dev_addr) tmr.delay(500000) ch0,ch1 = getFullLuminosity(dev_addr) disable(dev_addr)

if rc0 == nil and ch0 < 65535 then rc0 = ch0_16 end if rc1 == nil and ch1 < 65535 then rc1 = ch1_16 end

if ( rc0 == nil or rc1 == nil) then -- nom,denom = 16 * (322/81), 1 -- nom,denom = 5152,81 settimegain(dev_addr, M.TSL2561_INTEGRATIONTIME_101MS, M.TSL2561_GAIN_1X)

enable(dev_addr) tmr.delay(500000) ch0,ch1 = getFullLuminosity(dev_addr) disable(dev_addr)

if rc0 == nil then rc0 = ch0_5152/81 end if rc1 == nil then rc1 = ch1_5152/81 end end end end return rc0,rc1 end

In theory this outputs values in the range 0 to 2,364,640 although even with a bright torch focused directly on the sensor I couldn't get the numbers anywhere near that high.

ps. Watch out for the TSL2561_GAIN_1X constant that's been renamed from TSL2561_GAIN_0X

pps. I didn't bother with the 13.7ms integration time - see my explanation here:

http://ideasandbox.blogspot.co.uk/2015/06/tsl2561-light-sensor-ranges.html

— Reply to this email directly or view it on GitHub https://github.com/hamishcunningham/fishy-wifi/issues/6.

Hamish Cunningham Professor of Computer Science, University of Sheffield, UK +44 7920 765 455 https://twitter.com/@HCunningham hamish@gate.ac.uk https://pi.gate.ac.uk https://hamish.gate.ac.uk https://gate.ac.uk

— Reply to this email directly or view it on GitHub https://github.com/hamishcunningham/fishy-wifi/issues/6#issuecomment-114254801 .

layerzerolabs commented 6 years ago

It's a bit late... but I realised we've implemented your pull request a couple of years ago so let me say thanks again!