Built-in project · Arduino Uno R3

Object counter with an E18-D80NK on an Arduino Uno

An E18-D80NK infrared sensor on pin 2 with the pin’s pull-up: each object that passes adds one to the count on a 16x2 I2C LCD, with a beep. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner Runs in your browser. Free, and no account needed.

The parts retail for about $39. On Mokxi they're free.

Object counter with an E18-D80NK · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Object counter: an E18-D80NK infrared proximity sensor counts what passes
// it, on a 16x2 I2C LCD, with a beep from an active buzzer for each one.
//
//   2       the E18-D80NK's black wire, OUT (brown on 5 V, blue on GND)
//   8       the active buzzer's plus leg, its other leg on GND
//   LCD     SDA -> A4    SCL -> A5    VCC -> 5 V    GND -> GND
//
// The E18-D80NK sends a beam of modulated infrared out of its lens and looks
// for it coming back. Anything inside its range, set by the screw on its
// back (3 to 80 cm, 40 here), pulls OUT low. OUT is an NPN transistor's
// collector and nothing else: the sensor has no pull-up of its own, so the
// pin's INPUT_PULLUP holds it high while nothing is there. A loose input pin
// would count noise.
//
// One object is one high-to-low change on the pin, not every reading that is
// low, so the count goes up once however long a box stands in the beam. A
// box that rocks on the edge of the beam could still count twice, so a new
// count needs the pin to have been high for 50 ms first.
//
// Press Run, then drag the target closer than 40 cm on the sensor and back.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

const int SENSOR_PIN = 2;
const int BUZZER_PIN = 8;
const unsigned long CLEAR_MS = 50;  // the beam must be clear this long between objects

LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long count = 0;
bool present = false;
unsigned long lastBlocked = 0;
unsigned long beepUntil = 0;

void show() {
  lcd.setCursor(0, 1);
  lcd.print("Count: ");
  lcd.print(count);
  lcd.print("     ");
}

void setup() {
  pinMode(SENSOR_PIN, INPUT_PULLUP);
  pinMode(BUZZER_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Object counter");
  lcd.init();
  lcd.backlight();
  lcd.print("Object counter");
  show();
}

void loop() {
  unsigned long now = millis();
  bool blocked = digitalRead(SENSOR_PIN) == LOW;

  if (blocked) {
    // A new object: the beam was clear a moment ago, and for long enough.
    if (!present && now - lastBlocked >= CLEAR_MS) {
      count++;
      // The beep first: writing the LCD over I2C takes tens of milliseconds.
      digitalWrite(BUZZER_PIN, HIGH);
      beepUntil = now + 100;
      show();
      Serial.print("Object ");
      Serial.println(count);
    }
    present = true;
    lastBlocked = now;
  } else {
    present = false;
  }

  if (beepUntil != 0 && now >= beepUntil) {
    digitalWrite(BUZZER_PIN, LOW);
    beepUntil = 0;
  }
}

Parts list

5 parts, plus the jumper wires. They retail for about $39; on Mokxi they're free.

How it is wired

7 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Arduino Uno R3 pin GND; Buzzer pin 2
  • Arduino Uno R3 pin 8; Buzzer pin 1
  • Arduino Uno R3 pin 2; IR proximity switch, E18-D80NK pin OUT
  • Arduino Uno R3 pin 5V; IR proximity switch, E18-D80NK pin VCC; Character LCD with I2C backpack pin VCC
  • Ground: Arduino Uno R3 pin GND; IR proximity switch, E18-D80NK pin GND; Character LCD with I2C backpack pin GND
  • Arduino Uno R3 pin A4; Character LCD with I2C backpack pin SDA
  • Arduino Uno R3 pin A5; Character LCD with I2C backpack pin SCL

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.