Skip to content

Instantly share code, notes, and snippets.

@geekman
Created March 17, 2023 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geekman/d32add58aedf1d52380eaec42dd39417 to your computer and use it in GitHub Desktop.
Save geekman/d32add58aedf1d52380eaec42dd39417 to your computer and use it in GitHub Desktop.
Arduino code for reading OPT3001 sensor values
//
// Arduino test code to read OPT3001 sensor values
// https://www.ti.com/product/OPT3001
// https://www.ti.com/lit/gpn/opt3001 (datasheet)
//
#include <Wire.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
// configure
Wire.beginTransmission(0x44); // ADDR pulled to GND
Wire.write(0x01); // config register
Wire.write(0xCE); // range, etc
Wire.write(0x10);
Wire.endTransmission();
// partial write to change the read address
Wire.beginTransmission(0x44);
Wire.write(0); // result register
Wire.endTransmission(); // sends a STOP
}
void loop() {
Wire.requestFrom(0x44, 2); // read 2 bytes from last address
uint8_t buf[4];
uint16_t value;
while (Wire.available()) {
Wire.readBytes(buf, 2);
uint8_t exp = buf[0] >> 4;
value = buf[1] | ((buf[0] & 0x0F) << 8);
Serial.print(exp); // exponent (multiplier)
Serial.print(' ');
Serial.print(value); // fractional value
Serial.print('\n');
// some delay here, since conversion is 800ms cycle
delay(500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment