Skip to content

Instantly share code, notes, and snippets.

@nezarfadle
Last active June 22, 2020 09:53
Show Gist options
  • Save nezarfadle/981c4ee62637800033bfe3cef08da7ce to your computer and use it in GitHub Desktop.
Save nezarfadle/981c4ee62637800033bfe3cef08da7ce to your computer and use it in GitHub Desktop.

Download the library from here:

https://github.com/adafruit/Adafruit_VL53L0X

Code

#include <Wire.h>
#include "Adafruit_VL53L0X.h" 
Adafruit_VL53L0X lox = Adafruit_VL53L0X();

#define D6T_ID 0x0A
#define D6T_CMD 0x4C

int readBuffer[35];
float ptat;
float tdata[16];
int counter = 1;
double average = 0;

String getTemp( float data, int add, String extra )
{
  float temp = data + add;
  return String ( temp ) + extra ;
}

void setup()
{
  Wire.begin();
  lox.begin();
  Serial.begin(9600);
  Serial.println("OS Started ...");
}

void loop()
{
  
  Wire.beginTransmission( D6T_ID );
  Wire.write( D6T_CMD );
  Wire.endTransmission();
  Wire.requestFrom( D6T_ID, 35 );
  
  for( int i = 0; i < 35; i++ )
  {
    readBuffer[i] = Wire.read();
  }

  /*
    Bytes 1-2 are the PTAT data
        - The internal reference temperature
        - This is the temperature around the device
  */
  ptat = (readBuffer[0] + ( readBuffer[1] * 256 ) ) * 0.1;

  /*
    Bytes 3-34 are the temperature data of each pixel respectively
        - E.g. Bytes 3-4 are Pixel 0’s low and high byte
        - E.g. Bytes 4-5 are Pixel 1’s low and high byte
        - E.g. Bytes 6-7 are Pixel 2’s low and high byte
    Byte 35, the final byte, is the PEC
        - Packet error check byte
  */
  for( int i = 0; i < 16; i++ )
  {
    tdata[i] = (readBuffer[(i*2+2)] + ( readBuffer[i*2+3] * 256 ) ) * 0.1;
  }

  
  float tempf;
  float r = (tdata[0] * 9.0 / 5.0 ) + 32.0;
  
  if (  r > 0 )
  {
   
    float fraction = tdata[5]-(long)tdata[5];
    VL53L0X_RangingMeasurementData_t measure;
    lox.rangingTest(&measure, false);
    int distance = measure.RangeMilliMeter / 10;

    Serial.println( "Distance: " + String( distance ) );
    Serial.println( "Real Temp: " + String( tdata[5] ) );
    
    if( distance >= 28 && distance <= 38  )
      Serial.println( getTemp( tdata[5], 3.5, " | 1" ));
    else if( distance >= 39 && distance <= 45 )
      Serial.println( getTemp( tdata[5], 4.5, " | 2" ));
    else if( distance >= 46 && distance <= 51 )
      Serial.println( getTemp( tdata[5], 5.5, " | 3" ));
    else if( distance > 51 && distance <= 60 )
      Serial.println( getTemp( tdata[5], 4.5, " | 4" ));
        
    
  }
  delay( 800 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment