Build it step by step
0 of 8 doneWire the touch module
VCC, GND and I/O to D2.
Wire the LEDs
Three in parallel, each through 330 ohm, to D9.
Upload the sketch
The lamp starts off.
Touch the pad
Dim.
Touch again
Medium, then bright, then off.
Unplug and replug
The last level returns.
Hide it behind card
The pad still works through thin card.
Try a hold
One press gives one step only.
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
- Development BoardsBuyArduino Nano × 1
- SensorsBuy
- OptoelectronicsBuyLED 5 mm white × 3
- PassivesBuyResistor 330 Ω × 3
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Touch module VCC | Nano 5V | Power |
| Touch module GND | Nano GND | Common ground |
| Touch module I/O | Nano D2 | Goes high while touched |
| Three white LEDs in parallel | Nano D9 | Each LED has its own 330 ohm resistor |
| LED cathodes | Nano GND | Common 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.
| Check | Expected |
|---|---|
| First power up | Lamp off |
| One touch | Dim |
| Four touches | Back to off |
| Power cycle | Same level as before |
| Hold the pad | Only 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 →
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.
