Arduino library · Sensors

ClosedCube OPT3001: an example that runs

Yes. You can use the ClosedCube OPT3001 library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Ambient light, OPT3001). The example below, "Lux from an OPT3001 in continuous, auto-ranging mode", runs on the Arduino Uno R3.

Built in on Mokxi: #include <ClosedCube_OPT3001.h> and it builds, with nothing to install. Free, and no account needed.

What ClosedCube OPT3001 does

Light in lux from the OPT3001: begin(), the IDs, OPT3001_Config and writeConfig(), readResult() and the limits. That is what ClosedCube OPT3001 covers. The sketch below uses it on the Arduino Uno: “Lux from an OPT3001 in continuous, auto-ranging mode”.

The ClosedCube OPT3001 library, for the TI OPT3001 ambient light sensor.

OPT3001_Config overlays the configuration register's fields on its raw word, as upstream's does (the sketches the library ships write B1100 and B11, which are 0b1100 and 0b11). readResult() turns the exponent and mantissa into lux, 0.01 x 2^E x R.

The calls follow release 1.1.2 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager nameClosedCube OPT3001
AuthorClosedCube
Include#include <ClosedCube_OPT3001.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesAmbient light, OPT3001

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <ClosedCube_OPT3001.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "ClosedCube OPT3001" by ClosedCube from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add ClosedCube OPT3001 to lib_deps in PlatformIO. Mokxi follows the calls of release 1.1.2, so a sketch written here builds there too.

The calls you will use

In the order a sketch meets them.

ClosedCube_OPT3001 opt3001

Makes opt3001, the ClosedCube_OPT3001 object the calls below are made on.

opt3001.begin(OPT3001_ADDRESS)

Starts it up. Call it once in setup(). Arguments: address.

opt3001.readManufacturerID()

Returns the manufacturer id as a whole number.

opt3001.readDeviceID()

Returns the device id as a whole number.

OPT3001 result

Declares result, a OPT3001 that the example uses.

opt3001.readResult()

Returns the result as a OPT3001.

Example: Lux from an OPT3001 in continuous, auto-ranging mode

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <Wire.h>
#include <ClosedCube_OPT3001.h>

ClosedCube_OPT3001 opt3001;

#define OPT3001_ADDRESS 0x44

void setup() {
  Serial.begin(9600);
  opt3001.begin(OPT3001_ADDRESS);
  Serial.print("OPT3001 Manufacturer ID: 0x");
  Serial.println(opt3001.readManufacturerID(), HEX);
  Serial.print("OPT3001 Device ID: 0x");
  Serial.println(opt3001.readDeviceID(), HEX);
  OPT3001_Config newConfig;
  newConfig.rawData = 0;
  newConfig.RangeNumber = 0b1100;
  newConfig.ConvertionTime = 0b0;
  newConfig.Latch = 0b1;
  newConfig.ModeOfConversionOperation = 0b11;
  opt3001.writeConfig(newConfig);
}

void loop() {
  delay(150);
  OPT3001 result = opt3001.readResult();
  if (result.error == NO_ERROR) {
    Serial.print("OPT3001: ");
    Serial.print(result.lux, 1);
    Serial.println(" lux");
  }
  delay(350);
}

Wiring

  • Arduino Uno R3 A4 to Ambient light, OPT3001 SDA
  • Arduino Uno R3 A5 to Ambient light, OPT3001 SCL
  • Arduino Uno R3 5V to Ambient light, OPT3001 VCC
  • Arduino Uno R3 GND to Ambient light, OPT3001 GND

Open it in the editor, wired and ready to run

Projects that use ClosedCube OPT3001

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

Try ClosedCube OPT3001 without installing anything

Open the example, change the sketch, and keep your version in a free account.