Tugulab Blog.

Using Swift to control the Raspberry Pi GPIO pins and turn an LED on

Here we are with a Raspberry Pi with Raspbian and Swift 3.0 installed. Now we can actually do something with it. Like flashing an unsexy…
author avatar

clacla

Here we are with a Raspberry Pi with Raspbian and Swift 3.0 installed. Now we can actually do something with it. Like flashing an unsexy LED. But hey, we have to start from somewhere…

Step 1: Understanding the GPIO pins numbers

The core concept is that not all the pins in available on the board are GPIOs (General Purpose Input/Output). Some are power supply, others are ground connection and others are other-stuff.

The number inside the circle is the pin number. Some of these pins match a GPIO pin. For example the pin 7 matches the GPIO4 pin.

We can control only the GPIO pins, not the other ones. The other ones are used to power up your circuit or to do other-stuff.

Step 2: Connecting the circuit

We want to create a simple circuit where an LED is turn on by the GPIO4 (pin 7). So we create a simple circuit like this:

Step 3: Making sure we can actually set a value on a GPIO pin

We are installing a package that allow us to set values on a pin from command line. Just to make sure we’re actually able to turn the GPIO value high and low.

sudo apt-get install wiringpi

Now let’s set pin 7 as output and turn it off, then on and then off again:

gpio mode 7 out
gpio write 7 0
gpio write 7 1
gpio write 7 0

It should work. If it doesn’t, well, it’s very likely you’ve made some wiring mistake in your circuit.

Step 4: Using SwiftyGPIO library to control the GPIO pins

Uraimo has created a very nice Swift wrapper around the GPIO pins, so we can control them in our Swift app. There are few things that could be done better (e.g. more protocol oriented representation of the different GPIOs types, better representation of the GPIOs states and more immutability of a GPIO representation), but it’s still a good starting point.

Since the Swift Package Manager is still not working properly on Raspbian to date (July the 8th, 2016), we are going to clone the git repo and use the file directly within our code.

The important bit is to have all the sources in one folder, to be able to compile them all in one simple command.

mkdir ~/ledtest
mkdir ~/ledtest/Sources
cd ~/ledtest
git clone https://github.com/uraimo/SwiftyGPIO.git
cp SwiftyGPIO/Sources/* Sources/

Let’s create our main:

nano Sources/main.swift

with this code:

import Glibc

// Get the Pin where the LED will be attached to
let gpios = SwiftyGPIO.GPIOs(for: .RaspberryPi2)
guard let ledGPIO = gpios[.P4] else {
fatalError(“It has not been possible to initialised the LED GPIO pin”)
}

// Set the GPIO to output
ledGPIO.direction = .OUT

// Turn on and off the led few times
ledGPIO.value = 1
sleep(1)
ledGPIO.value = 0
sleep(1)
ledGPIO.value = 1
sleep(1)
ledGPIO.value = 0

Now compile and run as super user (it needs sudoer permissions to access the GPIO pins):

swiftc Sources/*.swift
sudo ./main

The LED should blink two times and then stop. Success!!

Step 5: Cleanse our soul with some cleaner code

I feel dirty in writing hacky scripts. I start regretting all my life decisions when I understand I wrote something that’s not even close to be acceptable good. So the following code is the v2 of the app.

But first I really suggest you to configure your Atom or SublimeText editor (on your main computer) to edit code remotely on your RPi, so you don’t have to use nano or vim. (Info for Atom and for SublimeText).

That should be it. Compile and run. The LED should keep blinking until you stop the execution.

This is the basic you need to know to start doing amazing things in electronics with a very nice programming language and all the things that comes with it.