Projects / Three-step torch with a push button

Three-step torch with a push button

Beginner1.3 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO8.8 mA

    Wire the LED

    Long leg through 220 ohm to D9, short leg to GND.

  2. NANO

    Wire the button

    One side to D2, the other side to GND.

  3. NANO8.9 mA

    Upload the sketch

    LED is off.

  4. NANO

    Press once

    Low brightness.

  5. NANO

    Press again twice

    Medium, then high.

  6. NANO

    Press a fourth time

    SOS flashes in Morse.

  7. NANO

    Press a fifth time

    Torch turns off.

  8. NANO

    Leave it on at high

    It turns off after 30 minutes.

In short

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

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

Wiring

FromToWhy
White LED plus through 220 ohmNano D9PWM brightness
White LED minusNano GNDGround
Mode buttonNano D2 to GNDInternal pull-up, pressed reads LOW
Nano powerUSB power bankGives 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.

CheckExpected
Press onceDim light
Press twiceBrighter
Press three timesFull brightness
Press four timesThree short, three long, three short flashes
Press five timesOff

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 →

Ask Lark AI

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.