Skip to content
A Swift library for the DS18B20 digital temperature sensor
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Examples/Raspi2
Sources
.gitignore
LICENSE
Package.resolved
Package.swift
README.md
ds18b20.png

README.md

DS18B20.swift

A Swift library for the DS18B20 Digital temperature sensor

Linux-only Swift 4 compatible License: MIT

DS18B20

Summary

This is a library for the DS18B20 temperature sensor, available in various forms, from classic ICs to waterproof probes.

The sensor uses the 1-Wire protocol to communicate.

Hardware Details

The sensor can be powered using 3.3V and the signal pin will need a pull-up resistor of 4.7KΩ connected to the same voltage.

The 1-Wire port on the RaspberryPi (pin 4) needs to be enabled via raspi-config before you can use it (restart required), and enabling 1-Wire you'll lose the ability to use the pin as a standard GPIO.

Usage

The temperature returned by the sensor is provided by the only property of the DS18B20 object: Temperature.

The value is in Celsius degrees, if you need a value in Fahrenheit a manual conversion will be necessary.

As for other libraries, to initialize this object you need to retrieve first a OneWireInterface instance from SwiftyGPIO and then initialize the DS18B20 sensor providing a slave id:

let onewires = SwiftyGPIO.hardware1Wires(for:.RaspberryPi2)!
let onewire = onewires[1]

let slaveId = onewire.getSlaves()[0]

let ds = DS18B20(onewire,slaveId: slaveId)

print(ds.Temperature())

The slave id is unique and identifies a specific sensor, since more than one could be connected to the same 1-Wire bus.

In most cases you'll just hardcode the slave id you want, instead of selecting one from the list of available slave devices that could sometime return items in a different order.

Supported Boards

Every board supported by SwiftyGPIO: RaspberryPis, BeagleBones, C.H.I.P., etc...

To use this library, you'll need a Linux ARM board with Swift 3.x/4.x.

The example below will use a RaspberryPi 2 board but you can easily modify the example to use one the other supported boards, a full working demo projects for the RaspberryPi2 is available in the Examples directory.

Installation

Please refer to the SwiftyGPIO readme for Swift installation instructions.

Once your board runs Swift, if your version support the Swift Package Manager, you can simply add this library as a dependency of your project and compile with swift build:

  let package = Package(
      name: "MyProject",
      dependencies: [
        .Package(url: "https://github.com/uraimo/DS18B20.swift.git", majorVersion: 2),
      ]
  ) 

The directory Examples contains sample projects that uses SPM, compile it and run the sample with ./.build/debug/TestDS18B20.

If SPM is not supported, you'll need to manually download the library and its dependencies:

wget https://raw.githubusercontent.com/uraimo/DS18B20.swift/master/Sources/DS18B20.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/SwiftyGPIO.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/Presets.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/SunXi.swift https://raw.githubusercontent.com/uraimo/SwiftyGPIO/master/Sources/1Wire.swift 

And once all the files have been downloaded, create an additional file that will contain the code of your application (e.g. main.swift). When your code is ready, compile it with:

swiftc *.swift

The compiler will create a main executable.

You can’t perform that action at this time.