Tugulab Blog.

Control the Raspberry Pi 2/3 GPIO pins with Swift 3.0 on Ubuntu 16.04

Here we are with a Raspberry Pi configured with Ubuntu 16.04 minimal and Swift 3.0 installed. Now let’s turn on and off a LED with Swift!
author avatar

clacla

Here we are with a Raspberry Pi configured with Ubuntu 16.04 minimal and Swift 3.0 installed. Now let’s turn on and off a LED with Swift!

Step 1: Understanding the GPIO pins numbers

The core concept is that not all the pins 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 aGPIO 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 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 pins

WiringPi is a set of tool to control the GPIOs pins from terminal. It’s actually very convenient for testing purposes. Sadly it’s not as simple as installing a deb package, but we need to fetch the source code and build it.

Before let’s make sure we’ve installed few dependencies needed for compiling it:

sudo apt-get install build-essential git-core

Then let’s build WiringPi:

cd ~
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build

Now you can test the binary has been compiled correctly by reading the state of all the Raspberry Pi pins:

gpio readall

which should print something like this:

Let’s move the gpio binary to root level, to make it more convenient to call:

sudo cp gpio/gpio /usr/bin/

Now let’s set pin 7 (the number in the wPi column) 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 could be some wiring error 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, but it’s a good starting point.

Since the Swift Package Manager is still not working properly on Ubuntu to date (August the 4th, 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 with 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.