Arduino Touchless Hand Sanitizer Dispenser with IR Sensor & Mini Pump

Build a touchless hand sanitizer dispenser with an Arduino Nano, an IR obstacle sensor and a 5 V mini pump driven by an IRLZ44N MOSFET: one measured dose per hand, a cooldown so a held hand never keeps pumping, a safety timeout and an EEPROM refill counter.

  • DifficultyEasy
  • Build time2–3 hours
  • Board / MCUArduino Nano / Uno (ATmega328P)
  • Parts14 items
  • CodeArduino C++

Need it ready-made? Buy Ready Project

Pressing a pump bottle with dirty hands defeats the point of sanitizing. This Arduino hand sanitizer dispenser is completely touchless: hold your hand under the nozzle, a small 5 V pump squirts one measured dose, and the dispenser waits until you take your hand away before it is ready again. It is a perfect beginner project for a home entrance, a classroom or a small shop, and it teaches three useful skills: reading a digital IR sensor, switching a motor with a logic-level MOSFET, and writing a non-blocking state machine.

An IR obstacle sensor detects the hand, an Arduino Nano times the dose, and an IRLZ44N MOSFET switches the mini submersible pump that sits inside the sanitizer bottle. A green LED shows ready, a red LED shows dispensing and later blinks when the bottle needs a refill, and a buzzer gives a short beep with every dose. The number of doses is saved in EEPROM, so the refill reminder survives power cuts.

Illustration of the Arduino touchless hand sanitizer dispenser: Arduino Nano with IR obstacle sensor, IRLZ44N MOSFET pump driver with flyback diode, 5 V mini pump, green and red LEDs, buzzer and 5 V 2 A adapter
Illustration of the touchless sanitizer dispenser build (pin labels are approximate – use the wiring table and the pin-accurate diagram below for the real connections).

How It Works

  • Hand detection: the IR module sends out infrared light and measures the reflection. When a hand is within its range (set with the blue trim pot, about 3–8 cm works well), its OUT pin goes LOW. The sketch only accepts a hand after the signal has stayed LOW for 120 ms, which filters out flicker, sunlight spikes and a hand quickly passing by.
  • Measured dose: once a hand is confirmed, D9 goes HIGH and the MOSFET turns the pump on for DOSE_MS (400 ms by default, roughly 0.8–1 ml with a typical 5 V mini pump). The pump then stops on its own, no matter how long the hand stays.
  • One dose per hand: after a dose the dispenser waits in a wait-for-removal state. Only after the hand has been gone for 120 ms and a 1.2 s cooldown has passed does it become ready again. Holding your hand under the nozzle therefore never empties the bottle.
  • Safety timeout: MAX_PUMP_MS (1.5 s) is a hard limit on any single pump run, and a 10 kΩ pull-down on the MOSFET gate keeps the pump off while the Nano boots or is being programmed.
  • MOSFET switch: the IRLZ44N is a logic-level MOSFET, so a 5 V Arduino pin turns it fully on. It sits on the low side: the pump’s minus wire goes to the drain, the source goes to ground. The 1N4007 flyback diode across the pump absorbs the voltage spike the motor produces when it switches off, protecting the MOSFET and the Arduino.
  • Dose counter and refill reminder: every dose increments a counter stored in EEPROM (EEPROM.put only rewrites changed bytes). After 450 doses – about one 500 ml bottle – the red LED blinks slowly. After refilling, hold your hand in front of the sensor while switching on for 3 seconds: three beeps confirm the counter is reset.
  • Non-blocking code: the loop uses millis() and four states (READY, DISPENSING, WAIT_REMOVE, COOLDOWN) instead of long delay() calls, so LEDs, buzzer and sensor are always handled on time.

Components Required

  • Arduino Nano (or Uno) with USB cable – 1
  • IR obstacle avoidance sensor module (FC-51 / TCRT5000 type, 3-pin: VCC, GND, OUT) – 1
  • IRLZ44N logic-level N-channel MOSFET – 1
  • 220 Ω resistor (MOSFET gate) – 1
  • 10 kΩ resistor (gate pull-down) – 1
  • 1N4007 diode (flyback across the pump) – 1
  • 3–6 V DC mini submersible pump with silicone tube (about 5 mm) – 1
  • Green 5 mm LED (ready) – 1
  • Red 5 mm LED (dispensing / cooldown / refill) – 1
  • 330 Ω resistors for the LEDs – 2
  • 5 V active buzzer – 1
  • 5 V 2 A DC adapter with DC jack breakout – 1
  • 470 µF 10 V electrolytic capacitor (across the 5 V rail) – 1
  • Sanitizer bottle (about 500 ml), nozzle, enclosure, breadboard/perfboard and jumper wires – 1 set

Wiring Table

Module pinArduino Nano pinNotes
IR sensor VCC5VModule works from 3.3–5 V
IR sensor GNDGNDCommon ground
IR sensor OUTD2LOW when a hand is detected (module has its own pull-up)
IRLZ44N GATE (left pin)D9 via 220 Ω10 kΩ from GATE to GND (pull-down)
IRLZ44N DRAIN (middle pin)Pump black (–) wireAlso the anode (plain end) of the 1N4007
IRLZ44N SOURCE (right pin)GNDUse a thick wire – carries the pump current
Pump red (+) wire5 V rail (adapter +)1N4007 stripe (cathode) also on this point
Green LED anodeD5 via 330 ΩCathode to GND – READY
Red LED anodeD6 via 330 ΩCathode to GND – dispensing / cooldown / refill blink
Active buzzer +D8Buzzer – to GND
5 V 2 A adapter +Nano 5V pin and 5 V railRegulated 5 V only – do not feed 5 V into VIN
5 V 2 A adapter –GND railCommon with Nano GND
470 µF capacitorAcross 5 V and GNDLong leg (+) to 5 V, close to the pump

Tip: if you keep the Nano on USB while testing, still connect the adapter’s minus to the Nano GND, and do not connect the adapter’s plus to the Nano 5V pin at the same time as USB unless your Nano has a protection diode on USB power.

Connection Diagram

This pin-accurate diagram is the authoritative wiring for the project; it matches the wiring table and the pin numbers in the code exactly.

Arduino Touchless Sanitizer Dispenser – Pin-Accurate Wiring Arduino Nano D2 IR sensor OUT (LOW = hand) D5 Green LED + 330 ohm to GND (READY) D6 Red LED + 330 ohm to GND (BUSY / REFILL) D8 Active buzzer +, buzzer – to GND D9 220 ohm to IRLZ44N GATE (10k gate to GND) 5V 5 V rail from 2 A adapter + (IR VCC, pump +) GND Common GND (adapter -, IR GND, MOSFET source) Pump driver (low-side switch): 5V + -> pump red (+) | pump black (-) -> IRLZ44N DRAIN (middle pin) | SOURCE (right pin) -> GND | GATE (left pin) <- 220 ohm <- D9 1N4007 across the pump: stripe (cathode) to 5V +, other end to the DRAIN. 10k from GATE to GND keeps the pump off at boot. 470 uF capacitor across 5V and GND near the pump. Power the Nano from the same 5 V supply into its 5V pin (not VIN), or by USB with GND shared.
Pin-accurate connection diagram for the Arduino touchless hand sanitizer dispenser.

Power Supply Notes

  • A 5 V mini submersible pump draws about 150–300 mA when running and much more for a moment when it starts. A 5 V 2 A adapter gives plenty of headroom for the pump, the Nano and the sensor.
  • Use one common ground for the adapter, the Nano, the sensor and the MOSFET source. Without a shared ground the MOSFET cannot be switched by D9.
  • The 470 µF capacitor smooths the dip when the pump starts, so the Nano does not reset and the IR sensor does not trigger falsely.
  • Keep the electronics above and away from the liquid. Alcohol-based sanitizer is flammable: no open flames nearby, and seal the wire entry into the bottle.

Code

Open the Arduino IDE, select Arduino Nano (processor ATmega328P, or ATmega328P Old Bootloader for many clone boards) or Arduino Uno, paste the sketch and upload. No extra libraries are needed – EEPROM.h is part of the AVR core. The sketch was compile-checked for the Nano and the Uno (about 3.7 KB flash, 206 bytes RAM). All timing values are at the top of the sketch, so you can tune the dose without touching the logic.

/*
  Arduino Touchless Hand Sanitizer Dispenser with IR Sensor & Mini Pump
  Arduino Project Hub - project #75 (original code)

  Board : Arduino Nano / Uno (ATmega328P)
  Pins  :
    D2  <- IR obstacle sensor OUT (LOW = hand detected)
    D9  -> IRLZ44N gate through 220 ohm (10k gate-to-GND pull-down)
    D5  -> green LED + 330 ohm  (READY)
    D6  -> red LED + 330 ohm    (DISPENSING / COOLDOWN / REFILL blink)
    D8  -> active buzzer +      (short beep per dose)

  Behaviour:
    - A hand must be seen steadily for DETECT_MS (debounce) before a dose.
    - The pump runs for DOSE_MS, then stops even if the hand stays.
    - The hand has to be removed (and COOLDOWN_MS must pass) before the
      next dose, so holding a hand under the nozzle never keeps pumping.
    - MAX_PUMP_MS is a hard safety limit on any single pump run.
    - A dose counter is kept in EEPROM; after REFILL_DOSES doses the red
      LED blinks as a refill reminder. Hold a hand under the sensor for
      RESET_HOLD_MS right after power-up to reset the counter (after refill).
*/

#include <EEPROM.h>

// ----- pin plan -----
const uint8_t PIN_IR     = 2;   // IR sensor OUT, active LOW
const uint8_t PIN_PUMP   = 9;   // MOSFET gate
const uint8_t PIN_GREEN  = 5;   // ready LED
const uint8_t PIN_RED    = 6;   // busy / refill LED
const uint8_t PIN_BUZZER = 8;   // active buzzer

// ----- adjustable settings -----
const unsigned long DETECT_MS     = 120;   // hand must be stable this long
const unsigned long DOSE_MS       = 400;   // pump on-time per dose (≈0.8-1 ml with a 5 V mini pump)
const unsigned long MAX_PUMP_MS   = 1500;  // absolute safety limit per run
const unsigned long COOLDOWN_MS   = 1200;  // minimum gap between doses
const unsigned long BEEP_MS       = 60;    // beep length at start of dose
const unsigned long RESET_HOLD_MS = 3000;  // hold at power-up to reset counter
const uint16_t      REFILL_DOSES  = 450;   // ~500 ml bottle / ~1 ml per dose

const int EE_ADDR = 0;          // EEPROM: 2-byte magic + 2-byte counter
const uint16_t EE_MAGIC = 0xA75A;

enum State { READY, DISPENSING, WAIT_REMOVE, COOLDOWN };
State state = READY;

uint16_t doseCount = 0;
unsigned long stateStart = 0;   // when the current state began
unsigned long handSince = 0;    // when the hand was first seen (0 = none)

// ---------- helpers ----------
bool handRaw() { return digitalRead(PIN_IR) == LOW; }

// Debounced hand detection: true only after DETECT_MS of continuous LOW
bool handPresent() {
  if (handRaw()) {
    if (handSince == 0) handSince = millis() | 1;   // |1 keeps it non-zero
    return millis() - handSince >= DETECT_MS;
  }
  handSince = 0;
  return false;
}

// Debounced "hand gone": sensor HIGH for DETECT_MS
bool handGone() {
  static unsigned long goneSince = 0;
  if (!handRaw()) {
    if (goneSince == 0) goneSince = millis() | 1;
    if (millis() - goneSince >= DETECT_MS) { goneSince = 0; return true; }
  } else {
    goneSince = 0;
  }
  return false;
}

void pump(bool on) { digitalWrite(PIN_PUMP, on ? HIGH : LOW); }

void loadCounter() {
  uint16_t magic;
  EEPROM.get(EE_ADDR, magic);
  if (magic != EE_MAGIC) {            // first run: initialise EEPROM
    doseCount = 0;
    EEPROM.put(EE_ADDR, EE_MAGIC);
    EEPROM.put(EE_ADDR + 2, doseCount);
  } else {
    EEPROM.get(EE_ADDR + 2, doseCount);
  }
}

void saveCounter() { EEPROM.put(EE_ADDR + 2, doseCount); }  // put() only writes changed bytes

bool refillNeeded() { return doseCount >= REFILL_DOSES; }

void setState(State s) { state = s; stateStart = millis(); }

void beep(uint8_t times, unsigned int len) {
  for (uint8_t i = 0; i < times; i++) {
    digitalWrite(PIN_BUZZER, HIGH); delay(len);
    digitalWrite(PIN_BUZZER, LOW);  delay(len);
  }
}

// LEDs: green = ready, red = busy; red blinks slowly when refill is due
void updateLeds() {
  bool busy = (state != READY);
  digitalWrite(PIN_GREEN, busy ? LOW : HIGH);
  if (busy) {
    digitalWrite(PIN_RED, HIGH);
  } else if (refillNeeded()) {
    digitalWrite(PIN_RED, (millis() / 500) % 2);
  } else {
    digitalWrite(PIN_RED, LOW);
  }
}

// ---------- setup ----------
void setup() {
  pinMode(PIN_PUMP, OUTPUT);
  pump(false);                        // pump OFF before anything else
  pinMode(PIN_IR, INPUT);             // module has its own pull-up
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_RED, OUTPUT);
  pinMode(PIN_BUZZER, OUTPUT);
  Serial.begin(9600);

  loadCounter();

  // Counter reset: keep a hand in front of the sensor during power-up
  unsigned long t0 = millis();
  while (handRaw() && millis() - t0 < RESET_HOLD_MS) {
    digitalWrite(PIN_RED, (millis() / 150) % 2);
  }
  if (handRaw()) {
    doseCount = 0;
    saveCounter();
    beep(3, 80);
    Serial.println(F("Dose counter reset"));
    while (handRaw()) { }             // wait until the hand is removed
  }
  digitalWrite(PIN_RED, LOW);

  beep(1, 100);
  Serial.print(F("Sanitizer ready. Doses so far: "));
  Serial.println(doseCount);
  setState(READY);
}

// ---------- main loop (non-blocking state machine) ----------
void loop() {
  unsigned long now = millis();

  switch (state) {
    case READY:
      if (handPresent()) {
        pump(true);
        digitalWrite(PIN_BUZZER, HIGH);
        setState(DISPENSING);
      }
      break;

    case DISPENSING: {
      unsigned long on = now - stateStart;
      if (on >= BEEP_MS) digitalWrite(PIN_BUZZER, LOW);
      if (on >= DOSE_MS || on >= MAX_PUMP_MS) {
        pump(false);
        digitalWrite(PIN_BUZZER, LOW);
        doseCount++;
        saveCounter();
        Serial.print(F("Dose #"));
        Serial.println(doseCount);
        if (doseCount == REFILL_DOSES) Serial.println(F("Refill reminder!"));
        setState(WAIT_REMOVE);
      }
      break;
    }

    case WAIT_REMOVE:                  // no second dose while hand stays
      pump(false);
      if (handGone()) setState(COOLDOWN);
      break;

    case COOLDOWN:
      pump(false);
      if (now - stateStart >= COOLDOWN_MS) {
        handSince = 0;
        setState(READY);
      }
      break;
  }

  updateLeds();
}

Calibration and Testing

  • Test without liquid first: power the circuit with the pump dry or in a glass of water. Open the Serial Monitor at 9600 baud: you should see “Sanitizer ready” and a “Dose #” line for each hand.
  • Sensor range: turn the IR module’s trim pot until its on-board LED lights only when your hand is about 3–8 cm below the nozzle. Too much range makes it trigger on the wall or the floor; too little makes it miss small hands.
  • Dose size: collect 10 doses in a measuring cup and divide by 10. Raise or lower DOSE_MS until one dose is about 1 ml. Keep MAX_PUMP_MS larger than DOSE_MS.
  • Prime the tube: the first few doses after filling only push air out of the tube. Keep the tube short and the nozzle a little higher than the pump outlet, but not much higher than the liquid, so it does not drip.
  • Refill counter: set REFILL_DOSES to your bottle volume divided by your measured dose. After refilling, hold your hand in front of the sensor while switching on until you hear three beeps.
  • Hold test: keep your hand under the nozzle – exactly one dose should come out. Remove it, wait a second and present it again for the next dose.

Troubleshooting

ProblemFix
Pump never runs, but the red LED lightsCheck the MOSFET pin order (Gate–Drain–Source from the left, text facing you), the common ground and that the pump + is on the 5 V rail. Make sure you used a logic-level IRLZ44N, not an IRFZ44N.
Pump runs all the timeThe MOSFET drain and source are swapped or the MOSFET is damaged. Check the 10 kΩ gate pull-down and replace the MOSFET if it was used without the flyback diode.
Dispenses with no hand presentReduce the IR sensor range with its trim pot, shield it from direct sunlight, and point it away from shiny surfaces.
Arduino resets when the pump startsAdd the 470 µF capacitor across 5 V and GND, use a 2 A adapter and thicker wires for the pump path.
Only a few drops come outRaise DOSE_MS, shorten the tube, remove kinks, or check that the pump is fully under the liquid. Thick gel sanitizer needs a liquid type – mini pumps cannot move gel.
Sanitizer drips after a doseLower the nozzle below the liquid level only slightly, or add a small one-way valve in the tube.
Red LED blinks slowly all the timeThat is the refill reminder. Refill and reset the counter by holding your hand in front of the sensor at power-up.
Upload fails on a Nano cloneSelect Processor: ATmega328P (Old Bootloader) and install the CH340 USB driver.

Ideas to Extend

  • Add an HC-SR04 ultrasonic sensor instead of the IR module for a more stable detection distance in bright places.
  • Add a 0.96 inch OLED to show total doses and a bottle level bar.
  • Add a non-contact liquid level sensor (XKC-Y25) on the bottle to detect an empty bottle directly.
  • Run it from a 2S Li-ion pack with a buck converter for a portable dispenser.

Leave a Comment