Code examples for TCRT5000 proximity detection

Basic version:

//
// Proximity sensing with TCRT5000
// written by Ted Burke - 8-11-2017
//

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);

  digitalWrite(2, HIGH);

  Serial.begin(9600);
}

void loop()
{
  int v, vth;

  v = analogRead(6);   // range is 0 to 1023
  vth = analogRead(7); // threshold voltage

  Serial.print(0);
  Serial.print(" ");
  Serial.print(1023);
  Serial.print(" ");
  Serial.print(vth);
  Serial.print(" ");
  Serial.println(v);

  if (v > vth)
  {
    digitalWrite(3, HIGH);
  }
  else
  {
    digitalWrite(3, LOW);
  }

  delay(100);
}

Background subtract:

//
// Proximity sensing with TCRT5000
// written by Ted Burke - 8-11-2017
//

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);

  Serial.begin(9600);
}

void loop()
{
  int v, vth, von, voff;

  voff = analogRead(6);   // range is 0 to 1023
  digitalWrite(2, HIGH);  // switch on IR LED
  delayMicroseconds(100); // 100 us delay to allow IR LED to light up
  von = analogRead(6);    // range is 0 to 1023
  digitalWrite(2, LOW);   // switch off LED

  v = von - voff;

  // Clamp v to the range 0 to 200
  if (v < 0) v = 0;
  if (v > 200) v = 200;
  
  vth = analogRead(7); // threshold voltage

  Serial.print(0);
  Serial.print(" ");
  Serial.print(200);
  Serial.print(" ");
  Serial.print(vth);
  Serial.print(" ");
  Serial.print(voff);
  Serial.print(" ");
  Serial.print(von);
  Serial.print(" ");
  Serial.println(v);

  if (v > vth)
  {
    digitalWrite(3, HIGH);
  }
  else
  {
    digitalWrite(3, LOW);
  }

  delay(100);
}

Beepz example:

//
// Proximity detection with TCRT5000
// Written by Ted Burke - 8-11-2017
//

void setup()
{
  pinMode(2, OUTPUT); // IR LED
  pinMode(3, OUTPUT); // green LED

  // Print sensor readings via serial
  Serial.begin(115200);

  // Audio output on pin D11
  pinMode(11, OUTPUT);
  TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
  TCCR2B = _BV(WGM22) | _BV(CS22);
  OCR2A = 180;
  OCR2B = 50;
}

long n = 0;
int v_on, v_off, previous_reflection, reflection, threshold;
long next_sample_time = 0;
float avg = 0;

void loop()
{
  // Wait for next sample time
  while(millis() < next_sample_time);
  next_sample_time = next_sample_time + 40;

  // Read on and off voltages from IR sensor
  threshold = analogRead(7) / 10;
  v_off = analogRead(6);
  digitalWrite(2, HIGH);
  delay(20);
  v_on = analogRead(6);
  digitalWrite(2, LOW);

  // Calculate reflection value and clamp to range 0-200
  previous_reflection = reflection;
  reflection = v_on - v_off;
  if (reflection < 0) reflection = 0;
  if (reflection > 200) reflection = 200;

  avg = 0.9*avg + 0.1*reflection;

  // Light the green LED if reflection is stronger than threshold
  if (avg > threshold)
  {
    digitalWrite(3, HIGH);
    OCR2A = 255 - avg;
  }
  else
  {
    digitalWrite(3, LOW);
    OCR2A = 0;
  }

  // Print readings
  Serial.print(0);
  Serial.print(" ");
  Serial.print(200);
  Serial.print(" ");
  Serial.print(threshold);
  Serial.print(" ");
  Serial.print(reflection);
  Serial.print(" ");
  Serial.println(avg);
}
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s