Build it step by step
0 of 8 doneInstall the libraries
SSD1306 and GFX.
Wire the sensor
Vcc to 5V, GND to GND, Vo to A0.
Add the capacitor
100 uF across the sensor supply.
Wire the display
A4 and A5 with power.
Upload the sketch
A distance appears.
Measure a card
Hold it at 10, 20, 30 and 40 cm.
Record the raw values
Compare with the formula result.
Test too close
Move below 10 cm and see Out of range.
How do you build an analog IR distance meter?
An infrared distance sensor shows how its curve is not a straight line, and you linearise it with a formula. You need Arduino Nano, Sharp GP2Y0A21YK0F IR distance sensor, Electrolytic Capacitor 100 µF, SSD1306 OLED Display and Breadboard 830 Tie Points. It takes 8 steps and about 90 minutes.
- Level
- Beginner
- Time
- About 90 min
- Parts
- 5
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Sensor red wire (Vcc) | Nano 5V | Check the colours on your connector |
| Sensor black wire (GND) | Nano GND | Common ground |
| Sensor yellow wire (Vo) | Nano A0 | Analogue output |
| 100 uF capacitor | Sensor Vcc to GND | Long leg to Vcc, close to the sensor |
| OLED VCC | Nano 5V | Check the label on your module |
| OLED GND | Nano GND | Common ground |
| OLED SDA | Nano A4 | I2C data |
| OLED SCL | Nano A5 | I2C clock |
Code
A Sharp infrared distance sensor sends out a beam of infrared light and measures where the reflection lands, then gives an analogue voltage. The voltage is high when an object is close and low when it is far, and the curve is not a straight line, which is the main lesson of this project. The sketch converts the reading to centimetres with a power-law formula that fits this sensor model, valid from about 10 to 80 centimetres. Closer than 10 centimetres the voltage actually falls again, so a very close object looks far away, and the sketch shows Out of range when the voltage is outside the safe part of the curve. A 100 microfarad capacitor across the supply smooths the sharp current pulses the sensor draws, which otherwise make the readings jump. The median of nine readings removes the remaining spikes. To check the formula, place a flat card at 10, 20, 30, 40 and 60 centimetres from a ruler, note the raw values and compare. Sunlight and shiny or very dark surfaces change the reading, so test indoors.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const int SENSOR = A0;
int readMedian() {
int v[9];
for (int i = 0; i < 9; i++) { v[i] = analogRead(SENSOR); delay(5); }
for (int i = 0; i < 8; i++)
for (int j = i + 1; j < 9; j++)
if (v[j] < v[i]) { int t = v[i]; v[i] = v[j]; v[j] = t; }
return v[4]; // the middle value ignores spikes
}
void setup() {
Serial.begin(9600);
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println("OLED not found"); while (1) { } }
oled.setTextColor(SSD1306_WHITE);
}
void loop() {
int raw = readMedian();
float volts = raw * 5.0 / 1023.0;
// Common fit for the GP2Y0A21YK0F, valid from about 10 to 80 cm
float cm = 12343.85 * pow(raw, -1.15);
bool inRange = (volts > 0.4 && volts < 3.0);
oled.clearDisplay();
oled.setTextSize(2);
oled.setCursor(0, 0);
if (inRange) { oled.print(cm, 0); oled.print(" cm"); } else { oled.print("Out of range"); }
oled.setTextSize(1);
oled.setCursor(0, 36);
oled.print("Volts: "); oled.println(volts, 2);
oled.print("Raw: "); oled.println(raw);
oled.display();
Serial.print(raw); Serial.print(","); Serial.print(volts, 3); Serial.print(","); Serial.println(cm, 1);
delay(200);
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
512,2.502,9.5 511,2.498,9.5 512,2.502,9.5 513,2.507,9.4 512,2.502,9.5
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Card at 20 cm | About 20 cm shown |
| Card at 40 cm | About 40 cm shown |
| Card very close | Out of range |
| Nothing in front | Out of range or the far limit |
| Sunlight on the sensor | Readings less stable |
If it does not work
Readings jump
Add the capacitor close to the sensor.
Always out of range
Check the Vo wire on A0 and the supply.
Distances too large or small
Your sensor model has a different curve, fit your own.
Wrong colours of wire
Check the label on the connector.
Display blank
Try address 0x3D instead of 0x3C.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the analog IR distance meter?
Arduino Nano, Sharp GP2Y0A21YK0F IR distance sensor, Electrolytic Capacitor 100 µF, SSD1306 OLED Display and Breadboard 830 Tie Points.
How long does the analog IR distance meter take to build?
About 90 minutes for a beginner maker, in 8 steps.
How do I know the analog IR distance meter works?
Card at 20 cm: About 20 cm shown. Card at 40 cm: About 40 cm shown. Card very close: Out of range.
Readings jump: what should I check?
Add the capacitor close to the sensor.
Always out of range: what should I check?
Check the Vo wire on A0 and the supply.
Distances too large or small: what should I check?
Your sensor model has a different curve, fit your own.
Wrong colours of wire: what should I check?
Check the label on the connector.
