Skip to content
SwiftyGPIO with RaspberryPi + DHT11 Module
Branch: develop
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Using Swift to display temperature and humidity of a DHT11 module


Raspberry Pi 2+ or Raspberry Pi 3 Model B

Model DHT11 or DHT22

Installing Dependencies

To use the provided prebuilt binaries you'll need the install the following dependencies:

sudo apt install clang libicu-dev libcurl4-nss-dev


Download last version swift ARM compatible with your board, see table:

Build Swift On ARM prebuilt-binaries

and Download


Unzip the files to a folder of your choice:

For example: /home/michel/swift-4.2.3/

After unzip, export path:

export PATH=/home/michel/swift-4.2.3/usr/bin:"${PATH}"

Test if everything went right run command:

swift --version

And the output should be like this:

Swift version 4.2.3 (swift-4.2.3-RELEASE)
Target: armv7-unknown-linux-gnueabihf

Clone and run project

$ git clone

$ cd DHT-SwiftyGPIO

$ swift run

Waiting output should be like this:

Temperatura: 25.0 Humidade: 41.0

Reference files

The DHT.swift was based on the project dhtxx But with modifications

DHT class init allows you to select the sensor type and data conversion by default is used Celcius

Enums Types

public enum SupportedSensor {
    case dht11, dht22

public enum ConversionType {
    case Fahrenheit, Celsius

init(pin: GPIO, for sensor: SupportedSensor, conversion: ConversionType = .Celsius) { }


Example create module DHT11 Object

let dht = DHT(pin: gpios[.P4]!, for: .dht11)


Erros iniciais de leitura ocorrem devido a falha inicial do pulso

Usually get a good reading within the past 60s, many times more often than that

I hope this project helps start with the creation of IoT projects with Swift

My Blog Post

You can’t perform that action at this time.