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 name | ClosedCube OPT3001 |
|---|---|
| Author | ClosedCube |
| Include | #include <ClosedCube_OPT3001.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | Ambient 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.
- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi
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.
#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
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.