Visualize TCRT5000 IR Sensor on OLED with a Real-Time Bar Graph

This simple yet powerful project uses a TCRT5000 IR Reflective Sensor and an SSD1306 OLED display to visualize distance or surface reflectivity as a horizontal bar graph in real time.

As objects move closer to the sensor, the bar grows longer — giving you instant feedback. This is great for proximity sensing, object tracking, or even basic gesture interfaces.

PCBWay offers high-quality PCB prototyping and assembly at an affordable price, starting at just $5 for 5 PCBs. With fast turnaround, great customer support, and easy online ordering, it’s a top choice for hobbyists and professionals alike.

Hardware Components

You’ll need the following hardware components to get started:

ComponentModel / TypeQty
Arduino UNOR3 Board1
TCRT5000 IR SensorReflective IR Module1
OLED DisplaySSD1306 128×64 I2C (0.96″)1
Jumper WiresMale-to-male1 set
BreadboardOptional1
USB CableFor Arduino1

Schematic

Make connections according to the circuit diagram given below.

Wiring / Connections

ComponentArduino UNONotes
OLED VCC5V or 3.3VPower for display
OLED GNDGNDGround
OLED SDAA4I2C Data
OLED SCLA5I2C Clock
TCRT5000 VCC5VPower for sensor
TCRT5000 GNDGNDGround
TCRT5000 A0 / OUTA0Analog sensor output

Arduino Code

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET    -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const int sensorPin = A0; // IR sensor analog output pin

void setup() {
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("OLED init failed"));
    for (;;);
  }
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
}

void loop() {
  int sensorValue = analogRead(sensorPin);
  int barLength = map(sensorValue, 640, 30, 0, 120); // Closer = longer bar
  barLength = constrain(barLength, 0, 120);

  display.clearDisplay();

  // Draw bar graph
  display.fillRect(4, 30, barLength, 10, SSD1306_WHITE);
  
  // Display numeric value
  display.setCursor(4, 10);
  display.setTextSize(1);
  display.print("IR Value: ");
  display.println(sensorValue);

  // Optional scale lines
  for (int i = 0; i <= 120; i += 20) {
    display.drawLine(4 + i, 28, 4 + i, 42, SSD1306_WHITE);
  }

  display.display();
  delay(100);
}

This program reads analog values from the TCRT5000 IR sensor and converts them into a visual bar graph on the SSD1306 OLED screen.

Key Features:

  • Live bar graph that grows as objects get closer
  • Text display showing the exact IR value
  • Optional scale markers every 20 pixels

Code Walkthrough

int sensorValue = analogRead(sensorPin);
  • Reads analog output from the IR sensor.
int barLength = map(sensorValue, 640, 30, 0, 120);
  • Maps sensor values (from dark to reflective surfaces) into a bar range (0 to 120 px). Higher reflectivity (closer object) gives a longer bar.
barLength = constrain(barLength, 0, 120);
  • Ensures the bar doesn’t go out of bounds.
display.fillRect(4, 30, barLength, 10, SSD1306_WHITE);
  • Draws the bar horizontally on screen.
display.setCursor(4, 10);
display.print("IR Value: ");
  • Prints the numeric sensor value above the bar.
for (int i = 0; i <= 120; i += 20) {
display.drawLine(4 + i, 28, 4 + i, 42, SSD1306_WHITE);
}

Draws vertical ticks every 20 pixels to serve as visual reference markers.

Conclusion

This project is perfect for learning about analog sensor input and real-time data visualization. It’s minimal in components but rich in insight!