Projects / Three-step touch lamp

Three-step touch lamp

Beginner1 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANOTTP223

    Wire the touch module

    VCC, GND and I/O to D2.

  2. NANO15.0 mA

    Wire the LEDs

    Three in parallel, each through 330 ohm, to D9.

  3. NANO

    Upload the sketch

    The lamp starts off.

  4. NANOTTP223

    Touch the pad

    Dim.

  5. NANOTTP223

    Touch again

    Medium, then bright, then off.

  6. NANO

    Unplug and replug

    The last level returns.

  7. NANO

    Hide it behind card

    The pad still works through thin card.

  8. NANO

    Try a hold

    One press gives one step only.

In short

How do you build a three-step touch lamp?

A touch pad cycles a lamp through off, dim, medium and bright, like a bedside lamp. You need Arduino Nano, Touch sensor module (TTP223), LED 5 mm white × 3, Resistor 330 Ω × 3 and Breadboard 830 Tie Points. It takes 8 steps and about 60 minutes.

Level
Beginner
Time
About 60 min
Parts
5

Parts list

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

Wiring

FromToWhy
Touch module VCCNano 5VPower
Touch module GNDNano GNDCommon ground
Touch module I/ONano D2Goes high while touched
Three white LEDs in parallelNano D9Each LED has its own 330 ohm resistor
LED cathodesNano GNDCommon ground

Code

The TTP223 touch module detects the capacitance of a finger through a thin layer of plastic or glass and gives a digital high output while it is touched. The sketch counts each new touch as one press and steps through four states: off, dim, medium and bright, and then back to off. Between steps the brightness fades smoothly with PWM so the lamp does not jump. The last state is saved in the Nano memory so the lamp comes back the same way after a power cut. The memory is written only when the state changes, because it wears out after about a hundred thousand writes, which is plenty for a lamp but would not be for a loop. Three white LEDs share one pin, and each has its own resistor, which keeps the current on the pin at about 18 milliamps in total. Do not add more LEDs to the same pin: use a transistor or a MOSFET for a bigger lamp. The module can be hidden behind a thin piece of acrylic or card, and the touch area is the pad on the board.

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

const int TOUCH = 2, LAMP = 9;
const int LEVELS[4] = {0, 25, 100, 255};      // off, dim, medium, bright
int state = 0;
int current = 0;
bool wasTouched = false;
unsigned long lastChange = 0;

void fadeTo(int target) {
  while (current != target) {
    current += (target > current) ? 1 : -1;
    analogWrite(LAMP, current);
    delay(2);
  }
}

void setup() {
  pinMode(TOUCH, INPUT);
  pinMode(LAMP, OUTPUT);
  state = EEPROM.read(0);
  if (state > 3) state = 0;                   // first run or bad value
  fadeTo(LEVELS[state]);
}

void loop() {
  bool touched = digitalRead(TOUCH);
  if (touched && !wasTouched && millis() - lastChange > 300) {
    state = (state + 1) % 4;
    fadeTo(LEVELS[state]);
    EEPROM.update(0, state);                  // remember the setting, written only on a change
    lastChange = millis();
  }
  wasTouched = touched;
}

Test checklist

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

CheckExpected
First power upLamp off
One touchDim
Four touchesBack to off
Power cycleSame level as before
Hold the padOnly one step

If it does not work

Lamp never changes

Check the I/O wire on D2 and the module power.

Steps twice per touch

Increase the 300 ms delay.

Lamp flickers

Check the LED wiring and ground.

Starts bright every time

The memory holds an old value, touch through the cycle once.

Pad too sensitive

Move the wires away from the pad.

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

Ask Lark AI

Common questions

What parts do I need for the three-step touch lamp?

Arduino Nano, Touch sensor module (TTP223), LED 5 mm white × 3, Resistor 330 Ω × 3 and Breadboard 830 Tie Points.

How long does the three-step touch lamp take to build?

About 60 minutes for a beginner maker, in 8 steps.

How do I know the three-step touch lamp works?

First power up: Lamp off. One touch: Dim. Four touches: Back to off.

Lamp never changes: what should I check?

Check the I/O wire on D2 and the module power.

Steps twice per touch: what should I check?

Increase the 300 ms delay.

Lamp flickers: what should I check?

Check the LED wiring and ground.

Starts bright every time: what should I check?

The memory holds an old value, touch through the cycle once.