PinoutSearch

SOS Morse Code Light (Arduino)

Blink an LED in Morse code (··· — — — ···). Learn to write and reuse your own functions.

Beginner15 min

What you need

Wiring

Hover a pin or a wire to trace the connection.

Code

sos.ino
// Blink "SOS" in Morse code on an LED.
const int ledPin = 13;
const int unit = 200;  // base time unit in ms

void dot()  { digitalWrite(ledPin, HIGH); delay(unit);     digitalWrite(ledPin, LOW); delay(unit); }
void dash() { digitalWrite(ledPin, HIGH); delay(unit * 3); digitalWrite(ledPin, LOW); delay(unit); }

void setup() { pinMode(ledPin, OUTPUT); }

void loop() {
  dot();  dot();  dot();        // S
  delay(unit * 2);
  dash(); dash(); dash();       // O
  delay(unit * 2);
  dot();  dot();  dot();        // S
  delay(unit * 6);              // pause before repeating
}

Steps

  1. Wire the LED on D13 through a 220Ω resistor, cathode to GND.
  2. Upload — the LED blinks S-O-S and repeats.
  3. Change `unit` to speed up or slow down the code.