Hand-wave lamp dimmer with an HC-SR04 and PWM on Arduino: design worksheet and code

A ₹605 design worksheet for a touchless lamp dimmer: the height of your hand above an ultrasonic sensor sets the brightness, with the distance-to-PWM mapping worked out and smoothing to stop flicker.

About the author · Sources, testing and AI policy

  • Parts₹605
  • Bench time2–3 h
  • LevelBeginner
  • ReviewedNov 2026

A ₹605 lamp you dim with a hand movement. An ultrasonic sensor faces up, the sketch maps the distance from 5 cm to 30 cm onto a PWM level, and a low-voltage LED lamp follows your hand. A short smoothing filter keeps the light steady, and a hold gesture locks the level.

Scope: beginner. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. The mapping table was computed by script. This page drives a small LED, or a low-voltage LED strip through a logic-level MOSFET, and nothing on mains. Ultrasonic sensors have a blind zone close to the face and are less reliable on soft or angled surfaces, so hands held flat work best.

How it works

The sketch maps distance d to brightness with b = (d − 5) / 25 × 255, clamped to the range 0 to 255. At 5 cm the lamp is off, at 17.5 cm it is half bright and at 30 cm it is full. Beyond 35 cm the sketch treats the reading as no hand and keeps the last level.

Raw ultrasonic readings jump by a centimetre or two. An exponential filter, smoothed = 0.8 × smoothed + 0.2 × new, removes most flicker with a small lag. Holding the hand still within 1 cm for two seconds locks the level until the hand leaves, so you can step away.

Bill of materials

PartQtyUnitTotal
Arduino Nano (or compatible)1₹400₹400
HC-SR04 ultrasonic sensor1₹90₹90
White LED, 5 mm1₹3₹3
220 ohm resistor1₹2₹2
Breadboard1₹60₹60
Jumper wires1₹50₹50
Parts total₹605

Scroll the table sideways to see every column.

Prices checked Nov 2026. Shipping and tools are not included.

Prices are illustrative figures for Indian sellers in November 2026, before shipping, and have not been checked against current listings.

The numbers

Hand heightPWM (0 to 255)
5 cm0
10 cm51
15 cm102
20 cm153
25 cm204
30 cm255
PWM = (d − 5) / 25 × 255, clamped. Distances are example design values.

The eye sees brightness roughly on a curve rather than a straight line, so a straight PWM ramp looks too bright too early. Squaring the 0 to 1 level before scaling, level² × 255, gives a gentler rise at the dim end; try both and keep the one that looks right to you.

Power and wiring

PartConnects toNote
HC-SR04 VCC and GND5V and GND
HC-SR04 TRIG / ECHOD7 / D8
LED anode via 220 ΩD9 (PWM)Cathode to GND
LED strip optionD9 to a logic-level MOSFET gateUse the strip’s own supply and a common ground
Point the sensor straight up, clear of the ceiling above and of nearby objects.

The sketch

// Hand-wave dimmer: Nano + HC-SR04 + LED on a PWM pin
// Design worksheet: tune the near and far distances for your lamp.

const int TRIG = 7, ECHO = 8, LED = 9;
const float NEAR_CM = 5.0, FAR_CM = 30.0, GONE_CM = 35.0;
const float ALPHA = 0.2;                    // smoothing: higher reacts faster
const bool GAMMA = true;                    // gentler dim end

float smooth = FAR_CM;
float held = 0;
unsigned long stillSince = 0;
bool locked = false;
int level = 0;

float ping() {
  digitalWrite(TRIG, LOW);  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  unsigned long t = pulseIn(ECHO, HIGH, 25000UL);
  return t == 0 ? 999.0 : t / 58.3;
}

void setup() {
  pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT); pinMode(LED, OUTPUT);
}

void loop() {
  float d = ping();
  if (d < GONE_CM) {
    smooth = (1 - ALPHA) * smooth + ALPHA * d;
    if (fabs(smooth - held) > 1.0) { held = smooth; stillSince = millis(); locked = false; }
    else if (!locked && millis() - stillSince > 2000) locked = true;
    if (!locked) {
      float x = constrain((smooth - NEAR_CM) / (FAR_CM - NEAR_CM), 0.0, 1.0);
      if (GAMMA) x = x * x;
      level = (int)(x * 255);
    }
  } else {
    locked = false;                          // hand gone: keep the last level
  }
  analogWrite(LED, level);
  delay(40);
}

When the hand leaves, the lamp keeps its last level, so you can walk away. To switch the lamp off, bring the hand down to the near mark. The 25 ms timeout on pulseIn limits the sensor to about 4 m, and a missed echo returns 999 so it is treated as no hand. The sketch has been proofread but not compiled on this site.

Test plan

  1. Distance check: hold a flat card at 10, 20 and 30 cm and compare readings with a ruler.
  2. Smoothing: hold a hand still and see that the light does not flicker.
  3. Lock: hold still for two seconds and check the level holds.
  4. Gamma: compare GAMMA on and off in a dark room.
  5. Surfaces: try a flat hand, a fist and a sleeve.
  6. Stray objects: place a shelf nearby and check it is not detected.

What usually goes wrong

SymptomLikely causeFix
FlickersNo smoothing or noisy sensorLower ALPHA
Jumps to full when nothing is thereEcho from an object aboveMove the sensor or lower GONE_CM
Does not respond to a sleeveSoft surface absorbs soundUse a bare hand
Locks when you do not wantHold time too shortRaise the 2000 ms
LED always fullSensor wired wronglyPrint the distance

Upgrades

  • Colour: use an RGB strip and map height to hue.
  • Gestures: detect a quick wave to toggle the light.
  • Two sensors: one for brightness, one for warmth.
  • Wall switch: mount in a box beside the lamp.

Cost and parts notes

The ₹605 total is an illustration and does not include shipping. Use the cost calculator to compare sellers.

Common questions

Why does the sensor ignore my sleeve?

Soft cloth absorbs ultrasound, so a bare hand works best.

Why smooth the readings?

Ultrasonic distances jump by a centimetre or two, which would flicker the lamp.

Can it dim a mains lamp?

Not with this circuit. Mains dimming needs proper hardware and a qualified electrician.

Why square the level?

Eyes respond to brightness on a curve, so the dim end needs finer steps.

Last reviewed: November 2026. This is a design worksheet with illustrative costs. It has not been bench-tested by this site; library names, pins and limits must be checked against your board and the current library versions, and measurements from your build are welcome.

More builds

Next builds to try

Free: the final-year project pack

A shortlist with BOMs, a report outline that matches the usual chapter scheme, a costing sheet and 40 viva questions. Built for the February–April submission season.

The Build Note

One short email when something is worth your bench time.

A new build sheet, a part price that moved, or a mistake worth avoiding. No daily drip, no recycled news.

One short email when a build or a price is worth your time. Unsubscribe in one click.