Skip to content
KC3SMW
KC3SMW

Adventures in Ham Radio

  • Welcome
  • HF Propagation
  • Posts
  • Packet Radio Maps
  • Contact
KC3SMW

Adventures in Ham Radio

Tinker’s Corner: Playing with Time – LED Patterns Using FOR Loops

admin, July 3, 2025July 3, 2025

In our recent PMRC RadioLabs session, we explored the basic Blink sketch, where an LED turned on and off at a fixed interval. Now, let’s take it further by experimenting with different timing patterns using the FOR loop.

In Arduino programming, loops help us repeat tasks, making our code more efficient. Instead of writing the same lines of code multiple times, we use a FOR loop to handle repetition automatically.

Understanding setup() and loop()

Every Arduino program consists of two main functions:

setup() – Runs Once

This function runs only once when the Arduino starts. It’s where we initialize settings, such as telling the Arduino which pins to use for inputs and outputs.

void setup() {

  pinMode(13, OUTPUT);  // Set pin 13 as an output

}

loop() – Runs Continuously

The loop() function repeats forever, allowing our Arduino to react and respond indefinitely.

void loop() {

  // This is where we write code that repeats

}

Wiring the Circuit

Since we are using an external LED, we need to connect it properly to the Arduino.

Wiring Components:

ComponentArduino Pin
220Ω ResistorBetween Pin 13 and LED (long leg +)
LED (long leg + / anode)Connected to the resistor
LED (short leg – / cathode)Connected to GND (Ground)

The 220Ω resistor limits the current flowing through the LED, preventing damage.

Understanding the FOR Loop

A FOR loop repeats a block of code a set number of times. Here’s how it’s structured:

for (initialization; condition; increment) {

  // Code to repeat

}

PartWhat It Does
initializationSets a starting value (e.g., int i = 0;).
conditionThe loop keeps running while this is true (e.g., i < 3).
incrementChanges the variable each loop (i++ increases i by 1).

Now, let’s use FOR loops to create different LED timing patterns.

Blinking “SOS” in Morse Code

One of the most famous timing patterns is SOS in Morse Code (… — …). Using a FOR loop, we can blink the LED in a structured and efficient way.

const int ledPin = 13;

void setup() {

  pinMode(ledPin, OUTPUT);

}

void loop() {

  // Short blinks (S) - 3 times

  for (int i = 0; i < 3; i++) {

    digitalWrite(ledPin, HIGH);  // LED ON

    delay(200);                  // Short blink

    digitalWrite(ledPin, LOW);   // LED OFF

    delay(200);

  }

  delay(600); // Pause between "S" and "O"

  // Long blinks (O) - 3 times

  for (int i = 0; i < 3; i++) {

    digitalWrite(ledPin, HIGH);

    delay(600);                  // Long blink

    digitalWrite(ledPin, LOW);

    delay(200);

  }

  delay(600); // Pause between "O" and "S"

  // Short blinks (S) - 3 times again

  for (int i = 0; i < 3; i++) {

    digitalWrite(ledPin, HIGH);

    delay(200);

    digitalWrite(ledPin, LOW);

    delay(200);

  }

  delay(2000); // Pause before repeating

}

Understanding the Code

pinMode(pin, mode) – Configures the LED pin

pinMode(ledPin, OUTPUT);

  • This tells the Arduino that pin 13 is an output, meaning it will send power to the LED.

digitalWrite(pin, value) – Turns the LED On/Off

digitalWrite(ledPin, HIGH);  // Turns LED ON

digitalWrite(ledPin, LOW);   // Turns LED OFF

  • HIGH means power is sent to the LED, turning it on.
  • LOW means no power, turning the LED off.

delay(milliseconds) – Pauses the Code

delay(200);  // Waits 200 milliseconds

  • This pauses the program so the LED stays on/off for the given time.

FOR Loop – Repeating the Blink Pattern

The first FOR loop blinks the LED 3 times for the “S” in SOS:

for (int i = 0; i < 3; i++) {

  • Starts at i = 0
  • Runs while i < 3 (three times: i = 0, 1, 2)
  • Increases i by 1 each loop (i++)

The same loop structure is used for the long “O” blinks, but with a longer delay.

Expanding the Idea – Challenge Yourself!

Now that we have the basic SOS blink pattern, here are a few challenges you can try on your own:

  1. Modify the pattern – Try blinking “HELLO” or your Callsign in Morse code!
  2. Speed Variation – Modify the delays inside the loop to gradually increase or decrease the blink speed.
  3. Double Blink Patterns – Instead of three blinks, try five short and five long for a new effect.

What’s Next?

Now that we’ve seen how FOR loops can be used for timing patterns, next time, we’ll explore potentiometers!

Uncategorized Arduino

Post navigation

Previous post

Recent Posts

  • Tinker’s Corner: Playing with Time – LED Patterns Using FOR Loops
  • Sparks, Mics, and Wires: Packet Radio and APRS
  • Building a Packet Radio Chat Interface with Python, HamShield, and Arduino
  • Command Line Ham Prop Data
  • Tape Measure Yagi
  • Building FLDIGI from source on Debian, Ubuntu, and Linux Mint
  • Unlocking the World of Ham Radio: Your Technician Class Adventure Begins
  • Packet Radio With Windows
  • SSTV Connecting Images Through the Airwaves
  • Pat Winlink Using AX25
  • Pat Winlink on Linux Using AGW
  • Winlink
  • APRS
  • Using Linux Packet Radio (4 of 4)
  • Setting Up AXPorts (3 of 4)
  • Configuring Direwolf (2 of 4)
  • Installing Software for Linux Packet Radio (1 of 4)
  • Exploring the Power of Digital Communication in Ham Radio, What is Packet Radio
©2025 KC3SMW | WordPress Theme by SuperbThemes