Things used in this project

Hardware components:
Pi 3 02
Raspberry Pi Raspberry Pi 3 Model B
09590 01
LED (generic)
12002 04
Breadboard (generic)
08377 02 l
Resistor 330 ohm
11026 02
Jumper wires (generic)
Software apps and online services:
Raspberry Pi Raspbian Jessie


GPIO Pin Layout
GPIO pin layout diagram for the Raspberry Pi 40 pin models


Swift 3.0 project file for Flash LED tutorial
// Created with PiCode for Swift 3.0
// A Swift-Lite project file
// type:project
// name:piCodeLEDTimer
// include:piCodeGPIO.swift

import Foundation

// auto detect board type (detects all boards with 40 GPIO pins)
let gpios = autoDetectBoardType()

// for older boards with 26 GPIO pins please set board type manually
//let gpios = PiCodeGPIO.RPIRev1
//let gpios = PiCodeGPIO.RPIRev2

// setup the GPIO pins
var gp = gpios[.P17]!   // Pin to conect the LED to
gp.direction = .OUT     // Pin connection direction - OUT - when turned on the pin will output 5v
gp.value = 0            // Set intial value 0 = off 1 = on

var intervalTimer: Timer!   // this timer controls the time intervals the LED turns on. As this is a repeating timer it needs to be added to a runloop.
var flashTimer: Timer!      // this timer controls the length of time the LED remains on.

var flashNumber = 5 // the number of flashes

var flashIntervalTime: Double = 2.0 // the time between the start of each flash

var flashLengthTime: Double = 0.75 // the length of time of each flash 

var runLoopTime: Double = 11 // the lenghth of time the runloop will run - set this to be longer than the time for all the flashes to complete
                             // the runloop can be set to continous if the is action you need.

func runIntervalTimer() {
    intervalTimer = Timer.scheduledTimer(withTimeInterval: flashIntervalTime, repeats: true, block: {(_) in flashOnLED()})

func runflashTimer() {
    flashTimer = Timer.scheduledTimer(withTimeInterval: flashLengthTime, repeats: false, block: {(_) in flashOffLED()})

func flashOnLED() {
    print("flash on")
    gp.value = 1

func flashOffLED() {
    gp.value = 0
    print("flash off")
    flashNumber -= 1
    if flashNumber == 0 {


let flashLEDLoop = RunLoop.current
flashLEDLoop.add(intervalTimer, forMode: .defaultRunLoopMode) Date(timeIntervalSinceNow: runLoopTime))



Development Engineer at SwiftPi - Bringing Swift to the Raspberry Pi. Designer and creator of Swift-Lite.



Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback


Similar projects you might like

Setting Up Your Pi Cap on the Raspberry Pi 1, 2 or 3
  • 69
  • 2


Follow this tutorial to set up your Pi Cap with a Raspberry Pi 1 A+/B+, Raspberry Pi 2 or Raspberry Pi 3.

Setting Up Your Pi Cap on the Raspberry Pi Zero
  • 76
  • 2


Follow this tutorial to set up your Pi Cap with a Raspberry Pi Zero.

Secure Simple Remote Access for Camera Viewing
  • 2,001
  • 8

Full instructions

Turn your RPi into a full featured camera with remote access, end2end encryption, and easy user enrollment.

  • 3,626
  • 14

Full instructions

A simple Pi-based camcorder using the very nice HyperPixel touchscreen from Pimoroni.

RPi Serial Console Automation using Arduino
  • 234
  • 3

Full instructions

A follow-up article to "One Button Restart of a Stalled Raspberry Pi": 3 digital pins provide 8 separate functions that can be called.

Run uTorrent on Raspberry Pi
  • 468
  • 2


Step-by-step instruction of running uTorrent on Raspberry Pi 2 using ExaGear Desktop.

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login