Projects / Dispenser use counter

Dispenser use counter

Beginner1.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Wire the sensor

    VCC, GND, SDA to A4, SCL to A5.

  2. NANO

    Wire the display

    VCC, GND, SDA to A4, SCL to A5 on the same bus.

  3. NANO

    Wire the reset button

    D4 to GND through the button.

  4. NANO

    Upload the sketch

    The display shows the saved count, zero when new.

  5. NANO

    Hold your hand 10 cm from the sensor

    The count goes up by one.

  6. NANO

    Take your hand away, then bring it back

    It counts again.

  7. NANO

    Mount the sensor beside the nozzle, aimed at the space under it

    Test with a few real uses.

In short

How do you build a dispenser use counter?

A time-of-flight sensor beside a hand sanitiser pump counts how many times a hand comes under the nozzle and shows the total on an OLED. It records only a count, never images or names. You need Arduino Nano, VL53L0X time-of-flight distance sensor, SSD1306 OLED Display, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 90 minutes.

Level
Beginner
Time
About 90 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
VL53L0X VCCNano 5VPower (the usual breakout has a regulator, check yours accepts 5 V, otherwise use 3V3)
VL53L0X GNDNano GNDCommon ground
VL53L0X SDA and SCLNano A4 and A5I2C, address 0x29
OLED VCCNano 5VPower (check your OLED accepts 5 V, otherwise use 3V3)
OLED GNDNano GNDCommon ground
OLED SDA and SCLNano A4 and A5I2C, address 0x3C
Reset buttonNano D4 and GNDHold for two seconds to clear the count

Code

The VL53L0X measures distance with a tiny infrared laser. Aim it across the space under a pump dispenser so that, with nothing there, it reads the far wall or nothing at all, and a hand under the nozzle reads closer than fifteen centimetres. The sketch counts one use when a hand appears and waits until the hand has gone beyond twenty-two centimetres before it can count again. The total is saved in the Nano's EEPROM, so it survives a power cut, and a two-second hold of the reset button clears it. The unit stores only a number, with no photographs, no names and no times. Keep the electronics clear of splashes, and keep in mind that alcohol-based sanitiser is flammable and can damage plastics, so wipe away spills and do not let it reach the board. The count shows how busy a station is. It does not measure hygiene or protect anyone from infection.

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 <EEPROM.h>
#include <Adafruit_VL53L0X.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_VL53L0X lox = Adafruit_VL53L0X();
Adafruit_SSD1306 oled(128, 64, &Wire, -1);

const uint8_t RESET_PIN = 4;
const int NEAR_MM = 150;        // a hand under the nozzle is closer than this
const int FAR_MM = 220;         // hand gone when beyond this (a gap avoids double counts)

unsigned int uses = 0;
bool handPresent = false;
unsigned long resetHeldSince = 0;

void draw() {
  oled.clearDisplay();
  oled.setTextColor(SSD1306_WHITE);
  oled.setTextSize(1);
  oled.setCursor(0, 0);
  oled.print("Dispenser uses");
  oled.setTextSize(4);
  oled.setCursor(0, 22);
  oled.print(uses);
  oled.display();
}

void setup() {
  pinMode(RESET_PIN, INPUT_PULLUP);
  Wire.begin();
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
  if (!lox.begin()) { while (true) {} }
  EEPROM.get(0, uses);
  if (uses == 0xFFFF) uses = 0;
  draw();
}

void loop() {
  VL53L0X_RangingMeasurementData_t m;
  lox.rangingTest(&m, false);
  if (m.RangeStatus != 4) {                    // 4 means out of range
    int mm = m.RangeMilliMeter;
    if (!handPresent && mm < NEAR_MM) {
      handPresent = true;
      uses++;
      EEPROM.put(0, uses);
      draw();
    } else if (handPresent && mm > FAR_MM) {
      handPresent = false;
    }
  } else if (handPresent) {
    handPresent = false;
  }

  if (digitalRead(RESET_PIN) == LOW) {
    if (resetHeldSince == 0) resetHeldSince = millis();
    if (millis() - resetHeldSince > 2000) {
      uses = 0;
      EEPROM.put(0, uses);
      draw();
      resetHeldSince = millis() + 3000;
    }
  } else {
    resetHeldSince = 0;
  }
  delay(40);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Hand 10 cm awayCount plus one
Hand held for 5 secondsStill one
Hand removed and returnedAnother one
Unplug and plug inCount kept
Hold reset for 2 secondsZero

If it does not work

Counts with no hand

The sensor sees a close wall, so aim it at open space or lower NEAR_MM.

Never counts

Check the I2C wires, the 0x29 address and that the VL53L0X is powered.

Double counts

Raise FAR_MM or keep the sensor steady.

Display blank

Check the OLED address 0x3C.

Count lost

Make sure the sketch writes to EEPROM and that you did not hold reset.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the dispenser use counter?

Arduino Nano, VL53L0X time-of-flight distance sensor, SSD1306 OLED Display, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the dispenser use counter take to build?

About 90 minutes for a beginner maker, in 7 steps.

How do I know the dispenser use counter works?

Hand 10 cm away: Count plus one. Hand held for 5 seconds: Still one. Hand removed and returned: Another one.

Counts with no hand: what should I check?

The sensor sees a close wall, so aim it at open space or lower NEAR_MM.

Never counts: what should I check?

Check the I2C wires, the 0x29 address and that the VL53L0X is powered.

Double counts: what should I check?

Raise FAR_MM or keep the sensor steady.

Display blank: what should I check?

Check the OLED address 0x3C.