Three-step torch with a push button
Build it step by step
0 of 8 doneWire the LED
Long leg through 220 ohm to D9, short leg to GND.
Wire the button
One side to D2, the other side to GND.
Upload the sketch
LED is off.
Press once
Low brightness.
Press again twice
Medium, then high.
Press a fourth time
SOS flashes in Morse.
Press a fifth time
Torch turns off.
Leave it on at high
It turns off after 30 minutes.
How do you build a three-step torch with a push button?
A single button steps a torch LED through low, medium, high, SOS flash and off. You need Arduino Nano, LED 5 mm white, Tactile push button 6 mm, Resistor 220 Ω, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 80 minutes.
- Level
- Beginner
- Time
- About 80 min
- Parts
- 6
Parts list
- Development BoardsBuyArduino Nano × 1
- OptoelectronicsBuyLED 5 mm white × 1
- SwitchesBuy
- PassivesBuyResistor 220 Ω × 1
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| White LED plus through 220 ohm | Nano D9 | PWM brightness |
| White LED minus | Nano GND | Ground |
| Mode button | Nano D2 to GND | Internal pull-up, pressed reads LOW |
| Nano power | USB power bank | Gives a portable torch |
Code
Each press of the button moves a counter through five modes, wrapping back to off after SOS. A press is counted on the falling edge, when the button goes from HIGH to LOW, plus a short delay for debounce, so holding the button down does not skip modes. The three brightness steps use analogWrite values of 20, 90 and 255. Because the eye is more sensitive to small changes in dim light, the steps are far apart in number but look about evenly spaced. The SOS mode steps through a table of durations in units, alternating on and off, using millis() so the button stays responsive during the flash. After 30 minutes in any lit mode the torch switches itself off to save the battery. A white LED with a 220 ohm resistor draws about 10 milliamps, so a power bank will run it for a very long time, though some power banks switch themselves off at such a low load.
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).
const uint8_t LED = 9;
const uint8_t BTN = 2;
// Modes: 0 off, 1 low, 2 medium, 3 high, 4 SOS flash
uint8_t mode = 0;
bool lastBtn = HIGH;
unsigned long modeStart = 0; // when the current mode began
const unsigned long AUTO_OFF_MS = 30UL * 60UL * 1000UL; // 30 minutes
// SOS in Morse: dot = 1 unit, dash = 3 units, gaps follow the standard timing.
// Each number is a duration in units, alternating on, off, on, off, ...
const uint8_t SOS[] = {1,1,1,1,1,3, 3,1,3,1,3,3, 1,1,1,1,1,7};
const uint16_t UNIT_MS = 180;
void showSOS() {
static uint8_t step = 0;
static unsigned long until = 0;
static bool on = false;
if (millis() >= until) {
on = (step % 2 == 0);
analogWrite(LED, on ? 255 : 0);
until = millis() + (unsigned long)SOS[step] * UNIT_MS;
step = (step + 1) % sizeof(SOS);
}
}
void setup() {
pinMode(LED, OUTPUT);
pinMode(BTN, INPUT_PULLUP);
}
void loop() {
bool b = digitalRead(BTN);
if (lastBtn == HIGH && b == LOW) { // new press
mode = (mode + 1) % 5;
modeStart = millis();
delay(30); // debounce
}
lastBtn = b;
if (mode != 0 && millis() - modeStart > AUTO_OFF_MS) mode = 0; // auto off
switch (mode) {
case 0: analogWrite(LED, 0); break;
case 1: analogWrite(LED, 20); break;
case 2: analogWrite(LED, 90); break;
case 3: analogWrite(LED, 255); break;
case 4: showSOS(); break;
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Press once | Dim light |
| Press twice | Brighter |
| Press three times | Full brightness |
| Press four times | Three short, three long, three short flashes |
| Press five times | Off |
If it does not work
LED never lights
Check the LED direction, the long leg goes to the resistor side.
Presses skip a mode
Increase the 30 ms debounce, or check for a loose button leg.
Brightness looks the same at medium and high
Raise the medium value from 90 to 120.
SOS looks wrong
Check the SOS table has the full eighteen numbers.
Nano resets when you press
Re-seat the jumper wires, there may be a loose connection.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the three-step torch with a push button?
Arduino Nano, LED 5 mm white, Tactile push button 6 mm, Resistor 220 Ω, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the three-step torch with a push button take to build?
About 80 minutes for a beginner maker, in 8 steps.
How do I know the three-step torch with a push button works?
Press once: Dim light. Press twice: Brighter. Press three times: Full brightness.
LED never lights: what should I check?
Check the LED direction, the long leg goes to the resistor side.
Presses skip a mode: what should I check?
Increase the 30 ms debounce, or check for a loose button leg.
Brightness looks the same at medium and high: what should I check?
Raise the medium value from 90 to 120.
SOS looks wrong: what should I check?
Check the SOS table has the full eighteen numbers.
