Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement TSL2561 module #611

Merged
merged 13 commits into from
Aug 30, 2015
Merged

Implement TSL2561 module #611

merged 13 commits into from
Aug 30, 2015

Conversation

Aeprox
Copy link
Contributor

@Aeprox Aeprox commented Aug 23, 2015

I've adapted some existing C code for interfacing with the tsl2561 light sensor chips over I2C. There's already a lua module for the device in the repo, but I figured this would have better performance and memory savings.

Any feedback is much appreciated.

tsl2561 module

tsl2561.init()

Description

Initializes the device on pins sdapin&sclpin. Optionally also configures the devices address and package. (Default: address pin floating (0x39) and FN package)

Syntax

tsl2561.init(sdapin, sclpin)
tsl2561.init(sdapin, sclpin, address)
tsl2561.init(sdapin, sclpin, address, package)

Parameters

  • sdapin: Pin number of the device's I2C sda connection
  • sclpin: Pin number of the device's I2C scl connection
  • (optional) address: Address of the device on the I2C bus
    • tsl2561.ADDRESS_GND
    • tsl2561.ADDRESS_FLOAT (default)
    • tsl2561.ADDRESS_VDD
  • (optional) package: Device's package type (slight difference in lux calculation)
    • tsl2561.PACKAGE_CS
    • tsl2561.PACKAGE_T_FN_CL (default)

Return

status: value indicating success or failure as explained below

  • 0: tsl2561.TSL2561_OK
  • 1: tsl2561.TSL2561_ERROR_I2CINIT: Can't initialize I2C bus
  • 2: tsl2561.TSL2561_ERROR_I2CBUSY: I2C bus busy
  • 3: tsl2561.TSL2561_ERROR_NOINIT: Initialize I2C bus before calling function
  • 4: tsl2561.TSL2561_ERROR_LAST

Example

status = tsl2561.init(5,6,tsl2561.ADDRESS_FLOAT, tsl2561.PACKAGE_T_FN_CL)
if (status == 0) then
  lux = tsl2561.getlux()
  print("Illuminance: "..lux.." lx") 
end

tsl2561.settiming()

Description

Sets the integration time and gain settings of the device. When init() is called, these values default to 402ms and no gain.

Syntax

tsl2561.settiming(integration, gain)

Parameters

  • integration: Sets the device's integration period. Valid options:
    • tsl2561.INTEGRATIONTIME_13MS
    • tsl2561.INTEGRATIONTIME_101MS
    • tsl2561.INTEGRATIONTIME_402MS (default)
  • gain: Sets the device's gain. Valid options:
    • tsl2561.GAIN_1X (default)
    • tsl2561.GAIN_16X

Return

status: value indicating success or failure as explained below

  • 0: tsl2561.TSL2561_OK
  • 1: tsl2561.TSL2561_ERROR_I2CINIT: Can't initialize I2C bus
  • 2: tsl2561.TSL2561_ERROR_I2CBUSY: I2C bus busy
  • 3: tsl2561.TSL2561_ERROR_NOINIT: Initialize I2C bus before calling function
  • 4: tsl2561.TSL2561_ERROR_LAST

Example

status = tsl2561.init(5,6,tsl2561.ADDRESS_FLOAT, tsl2561.PACKAGE_T_FN_CL)
if (status == 0) then
  status = tsl2561.settiming(tsl2561.INTEGRATIONTIME_101MS, tsl2561.GAIN_16X)
end
if (status == 0) then
  lux = tsl2561.getlux()
  print("Illuminance: "..lux.." lx") 
end

tsl2561.getlux()

Description

Reads sensor values from the device and returns calculated lux value.

Syntax

tsl2561.getlux()

Parameters

nil

Return

  • lux: The calculated illuminance in lux (lx).
  • status: value indicating success or failure as explained below
    • 0: tsl2561.TSL2561_OK
    • 1: tsl2561.TSL2561_ERROR_I2CINIT: Can't initialize I2C bus
    • 2: tsl2561.TSL2561_ERROR_I2CBUSY: I2C bus busy
    • 3: tsl2561.TSL2561_ERROR_NOINIT: Initialize I2C bus before calling function
    • 4: tsl2561.TSL2561_ERROR_LAST

Example

status = tsl2561.init(5,6,tsl2561.ADDRESS_FLOAT, tsl2561.PACKAGE_T_FN_CL)

if (status == 0) then
  lux = tsl2561.getlux()
  print("Illuminance: "..lux.." lx") 
end

tsl2561.getrawchannels()

Description

Reads the device's 2 sensors and returns their values.

Syntax

tsl2561.getrawchannels()

Parameters

nil

Return

  • ch0: Value of the broad spectrum sensor
  • ch1: Value of the IR sensor
  • status: value indicating success or failure as explained below
    • 0: tsl2561.TSL2561_OK
    • 1: tsl2561.TSL2561_ERROR_I2CINIT: Can't initialize I2C bus
    • 2: tsl2561.TSL2561_ERROR_I2CBUSY: I2C bus busy
    • 3: tsl2561.TSL2561_ERROR_NOINIT: Initialize I2C bus before calling function
    • 4: tsl2561.TSL2561_ERROR_LAST

Example

status = tsl2561.init(5,6,tsl2561.ADDRESS_FLOAT, tsl2561.PACKAGE_T_FN_CL)

if (status == 0) then
  ch0, ch1 = tsl2561.getrawchannels()
  print("Raw values: "..ch0,ch1)
  lux = tsl2561.getlux()
  print("Illuminance: "..lux.." lx") 
end

vowstar added a commit that referenced this pull request Aug 30, 2015
@vowstar vowstar merged commit 2775ca6 into nodemcu:dev Aug 30, 2015
@Aeprox Aeprox deleted the devTSL2561 branch October 22, 2015 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants