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.
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.

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 longdelay()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 pin | Arduino Nano pin | Notes |
|---|---|---|
| IR sensor VCC | 5V | Module works from 3.3–5 V |
| IR sensor GND | GND | Common ground |
| IR sensor OUT | D2 | LOW 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 (–) wire | Also the anode (plain end) of the 1N4007 |
| IRLZ44N SOURCE (right pin) | GND | Use a thick wire – carries the pump current |
| Pump red (+) wire | 5 V rail (adapter +) | 1N4007 stripe (cathode) also on this point |
| Green LED anode | D5 via 330 Ω | Cathode to GND – READY |
| Red LED anode | D6 via 330 Ω | Cathode to GND – dispensing / cooldown / refill blink |
| Active buzzer + | D8 | Buzzer – to GND |
| 5 V 2 A adapter + | Nano 5V pin and 5 V rail | Regulated 5 V only – do not feed 5 V into VIN |
| 5 V 2 A adapter – | GND rail | Common with Nano GND |
| 470 µF capacitor | Across 5 V and GND | Long 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.
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_MSuntil one dose is about 1 ml. KeepMAX_PUMP_MSlarger thanDOSE_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_DOSESto 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
| Problem | Fix |
|---|---|
| Pump never runs, but the red LED lights | Check 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 time | The 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 present | Reduce 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 starts | Add 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 out | Raise 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 dose | Lower the nozzle below the liquid level only slightly, or add a small one-way valve in the tube. |
| Red LED blinks slowly all the time | That 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 clone | Select 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.