Close

Non-contact IR Thermometer Updates

A project log for Voight-Kampff Machine

Distinguishing Humans from Replicant's

tom-meehanTom Meehan 04/07/2017 at 06:590 Comments

Testing Harbour Freight non-contact IR temp sensor. Drawing from DIY Thermal Camera for information on connecting to Arduino for sending serial information to laptop.

I adapted and simplified some code to read and send the temperature data:

/*Harbour Freight Non-Contact IR Temp Sensor
adapted from https://dorkbotpdx.org/blog/scott_d/inexpensive_ir_based_temperature_sensor_for_microprocessors
  changed to use serial monitor instead of a 16x2 LCD and function order re-organized
    setup -> loop -> readBit  with indentations added to make reading easier
*/

#define CLK 3
#define DATA 4

volatile int nbits = 0;
volatile byte hexbyte = 0;
volatile byte read_byte;
volatile int byte_ready = 0;

volatile unsigned char message[4];
volatile int nbytes = 0;
volatile int message_waiting = 0;

unsigned long last_time = 0;

float temp;
float ambient;

void setup() {
  delay(2000);
  Serial.begin(9600);

  pinMode(CLK, INPUT);
  pinMode(DATA, INPUT);
  attachInterrupt(1, readBit, FALLING);
}

void loop() {
  if (message_waiting == 1) {
    last_time = millis();
  if (message[0] == 0x4c) {
    int t = message[1]<<8 | message[2];
    temp = t/16.0 -273.15;
    Serial.print(millis()/1000.0);Serial.print(" ");
    Serial.println(temp);
    } 
      else if (message[0] == 0x66) {
        int t = message[1]<<8 | message[2];
        ambient = t/16.0 -273.15;
      }
   message_waiting = 0;
  }

  if (millis() - last_time > 1000) {
    nbits = 0;
    nbytes = 0;
    hexbyte = 0;
    message_waiting = 0;
    byte_ready = 0;
    last_time = millis();
    }
}

void readBit() {
  int val = digitalRead(DATA);
  nbits++;
  int bit = (val == HIGH) ? 1 : 0;
  hexbyte = (hexbyte << 1) | bit;
  if (nbits == 8) {
    if (byte_ready == 0) {
      read_byte = hexbyte;
      byte_ready = 1;
      }
    if (hexbyte == 0xd) {
      nbytes = 0;
      message_waiting = 1;
      } 
    else if (message_waiting == 0) {
      if (nbytes < 4) {
        message[nbytes] = hexbyte;
      }
      nbytes++;
    }
  hexbyte = 0;
  nbits = 0;
  }
}
Finally have a better understanding of the S:D ratio that is given on commercial non-contact temperature sensor units. The Harbour Freight one that I purchased states that it 6:1 spot to distance ratio, this means that at 6 inches the spot size (area read) is 1 inch and at 12 inches the spot size is 2 inches.

A good illustration (from Grainger, shows a 12:1 ratio):

Discussions