The Swift Package Manager offers an easy way to add packages to your Raspberry Pi Swift projects. In this post, I’ll use it to flash an LED. Hackernoon’s post was helpful to me, and what I describe is largely based on it, with a few changes.

Set up the circuit

The circuit for flashing an LED is simple. Follow the same setup as my previous post: connect the short pin on the LED to ground (GND), connect the long pin of the LED to a 220 Ohm resistor, which is in turn connected to the G17 GPIO pin on the Raspberry Pi, which will supply 3.3 volts on command.

Install git

The Swift package manager will commonly install code from git archives, so you will need to install git if you do not already have it. On the Raspberry Pi, type this into the Terminal to install git, but skip it if you know git is installed:

sudo apt-get install git-core

Write the Swift project on the Mac

In Terminal on the Mac, type the following to create the Swift project:

mkdir blinker && cd blinker
swift package init --type executable

To blink the LED, we will use Umberto Raimondi’s SwiftyGPIO library. Later, be sure out the IBM Swift Package Catalog for other packages — you are sure to find inspiration here. To use a particular package, we need to include it in the Package.swift file of the project we just created. Open the blinker folder, and open the Package.swift file, which should be at the root level. Edit the line that begins let package = Package( to look like what follows, then close and save the file:

let package = Package(
  name: "blinker",
  dependencies: [
    .Package(url: "", majorVersion: 0)

Back in terminal, and still in the blinker directory, download the SwiftyGPIO package:

swift package update

Although you can edit the main.swift file in the Sources directory, you can optionally use the Swift Package Manager to create the project that can be opened in Xcode:

swift package generate-xcodeproj

Either by opening this Xcode project or by opening the main.swift file (in blinker/Sources), edit the main.swift file as follows:

#if os(Linux)
  import Glibc
import SwiftyGPIO
import Foundation
let pins = SwiftyGPIO.GPIOs(for: .RaspberryPi3)
guard let ledPin = pins[GPIOName.P17] else {
  fatalError("GPIO pin 17 could not be initialized")
ledPin.direction = .OUT
ledPin.value = 0
while true {
  ledPin.value = ledPin.value == 0 ? 1 : 0
  usleep(500000) // microseconds

The #if os(Linux) check prevents an error about macOS not having the Glibc module, which is needed on the Raspberry Pi. The code first gets the pin assignments for the Raspberry Pi 3 (the model I am using; check out the GPIO values to find the one that matches your model). Pin 17 is obtained, as the LED as connected to it, and the pin is set for output (.OUT) and is initially turned off (ledPin.value = 0). The program then enters an infinite loop in which it reverses the current pin value (0 is off, 1 is on), and then waits 0.5 seconds. Changing the value inside usleep() will set the speed of flashing; its value is in microseconds.

Build and run the Swift project on the Raspberry Pi

Transfer the blinker directory to the Raspberry Pi, which could be done several ways, such as via a USB drive or by using scp, which is what I use. My scp command looks like this:

cd ..
scp -r blinker straton@

The -r flag instructs scp to copy recursively, that is, all folders in files within the blinker folder. What follows is the user name (straton for me) and IP address, followed by a colon and the path for the blinker directory on the Pi.

In the terminal on the Raspberry Pi (I use ssh to connect from the Mac, using ssh straton@, go to the blinker directory and build the Swift app.

cd Documents/blinker
swift build

Now run the app and watch the LED blink. Use ctrl-Z to terminate the app.